Skip to content

Commit b56e020

Browse files
committed
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)
1 parent 11a1bb1 commit b56e020

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

crates/ide/src/syntax_highlighting.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -484,9 +484,9 @@ fn highlight_element(
484484
match name_kind {
485485
Some(NameClass::ExternCrate(_)) => HighlightTag::Module.into(),
486486
Some(NameClass::Definition(def)) => {
487-
highlight_def(sema, db, def, None, false) | HighlightModifier::Definition
487+
highlight_def(sema, db, def, None) | HighlightModifier::Definition
488488
}
489-
Some(NameClass::ConstReference(def)) => highlight_def(sema, db, def, None, false),
489+
Some(NameClass::ConstReference(def)) => highlight_def(sema, db, def, None),
490490
Some(NameClass::FieldShorthand { field, .. }) => {
491491
let mut h = HighlightTag::Field.into();
492492
if let Definition::Field(field) = field {
@@ -519,13 +519,20 @@ fn highlight_element(
519519
binding_hash = Some(calc_binding_hash(&name, *shadow_count))
520520
}
521521
};
522-
let possibly_unsafe = match name_ref.syntax().parent() {
523-
Some(parent) => {
524-
matches!(parent.kind(), FIELD_EXPR | RECORD_PAT_FIELD)
522+
523+
let mut h = highlight_def(sema, db, def, Some(name_ref.clone()));
524+
525+
if let Some(parent) = name_ref.syntax().parent() {
526+
if matches!(parent.kind(), FIELD_EXPR | RECORD_PAT_FIELD) {
527+
if let Definition::Field(field) = def {
528+
if let VariantDef::Union(_) = field.parent_def(db) {
529+
h |= HighlightModifier::Unsafe;
530+
}
531+
}
525532
}
526-
None => false,
527-
};
528-
highlight_def(sema, db, def, Some(name_ref), possibly_unsafe)
533+
}
534+
535+
h
529536
}
530537
NameRefClass::FieldShorthand { .. } => HighlightTag::Field.into(),
531538
},
@@ -734,20 +741,10 @@ fn highlight_def(
734741
db: &RootDatabase,
735742
def: Definition,
736743
name_ref: Option<ast::NameRef>,
737-
possibly_unsafe: bool,
738744
) -> Highlight {
739745
match def {
740746
Definition::Macro(_) => HighlightTag::Macro,
741-
Definition::Field(field) => {
742-
let mut h = HighlightTag::Field.into();
743-
if possibly_unsafe {
744-
if let VariantDef::Union(_) = field.parent_def(db) {
745-
h |= HighlightModifier::Unsafe;
746-
}
747-
}
748-
749-
return h;
750-
}
747+
Definition::Field(_) => HighlightTag::Field,
751748
Definition::ModuleDef(def) => match def {
752749
hir::ModuleDef::Module(_) => HighlightTag::Module,
753750
hir::ModuleDef::Function(func) => {

0 commit comments

Comments
 (0)