Skip to content

Commit

Permalink
Fix different kinds values used for worktree_id (zed-industries#17523)
Browse files Browse the repository at this point in the history
  • Loading branch information
SomeoneToIgnore authored Sep 7, 2024
1 parent 47aec5e commit 8985fd8
Show file tree
Hide file tree
Showing 18 changed files with 136 additions and 127 deletions.
8 changes: 4 additions & 4 deletions crates/collab/src/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3324,7 +3324,7 @@ async fn test_local_settings(
let store = cx.global::<SettingsStore>();
assert_eq!(
store
.local_settings(worktree_b.entity_id().as_u64() as _)
.local_settings(worktree_b.read(cx).id())
.collect::<Vec<_>>(),
&[
(Path::new("").into(), r#"{"tab_size":2}"#.to_string()),
Expand All @@ -3343,7 +3343,7 @@ async fn test_local_settings(
let store = cx.global::<SettingsStore>();
assert_eq!(
store
.local_settings(worktree_b.entity_id().as_u64() as _)
.local_settings(worktree_b.read(cx).id())
.collect::<Vec<_>>(),
&[
(Path::new("").into(), r#"{}"#.to_string()),
Expand Down Expand Up @@ -3372,7 +3372,7 @@ async fn test_local_settings(
let store = cx.global::<SettingsStore>();
assert_eq!(
store
.local_settings(worktree_b.entity_id().as_u64() as _)
.local_settings(worktree_b.read(cx).id())
.collect::<Vec<_>>(),
&[
(Path::new("a").into(), r#"{"tab_size":8}"#.to_string()),
Expand Down Expand Up @@ -3404,7 +3404,7 @@ async fn test_local_settings(
let store = cx.global::<SettingsStore>();
assert_eq!(
store
.local_settings(worktree_b.entity_id().as_u64() as _)
.local_settings(worktree_b.read(cx).id())
.collect::<Vec<_>>(),
&[(Path::new("a").into(), r#"{"hard_tabs":true}"#.to_string()),]
)
Expand Down
4 changes: 2 additions & 2 deletions crates/copilot/src/copilot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1250,8 +1250,8 @@ mod tests {
unimplemented!()
}

fn worktree_id(&self) -> usize {
0
fn worktree_id(&self, _: &AppContext) -> settings::WorktreeId {
settings::WorktreeId::from_usize(0)
}

fn is_private(&self) -> bool {
Expand Down
15 changes: 11 additions & 4 deletions crates/editor/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ use parking_lot::{Mutex, RwLock};
use project::project_settings::{GitGutterSetting, ProjectSettings};
use project::{
CodeAction, Completion, CompletionIntent, FormatTrigger, Item, Location, Project, ProjectPath,
ProjectTransaction, TaskSourceKind, WorktreeId,
ProjectTransaction, TaskSourceKind,
};
use rand::prelude::*;
use rpc::{proto::*, ErrorExt};
use scroll::{Autoscroll, OngoingScroll, ScrollAnchor, ScrollManager, ScrollbarAutoHide};
use selections_collection::{resolve_multiple, MutableSelectionsCollection, SelectionsCollection};
use serde::{Deserialize, Serialize};
use settings::{update_settings_file, Settings, SettingsStore};
use settings::{update_settings_file, Settings, SettingsLocation, SettingsStore};
use smallvec::SmallVec;
use snippet::Snippet;
use std::{
Expand Down Expand Up @@ -8742,7 +8742,7 @@ impl Editor {
let (worktree_id, file) = project
.buffer_for_id(runnable.buffer, cx)
.and_then(|buffer| buffer.read(cx).file())
.map(|file| (WorktreeId::from_usize(file.worktree_id()), file.clone()))
.map(|file| (file.worktree_id(cx), file.clone()))
.unzip();

(project.task_inventory().clone(), worktree_id, file)
Expand Down Expand Up @@ -11421,7 +11421,14 @@ impl Editor {
.redacted_ranges(search_range, |file| {
if let Some(file) = file {
file.is_private()
&& EditorSettings::get(Some(file.as_ref().into()), cx).redact_private_values
&& EditorSettings::get(
Some(SettingsLocation {
worktree_id: file.worktree_id(cx),
path: file.path().as_ref(),
}),
cx,
)
.redact_private_values
} else {
false
}
Expand Down
6 changes: 3 additions & 3 deletions crates/extension/src/wasm_host/wit/since_v0_1_0.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::wasm_host::{wit::ToWasmtimeResult, WasmState};
use ::http_client::AsyncBody;
use ::settings::Settings;
use ::settings::{Settings, WorktreeId};
use anyhow::{anyhow, bail, Context, Result};
use async_compression::futures::bufread::GzipDecoder;
use async_tar::Archive;
Expand Down Expand Up @@ -76,7 +76,7 @@ impl HostWorktree for WasmState {
delegate: Resource<Arc<dyn LspAdapterDelegate>>,
) -> wasmtime::Result<u64> {
let delegate = self.table.get(&delegate)?;
Ok(delegate.worktree_id())
Ok(delegate.worktree_id().to_proto())
}

async fn root_path(
Expand Down Expand Up @@ -393,7 +393,7 @@ impl ExtensionImports for WasmState {
let location = location
.as_ref()
.map(|location| ::settings::SettingsLocation {
worktree_id: location.worktree_id as usize,
worktree_id: WorktreeId::from_proto(location.worktree_id),
path: Path::new(&location.path),
});

Expand Down
7 changes: 4 additions & 3 deletions crates/language/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use gpui::{
use lsp::LanguageServerId;
use parking_lot::Mutex;
use serde_json::Value;
use settings::WorktreeId;
use similar::{ChangeTag, TextDiff};
use smallvec::SmallVec;
use smol::future::yield_now;
Expand Down Expand Up @@ -361,7 +362,7 @@ pub trait File: Send + Sync {
/// Returns the id of the worktree to which this file belongs.
///
/// This is needed for looking up project-specific settings.
fn worktree_id(&self) -> usize;
fn worktree_id(&self, cx: &AppContext) -> WorktreeId;

/// Returns whether the file has been deleted.
fn is_deleted(&self) -> bool;
Expand Down Expand Up @@ -4172,8 +4173,8 @@ impl File for TestFile {
self.path().file_name().unwrap_or(self.root_name.as_ref())
}

fn worktree_id(&self) -> usize {
0
fn worktree_id(&self, _: &AppContext) -> WorktreeId {
WorktreeId::from_usize(0)
}

fn is_deleted(&self) -> bool {
Expand Down
3 changes: 2 additions & 1 deletion crates/language/src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use schemars::{
};
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use serde_json::Value;
use settings::WorktreeId;
use smol::future::FutureExt as _;
use std::num::NonZeroU32;
use std::{
Expand Down Expand Up @@ -280,7 +281,7 @@ impl CachedLspAdapter {
pub trait LspAdapterDelegate: Send + Sync {
fn show_notification(&self, message: &str, cx: &mut AppContext);
fn http_client(&self) -> Arc<dyn HttpClient>;
fn worktree_id(&self) -> u64;
fn worktree_id(&self) -> WorktreeId;
fn worktree_root_path(&self) -> &Path;
fn update_status(&self, language: LanguageServerName, status: LanguageServerBinaryStatus);

Expand Down
14 changes: 4 additions & 10 deletions crates/language/src/language_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@ use settings::{add_references_to_properties, Settings, SettingsLocation, Setting
use std::{num::NonZeroU32, path::Path, sync::Arc};
use util::serde::default_true;

impl<'a> From<&'a dyn File> for SettingsLocation<'a> {
fn from(val: &'a dyn File) -> Self {
SettingsLocation {
worktree_id: val.worktree_id(),
path: val.path().as_ref(),
}
}
}

/// Initializes the language settings.
pub fn init(cx: &mut AppContext) {
AllLanguageSettings::register(cx);
Expand All @@ -49,7 +40,10 @@ pub fn all_language_settings<'a>(
file: Option<&Arc<dyn File>>,
cx: &'a AppContext,
) -> &'a AllLanguageSettings {
let location = file.map(|f| f.as_ref().into());
let location = file.map(|f| SettingsLocation {
worktree_id: f.worktree_id(cx),
path: f.path().as_ref(),
});
AllLanguageSettings::get(location, cx)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/languages/src/yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl LspAdapter for YamlLspAdapter {
cx: &mut AsyncAppContext,
) -> Result<Value> {
let location = SettingsLocation {
worktree_id: delegate.worktree_id() as usize,
worktree_id: delegate.worktree_id(),
path: delegate.worktree_root_path(),
};

Expand Down
6 changes: 3 additions & 3 deletions crates/project/src/lsp_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4250,7 +4250,7 @@ impl LspStore {

let project_settings = ProjectSettings::get(
Some(SettingsLocation {
worktree_id: worktree_id.to_proto() as usize,
worktree_id,
path: Path::new(""),
}),
cx,
Expand Down Expand Up @@ -6408,8 +6408,8 @@ impl LspAdapterDelegate for ProjectLspAdapterDelegate {
self.http_client.clone()
}

fn worktree_id(&self) -> u64 {
self.worktree.id().to_proto()
fn worktree_id(&self) -> WorktreeId {
self.worktree.id()
}

fn worktree_root_path(&self) -> &Path {
Expand Down
6 changes: 3 additions & 3 deletions crates/project/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use futures::{

use git::{blame::Blame, repository::GitRepository};
use gpui::{
AnyModel, AppContext, AsyncAppContext, BorrowAppContext, Context, Entity, EventEmitter, Model,
AnyModel, AppContext, AsyncAppContext, BorrowAppContext, Context, EventEmitter, Model,
ModelContext, SharedString, Task, WeakModel, WindowContext,
};
use itertools::Itertools;
Expand Down Expand Up @@ -1606,7 +1606,7 @@ impl Project {
self.worktree_store.update(cx, |worktree_store, cx| {
for worktree in worktree_store.worktrees() {
store
.clear_local_settings(worktree.entity_id().as_u64() as usize, cx)
.clear_local_settings(worktree.read(cx).id(), cx)
.log_err();
}
});
Expand Down Expand Up @@ -5186,7 +5186,7 @@ impl EventEmitter<Event> for Project {}
impl<'a> From<&'a ProjectPath> for SettingsLocation<'a> {
fn from(val: &'a ProjectPath) -> Self {
SettingsLocation {
worktree_id: val.worktree_id.to_usize(),
worktree_id: val.worktree_id,
path: val.path.as_ref(),
}
}
Expand Down
11 changes: 3 additions & 8 deletions crates/project/src/project_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl SettingsObserver {
let store = cx.global::<SettingsStore>();
for worktree in self.worktree_store.read(cx).worktrees() {
let worktree_id = worktree.read(cx).id().to_proto();
for (path, content) in store.local_settings(worktree.entity_id().as_u64() as usize) {
for (path, content) in store.local_settings(worktree.read(cx).id()) {
downstream_client
.send(proto::UpdateWorktreeSettings {
project_id,
Expand Down Expand Up @@ -416,17 +416,12 @@ impl SettingsObserver {
settings_contents: impl IntoIterator<Item = (Arc<Path>, Option<String>)>,
cx: &mut ModelContext<Self>,
) {
let worktree_id = worktree.entity_id();
let worktree_id = worktree.read(cx).id();
let remote_worktree_id = worktree.read(cx).id();
cx.update_global::<SettingsStore, _>(|store, cx| {
for (directory, file_content) in settings_contents {
store
.set_local_settings(
worktree_id.as_u64() as usize,
directory.clone(),
file_content.as_deref(),
cx,
)
.set_local_settings(worktree_id, directory.clone(), file_content.as_deref(), cx)
.log_err();
if let Some(downstream_client) = &self.downstream_client {
downstream_client
Expand Down
19 changes: 10 additions & 9 deletions crates/project/src/task_inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,15 +579,16 @@ impl ContextProvider for BasicContextProvider {
if !selected_text.trim().is_empty() {
task_variables.insert(VariableName::SelectedText, selected_text);
}
let worktree_abs_path = buffer
.file()
.map(|file| WorktreeId::from_usize(file.worktree_id()))
.and_then(|worktree_id| {
self.project
.read(cx)
.worktree_for_id(worktree_id, cx)
.map(|worktree| worktree.read(cx).abs_path())
});
let worktree_abs_path =
buffer
.file()
.map(|file| file.worktree_id(cx))
.and_then(|worktree_id| {
self.project
.read(cx)
.worktree_for_id(worktree_id, cx)
.map(|worktree| worktree.read(cx).abs_path())
});
if let Some(worktree_path) = worktree_abs_path {
task_variables.insert(
VariableName::WorktreeRoot,
Expand Down
2 changes: 1 addition & 1 deletion crates/project/src/terminals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl Project {
if let Some(path) = path.as_ref() {
if let Some((worktree, _)) = self.find_worktree(path, cx) {
settings_location = Some(SettingsLocation {
worktree_id: worktree.read(cx).id().to_usize(),
worktree_id: worktree.read(cx).id(),
path,
});
}
Expand Down
2 changes: 1 addition & 1 deletion crates/remote_server/src/remote_editing_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ async fn test_remote_settings(cx: &mut TestAppContext, server_cx: &mut TestAppCo
assert_eq!(
AllLanguageSettings::get(
Some(SettingsLocation {
worktree_id: worktree_id.into(),
worktree_id,
path: Path::new("src/lib.rs")
}),
cx
Expand Down
35 changes: 34 additions & 1 deletion crates/settings/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod settings_store;

use gpui::AppContext;
use rust_embed::RustEmbed;
use std::{borrow::Cow, str};
use std::{borrow::Cow, fmt, str};
use util::asset_str;

pub use editable_setting_control::*;
Expand All @@ -15,6 +15,39 @@ pub use keymap_file::KeymapFile;
pub use settings_file::*;
pub use settings_store::{Settings, SettingsLocation, SettingsSources, SettingsStore};

#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash, PartialOrd, Ord)]
pub struct WorktreeId(usize);

impl From<WorktreeId> for usize {
fn from(value: WorktreeId) -> Self {
value.0
}
}

impl WorktreeId {
pub fn from_usize(handle_id: usize) -> Self {
Self(handle_id)
}

pub fn from_proto(id: u64) -> Self {
Self(id as usize)
}

pub fn to_proto(&self) -> u64 {
self.0 as u64
}

pub fn to_usize(&self) -> usize {
self.0
}
}

impl fmt::Display for WorktreeId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
std::fmt::Display::fmt(&self.0, f)
}
}

#[derive(RustEmbed)]
#[folder = "../../assets"]
#[include = "settings/*"]
Expand Down
Loading

0 comments on commit 8985fd8

Please sign in to comment.