mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 04:53:34 +00:00
use slightly more idiomatic api for cfg
This commit is contained in:
parent
93199002af
commit
355419d404
7 changed files with 59 additions and 40 deletions
|
@ -43,8 +43,12 @@ pub fn load_cargo(root: &Path) -> Result<(AnalysisHost, FxHashMap<SourceRootId,
|
|||
);
|
||||
|
||||
// FIXME: cfg options?
|
||||
let default_cfg_options =
|
||||
get_rustc_cfg_options().atom("test".into()).atom("debug_assertion".into());
|
||||
let default_cfg_options = {
|
||||
let mut opts = get_rustc_cfg_options();
|
||||
opts.insert_atom("test".into());
|
||||
opts.insert_atom("debug_assertion".into());
|
||||
opts
|
||||
};
|
||||
|
||||
let (crate_graph, _crate_names) =
|
||||
ws.to_crate_graph(&default_cfg_options, &mut |path: &Path| {
|
||||
|
|
|
@ -36,26 +36,20 @@ impl CfgOptions {
|
|||
self.check(&parse_cfg(attr))
|
||||
}
|
||||
|
||||
pub fn atom(mut self, name: SmolStr) -> CfgOptions {
|
||||
self.atoms.insert(name);
|
||||
self
|
||||
pub fn insert_atom(&mut self, key: SmolStr) {
|
||||
self.atoms.insert(key);
|
||||
}
|
||||
|
||||
pub fn key_value(mut self, key: SmolStr, value: SmolStr) -> CfgOptions {
|
||||
pub fn remove_atom(&mut self, name: &str) {
|
||||
self.atoms.remove(name);
|
||||
}
|
||||
|
||||
pub fn insert_key_value(&mut self, key: SmolStr, value: SmolStr) {
|
||||
self.key_values.insert((key, value));
|
||||
self
|
||||
}
|
||||
|
||||
/// Shortcut to set features
|
||||
pub fn features(mut self, iter: impl IntoIterator<Item = SmolStr>) -> CfgOptions {
|
||||
for feat in iter {
|
||||
self = self.key_value("feature".into(), feat);
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn remove_atom(mut self, name: &SmolStr) -> CfgOptions {
|
||||
self.atoms.remove(name);
|
||||
self
|
||||
pub fn insert_features(&mut self, iter: impl IntoIterator<Item = SmolStr>) {
|
||||
iter.into_iter().for_each(|feat| self.insert_key_value("feature".into(), feat));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -561,12 +561,14 @@ fn cfg_test() {
|
|||
"#,
|
||||
crate_graph! {
|
||||
"main": ("/main.rs", ["std"]),
|
||||
"std": ("/lib.rs", [], CfgOptions::default()
|
||||
.atom("test".into())
|
||||
.key_value("feature".into(), "foo".into())
|
||||
.key_value("feature".into(), "bar".into())
|
||||
.key_value("opt".into(), "42".into())
|
||||
),
|
||||
"std": ("/lib.rs", [], {
|
||||
let mut opts = CfgOptions::default();
|
||||
opts.insert_atom("test".into());
|
||||
opts.insert_key_value("feature".into(), "foo".into());
|
||||
opts.insert_key_value("feature".into(), "bar".into());
|
||||
opts.insert_key_value("opt".into(), "42".into());
|
||||
opts
|
||||
}),
|
||||
},
|
||||
);
|
||||
|
||||
|
|
|
@ -62,7 +62,11 @@ impl S {
|
|||
"#,
|
||||
);
|
||||
db.set_crate_graph_from_fixture(crate_graph! {
|
||||
"main": ("/main.rs", ["foo"], CfgOptions::default().atom("test".into())),
|
||||
"main": ("/main.rs", ["foo"], {
|
||||
let mut opts = CfgOptions::default();
|
||||
opts.insert_atom("test".into());
|
||||
opts
|
||||
}),
|
||||
"foo": ("/foo.rs", []),
|
||||
});
|
||||
assert_eq!("(i32, {unknown}, i32, {unknown})", type_at_pos(&db, pos));
|
||||
|
|
|
@ -325,7 +325,8 @@ impl Analysis {
|
|||
let file_id = FileId(0);
|
||||
// FIXME: cfg options
|
||||
// Default to enable test for single file.
|
||||
let cfg_options = CfgOptions::default().atom("test".into());
|
||||
let mut cfg_options = CfgOptions::default();
|
||||
cfg_options.insert_atom("test".into());
|
||||
crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options);
|
||||
change.add_file(source_root, file_id, "main.rs".into(), Arc::new(text));
|
||||
change.set_crate_graph(crate_graph);
|
||||
|
|
|
@ -98,8 +98,12 @@ impl WorldState {
|
|||
}
|
||||
|
||||
// FIXME: Read default cfgs from config
|
||||
let default_cfg_options =
|
||||
get_rustc_cfg_options().atom("test".into()).atom("debug_assertion".into());
|
||||
let default_cfg_options = {
|
||||
let mut opts = get_rustc_cfg_options();
|
||||
opts.insert_atom("test".into());
|
||||
opts.insert_atom("debug_assertion".into());
|
||||
opts
|
||||
};
|
||||
|
||||
// Create crate graph from all the workspaces
|
||||
let mut crate_graph = CrateGraph::default();
|
||||
|
|
|
@ -134,13 +134,16 @@ impl ProjectWorkspace {
|
|||
json_project::Edition::Edition2015 => Edition::Edition2015,
|
||||
json_project::Edition::Edition2018 => Edition::Edition2018,
|
||||
};
|
||||
let mut cfg_options = default_cfg_options.clone();
|
||||
for name in &krate.atom_cfgs {
|
||||
cfg_options = cfg_options.atom(name.into());
|
||||
}
|
||||
for (key, value) in &krate.key_value_cfgs {
|
||||
cfg_options = cfg_options.key_value(key.into(), value.into());
|
||||
}
|
||||
let cfg_options = {
|
||||
let mut opts = default_cfg_options.clone();
|
||||
for name in &krate.atom_cfgs {
|
||||
opts.insert_atom(name.into());
|
||||
}
|
||||
for (key, value) in &krate.key_value_cfgs {
|
||||
opts.insert_key_value(key.into(), value.into());
|
||||
}
|
||||
opts
|
||||
};
|
||||
crates.insert(
|
||||
crate_id,
|
||||
crate_graph.add_crate_root(file_id, edition, cfg_options),
|
||||
|
@ -171,7 +174,12 @@ impl ProjectWorkspace {
|
|||
for krate in sysroot.crates() {
|
||||
if let Some(file_id) = load(krate.root(&sysroot)) {
|
||||
// Crates from sysroot have `cfg(test)` disabled
|
||||
let cfg_options = default_cfg_options.clone().remove_atom(&"test".into());
|
||||
let cfg_options = {
|
||||
let mut opts = default_cfg_options.clone();
|
||||
opts.remove_atom("test");
|
||||
opts
|
||||
};
|
||||
|
||||
let crate_id =
|
||||
crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options);
|
||||
sysroot_crates.insert(krate, crate_id);
|
||||
|
@ -202,9 +210,11 @@ impl ProjectWorkspace {
|
|||
let root = tgt.root(&cargo);
|
||||
if let Some(file_id) = load(root) {
|
||||
let edition = pkg.edition(&cargo);
|
||||
let cfg_options = default_cfg_options
|
||||
.clone()
|
||||
.features(pkg.features(&cargo).iter().map(Into::into));
|
||||
let cfg_options = {
|
||||
let mut opts = default_cfg_options.clone();
|
||||
opts.insert_features(pkg.features(&cargo).iter().map(Into::into));
|
||||
opts
|
||||
};
|
||||
let crate_id =
|
||||
crate_graph.add_crate_root(file_id, edition, cfg_options);
|
||||
names.insert(crate_id, pkg.name(&cargo).to_string());
|
||||
|
@ -321,11 +331,11 @@ pub fn get_rustc_cfg_options() -> CfgOptions {
|
|||
Ok(rustc_cfgs) => {
|
||||
for line in rustc_cfgs.lines() {
|
||||
match line.find('=') {
|
||||
None => cfg_options = cfg_options.atom(line.into()),
|
||||
None => cfg_options.insert_atom(line.into()),
|
||||
Some(pos) => {
|
||||
let key = &line[..pos];
|
||||
let value = line[pos + 1..].trim_matches('"');
|
||||
cfg_options = cfg_options.key_value(key.into(), value.into());
|
||||
cfg_options.insert_key_value(key.into(), value.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue