Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit f5f68e4

Browse files
committed
Make workspace fields of config private
1 parent 9b835f3 commit f5f68e4

File tree

4 files changed

+28
-26
lines changed

4 files changed

+28
-26
lines changed

crates/rust-analyzer/src/config.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use project_model::{
2727
};
2828
use rustc_hash::{FxHashMap, FxHashSet};
2929
use serde::{de::DeserializeOwned, Deserialize};
30-
use vfs::AbsPathBuf;
30+
use vfs::{AbsPath, AbsPathBuf};
3131

3232
use crate::{
3333
caps::completion_item_edit_resolve,
@@ -535,8 +535,9 @@ impl Default for ConfigData {
535535

536536
#[derive(Debug, Clone)]
537537
pub struct Config {
538-
pub discovered_projects: Option<Vec<ProjectManifest>>,
539-
pub workspace_roots: Vec<AbsPathBuf>,
538+
discovered_projects: Option<Vec<ProjectManifest>>,
539+
/// The workspace roots as registered by the LSP client
540+
workspace_roots: Vec<AbsPathBuf>,
540541
caps: lsp_types::ClientCapabilities,
541542
root_path: AbsPathBuf,
542543
data: ConfigData,
@@ -758,6 +759,16 @@ impl Config {
758759
self.discovered_projects = Some(discovered);
759760
}
760761

762+
pub fn remove_workspace(&mut self, path: &AbsPath) {
763+
if let Some(position) = self.workspace_roots.iter().position(|it| it == path) {
764+
self.workspace_roots.remove(position);
765+
}
766+
}
767+
768+
pub fn add_workspaces(&mut self, paths: impl Iterator<Item = AbsPathBuf>) {
769+
self.workspace_roots.extend(paths);
770+
}
771+
761772
pub fn update(&mut self, mut json: serde_json::Value) -> Result<(), ConfigUpdateError> {
762773
tracing::info!("updating config from JSON: {:#}", json);
763774
if json.is_null() || json.as_object().map_or(false, |it| it.is_empty()) {

crates/rust-analyzer/src/handlers.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -777,14 +777,7 @@ pub(crate) fn handle_runnables(
777777
}
778778
}
779779
None => {
780-
if !snap.config.linked_projects().is_empty()
781-
|| !snap
782-
.config
783-
.discovered_projects
784-
.as_ref()
785-
.map(|projects| projects.is_empty())
786-
.unwrap_or(true)
787-
{
780+
if !snap.config.linked_projects().is_empty() {
788781
res.push(lsp_ext::Runnable {
789782
label: "cargo check --workspace".to_string(),
790783
location: None,

crates/rust-analyzer/src/main_loop.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -908,8 +908,10 @@ impl GlobalState {
908908
// Re-fetch workspaces if a workspace related file has changed
909909
if let Some(abs_path) = vfs_path.as_path() {
910910
if reload::should_refresh_for_change(abs_path, ChangeKind::Modify) {
911-
this.fetch_workspaces_queue
912-
.request_op(format!("DidSaveTextDocument {}", abs_path.display()), ());
911+
this.fetch_workspaces_queue.request_op(
912+
format!("DidSaveTextDocument {}", abs_path.display()),
913+
(),
914+
);
913915
}
914916
}
915917

@@ -972,8 +974,7 @@ impl GlobalState {
972974
for workspace in params.event.removed {
973975
let Ok(path) = workspace.uri.to_file_path() else { continue };
974976
let Ok(path) = AbsPathBuf::try_from(path) else { continue };
975-
let Some(position) = config.workspace_roots.iter().position(|it| it == &path) else { continue };
976-
config.workspace_roots.remove(position);
977+
config.remove_workspace(&path);
977978
}
978979

979980
let added = params
@@ -982,11 +983,12 @@ impl GlobalState {
982983
.into_iter()
983984
.filter_map(|it| it.uri.to_file_path().ok())
984985
.filter_map(|it| AbsPathBuf::try_from(it).ok());
985-
config.workspace_roots.extend(added);
986-
if !config.has_linked_projects() && config.detached_files().is_empty() {
987-
config.rediscover_workspaces();
988-
this.fetch_workspaces_queue.request_op("client workspaces changed".to_string(), ())
989-
}
986+
config.add_workspaces(added);
987+
if !config.has_linked_projects() && config.detached_files().is_empty() {
988+
config.rediscover_workspaces();
989+
this.fetch_workspaces_queue
990+
.request_op("client workspaces changed".to_string(), ())
991+
}
990992

991993
Ok(())
992994
})?

crates/rust-analyzer/tests/slow-tests/support.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,6 @@ impl<'a> Project<'a> {
101101
if roots.is_empty() {
102102
roots.push(tmp_dir_path.clone());
103103
}
104-
let discovered_projects = roots
105-
.into_iter()
106-
.map(|it| ProjectManifest::discover_single(&it).unwrap())
107-
.collect::<Vec<_>>();
108104

109105
let mut config = Config::new(
110106
tmp_dir_path,
@@ -144,10 +140,10 @@ impl<'a> Project<'a> {
144140
})),
145141
..Default::default()
146142
},
147-
Vec::new(),
143+
roots,
148144
);
149-
config.discovered_projects = Some(discovered_projects);
150145
config.update(self.config).expect("invalid config");
146+
config.rediscover_workspaces();
151147

152148
Server::new(tmp_dir, config)
153149
}

0 commit comments

Comments
 (0)