Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recategorize config classes #17945

Merged
merged 13 commits into from
Aug 31, 2024
2 changes: 1 addition & 1 deletion crates/rust-analyzer/src/cli/scip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl flags::Scip {
// FIXME @alibektas : What happens to errors without logging?
error!(?error_sink, "Config Error(s)");
}
let cargo_config = config.cargo();
let cargo_config = config.cargo(None);
let (db, vfs, _) = load_workspace_at(
root.as_path().as_ref(),
&cargo_config,
Expand Down
1,173 changes: 559 additions & 614 deletions crates/rust-analyzer/src/config.rs

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions crates/rust-analyzer/src/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,10 @@ impl GlobalStateSnapshot {
file_id_to_url(&self.vfs_read(), id)
}

pub(crate) fn vfs_path_to_file_id(&self, vfs_path: &VfsPath) -> anyhow::Result<FileId> {
vfs_path_to_file_id(&self.vfs_read(), vfs_path)
}

pub(crate) fn file_line_index(&self, file_id: FileId) -> Cancellable<LineIndex> {
let endings = self.vfs.read().1[&file_id];
let index = self.analysis.file_line_index(file_id)?;
Expand Down Expand Up @@ -725,3 +729,9 @@ pub(crate) fn url_to_file_id(vfs: &vfs::Vfs, url: &Url) -> anyhow::Result<FileId
let res = vfs.file_id(&path).ok_or_else(|| anyhow::format_err!("file not found: {path}"))?;
Ok(res)
}

pub(crate) fn vfs_path_to_file_id(vfs: &vfs::Vfs, vfs_path: &VfsPath) -> anyhow::Result<FileId> {
let res =
vfs.file_id(vfs_path).ok_or_else(|| anyhow::format_err!("file not found: {vfs_path}"))?;
Ok(res)
}
30 changes: 18 additions & 12 deletions crates/rust-analyzer/src/handlers/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,18 @@ pub(crate) fn handle_did_save_text_document(
state: &mut GlobalState,
params: DidSaveTextDocumentParams,
) -> anyhow::Result<()> {
if state.config.script_rebuild_on_save() && state.build_deps_changed {
state.build_deps_changed = false;
state
.fetch_build_data_queue
.request_op("build_deps_changed - save notification".to_owned(), ());
}

if let Ok(vfs_path) = from_proto::vfs_path(&params.text_document.uri) {
let snap = state.snapshot();
let file_id = snap.vfs_path_to_file_id(&vfs_path)?;
let sr = snap.analysis.source_root_id(file_id)?;

if state.config.script_rebuild_on_save(Some(sr)) && state.build_deps_changed {
state.build_deps_changed = false;
state
.fetch_build_data_queue
.request_op("build_deps_changed - save notification".to_owned(), ());
}

// Re-fetch workspaces if a workspace related file has changed
if let Some(path) = vfs_path.as_path() {
let additional_files = &state
Expand Down Expand Up @@ -182,15 +186,16 @@ pub(crate) fn handle_did_save_text_document(
}
}

if !state.config.check_on_save() || run_flycheck(state, vfs_path) {
if !state.config.check_on_save(Some(sr)) || run_flycheck(state, vfs_path) {
return Ok(());
}
} else if state.config.check_on_save() {
} else if state.config.check_on_save(None) {
// No specific flycheck was triggered, so let's trigger all of them.
for flycheck in state.flycheck.iter() {
flycheck.restart_workspace(None);
}
}

Ok(())
}

Expand Down Expand Up @@ -288,6 +293,7 @@ fn run_flycheck(state: &mut GlobalState, vfs_path: VfsPath) -> bool {
let file_id = state.vfs.read().0.file_id(&vfs_path);
if let Some(file_id) = file_id {
let world = state.snapshot();
let source_root_id = world.analysis.source_root_id(file_id).ok();
let mut updated = false;
let task = move || -> std::result::Result<(), ide::Cancelled> {
// Is the target binary? If so we let flycheck run only for the workspace that contains the crate.
Expand Down Expand Up @@ -373,9 +379,9 @@ fn run_flycheck(state: &mut GlobalState, vfs_path: VfsPath) -> bool {
for (id, package) in workspace_ids.clone() {
if id == flycheck.id() {
updated = true;
match package
.filter(|_| !world.config.flycheck_workspace() || target.is_some())
{
match package.filter(|_| {
!world.config.flycheck_workspace(source_root_id) || target.is_some()
}) {
Some(package) => flycheck
.restart_for_package(package, target.clone().map(TupleExt::head)),
None => flycheck.restart_workspace(saved_file.clone()),
Expand Down
11 changes: 6 additions & 5 deletions crates/rust-analyzer/src/handlers/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ pub(crate) fn handle_run_test(

let handle = CargoTestHandle::new(
test_path,
state.config.cargo_test_options(),
state.config.cargo_test_options(None),
cargo.workspace_root(),
test_target,
state.test_run_sender.clone(),
Expand Down Expand Up @@ -565,7 +565,7 @@ pub(crate) fn handle_workspace_symbol(
) -> anyhow::Result<Option<lsp_types::WorkspaceSymbolResponse>> {
let _p = tracing::info_span!("handle_workspace_symbol").entered();

let config = snap.config.workspace_symbol();
let config = snap.config.workspace_symbol(None);
let (all_symbols, libs) = decide_search_scope_and_kind(&params, &config);

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

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

if let Some(edition) = edition {
Expand Down Expand Up @@ -2177,7 +2178,7 @@ fn run_rustfmt(
_ => process::Command::new(cmd),
};

cmd.envs(snap.config.extra_env());
cmd.envs(snap.config.extra_env(source_root_id));
cmd.args(args);
cmd
}
Expand Down
3 changes: 2 additions & 1 deletion crates/rust-analyzer/src/lsp/to_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1365,8 +1365,9 @@ pub(crate) fn runnable(
snap: &GlobalStateSnapshot,
runnable: Runnable,
) -> Cancellable<Option<lsp_ext::Runnable>> {
let config = snap.config.runnables();
let target_spec = TargetSpec::for_file(snap, runnable.nav.file_id)?;
let source_root = snap.analysis.source_root_id(runnable.nav.file_id).ok();
let config = snap.config.runnables(source_root);

match target_spec {
Some(TargetSpec::Cargo(spec)) => {
Expand Down
12 changes: 6 additions & 6 deletions crates/rust-analyzer/src/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ impl GlobalState {
if self.is_quiescent() {
let became_quiescent = !was_quiescent;
if became_quiescent {
if self.config.check_on_save() {
if self.config.check_on_save(None) {
// Project has loaded properly, kick off initial flycheck
self.flycheck.iter().for_each(|flycheck| flycheck.restart_workspace(None));
}
Expand Down Expand Up @@ -434,7 +434,7 @@ impl GlobalState {

let project_or_mem_docs_changed =
became_quiescent || state_changed || memdocs_added_or_removed;
if project_or_mem_docs_changed && self.config.publish_diagnostics() {
if project_or_mem_docs_changed && self.config.publish_diagnostics(None) {
self.update_diagnostics();
}
if project_or_mem_docs_changed && self.config.test_explorer() {
Expand All @@ -455,7 +455,7 @@ impl GlobalState {
}
}

if self.config.cargo_autoreload_config()
if self.config.cargo_autoreload_config(None)
|| self.config.discover_workspace_config().is_some()
{
if let Some((cause, FetchWorkspaceRequest { path, force_crate_graph_reload })) =
Expand Down Expand Up @@ -925,7 +925,7 @@ impl GlobalState {
FlycheckMessage::AddDiagnostic { id, workspace_root, diagnostic } => {
let snap = self.snapshot();
let diagnostics = crate::diagnostics::to_proto::map_rust_diagnostic_to_lsp(
&self.config.diagnostics_map(),
&self.config.diagnostics_map(None),
&diagnostic,
&workspace_root,
&snap,
Expand Down Expand Up @@ -973,9 +973,9 @@ impl GlobalState {
// When we're running multiple flychecks, we have to include a disambiguator in
// the title, or the editor complains. Note that this is a user-facing string.
let title = if self.flycheck.len() == 1 {
format!("{}", self.config.flycheck())
format!("{}", self.config.flycheck(None))
} else {
format!("{} (#{})", self.config.flycheck(), id + 1)
format!("{} (#{})", self.config.flycheck(None), id + 1)
};
self.report_progress(
&title,
Expand Down
18 changes: 9 additions & 9 deletions crates/rust-analyzer/src/reload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl GlobalState {
{
let req = FetchWorkspaceRequest { path: None, force_crate_graph_reload: false };
self.fetch_workspaces_queue.request_op("discovered projects changed".to_owned(), req)
} else if self.config.flycheck() != old_config.flycheck() {
} else if self.config.flycheck(None) != old_config.flycheck(None) {
self.reload_flycheck();
}

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

if !self.config.cargo_autoreload()
if !self.config.cargo_autoreload_config(None)
&& self.is_quiescent()
&& self.fetch_workspaces_queue.op_requested()
&& self.config.discover_workspace_config().is_none()
Expand Down Expand Up @@ -264,7 +264,7 @@ impl GlobalState {
.map(ManifestPath::try_from)
.filter_map(Result::ok)
.collect();
let cargo_config = self.config.cargo();
let cargo_config = self.config.cargo(None);
let discover_command = self.config.discover_workspace_config().cloned();
let is_quiescent = !(self.discover_workspace_queue.op_in_progress()
|| self.vfs_progress_config_version < self.vfs_config_version
Expand Down Expand Up @@ -357,7 +357,7 @@ impl GlobalState {
pub(crate) fn fetch_build_data(&mut self, cause: Cause) {
info!(%cause, "will fetch build data");
let workspaces = Arc::clone(&self.workspaces);
let config = self.config.cargo();
let config = self.config.cargo(None);
let root_path = self.config.root_path().clone();

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

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

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

if self.config.run_build_scripts() {
if self.config.run_build_scripts(None) {
self.build_deps_changed = false;
self.fetch_build_data_queue.request_op("workspace updated".to_owned(), ());
}
Expand Down Expand Up @@ -627,7 +627,7 @@ impl GlobalState {
..
} => cargo_config_extra_env
.iter()
.chain(self.config.extra_env())
.chain(self.config.extra_env(None))
.map(|(a, b)| (a.clone(), b.clone()))
.chain(
ws.sysroot
Expand Down Expand Up @@ -702,7 +702,7 @@ impl GlobalState {
vfs.file_id(&vfs_path)
};

ws_to_crate_graph(&self.workspaces, self.config.extra_env(), load)
ws_to_crate_graph(&self.workspaces, self.config.extra_env(None), load)
};
let mut change = ChangeWithProcMacros::new();
if self.config.expand_proc_macros() {
Expand Down Expand Up @@ -791,7 +791,7 @@ impl GlobalState {

fn reload_flycheck(&mut self) {
let _p = tracing::info_span!("GlobalState::reload_flycheck").entered();
let config = self.config.flycheck();
let config = self.config.flycheck(None);
let sender = self.flycheck_sender.clone();
let invocation_strategy = match config {
FlycheckConfig::CargoCommand { .. } => {
Expand Down
4 changes: 2 additions & 2 deletions crates/rust-analyzer/src/target_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl CargoTargetSpec {
kind: &RunnableKind,
cfg: &Option<CfgExpr>,
) -> (Vec<String>, Vec<String>) {
let config = snap.config.runnables();
let config = snap.config.runnables(None);
let extra_test_binary_args = config.extra_test_binary_args;

let mut cargo_args = Vec::new();
Expand Down Expand Up @@ -168,7 +168,7 @@ impl CargoTargetSpec {
(Default::default(), Default::default())
};

let cargo_config = snap.config.cargo();
let cargo_config = snap.config.cargo(None);

match &cargo_config.features {
CargoFeatures::All => {
Expand Down