From b56e02007798db24600ccae51c0e892919635cad Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 19 Aug 2020 17:53:41 +0200 Subject: 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) --- crates/ide/src/syntax_highlighting.rs | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) (limited to 'crates/ide/src/syntax_highlighting.rs') diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index ad49f8e17..db8aaed48 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs @@ -484,9 +484,9 @@ fn highlight_element( match name_kind { Some(NameClass::ExternCrate(_)) => HighlightTag::Module.into(), 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, .. }) => { let mut h = HighlightTag::Field.into(); if let Definition::Field(field) = field { @@ -519,13 +519,20 @@ fn highlight_element( binding_hash = Some(calc_binding_hash(&name, *shadow_count)) } }; - let possibly_unsafe = match name_ref.syntax().parent() { - Some(parent) => { - matches!(parent.kind(), FIELD_EXPR | RECORD_PAT_FIELD) + + let mut h = highlight_def(sema, db, def, Some(name_ref.clone())); + + 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(), }, @@ -734,20 +741,10 @@ fn highlight_def( db: &RootDatabase, def: Definition, name_ref: Option, - possibly_unsafe: bool, ) -> Highlight { match def { Definition::Macro(_) => HighlightTag::Macro, - Definition::Field(field) => { - let mut h = HighlightTag::Field.into(); - if possibly_unsafe { - if let VariantDef::Union(_) = field.parent_def(db) { - h |= HighlightModifier::Unsafe; - } - } - - return h; - } + Definition::Field(_) => HighlightTag::Field, Definition::ModuleDef(def) => match def { hir::ModuleDef::Module(_) => HighlightTag::Module, hir::ModuleDef::Function(func) => { -- cgit v1.2.3