Skip to content

Commit 09a421a

Browse files
committed
Auto merge of rust-lang#17945 - alibektas:ratoml_categorization, r=alibektas
Recategorize config classes
2 parents 63bb576 + 1350769 commit 09a421a

File tree

9 files changed

+613
-650
lines changed

9 files changed

+613
-650
lines changed

src/tools/rust-analyzer/crates/rust-analyzer/src/cli/scip.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl flags::Scip {
5151
// FIXME @alibektas : What happens to errors without logging?
5252
error!(?error_sink, "Config Error(s)");
5353
}
54-
let cargo_config = config.cargo();
54+
let cargo_config = config.cargo(None);
5555
let (db, vfs, _) = load_workspace_at(
5656
root.as_path().as_ref(),
5757
&cargo_config,

src/tools/rust-analyzer/crates/rust-analyzer/src/config.rs

Lines changed: 559 additions & 614 deletions
Large diffs are not rendered by default.

src/tools/rust-analyzer/crates/rust-analyzer/src/global_state.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,10 @@ impl GlobalStateSnapshot {
631631
file_id_to_url(&self.vfs_read(), id)
632632
}
633633

634+
pub(crate) fn vfs_path_to_file_id(&self, vfs_path: &VfsPath) -> anyhow::Result<FileId> {
635+
vfs_path_to_file_id(&self.vfs_read(), vfs_path)
636+
}
637+
634638
pub(crate) fn file_line_index(&self, file_id: FileId) -> Cancellable<LineIndex> {
635639
let endings = self.vfs.read().1[&file_id];
636640
let index = self.analysis.file_line_index(file_id)?;
@@ -725,3 +729,9 @@ pub(crate) fn url_to_file_id(vfs: &vfs::Vfs, url: &Url) -> anyhow::Result<FileId
725729
let res = vfs.file_id(&path).ok_or_else(|| anyhow::format_err!("file not found: {path}"))?;
726730
Ok(res)
727731
}
732+
733+
pub(crate) fn vfs_path_to_file_id(vfs: &vfs::Vfs, vfs_path: &VfsPath) -> anyhow::Result<FileId> {
734+
let res =
735+
vfs.file_id(vfs_path).ok_or_else(|| anyhow::format_err!("file not found: {vfs_path}"))?;
736+
Ok(res)
737+
}

src/tools/rust-analyzer/crates/rust-analyzer/src/handlers/notification.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,18 @@ pub(crate) fn handle_did_save_text_document(
145145
state: &mut GlobalState,
146146
params: DidSaveTextDocumentParams,
147147
) -> anyhow::Result<()> {
148-
if state.config.script_rebuild_on_save() && state.build_deps_changed {
149-
state.build_deps_changed = false;
150-
state
151-
.fetch_build_data_queue
152-
.request_op("build_deps_changed - save notification".to_owned(), ());
153-
}
154-
155148
if let Ok(vfs_path) = from_proto::vfs_path(&params.text_document.uri) {
149+
let snap = state.snapshot();
150+
let file_id = snap.vfs_path_to_file_id(&vfs_path)?;
151+
let sr = snap.analysis.source_root_id(file_id)?;
152+
153+
if state.config.script_rebuild_on_save(Some(sr)) && state.build_deps_changed {
154+
state.build_deps_changed = false;
155+
state
156+
.fetch_build_data_queue
157+
.request_op("build_deps_changed - save notification".to_owned(), ());
158+
}
159+
156160
// Re-fetch workspaces if a workspace related file has changed
157161
if let Some(path) = vfs_path.as_path() {
158162
let additional_files = &state
@@ -182,15 +186,16 @@ pub(crate) fn handle_did_save_text_document(
182186
}
183187
}
184188

185-
if !state.config.check_on_save() || run_flycheck(state, vfs_path) {
189+
if !state.config.check_on_save(Some(sr)) || run_flycheck(state, vfs_path) {
186190
return Ok(());
187191
}
188-
} else if state.config.check_on_save() {
192+
} else if state.config.check_on_save(None) {
189193
// No specific flycheck was triggered, so let's trigger all of them.
190194
for flycheck in state.flycheck.iter() {
191195
flycheck.restart_workspace(None);
192196
}
193197
}
198+
194199
Ok(())
195200
}
196201

@@ -288,6 +293,7 @@ fn run_flycheck(state: &mut GlobalState, vfs_path: VfsPath) -> bool {
288293
let file_id = state.vfs.read().0.file_id(&vfs_path);
289294
if let Some(file_id) = file_id {
290295
let world = state.snapshot();
296+
let source_root_id = world.analysis.source_root_id(file_id).ok();
291297
let mut updated = false;
292298
let task = move || -> std::result::Result<(), ide::Cancelled> {
293299
// Is the target binary? If so we let flycheck run only for the workspace that contains the crate.
@@ -373,9 +379,9 @@ fn run_flycheck(state: &mut GlobalState, vfs_path: VfsPath) -> bool {
373379
for (id, package) in workspace_ids.clone() {
374380
if id == flycheck.id() {
375381
updated = true;
376-
match package
377-
.filter(|_| !world.config.flycheck_workspace() || target.is_some())
378-
{
382+
match package.filter(|_| {
383+
!world.config.flycheck_workspace(source_root_id) || target.is_some()
384+
}) {
379385
Some(package) => flycheck
380386
.restart_for_package(package, target.clone().map(TupleExt::head)),
381387
None => flycheck.restart_workspace(saved_file.clone()),

src/tools/rust-analyzer/crates/rust-analyzer/src/handlers/request.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ pub(crate) fn handle_run_test(
256256

257257
let handle = CargoTestHandle::new(
258258
test_path,
259-
state.config.cargo_test_options(),
259+
state.config.cargo_test_options(None),
260260
cargo.workspace_root(),
261261
test_target,
262262
state.test_run_sender.clone(),
@@ -565,7 +565,7 @@ pub(crate) fn handle_workspace_symbol(
565565
) -> anyhow::Result<Option<lsp_types::WorkspaceSymbolResponse>> {
566566
let _p = tracing::info_span!("handle_workspace_symbol").entered();
567567

568-
let config = snap.config.workspace_symbol();
568+
let config = snap.config.workspace_symbol(None);
569569
let (all_symbols, libs) = decide_search_scope_and_kind(&params, &config);
570570

571571
let query = {
@@ -852,6 +852,7 @@ pub(crate) fn handle_runnables(
852852
) -> anyhow::Result<Vec<lsp_ext::Runnable>> {
853853
let _p = tracing::info_span!("handle_runnables").entered();
854854
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
855+
let source_root = snap.analysis.source_root_id(file_id).ok();
855856
let line_index = snap.file_line_index(file_id)?;
856857
let offset = params.position.and_then(|it| from_proto::offset(&line_index, it).ok());
857858
let target_spec = TargetSpec::for_file(&snap, file_id)?;
@@ -894,7 +895,7 @@ pub(crate) fn handle_runnables(
894895
}
895896

896897
// Add `cargo check` and `cargo test` for all targets of the whole package
897-
let config = snap.config.runnables();
898+
let config = snap.config.runnables(source_root);
898899
match target_spec {
899900
Some(TargetSpec::Cargo(spec)) => {
900901
let is_crate_no_std = snap.analysis.is_crate_no_std(spec.crate_id)?;
@@ -2119,7 +2120,7 @@ fn run_rustfmt(
21192120
RustfmtConfig::Rustfmt { extra_args, enable_range_formatting } => {
21202121
// FIXME: Set RUSTUP_TOOLCHAIN
21212122
let mut cmd = process::Command::new(toolchain::Tool::Rustfmt.path());
2122-
cmd.envs(snap.config.extra_env());
2123+
cmd.envs(snap.config.extra_env(source_root_id));
21232124
cmd.args(extra_args);
21242125

21252126
if let Some(edition) = edition {
@@ -2177,7 +2178,7 @@ fn run_rustfmt(
21772178
_ => process::Command::new(cmd),
21782179
};
21792180

2180-
cmd.envs(snap.config.extra_env());
2181+
cmd.envs(snap.config.extra_env(source_root_id));
21812182
cmd.args(args);
21822183
cmd
21832184
}

src/tools/rust-analyzer/crates/rust-analyzer/src/lsp/to_proto.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1369,8 +1369,9 @@ pub(crate) fn runnable(
13691369
snap: &GlobalStateSnapshot,
13701370
runnable: Runnable,
13711371
) -> Cancellable<Option<lsp_ext::Runnable>> {
1372-
let config = snap.config.runnables();
13731372
let target_spec = TargetSpec::for_file(snap, runnable.nav.file_id)?;
1373+
let source_root = snap.analysis.source_root_id(runnable.nav.file_id).ok();
1374+
let config = snap.config.runnables(source_root);
13741375

13751376
match target_spec {
13761377
Some(TargetSpec::Cargo(spec)) => {

src/tools/rust-analyzer/crates/rust-analyzer/src/main_loop.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ impl GlobalState {
404404
if self.is_quiescent() {
405405
let became_quiescent = !was_quiescent;
406406
if became_quiescent {
407-
if self.config.check_on_save() {
407+
if self.config.check_on_save(None) {
408408
// Project has loaded properly, kick off initial flycheck
409409
self.flycheck.iter().for_each(|flycheck| flycheck.restart_workspace(None));
410410
}
@@ -434,7 +434,7 @@ impl GlobalState {
434434

435435
let project_or_mem_docs_changed =
436436
became_quiescent || state_changed || memdocs_added_or_removed;
437-
if project_or_mem_docs_changed && self.config.publish_diagnostics() {
437+
if project_or_mem_docs_changed && self.config.publish_diagnostics(None) {
438438
self.update_diagnostics();
439439
}
440440
if project_or_mem_docs_changed && self.config.test_explorer() {
@@ -455,7 +455,7 @@ impl GlobalState {
455455
}
456456
}
457457

458-
if self.config.cargo_autoreload_config()
458+
if self.config.cargo_autoreload_config(None)
459459
|| self.config.discover_workspace_config().is_some()
460460
{
461461
if let Some((cause, FetchWorkspaceRequest { path, force_crate_graph_reload })) =
@@ -925,7 +925,7 @@ impl GlobalState {
925925
FlycheckMessage::AddDiagnostic { id, workspace_root, diagnostic } => {
926926
let snap = self.snapshot();
927927
let diagnostics = crate::diagnostics::to_proto::map_rust_diagnostic_to_lsp(
928-
&self.config.diagnostics_map(),
928+
&self.config.diagnostics_map(None),
929929
&diagnostic,
930930
&workspace_root,
931931
&snap,
@@ -973,9 +973,9 @@ impl GlobalState {
973973
// When we're running multiple flychecks, we have to include a disambiguator in
974974
// the title, or the editor complains. Note that this is a user-facing string.
975975
let title = if self.flycheck.len() == 1 {
976-
format!("{}", self.config.flycheck())
976+
format!("{}", self.config.flycheck(None))
977977
} else {
978-
format!("{} (#{})", self.config.flycheck(), id + 1)
978+
format!("{} (#{})", self.config.flycheck(None), id + 1)
979979
};
980980
self.report_progress(
981981
&title,

src/tools/rust-analyzer/crates/rust-analyzer/src/reload.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl GlobalState {
100100
{
101101
let req = FetchWorkspaceRequest { path: None, force_crate_graph_reload: false };
102102
self.fetch_workspaces_queue.request_op("discovered projects changed".to_owned(), req)
103-
} else if self.config.flycheck() != old_config.flycheck() {
103+
} else if self.config.flycheck(None) != old_config.flycheck(None) {
104104
self.reload_flycheck();
105105
}
106106

@@ -122,7 +122,7 @@ impl GlobalState {
122122
};
123123
let mut message = String::new();
124124

125-
if !self.config.cargo_autoreload()
125+
if !self.config.cargo_autoreload_config(None)
126126
&& self.is_quiescent()
127127
&& self.fetch_workspaces_queue.op_requested()
128128
&& self.config.discover_workspace_config().is_none()
@@ -264,7 +264,7 @@ impl GlobalState {
264264
.map(ManifestPath::try_from)
265265
.filter_map(Result::ok)
266266
.collect();
267-
let cargo_config = self.config.cargo();
267+
let cargo_config = self.config.cargo(None);
268268
let discover_command = self.config.discover_workspace_config().cloned();
269269
let is_quiescent = !(self.discover_workspace_queue.op_in_progress()
270270
|| self.vfs_progress_config_version < self.vfs_config_version
@@ -357,7 +357,7 @@ impl GlobalState {
357357
pub(crate) fn fetch_build_data(&mut self, cause: Cause) {
358358
info!(%cause, "will fetch build data");
359359
let workspaces = Arc::clone(&self.workspaces);
360-
let config = self.config.cargo();
360+
let config = self.config.cargo(None);
361361
let root_path = self.config.root_path().clone();
362362

363363
self.task_pool.handle.spawn_with_sender(ThreadIntent::Worker, move |sender| {
@@ -382,7 +382,7 @@ impl GlobalState {
382382

383383
pub(crate) fn fetch_proc_macros(&mut self, cause: Cause, paths: Vec<ProcMacroPaths>) {
384384
info!(%cause, "will load proc macros");
385-
let ignored_proc_macros = self.config.ignored_proc_macros().clone();
385+
let ignored_proc_macros = self.config.ignored_proc_macros(None).clone();
386386
let proc_macro_clients = self.proc_macro_clients.clone();
387387

388388
self.task_pool.handle.spawn_with_sender(ThreadIntent::Worker, move |sender| {
@@ -507,7 +507,7 @@ impl GlobalState {
507507
// FIXME: can we abort the build scripts here if they are already running?
508508
self.workspaces = Arc::new(workspaces);
509509

510-
if self.config.run_build_scripts() {
510+
if self.config.run_build_scripts(None) {
511511
self.build_deps_changed = false;
512512
self.fetch_build_data_queue.request_op("workspace updated".to_owned(), ());
513513
}
@@ -627,7 +627,7 @@ impl GlobalState {
627627
..
628628
} => cargo_config_extra_env
629629
.iter()
630-
.chain(self.config.extra_env())
630+
.chain(self.config.extra_env(None))
631631
.map(|(a, b)| (a.clone(), b.clone()))
632632
.chain(
633633
ws.sysroot
@@ -702,7 +702,7 @@ impl GlobalState {
702702
vfs.file_id(&vfs_path)
703703
};
704704

705-
ws_to_crate_graph(&self.workspaces, self.config.extra_env(), load)
705+
ws_to_crate_graph(&self.workspaces, self.config.extra_env(None), load)
706706
};
707707
let mut change = ChangeWithProcMacros::new();
708708
if self.config.expand_proc_macros() {
@@ -791,7 +791,7 @@ impl GlobalState {
791791

792792
fn reload_flycheck(&mut self) {
793793
let _p = tracing::info_span!("GlobalState::reload_flycheck").entered();
794-
let config = self.config.flycheck();
794+
let config = self.config.flycheck(None);
795795
let sender = self.flycheck_sender.clone();
796796
let invocation_strategy = match config {
797797
FlycheckConfig::CargoCommand { .. } => {

src/tools/rust-analyzer/crates/rust-analyzer/src/target_spec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl CargoTargetSpec {
113113
kind: &RunnableKind,
114114
cfg: &Option<CfgExpr>,
115115
) -> (Vec<String>, Vec<String>) {
116-
let config = snap.config.runnables();
116+
let config = snap.config.runnables(None);
117117
let extra_test_binary_args = config.extra_test_binary_args;
118118

119119
let mut cargo_args = Vec::new();
@@ -168,7 +168,7 @@ impl CargoTargetSpec {
168168
(Default::default(), Default::default())
169169
};
170170

171-
let cargo_config = snap.config.cargo();
171+
let cargo_config = snap.config.cargo(None);
172172

173173
match &cargo_config.features {
174174
CargoFeatures::All => {

0 commit comments

Comments
 (0)