Skip to content

Commit 6434ada

Browse files
bors[bot]Veykril
andauthored
Merge #10930
10930: fix: Fix self keyword not being tagged as such in highlighting properly r=Veykril a=Veykril Fixes #10575 bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents 372a7cf + b35a50c commit 6434ada

File tree

3 files changed

+57
-61
lines changed

3 files changed

+57
-61
lines changed

crates/ide/src/syntax_highlighting/highlight.rs

Lines changed: 55 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -244,77 +244,71 @@ fn highlight_name_ref(
244244
name_ref: ast::NameRef,
245245
) -> Highlight {
246246
let db = sema.db;
247-
highlight_method_call_by_name_ref(sema, krate, &name_ref).unwrap_or_else(|| {
248-
let name_class = match NameRefClass::classify(sema, &name_ref) {
249-
Some(name_kind) => name_kind,
250-
None => {
251-
return if syntactic_name_ref_highlighting {
252-
highlight_name_ref_by_syntax(name_ref, sema, krate)
253-
} else {
254-
// FIXME: Workaround for https://github.com/rust-analyzer/rust-analyzer/issues/10708
255-
//
256-
// Some popular proc macros (namely async_trait) will rewrite `self` in such a way that it no
257-
// longer resolves via NameRefClass. If we can't be resolved, but we know we're a self token,
258-
// within a function with a self param, pretend to still be `self`, rather than
259-
// an unresolved reference.
260-
if name_ref.self_token().is_some() && is_in_fn_with_self_param(&name_ref) {
261-
SymbolKind::SelfParam.into()
262-
} else {
263-
HlTag::UnresolvedReference.into()
264-
}
265-
};
266-
}
267-
};
268-
let mut h = match name_class {
269-
NameRefClass::Definition(def) => {
270-
if let Definition::Local(local) = &def {
271-
if let Some(name) = local.name(db) {
272-
let shadow_count = bindings_shadow_count.entry(name.clone()).or_default();
273-
*binding_hash = Some(calc_binding_hash(&name, *shadow_count))
274-
}
275-
};
247+
if let Some(res) = highlight_method_call_by_name_ref(sema, krate, &name_ref) {
248+
return res;
249+
}
276250

277-
let mut h = highlight_def(sema, krate, def);
251+
let name_class = match NameRefClass::classify(sema, &name_ref) {
252+
Some(name_kind) => name_kind,
253+
None if syntactic_name_ref_highlighting => {
254+
return highlight_name_ref_by_syntax(name_ref, sema, krate)
255+
}
256+
// FIXME: Workaround for https://github.com/rust-analyzer/rust-analyzer/issues/10708
257+
//
258+
// Some popular proc macros (namely async_trait) will rewrite `self` in such a way that it no
259+
// longer resolves via NameRefClass. If we can't be resolved, but we know we're a self token,
260+
// within a function with a self param, pretend to still be `self`, rather than
261+
// an unresolved reference.
262+
None if name_ref.self_token().is_some() && is_in_fn_with_self_param(&name_ref) => {
263+
return SymbolKind::SelfParam.into()
264+
}
265+
None => return HlTag::UnresolvedReference.into(),
266+
};
267+
let mut h = match name_class {
268+
NameRefClass::Definition(def) => {
269+
if let Definition::Local(local) = &def {
270+
if let Some(name) = local.name(db) {
271+
let shadow_count = bindings_shadow_count.entry(name.clone()).or_default();
272+
*binding_hash = Some(calc_binding_hash(&name, *shadow_count))
273+
}
274+
};
275+
276+
let mut h = highlight_def(sema, krate, def);
278277

279-
match def {
280-
Definition::Local(local)
281-
if is_consumed_lvalue(name_ref.syntax(), &local, db) =>
278+
match def {
279+
Definition::Local(local) if is_consumed_lvalue(name_ref.syntax(), &local, db) => {
280+
h |= HlMod::Consuming;
281+
}
282+
Definition::Trait(trait_) if trait_.is_unsafe(db) => {
283+
if ast::Impl::for_trait_name_ref(&name_ref)
284+
.map_or(false, |impl_| impl_.unsafe_token().is_some())
282285
{
283-
h |= HlMod::Consuming;
286+
h |= HlMod::Unsafe;
284287
}
285-
Definition::Trait(trait_) if trait_.is_unsafe(db) => {
286-
if ast::Impl::for_trait_name_ref(&name_ref)
287-
.map_or(false, |impl_| impl_.unsafe_token().is_some())
288-
{
289-
h |= HlMod::Unsafe;
290-
}
291-
}
292-
Definition::Field(field) => {
293-
if let Some(parent) = name_ref.syntax().parent() {
294-
if matches!(parent.kind(), FIELD_EXPR | RECORD_PAT_FIELD) {
295-
if let hir::VariantDef::Union(_) = field.parent_def(db) {
296-
h |= HlMod::Unsafe;
297-
}
288+
}
289+
Definition::Field(field) => {
290+
if let Some(parent) = name_ref.syntax().parent() {
291+
if matches!(parent.kind(), FIELD_EXPR | RECORD_PAT_FIELD) {
292+
if let hir::VariantDef::Union(_) = field.parent_def(db) {
293+
h |= HlMod::Unsafe;
298294
}
299295
}
300296
}
301-
_ => (),
302297
}
303-
304-
h
305-
}
306-
NameRefClass::FieldShorthand { .. } => SymbolKind::Field.into(),
307-
};
308-
if h.tag == HlTag::Symbol(SymbolKind::Module) {
309-
if name_ref.self_token().is_some() {
310-
return SymbolKind::SelfParam.into();
311-
}
312-
if name_ref.crate_token().is_some() || name_ref.super_token().is_some() {
313-
h.tag = HlTag::Keyword;
298+
_ => (),
314299
}
300+
301+
h
315302
}
316-
h
317-
})
303+
NameRefClass::FieldShorthand { .. } => SymbolKind::Field.into(),
304+
};
305+
if name_ref.self_token().is_some() {
306+
h.tag = HlTag::Symbol(SymbolKind::SelfParam);
307+
}
308+
if name_ref.crate_token().is_some() || name_ref.super_token().is_some() {
309+
h.tag = HlTag::Keyword;
310+
}
311+
h
318312
}
319313

320314
fn highlight_name(

crates/ide/src/syntax_highlighting/test_data/highlighting.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
<span class="brace">}</span>
8888
<span class="brace">}</span>
8989

90+
<span class="keyword">use</span> <span class="self_keyword crate_root">self</span><span class="operator">::</span><span class="struct">FooCopy</span><span class="operator">::</span><span class="brace">{</span><span class="self_keyword">self</span> <span class="keyword">as</span> <span class="struct declaration">BarCopy</span><span class="brace">}</span><span class="semicolon">;</span>
9091
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="attribute attribute default_library library">derive</span><span class="parenthesis attribute">(</span><span class="derive attribute default_library library">Copy</span><span class="parenthesis attribute">)</span><span class="attribute_bracket attribute">]</span>
9192
<span class="keyword">struct</span> <span class="struct declaration">FooCopy</span> <span class="brace">{</span>
9293
<span class="field declaration">x</span><span class="colon">:</span> <span class="builtin_type">u32</span><span class="comma">,</span>

crates/ide/src/syntax_highlighting/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ impl Foo {
6060
}
6161
}
6262
63+
use self::FooCopy::{self as BarCopy};
6364
#[derive(Copy)]
6465
struct FooCopy {
6566
x: u32,

0 commit comments

Comments
 (0)