Skip to content

Commit 74e72e9

Browse files
bors[bot]Veetahamatthewjasper
authored
Merge #4394 #4414
4394: Simplify r=matklad a=Veetaha 4414: Highlighting improvements r=matklad a=matthewjasper - `static mut`s are highlighted as `mutable`. - The name of the macro declared by `macro_rules!` is now highlighted. Co-authored-by: veetaha <veetaha2@gmail.com> Co-authored-by: Matthew Jasper <mjjasper1@gmail.com>
3 parents a3dbd27 + 2f7c30c + 22b75c4 commit 74e72e9

File tree

11 files changed

+99
-47
lines changed

11 files changed

+99
-47
lines changed

crates/ra_hir/src/code_model.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,10 @@ impl Static {
678678
pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
679679
db.static_data(self.id).name.clone()
680680
}
681+
682+
pub fn is_mut(self, db: &dyn HirDatabase) -> bool {
683+
db.static_data(self.id).mutable
684+
}
681685
}
682686

683687
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]

crates/ra_hir_def/src/data.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,6 @@ impl ConstData {
251251
Arc::new(ConstData::new(db, vis_default, node))
252252
}
253253

254-
pub(crate) fn static_data_query(db: &dyn DefDatabase, konst: StaticId) -> Arc<ConstData> {
255-
let node = konst.lookup(db).source(db);
256-
Arc::new(ConstData::new(db, RawVisibility::private(), node))
257-
}
258-
259254
fn new<N: NameOwner + TypeAscriptionOwner + VisibilityOwner>(
260255
db: &dyn DefDatabase,
261256
vis_default: RawVisibility,
@@ -270,6 +265,32 @@ impl ConstData {
270265
}
271266
}
272267

268+
#[derive(Debug, Clone, PartialEq, Eq)]
269+
pub struct StaticData {
270+
pub name: Option<Name>,
271+
pub type_ref: TypeRef,
272+
pub visibility: RawVisibility,
273+
pub mutable: bool,
274+
}
275+
276+
impl StaticData {
277+
pub(crate) fn static_data_query(db: &dyn DefDatabase, konst: StaticId) -> Arc<StaticData> {
278+
let node = konst.lookup(db).source(db);
279+
let ctx = LowerCtx::new(db, node.file_id);
280+
281+
let name = node.value.name().map(|n| n.as_name());
282+
let type_ref = TypeRef::from_ast_opt(&ctx, node.value.ascribed_type());
283+
let mutable = node.value.mut_token().is_some();
284+
let visibility = RawVisibility::from_ast_with_default(
285+
db,
286+
RawVisibility::private(),
287+
node.map(|n| n.visibility()),
288+
);
289+
290+
Arc::new(StaticData { name, type_ref, visibility, mutable })
291+
}
292+
}
293+
273294
fn collect_items_in_macros(
274295
db: &dyn DefDatabase,
275296
expander: &mut Expander,

crates/ra_hir_def/src/db.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
adt::{EnumData, StructData},
1111
attr::Attrs,
1212
body::{scope::ExprScopes, Body, BodySourceMap},
13-
data::{ConstData, FunctionData, ImplData, TraitData, TypeAliasData},
13+
data::{ConstData, FunctionData, ImplData, StaticData, TraitData, TypeAliasData},
1414
docs::Documentation,
1515
generics::GenericParams,
1616
lang_item::{LangItemTarget, LangItems},
@@ -77,8 +77,8 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> {
7777
#[salsa::invoke(ConstData::const_data_query)]
7878
fn const_data(&self, konst: ConstId) -> Arc<ConstData>;
7979

80-
#[salsa::invoke(ConstData::static_data_query)]
81-
fn static_data(&self, konst: StaticId) -> Arc<ConstData>;
80+
#[salsa::invoke(StaticData::static_data_query)]
81+
fn static_data(&self, konst: StaticId) -> Arc<StaticData>;
8282

8383
#[salsa::invoke(Body::body_with_source_map_query)]
8484
fn body_with_source_map(&self, def: DefWithBodyId) -> (Arc<Body>, Arc<BodySourceMap>);

crates/ra_hir_ty/src/infer.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_hash::FxHashMap;
2222

2323
use hir_def::{
2424
body::Body,
25-
data::{ConstData, FunctionData},
25+
data::{ConstData, FunctionData, StaticData},
2626
expr::{BindingAnnotation, ExprId, PatId},
2727
lang_item::LangItemTarget,
2828
path::{path, Path},
@@ -71,7 +71,7 @@ pub(crate) fn infer_query(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc<Infer
7171
match def {
7272
DefWithBodyId::ConstId(c) => ctx.collect_const(&db.const_data(c)),
7373
DefWithBodyId::FunctionId(f) => ctx.collect_fn(&db.function_data(f)),
74-
DefWithBodyId::StaticId(s) => ctx.collect_const(&db.static_data(s)),
74+
DefWithBodyId::StaticId(s) => ctx.collect_static(&db.static_data(s)),
7575
}
7676

7777
ctx.infer_body();
@@ -485,6 +485,10 @@ impl<'a> InferenceContext<'a> {
485485
self.return_ty = self.make_ty(&data.type_ref);
486486
}
487487

488+
fn collect_static(&mut self, data: &StaticData) {
489+
self.return_ty = self.make_ty(&data.type_ref);
490+
}
491+
488492
fn collect_fn(&mut self, data: &FunctionData) {
489493
let body = Arc::clone(&self.body); // avoid borrow checker problem
490494
let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver)

crates/ra_ide/src/snapshots/highlight_strings.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
.keyword.unsafe { color: #BC8383; font-weight: bold; }
2828
.control { font-style: italic; }
2929
</style>
30-
<pre><code><span class="macro">macro_rules!</span> println {
30+
<pre><code><span class="macro">macro_rules!</span> <span class="macro declaration">println</span> {
3131
($($arg:tt)*) =&gt; ({
3232
$<span class="keyword">crate</span>::io::_print($<span class="keyword">crate</span>::format_args_nl!($($arg)*));
3333
})
3434
}
3535
#[rustc_builtin_macro]
36-
<span class="macro">macro_rules!</span> format_args_nl {
36+
<span class="macro">macro_rules!</span> <span class="macro declaration">format_args_nl</span> {
3737
($fmt:expr) =&gt; {{ <span class="comment">/* compiler built-in */</span> }};
3838
($fmt:expr, $($args:tt)*) =&gt; {{ <span class="comment">/* compiler built-in */</span> }};
3939
}

crates/ra_ide/src/snapshots/highlighting.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@
3333
<span class="keyword">pub</span> <span class="field declaration">y</span>: <span class="builtin_type">i32</span>,
3434
}
3535

36+
<span class="keyword">static</span> <span class="keyword">mut</span> <span class="static declaration mutable">STATIC_MUT</span>: <span class="builtin_type">i32</span> = <span class="numeric_literal">0</span>;
37+
3638
<span class="keyword">fn</span> <span class="function declaration">foo</span>&lt;<span class="lifetime declaration">'a</span>, <span class="type_param declaration">T</span>&gt;() -&gt; <span class="type_param">T</span> {
3739
<span class="function">foo</span>::&lt;<span class="lifetime">'a</span>, <span class="builtin_type">i32</span>&gt;()
3840
}
3941

40-
<span class="macro">macro_rules!</span> def_fn {
42+
<span class="macro">macro_rules!</span> <span class="macro declaration">def_fn</span> {
4143
($($tt:tt)*) =&gt; {$($tt)*}
4244
}
4345

@@ -56,7 +58,10 @@
5658
<span class="keyword">let</span> <span class="variable declaration">x</span> = <span class="numeric_literal">92</span>;
5759
<span class="variable mutable">vec</span>.<span class="unresolved_reference">push</span>(<span class="struct">Foo</span> { <span class="field">x</span>, <span class="field">y</span>: <span class="numeric_literal">1</span> });
5860
}
59-
<span class="keyword unsafe">unsafe</span> { <span class="variable mutable">vec</span>.<span class="unresolved_reference">set_len</span>(<span class="numeric_literal">0</span>); }
61+
<span class="keyword unsafe">unsafe</span> {
62+
<span class="variable mutable">vec</span>.<span class="unresolved_reference">set_len</span>(<span class="numeric_literal">0</span>);
63+
<span class="static mutable">STATIC_MUT</span> = <span class="numeric_literal">1</span>;
64+
}
6065

6166
<span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable declaration mutable">x</span> = <span class="numeric_literal">42</span>;
6267
<span class="keyword">let</span> <span class="variable declaration mutable">y</span> = &<span class="keyword">mut</span> <span class="variable mutable">x</span>;

crates/ra_ide/src/syntax_highlighting.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,19 @@ pub(crate) fn highlight(
167167
binding_hash: None,
168168
});
169169
}
170+
if let Some(name) = mc.is_macro_rules() {
171+
if let Some((highlight, binding_hash)) = highlight_element(
172+
&sema,
173+
&mut bindings_shadow_count,
174+
name.syntax().clone().into(),
175+
) {
176+
stack.add(HighlightedRange {
177+
range: name.syntax().text_range(),
178+
highlight,
179+
binding_hash,
180+
});
181+
}
182+
}
170183
continue;
171184
}
172185
WalkEvent::Leave(Some(mc)) => {
@@ -431,10 +444,16 @@ fn highlight_name(db: &RootDatabase, def: Definition) -> Highlight {
431444
hir::ModuleDef::Adt(hir::Adt::Union(_)) => HighlightTag::Union,
432445
hir::ModuleDef::EnumVariant(_) => HighlightTag::EnumVariant,
433446
hir::ModuleDef::Const(_) => HighlightTag::Constant,
434-
hir::ModuleDef::Static(_) => HighlightTag::Static,
435447
hir::ModuleDef::Trait(_) => HighlightTag::Trait,
436448
hir::ModuleDef::TypeAlias(_) => HighlightTag::TypeAlias,
437449
hir::ModuleDef::BuiltinType(_) => HighlightTag::BuiltinType,
450+
hir::ModuleDef::Static(s) => {
451+
let mut h = Highlight::new(HighlightTag::Static);
452+
if s.is_mut(db) {
453+
h |= HighlightModifier::Mutable;
454+
}
455+
return h;
456+
}
438457
},
439458
Definition::SelfType(_) => HighlightTag::SelfType,
440459
Definition::TypeParam(_) => HighlightTag::TypeParam,

crates/ra_ide/src/syntax_highlighting/tests.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ struct Foo {
1717
pub y: i32,
1818
}
1919
20+
static mut STATIC_MUT: i32 = 0;
21+
2022
fn foo<'a, T>() -> T {
2123
foo::<'a, i32>()
2224
}
@@ -40,7 +42,10 @@ fn main() {
4042
let x = 92;
4143
vec.push(Foo { x, y: 1 });
4244
}
43-
unsafe { vec.set_len(0); }
45+
unsafe {
46+
vec.set_len(0);
47+
STATIC_MUT = 1;
48+
}
4449
4550
let mut x = 42;
4651
let y = &mut x;

crates/ra_toolchain/src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,9 @@ fn lookup_in_path(exec: &str) -> bool {
5353
let paths = env::var_os("PATH").unwrap_or_default();
5454
let mut candidates = env::split_paths(&paths).flat_map(|path| {
5555
let candidate = path.join(&exec);
56-
let with_exe = if env::consts::EXE_EXTENSION == "" {
57-
None
58-
} else {
59-
Some(candidate.with_extension(env::consts::EXE_EXTENSION))
56+
let with_exe = match env::consts::EXE_EXTENSION {
57+
"" => None,
58+
it => Some(candidate.with_extension(it)),
6059
};
6160
iter::once(candidate).chain(with_exe)
6261
});

crates/rust-analyzer/src/main_loop.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ pub fn main_loop(ws_roots: Vec<PathBuf>, config: Config, connection: Connection)
174174
};
175175

176176
loop_state.roots_total = world_state.vfs.read().n_roots();
177-
loop_state.roots_scanned = 0;
178177

179178
let pool = ThreadPool::default();
180179
let (task_sender, task_receiver) = unbounded::<Task>();
@@ -401,10 +400,12 @@ fn loop_turn(
401400
}
402401

403402
let max_in_flight_libs = pool.max_count().saturating_sub(2).max(1);
404-
while loop_state.in_flight_libraries < max_in_flight_libs
405-
&& !loop_state.pending_libraries.is_empty()
406-
{
407-
let (root, files) = loop_state.pending_libraries.pop().unwrap();
403+
while loop_state.in_flight_libraries < max_in_flight_libs {
404+
let (root, files) = match loop_state.pending_libraries.pop() {
405+
Some(it) => it,
406+
None => break,
407+
};
408+
408409
loop_state.in_flight_libraries += 1;
409410
let sender = libdata_sender.clone();
410411
pool.execute(move || {

0 commit comments

Comments
 (0)