Skip to content

Commit ca6ddd8

Browse files
committed
Enable rust_analyzer for cfgs when code is being analyzed by rust-analyzer
1 parent 3325622 commit ca6ddd8

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

crates/ide-db/src/search.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ impl Definition {
221221
}
222222

223223
// def is crate root
224-
// FIXME: We don't do searches for crates currently, as a crate does not actually have a single name
225224
if let &Definition::Module(module) = self {
226225
if module.is_crate_root() {
227226
return SearchScope::reverse_dependencies(db, module.krate());
@@ -393,7 +392,10 @@ impl<'a> FindUsages<'a> {
393392
let name = match self.def {
394393
// special case crate modules as these do not have a proper name
395394
Definition::Module(module) if module.is_crate_root() => {
396-
// FIXME: This assumes the crate name is always equal to its display name when it really isn't
395+
// FIXME: This assumes the crate name is always equal to its display name when it
396+
// really isn't
397+
// we should instead look at the dependency edge name and recursively search our way
398+
// up the ancestors
397399
module
398400
.krate()
399401
.display_name(self.sema.db)

crates/project-model/src/workspace.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! metadata` or `rust-project.json`) into representation stored in the salsa
33
//! database -- `CrateGraph`.
44
5-
use std::{collections::VecDeque, fmt, fs, process::Command, str::FromStr, sync};
5+
use std::{collections::VecDeque, fmt, fs, iter, process::Command, str::FromStr, sync};
66

77
use anyhow::{format_err, Context};
88
use base_db::{
@@ -730,6 +730,7 @@ fn project_json_to_crate_graph(
730730
)
731731
});
732732

733+
let r_a_cfg_flag = CfgFlag::Atom("rust_analyzer".to_owned());
733734
let mut cfg_cache: FxHashMap<&str, Vec<CfgFlag>> = FxHashMap::default();
734735
let crates: FxHashMap<CrateId, CrateId> = project
735736
.crates()
@@ -765,7 +766,12 @@ fn project_json_to_crate_graph(
765766
*edition,
766767
display_name.clone(),
767768
version.clone(),
768-
target_cfgs.iter().chain(cfg.iter()).cloned().collect(),
769+
target_cfgs
770+
.iter()
771+
.chain(cfg.iter())
772+
.chain(iter::once(&r_a_cfg_flag))
773+
.cloned()
774+
.collect(),
769775
None,
770776
env,
771777
*is_proc_macro,
@@ -820,7 +826,7 @@ fn cargo_to_crate_graph(
820826
sysroot: Option<&Sysroot>,
821827
rustc_cfg: Vec<CfgFlag>,
822828
override_cfg: &CfgOverrides,
823-
// Don't compute cfg and use this if present
829+
// Don't compute cfg and use this if present, only used for the sysroot experiment hack
824830
forced_cfg: Option<CfgOptions>,
825831
build_scripts: &WorkspaceBuildScripts,
826832
target_layout: TargetLayoutLoadResult,
@@ -842,12 +848,7 @@ fn cargo_to_crate_graph(
842848
None => (SysrootPublicDeps::default(), None),
843849
};
844850

845-
let cfg_options = {
846-
let mut cfg_options = CfgOptions::default();
847-
cfg_options.extend(rustc_cfg);
848-
cfg_options.insert_atom("debug_assertions".into());
849-
cfg_options
850-
};
851+
let cfg_options = create_cfg_options(rustc_cfg);
851852

852853
// Mapping of a package to its library target
853854
let mut pkg_to_lib_crate = FxHashMap::default();
@@ -1029,8 +1030,7 @@ fn detached_files_to_crate_graph(
10291030
None => (SysrootPublicDeps::default(), None),
10301031
};
10311032

1032-
let mut cfg_options = CfgOptions::default();
1033-
cfg_options.extend(rustc_cfg);
1033+
let cfg_options = create_cfg_options(rustc_cfg);
10341034

10351035
for detached_file in detached_files {
10361036
let file_id = match load(detached_file) {
@@ -1295,8 +1295,7 @@ fn sysroot_to_crate_graph(
12951295
channel: Option<ReleaseChannel>,
12961296
) -> (SysrootPublicDeps, Option<CrateId>) {
12971297
let _p = profile::span("sysroot_to_crate_graph");
1298-
let mut cfg_options = CfgOptions::default();
1299-
cfg_options.extend(rustc_cfg.clone());
1298+
let cfg_options = create_cfg_options(rustc_cfg.clone());
13001299
let sysroot_crates: FxHashMap<SysrootCrate, CrateId> = match &sysroot.hack_cargo_workspace {
13011300
Some(cargo) => handle_hack_cargo_workspace(
13021301
load,
@@ -1475,3 +1474,11 @@ fn inject_cargo_env(package: &PackageData, env: &mut Env) {
14751474

14761475
env.set("CARGO_PKG_LICENSE_FILE", String::new());
14771476
}
1477+
1478+
fn create_cfg_options(rustc_cfg: Vec<CfgFlag>) -> CfgOptions {
1479+
let mut cfg_options = CfgOptions::default();
1480+
cfg_options.extend(rustc_cfg);
1481+
cfg_options.insert_atom("debug_assertions".into());
1482+
cfg_options.insert_atom("rust_analyzer".into());
1483+
cfg_options
1484+
}

0 commit comments

Comments
 (0)