1969: restore coloring of attributes r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-10-08 11:55:27 +00:00 committed by GitHub
commit 06a8deae4a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 82 additions and 45 deletions

View file

@ -43,8 +43,12 @@ pub fn load_cargo(root: &Path) -> Result<(AnalysisHost, FxHashMap<SourceRootId,
); );
// FIXME: cfg options? // FIXME: cfg options?
let default_cfg_options = let default_cfg_options = {
get_rustc_cfg_options().atom("test".into()).atom("debug_assertion".into()); let mut opts = get_rustc_cfg_options();
opts.insert_atom("test".into());
opts.insert_atom("debug_assertion".into());
opts
};
let (crate_graph, _crate_names) = let (crate_graph, _crate_names) =
ws.to_crate_graph(&default_cfg_options, &mut |path: &Path| { ws.to_crate_graph(&default_cfg_options, &mut |path: &Path| {

View file

@ -36,26 +36,20 @@ impl CfgOptions {
self.check(&parse_cfg(attr)) self.check(&parse_cfg(attr))
} }
pub fn atom(mut self, name: SmolStr) -> CfgOptions { pub fn insert_atom(&mut self, key: SmolStr) {
self.atoms.insert(name); self.atoms.insert(key);
self
} }
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.key_values.insert((key, value));
self
} }
/// Shortcut to set features /// Shortcut to set features
pub fn features(mut self, iter: impl IntoIterator<Item = SmolStr>) -> CfgOptions { pub fn insert_features(&mut self, iter: impl IntoIterator<Item = SmolStr>) {
for feat in iter { iter.into_iter().for_each(|feat| self.insert_key_value("feature".into(), feat));
self = self.key_value("feature".into(), feat);
}
self
}
pub fn remove_atom(mut self, name: &SmolStr) -> CfgOptions {
self.atoms.remove(name);
self
} }
} }

View file

