mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
Apply couple of rule of thumbs to simplify highlighting code
Main one: instead of adding a parameter to function to handle special case, make the caller handle it. Second main one: make sure that function does a reasonable thing. `highlight_def` picks a color for def, *regardless* of the context the def is use. Feeding an info from the call-site muddies the responsibilities here. Minor smells, flagging the function as having space for improvement in the first place: * many parameters, some of which are set as constants on most call-sites (introduce severalfunction instad) * boolean param (add two functions instead)
This commit is contained in:
parent
11a1bb1c3e
commit
b56e020077
1 changed files with 16 additions and 19 deletions
|
@ -484,9 +484,9 @@ fn highlight_element(
|
||||||
match name_kind {
|
match name_kind {
|
||||||
Some(NameClass::ExternCrate(_)) => HighlightTag::Module.into(),
|
Some(NameClass::ExternCrate(_)) => HighlightTag::Module.into(),
|
||||||
Some(NameClass::Definition(def)) => {
|
Some(NameClass::Definition(def)) => {
|
||||||
highlight_def(sema, db, def, None, false) | HighlightModifier::Definition
|
highlight_def(sema, db, def, None) | HighlightModifier::Definition
|
||||||
}
|
}
|
||||||
Some(NameClass::ConstReference(def)) => highlight_def(sema, db, def, None, false),
|
Some(NameClass::ConstReference(def)) => highlight_def(sema, db, def, None),
|
||||||
Some(NameClass::FieldShorthand { field, .. }) => {
|
Some(NameClass::FieldShorthand { field, .. }) => {
|
||||||
let mut h = HighlightTag::Field.into();
|
let mut h = HighlightTag::Field.into();
|
||||||
if let Definition::Field(field) = field {
|
if let Definition::Field(field) = field {
|
||||||
|
@ -519,13 +519,20 @@ fn highlight_element(
|
||||||
binding_hash = Some(calc_binding_hash(&name, *shadow_count))
|
binding_hash = Some(calc_binding_hash(&name, *shadow_count))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let possibly_unsafe = match name_ref.syntax().parent() {
|
|
||||||
Some(parent) => {
|
let mut h = highlight_def(sema, db, def, Some(name_ref.clone()));
|
||||||
matches!(parent.kind(), FIELD_EXPR | RECORD_PAT_FIELD)
|
|
||||||
|
if let Some(parent) = name_ref.syntax().parent() {
|
||||||
|
if matches!(parent.kind(), FIELD_EXPR | RECORD_PAT_FIELD) {
|
||||||
|
if let Definition::Field(field) = def {
|
||||||
|
if let VariantDef::Union(_) = field.parent_def(db) {
|
||||||
|
h |= HighlightModifier::Unsafe;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
None => false,
|
}
|
||||||
};
|
|
||||||
highlight_def(sema, db, def, Some(name_ref), possibly_unsafe)
|
h
|
||||||
}
|
}
|
||||||
NameRefClass::FieldShorthand { .. } => HighlightTag::Field.into(),
|
NameRefClass::FieldShorthand { .. } => HighlightTag::Field.into(),
|
||||||
},
|
},
|
||||||
|
@ -734,20 +741,10 @@ fn highlight_def(
|
||||||
db: &RootDatabase,
|
db: &RootDatabase,
|
||||||
def: Definition,
|
def: Definition,
|
||||||
name_ref: Option<ast::NameRef>,
|
name_ref: Option<ast::NameRef>,
|
||||||
possibly_unsafe: bool,
|
|
||||||
) -> Highlight {
|
) -> Highlight {
|
||||||
match def {
|
match def {
|
||||||
Definition::Macro(_) => HighlightTag::Macro,
|
Definition::Macro(_) => HighlightTag::Macro,
|
||||||
Definition::Field(field) => {
|
Definition::Field(_) => HighlightTag::Field,
|
||||||
let mut h = HighlightTag::Field.into();
|
|
||||||
if possibly_unsafe {
|
|
||||||
if let VariantDef::Union(_) = field.parent_def(db) {
|
|
||||||
h |= HighlightModifier::Unsafe;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return h;
|
|
||||||
}
|
|
||||||
Definition::ModuleDef(def) => match def {
|
Definition::ModuleDef(def) => match def {
|
||||||
hir::ModuleDef::Module(_) => HighlightTag::Module,
|
hir::ModuleDef::Module(_) => HighlightTag::Module,
|
||||||
hir::ModuleDef::Function(func) => {
|
hir::ModuleDef::Function(func) => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue