From 183bff580e395279b22f513ea9a6edd9707f7de1 Mon Sep 17 00:00:00 2001 From: R Aadarsh Date: Sun, 5 Oct 2025 18:07:19 +0530 Subject: [PATCH] - Add a field `encoding` in both `Workspace` and `Project` - Pass encoding to `ProjectRegistry::open_path` and set the `encoding` field in `Project` --- crates/fs/src/encodings.rs | 2 +- crates/project/src/project.rs | 5 +++++ crates/workspace/src/workspace.rs | 28 +++++++++++++++++++++------- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/crates/fs/src/encodings.rs b/crates/fs/src/encodings.rs index 1759f8917c643c6697c198a32ed94fd1b25229c0..780da0cadc54746a45775f113faf7835e14fd446 100644 --- a/crates/fs/src/encodings.rs +++ b/crates/fs/src/encodings.rs @@ -9,7 +9,7 @@ use encoding_rs::Encoding; /// A wrapper around `encoding_rs::Encoding` to implement `Send` and `Sync`. /// Since the reference is static, it is safe to send it across threads. -pub struct EncodingWrapper(&'static Encoding); +pub struct EncodingWrapper(pub &'static Encoding); impl Debug for EncodingWrapper { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs index 7c7fe9a43091611a53dbde0ecbaf6691b7d768d0..044c1501064bdb195c01f829548cc2acba99c8fa 100644 --- a/crates/project/src/project.rs +++ b/crates/project/src/project.rs @@ -26,6 +26,7 @@ mod project_tests; mod environment; use buffer_diff::BufferDiff; use context_server_store::ContextServerStore; +use encoding_rs::Encoding; pub use environment::ProjectEnvironmentEvent; use git::repository::get_git_committer; use git_store::{Repository, RepositoryId}; @@ -215,6 +216,7 @@ pub struct Project { settings_observer: Entity, toolchain_store: Option>, agent_location: Option, + pub encoding: Arc>, } #[derive(Clone, Debug, PartialEq, Eq)] @@ -1225,6 +1227,7 @@ impl Project { toolchain_store: Some(toolchain_store), agent_location: None, + encoding: Arc::new(std::sync::Mutex::new(encoding_rs::UTF_8)), } }) } @@ -1410,6 +1413,7 @@ impl Project { toolchain_store: Some(toolchain_store), agent_location: None, + encoding: Arc::new(std::sync::Mutex::new(encoding_rs::UTF_8)), }; // remote server -> local machine handlers @@ -1663,6 +1667,7 @@ impl Project { remotely_created_models: Arc::new(Mutex::new(RemotelyCreatedModels::default())), toolchain_store: None, agent_location: None, + encoding: Arc::new(std::sync::Mutex::new(encoding_rs::UTF_8)), }; project.set_role(role, cx); for worktree in worktrees { diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index bf21e72fb38ebe509a2cff7da087c7b31c155029..d39864abdb140955b6029f0d320eccc5c623f6fb 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -624,6 +624,7 @@ type BuildProjectItemForPathFn = fn( &Entity, &ProjectPath, + Option, &mut Window, &mut App, ) -> Option, WorkspaceItemBuilder)>>>; @@ -645,8 +646,12 @@ impl ProjectItemRegistry { }, ); self.build_project_item_for_path_fns - .push(|project, project_path, window, cx| { + .push(|project, project_path, encoding, window, cx| { let project_path = project_path.clone(); + let EncodingWrapper(encoding) = encoding.unwrap_or_default(); + + project.update(cx, |project, _| {*project.encoding.lock().unwrap() = encoding}); + let is_file = project .read(cx) .entry_for_path(&project_path, cx) @@ -715,14 +720,17 @@ impl ProjectItemRegistry { &self, project: &Entity, path: &ProjectPath, + encoding: Option, window: &mut Window, cx: &mut App, ) -> Task, WorkspaceItemBuilder)>> { - let Some(open_project_item) = self - .build_project_item_for_path_fns - .iter() - .rev() - .find_map(|open_project_item| open_project_item(project, path, window, cx)) + let Some(open_project_item) = + self.build_project_item_for_path_fns + .iter() + .rev() + .find_map(|open_project_item| { + open_project_item(project, path, encoding.clone(), window, cx) + }) else { return Task::ready(Err(anyhow!("cannot open file {:?}", path.path))); }; @@ -3566,7 +3574,13 @@ impl Workspace { cx: &mut App, ) -> Task, WorkspaceItemBuilder)>> { let registry = cx.default_global::().clone(); - registry.open_path(self.project(), &path, window, cx) + registry.open_path( + self.project(), + &path, + Some(EncodingWrapper::new(*self.encoding.lock().unwrap())), + window, + cx, + ) } pub fn find_project_item(