@ -278,7 +278,10 @@ macro_rules! crate_graph {
$crate_path:literal, $crate_path:literal,
$($edition:literal,)? $($edition:literal,)?
[$($dep:literal),*] [$($dep:literal),*]
$(,$cfg:expr)? $(, cfg = {
$($key:literal $(= $value:literal)?),*
$(,)?
})?
), ),
)*) => {{ )*) => {{
let mut res = $crate::mock::CrateGraphFixture::default(); let mut res = $crate::mock::CrateGraphFixture::default();
@ -286,7 +289,19 @@ macro_rules! crate_graph {
#[allow(unused_mut, unused_assignments)] #[allow(unused_mut, unused_assignments)]
let mut edition = ra_db::Edition::Edition2018; let mut edition = ra_db::Edition::Edition2018;
$(edition = ra_db::Edition::from_string($edition);)? $(edition = ra_db::Edition::from_string($edition);)?
let cfg_options = { ::ra_cfg::CfgOptions::default() $(; $cfg)? }; let cfg_options = {
#[allow(unused_mut)]
let mut cfg = ::ra_cfg::CfgOptions::default();
$(
$(
if 0 == 0 $(+ { drop($value); 1})? {
cfg.insert_atom($key.into());
}
$(cfg.insert_key_value($key.into(), $value.into());)?
)*
)?
cfg
};
res.0.push(( res.0.push((
$crate_name.to_string(), $crate_name.to_string(),
($crate_path.to_string(), edition, cfg_options, vec![$($dep.to_string()),*]) ($crate_path.to_string(), edition, cfg_options, vec![$($dep.to_string()),*])

View file

@ -7,7 +7,6 @@ mod mod_resolution;
use std::sync::Arc; use std::sync::Arc;
use insta::assert_snapshot; use insta::assert_snapshot;
use ra_cfg::CfgOptions;
use ra_db::SourceDatabase; use ra_db::SourceDatabase;
use test_utils::covers; use test_utils::covers;
@ -561,12 +560,12 @@ fn cfg_test() {
"#, "#,
crate_graph! { crate_graph! {
"main": ("/main.rs", ["std"]), "main": ("/main.rs", ["std"]),
"std": ("/lib.rs", [], CfgOptions::default() "std": ("/lib.rs", [], cfg = {
.atom("test".into()) "test",
.key_value("feature".into(), "foo".into()) "feature" = "foo",
.key_value("feature".into(), "bar".into()) "feature" = "bar",
.key_value("opt".into(), "42".into()) "opt" = "42",
), }),
}, },
); );

View file

@ -3,7 +3,6 @@ use std::sync::Arc;
use insta::assert_snapshot; use insta::assert_snapshot;
use ra_cfg::CfgOptions;
use ra_db::{salsa::Database, FilePosition, SourceDatabase}; use ra_db::{salsa::Database, FilePosition, SourceDatabase};
use ra_syntax::{ use ra_syntax::{
algo, algo,
@ -62,7 +61,7 @@ impl S {
"#, "#,
); );
db.set_crate_graph_from_fixture(crate_graph! { db.set_crate_graph_from_fixture(crate_graph! {
"main": ("/main.rs", ["foo"], CfgOptions::default().atom("test".into())), "main": ("/main.rs", ["foo"], cfg = { "test" }),
"foo": ("/foo.rs", []), "foo": ("/foo.rs", []),
}); });
assert_eq!("(i32, {unknown}, i32, {unknown})", type_at_pos(&db, pos)); assert_eq!("(i32, {unknown}, i32, {unknown})", type_at_pos(&db, pos));

View file

@ -325,7 +325,8 @@ impl Analysis {
let file_id = FileId(0); let file_id = FileId(0);
// FIXME: cfg options // FIXME: cfg options
// Default to enable test for single file. // 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); 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.add_file(source_root, file_id, "main.rs".into(), Arc::new(text));
change.set_crate_graph(crate_graph); change.set_crate_graph(crate_graph);

View file

@ -19,7 +19,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
.keyword\.unsafe { color: #DFAF8F; } .keyword\.unsafe { color: #DFAF8F; }
.keyword\.control { color: #F0DFAF; font-weight: bold; } .keyword\.control { color: #F0DFAF; font-weight: bold; }
</style> </style>
<pre><code><span class="attribute">#</span><span class="attribute">[</span><span class="attribute text">derive</span><span class="attribute">(</span><span class="attribute">Clone</span><span class="attribute">,</span><span class="attribute"> </span><span class="attribute">Debug</span><span class="attribute">)</span><span class="attribute">]</span> <pre><code><span class="attribute">#</span><span class="attribute">[</span><span class="attribute">derive</span><span class="attribute">(</span><span class="attribute">Clone</span><span class="attribute">,</span><span class="attribute"> </span><span class="attribute">Debug</span><span class="attribute">)</span><span class="attribute">]</span>
<span class="keyword">struct</span> <span class="type">Foo</span> { <span class="keyword">struct</span> <span class="type">Foo</span> {
<span class="keyword">pub</span> <span class="field">x</span>: <span class="type">i32</span>, <span class="keyword">pub</span> <span class="field">x</span>: <span class="type">i32</span>,
<span class="keyword">pub</span> <span class="field">y</span>: <span class="type">i32</span>, <span class="keyword">pub</span> <span class="field">y</span>: <span class="type">i32</span>,

View file

@ -97,6 +97,9 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => "string", STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => "string",
ATTR => "attribute", ATTR => "attribute",
NAME_REF => { NAME_REF => {
if node.ancestors().any(|it| it.kind() == ATTR) {
continue;
}
if let Some(name_ref) = node.as_node().cloned().and_then(ast::NameRef::cast) { if let Some(name_ref) = node.as_node().cloned().and_then(ast::NameRef::cast) {
// FIXME: try to reuse the SourceAnalyzers // FIXME: try to reuse the SourceAnalyzers
let analyzer = hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None); let analyzer = hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None);

View file

@ -98,8 +98,12 @@ impl WorldState {
} }
// FIXME: Read default cfgs from config // FIXME: Read default cfgs from config
let default_cfg_options = let default_cfg_options = {
get_rustc_cfg_options().atom("test".into()).atom("debug_assertion".into()); 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 // Create crate graph from all the workspaces
let mut crate_graph = CrateGraph::default(); let mut crate_graph = CrateGraph::default();

View file

@ -134,13 +134,16 @@ impl ProjectWorkspace {
json_project::Edition::Edition2015 => Edition::Edition2015, json_project::Edition::Edition2015 => Edition::Edition2015,
json_project::Edition::Edition2018 => Edition::Edition2018, json_project::Edition::Edition2018 => Edition::Edition2018,
}; };
let mut cfg_options = default_cfg_options.clone(); let cfg_options = {
let mut opts = default_cfg_options.clone();
for name in &krate.atom_cfgs { for name in &krate.atom_cfgs {
cfg_options = cfg_options.atom(name.into()); opts.insert_atom(name.into());
} }
for (key, value) in &krate.key_value_cfgs { for (key, value) in &krate.key_value_cfgs {
cfg_options = cfg_options.key_value(key.into(), value.into()); opts.insert_key_value(key.into(), value.into());
} }
opts
};
crates.insert( crates.insert(
crate_id, crate_id,
crate_graph.add_crate_root(file_id, edition, cfg_options), crate_graph.add_crate_root(file_id, edition, cfg_options),
@ -171,7 +174,12 @@ impl ProjectWorkspace {
for krate in sysroot.crates() { for krate in sysroot.crates() {
if let Some(file_id) = load(krate.root(&sysroot)) { if let Some(file_id) = load(krate.root(&sysroot)) {
// Crates from sysroot have `cfg(test)` disabled // 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 = let crate_id =
crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options); crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options);
sysroot_crates.insert(krate, crate_id); sysroot_crates.insert(krate, crate_id);
@ -202,9 +210,11 @@ impl ProjectWorkspace {
let root = tgt.root(&cargo); let root = tgt.root(&cargo);
if let Some(file_id) = load(root) { if let Some(file_id) = load(root) {
let edition = pkg.edition(&cargo); let edition = pkg.edition(&cargo);
let cfg_options = default_cfg_options let cfg_options = {
.clone() let mut opts = default_cfg_options.clone();
.features(pkg.features(&cargo).iter().map(Into::into)); opts.insert_features(pkg.features(&cargo).iter().map(Into::into));
opts
};
let crate_id = let crate_id =
crate_graph.add_crate_root(file_id, edition, cfg_options); crate_graph.add_crate_root(file_id, edition, cfg_options);
names.insert(crate_id, pkg.name(&cargo).to_string()); names.insert(crate_id, pkg.name(&cargo).to_string());
@ -310,6 +320,14 @@ fn find_cargo_toml(path: &Path) -> Result<PathBuf> {
pub fn get_rustc_cfg_options() -> CfgOptions { pub fn get_rustc_cfg_options() -> CfgOptions {
let mut cfg_options = CfgOptions::default(); let mut cfg_options = CfgOptions::default();
// Some nightly-only cfgs, which are required for stdlib
{
cfg_options.insert_atom("target_thread_local".into());
for &target_has_atomic in ["16", "32", "64", "8", "cas", "ptr"].iter() {
cfg_options.insert_key_value("target_has_atomic".into(), target_has_atomic.into())
}
}
match (|| -> Result<_> { match (|| -> Result<_> {
// `cfg(test)` and `cfg(debug_assertion)` are handled outside, so we suppress them here. // `cfg(test)` and `cfg(debug_assertion)` are handled outside, so we suppress them here.
let output = Command::new("rustc").args(&["--print", "cfg", "-O"]).output()?; let output = Command::new("rustc").args(&["--print", "cfg", "-O"]).output()?;
@ -321,11 +339,11 @@ pub fn get_rustc_cfg_options() -> CfgOptions {
Ok(rustc_cfgs) => { Ok(rustc_cfgs) => {
for line in rustc_cfgs.lines() { for line in rustc_cfgs.lines() {
match line.find('=') { match line.find('=') {
None => cfg_options = cfg_options.atom(line.into()), None => cfg_options.insert_atom(line.into()),
Some(pos) => { Some(pos) => {
let key = &line[..pos]; let key = &line[..pos];
let value = line[pos + 1..].trim_matches('"'); 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());
} }
} }
} }