aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/syntax_highlighting.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-08-19 16:53:41 +0100
committerAleksey Kladov <[email protected]>2020-08-19 16:53:41 +0100
commitb56e02007798db24600ccae51c0e892919635cad (patch)
tree100c26d33895d03ecf2ea03659657d4904e8f130 /crates/ide/src/syntax_highlighting.rs
parent11a1bb1c3ec02456813ccb5cb0bdb76f9d39b2ec (diff)
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)
Diffstat (limited to 'crates/ide/src/syntax_highlighting.rs')
-rw-r--r--crates/ide/src/syntax_highlighting.rs35
1 files changed, 16 insertions, 19 deletions
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(
484 match name_kind { 484 match name_kind {
485 Some(NameClass::ExternCrate(_)) => HighlightTag::Module.into(), 485 Some(NameClass::ExternCrate(_)) => HighlightTag::Module.into(),
486 Some(NameClass::Definition(def)) => { 486 Some(NameClass::Definition(def)) => {
487 highlight_def(sema, db, def, None, false) | HighlightModifier::Definition 487 highlight_def(sema, db, def, None) | HighlightModifier::Definition
488 } 488 }
489 Some(NameClass::ConstReference(def)) => highlight_def(sema, db, def, None, false), 489 Some(NameClass::ConstReference(def)) => highlight_def(sema, db, def, None),
490 Some(NameClass::FieldShorthand { field, .. }) => { 490 Some(NameClass::FieldShorthand { field, .. }) => {
491 let mut h = HighlightTag::Field.into(); 491 let mut h = HighlightTag::Field.into();
492 if let Definition::Field(field) = field { 492 if let Definition::Field(field) = field {
@@ -519,13 +519,20 @@ fn highlight_element(
519 binding_hash = Some(calc_binding_hash(&name, *shadow_count)) 519 binding_hash = Some(calc_binding_hash(&name, *shadow_count))
520 } 520 }
521 }; 521 };
522 let possibly_unsafe = match name_ref.syntax().parent() { 522
523 Some(parent) => { 523 let mut h = highlight_def(sema, db, def, Some(name_ref.clone()));
524 matches!(parent.kind(), FIELD_EXPR | RECORD_PAT_FIELD) 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 }
525 } 532 }
526 None => false, 533 }
527 }; 534
528 highlight_def(sema, db, def, Some(name_ref), possibly_unsafe) 535 h
529 } 536 }
530 NameRefClass::FieldShorthand { .. } => HighlightTag::Field.into(), 537 NameRefClass::FieldShorthand { .. } => HighlightTag::Field.into(),
531 }, 538 },
@@ -734,20 +741,10 @@ fn highlight_def(
734 db: &RootDatabase, 741 db: &RootDatabase,
735 def: Definition, 742 def: Definition,
736 name_ref: Option<ast::NameRef>, 743 name_ref: Option<ast::NameRef>,
737 possibly_unsafe: bool,
738) -> Highlight { 744) -> Highlight {
739 match def { 745 match def {
740 Definition::Macro(_) => HighlightTag::Macro, 746 Definition::Macro(_) => HighlightTag::Macro,
741 Definition::Field(field) => { 747 Definition::Field(_) => HighlightTag::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 }
751 Definition::ModuleDef(def) => match def { 748 Definition::ModuleDef(def) => match def {
752 hir::ModuleDef::Module(_) => HighlightTag::Module, 749 hir::ModuleDef::Module(_) => HighlightTag::Module,
753 hir::ModuleDef::Function(func) => { 750 hir::ModuleDef::Function(func) => {