]> git.lizzy.rs Git - rust.git/commitdiff
Apply couple of rule of thumbs to simplify highlighting code
authorAleksey Kladov <aleksey.kladov@gmail.com>
Wed, 19 Aug 2020 15:53:41 +0000 (17:53 +0200)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Wed, 19 Aug 2020 15:53:41 +0000 (17:53 +0200)
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

index ad49f8e1713e3f63e4ae40d1191e4e1d7e11a174..db8aaed48c9c68a607339fb340febc7d37b990bb 100644 (file)
@@ -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<ast::NameRef>,
-    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) => {