fix: Fix self keyword not being tagged as such in highlighting properly

This commit is contained in:
Lukas Wirth 2021-12-04 19:07:18 +01:00
parent 3472105ad9
commit b35a50cb10
3 changed files with 57 additions and 61 deletions

View file

@ -244,26 +244,25 @@ fn highlight_name_ref(
name_ref: ast::NameRef, name_ref: ast::NameRef,
) -> Highlight { ) -> Highlight {
let db = sema.db; let db = sema.db;
highlight_method_call_by_name_ref(sema, krate, &name_ref).unwrap_or_else(|| { if let Some(res) = highlight_method_call_by_name_ref(sema, krate, &name_ref) {
return res;
}
let name_class = match NameRefClass::classify(sema, &name_ref) { let name_class = match NameRefClass::classify(sema, &name_ref) {
Some(name_kind) => name_kind, Some(name_kind) => name_kind,
None => { None if syntactic_name_ref_highlighting => {
return if syntactic_name_ref_highlighting { return highlight_name_ref_by_syntax(name_ref, sema, krate)
highlight_name_ref_by_syntax(name_ref, sema, krate) }
} else {
// FIXME: Workaround for https://github.com/rust-analyzer/rust-analyzer/issues/10708 // FIXME: Workaround for https://github.com/rust-analyzer/rust-analyzer/issues/10708
// //
// Some popular proc macros (namely async_trait) will rewrite `self` in such a way that it no // Some popular proc macros (namely async_trait) will rewrite `self` in such a way that it no
// longer resolves via NameRefClass. If we can't be resolved, but we know we're a self token, // longer resolves via NameRefClass. If we can't be resolved, but we know we're a self token,
// within a function with a self param, pretend to still be `self`, rather than // within a function with a self param, pretend to still be `self`, rather than
// an unresolved reference. // an unresolved reference.
if name_ref.self_token().is_some() && is_in_fn_with_self_param(&name_ref) { None if name_ref.self_token().is_some() && is_in_fn_with_self_param(&name_ref) => {
SymbolKind::SelfParam.into() return SymbolKind::SelfParam.into()
} else {
HlTag::UnresolvedReference.into()
}
};
} }
None => return HlTag::UnresolvedReference.into(),
}; };
let mut h = match name_class { let mut h = match name_class {
NameRefClass::Definition(def) => { NameRefClass::Definition(def) => {
@ -277,9 +276,7 @@ fn highlight_name_ref(
let mut h = highlight_def(sema, krate, def); let mut h = highlight_def(sema, krate, def);
match def { match def {
Definition::Local(local) Definition::Local(local) if is_consumed_lvalue(name_ref.syntax(), &local, db) => {
if is_consumed_lvalue(name_ref.syntax(), &local, db) =>
{
h |= HlMod::Consuming; h |= HlMod::Consuming;
} }
Definition::Trait(trait_) if trait_.is_unsafe(db) => { Definition::Trait(trait_) if trait_.is_unsafe(db) => {
@ -305,16 +302,13 @@ fn highlight_name_ref(
} }
NameRefClass::FieldShorthand { .. } => SymbolKind::Field.into(), NameRefClass::FieldShorthand { .. } => SymbolKind::Field.into(),
}; };
if h.tag == HlTag::Symbol(SymbolKind::Module) {
if name_ref.self_token().is_some() { if name_ref.self_token().is_some() {
return SymbolKind::SelfParam.into(); h.tag = HlTag::Symbol(SymbolKind::SelfParam);
} }
if name_ref.crate_token().is_some() || name_ref.super_token().is_some() { if name_ref.crate_token().is_some() || name_ref.super_token().is_some() {
h.tag = HlTag::Keyword; h.tag = HlTag::Keyword;
} }
}
h h
})
} }
fn highlight_name( fn highlight_name(

View file

@ -87,6 +87,7 @@ proc_macros::<span class="macro">mirror!</span> <span class="brace">{</span>
<span class="brace">}</span> <span class="brace">}</span>
<span class="brace">}</span> <span class="brace">}</span>
<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>
<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> <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>
<span class="keyword">struct</span> <span class="struct declaration">FooCopy</span> <span class="brace">{</span> <span class="keyword">struct</span> <span class="struct declaration">FooCopy</span> <span class="brace">{</span>
<span class="field declaration">x</span><span class="colon">:</span> <span class="builtin_type">u32</span><span class="comma">,</span> <span class="field declaration">x</span><span class="colon">:</span> <span class="builtin_type">u32</span><span class="comma">,</span>

View file

@ -60,6 +60,7 @@ impl Foo {
} }
} }
use self::FooCopy::{self as BarCopy};
#[derive(Copy)] #[derive(Copy)]
struct FooCopy { struct FooCopy {
x: u32, x: u32,