From c005ce60a048a5df79b93ce45911ab0c6952a41b Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 23 Apr 2021 16:43:48 +0200 Subject: Tag `yield` and `await` as ControlFlow in semantic highlighting --- crates/ide/src/syntax_highlighting/highlight.rs | 6 ++++-- crates/ide/src/syntax_highlighting/tags.rs | 12 +++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) (limited to 'crates/ide') diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index 18552459b..62213dc47 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs @@ -254,15 +254,17 @@ pub(super) fn element( k if k.is_keyword() => { let h = Highlight::new(HlTag::Keyword); match k { - T![break] + T![await] + | T![break] | T![continue] | T![else] | T![if] + | T![in] | T![loop] | T![match] | T![return] | T![while] - | T![in] => h | HlMod::ControlFlow, + | T![yield] => h | HlMod::ControlFlow, T![for] if !is_child_of_impl(&element) => h | HlMod::ControlFlow, T![unsafe] => h | HlMod::Unsafe, T![true] | T![false] => HlTag::BoolLiteral.into(), diff --git a/crates/ide/src/syntax_highlighting/tags.rs b/crates/ide/src/syntax_highlighting/tags.rs index e58392d67..336af3f4c 100644 --- a/crates/ide/src/syntax_highlighting/tags.rs +++ b/crates/ide/src/syntax_highlighting/tags.rs @@ -47,21 +47,27 @@ pub enum HlMod { /// `foo` in `fn foo(x: i32)` is a definition, `foo` in `foo(90 + 2)` is /// not. Definition, + /// Doc-strings like this one. Documentation, + /// Highlighting injection like rust code in doc strings or ra_fixture. Injected, + /// Mutable binding. Mutable, + /// Value that is being consumed in a function call Consuming, + /// Callable item or value. Callable, - /// Used for associated functions + /// Used for associated functions. Static, - /// Used for items in impls&traits. + /// Used for items in traits and impls. Associated, /// Used for intra doc links in doc injection. IntraDocLink, /// Used for items in traits and trait impls. Trait, - /// Keep this last! + // Keep this last! + /// Used for unsafe functions, mutable statics, union accesses and unsafe operations. Unsafe, } -- cgit v1.2.3 From ef6f596b4b9782406246008b0c49e1101c101fc4 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 23 Apr 2021 16:57:03 +0200 Subject: Sort HlMod variants and ALL const --- crates/ide/src/syntax_highlighting/tags.rs | 27 +++++++++--------- .../test_data/highlight_assoc_functions.html | 12 ++++---- .../test_data/highlight_doctest.html | 12 ++++---- .../test_data/highlight_injection.html | 2 +- .../test_data/highlight_unsafe.html | 6 ++-- .../test_data/highlighting.html | 32 +++++++++++----------- 6 files changed, 45 insertions(+), 46 deletions(-) (limited to 'crates/ide') diff --git a/crates/ide/src/syntax_highlighting/tags.rs b/crates/ide/src/syntax_highlighting/tags.rs index 336af3f4c..3bee0ae46 100644 --- a/crates/ide/src/syntax_highlighting/tags.rs +++ b/crates/ide/src/syntax_highlighting/tags.rs @@ -40,8 +40,14 @@ pub enum HlTag { #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] #[repr(u8)] pub enum HlMod { + /// Used for items in traits and impls. + Associated = 0, /// Used to differentiate individual elements within attributes. - Attribute = 0, + Attribute, + /// Callable item or value. + Callable, + /// Value that is being consumed in a function call + Consuming, /// Used with keywords like `if` and `break`. ControlFlow, /// `foo` in `fn foo(x: i32)` is a definition, `foo` in `foo(90 + 2)` is @@ -51,21 +57,14 @@ pub enum HlMod { Documentation, /// Highlighting injection like rust code in doc strings or ra_fixture. Injected, + /// Used for intra doc links in doc injection. + IntraDocLink, /// Mutable binding. Mutable, - /// Value that is being consumed in a function call - Consuming, - /// Callable item or value. - Callable, /// Used for associated functions. Static, - /// Used for items in traits and impls. - Associated, - /// Used for intra doc links in doc injection. - IntraDocLink, /// Used for items in traits and trait impls. Trait, - // Keep this last! /// Used for unsafe functions, mutable statics, union accesses and unsafe operations. Unsafe, @@ -175,17 +174,17 @@ impl fmt::Display for HlTag { impl HlMod { const ALL: &'static [HlMod; HlMod::Unsafe as u8 as usize + 1] = &[ + HlMod::Associated, HlMod::Attribute, + HlMod::Callable, + HlMod::Consuming, HlMod::ControlFlow, HlMod::Definition, HlMod::Documentation, - HlMod::IntraDocLink, HlMod::Injected, + HlMod::IntraDocLink, HlMod::Mutable, - HlMod::Consuming, - HlMod::Callable, HlMod::Static, - HlMod::Associated, HlMod::Trait, HlMod::Unsafe, ]; diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_assoc_functions.html b/crates/ide/src/syntax_highlighting/test_data/highlight_assoc_functions.html index 8cde3906c..a0ea1db34 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_assoc_functions.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_assoc_functions.html @@ -42,17 +42,17 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd struct foo {} impl foo { - pub fn is_static() {} - pub fn is_not_static(&self) {} + pub fn is_static() {} + pub fn is_not_static(&self) {} } trait t { - fn t_is_static() {} - fn t_is_not_static(&self) {} + fn t_is_static() {} + fn t_is_not_static(&self) {} } impl t for foo { - pub fn is_static() {} - pub fn is_not_static(&self) {} + pub fn is_static() {} + pub fn is_not_static(&self) {} } \ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html index 6ee6d85fb..638f42c2f 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html @@ -50,7 +50,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd // KILLER WHALE /// Ishmael."; /// ``` - pub const bar: bool = true; + pub const bar: bool = true; /// Constructs a new `Foo`. /// @@ -60,7 +60,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd /// # #![allow(unused_mut)] /// let mut foo: Foo = Foo::new(); /// ``` - pub const fn new() -> Foo { + pub const fn new() -> Foo { Foo { bar: true } } @@ -94,15 +94,15 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd /// ```sh /// echo 1 /// ``` - pub fn foo(&self) -> bool { + pub fn foo(&self) -> bool { true } } -/// [`Foo`](Foo) is a struct -/// This function is > [`all_the_links`](all_the_links) < +/// [`Foo`](Foo) is a struct +/// This function is > [`all_the_links`](all_the_links) < /// [`noop`](noop) is a macro below -/// [`Item`] is a struct in the module [`module`] +/// [`Item`] is a struct in the module [`module`] /// /// [`Item`]: module::Item /// [mix_and_match]: ThisShouldntResolve diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_injection.html b/crates/ide/src/syntax_highlighting/test_data/highlight_injection.html index 7c6694a27..6202a03ce 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_injection.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_injection.html @@ -42,7 +42,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd fn main() { fixture(r#" trait Foo { - fn foo() { + fn foo() { println!("2 + 2 = {}", 4); } }"# diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html b/crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html index 72910421d..68165bdbf 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html @@ -47,7 +47,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd struct HasUnsafeFn; impl HasUnsafeFn { - unsafe fn unsafe_method(&self) {} + unsafe fn unsafe_method(&self) {} } struct TypeForStaticMut { @@ -62,11 +62,11 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd } trait DoTheAutoref { - fn calls_autoref(&self); + fn calls_autoref(&self); } impl DoTheAutoref for u16 { - fn calls_autoref(&self) {} + fn calls_autoref(&self) {} } fn main() { diff --git a/crates/ide/src/syntax_highlighting/test_data/highlighting.html b/crates/ide/src/syntax_highlighting/test_data/highlighting.html index c43bcb691..4319e8b50 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlighting.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlighting.html @@ -67,25 +67,25 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd } trait Bar { - fn bar(&self) -> i32; + fn bar(&self) -> i32; } impl Bar for Foo { - fn bar(&self) -> i32 { + fn bar(&self) -> i32 { self.x } } impl Foo { - fn baz(mut self, f: Foo) -> i32 { - f.baz(self) + fn baz(mut self, f: Foo) -> i32 { + f.baz(self) } - fn qux(&mut self) { + fn qux(&mut self) { self.x = 0; } - fn quop(&self) -> i32 { + fn quop(&self) -> i32 { self.x } } @@ -96,15 +96,15 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd } impl FooCopy { - fn baz(self, f: FooCopy) -> u32 { + fn baz(self, f: FooCopy) -> u32 { f.baz(self) } - fn qux(&mut self) { + fn qux(&mut self) { self.x = 0; } - fn quop(&self) -> u32 { + fn quop(&self) -> u32 { self.x } } @@ -128,7 +128,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd } use ops::Fn; -fn baz<F: Fn() -> ()>(f: F) { +fn baz<F: Fn() -> ()>(f: F) { f() } @@ -199,16 +199,16 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd let mut foo = Foo { x, y: x }; let foo2 = Foo { x, y: x }; foo.quop(); - foo.qux(); - foo.baz(foo2); + foo.qux(); + foo.baz(foo2); let mut copy = FooCopy { x }; copy.quop(); - copy.qux(); + copy.qux(); copy.baz(copy); - let a = |x| x; - let bar = Foo::baz; + let a = |x| x; + let bar = Foo::baz; let baz = -42; let baz = -baz; @@ -228,7 +228,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd use Option::*; impl<T> Option<T> { - fn and<U>(self, other: Option<U>) -> Option<(T, U)> { + fn and<U>(self, other: Option<U>) -> Option<(T, U)> { match other { None => unimplemented!(), Nope => Nope, -- cgit v1.2.3 From 1e88f5ec8e0ca6692d705b2100299822abfc4220 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 23 Apr 2021 17:19:46 +0200 Subject: Simplify highlight token match guards --- crates/ide/src/syntax_highlighting/highlight.rs | 176 ++++++++++-------------- crates/ide/src/syntax_highlighting/tags.rs | 18 +++ 2 files changed, 92 insertions(+), 102 deletions(-) (limited to 'crates/ide') diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index 62213dc47..8731699dc 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs @@ -1,6 +1,6 @@ //! Computes color for a single element. -use hir::{AsAssocItem, AssocItemContainer, Semantics, VariantDef}; +use hir::{AsAssocItem, Semantics}; use ide_db::{ defs::{Definition, NameClass, NameRefClass}, RootDatabase, SymbolKind, @@ -45,28 +45,26 @@ pub(super) fn element( }; match name_kind { - Some(NameClass::ExternCrate(_)) => HlTag::Symbol(SymbolKind::Module).into(), + Some(NameClass::ExternCrate(_)) => SymbolKind::Module.into(), Some(NameClass::Definition(def)) => highlight_def(db, def) | HlMod::Definition, Some(NameClass::ConstReference(def)) => highlight_def(db, def), Some(NameClass::PatFieldShorthand { field_ref, .. }) => { let mut h = HlTag::Symbol(SymbolKind::Field).into(); if let Definition::Field(field) = field_ref { - if let VariantDef::Union(_) = field.parent_def(db) { + if let hir::VariantDef::Union(_) = field.parent_def(db) { h |= HlMod::Unsafe; } } - h } None => highlight_name_by_syntax(name) | HlMod::Definition, } } - // Highlight references like the definitions they resolve to NAME_REF if element.ancestors().any(|it| it.kind() == ATTR) => { // even though we track whether we are in an attribute or not we still need this special case // as otherwise we would emit unresolved references for name refs inside attributes - Highlight::from(HlTag::Symbol(SymbolKind::Function)) + SymbolKind::Function.into() } NAME_REF => { let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap(); @@ -74,7 +72,7 @@ pub(super) fn element( let is_self = name_ref.self_token().is_some(); let h = match NameRefClass::classify(sema, &name_ref) { Some(name_kind) => match name_kind { - NameRefClass::ExternCrate(_) => HlTag::Symbol(SymbolKind::Module).into(), + NameRefClass::ExternCrate(_) => SymbolKind::Module.into(), NameRefClass::Definition(def) => { if let Definition::Local(local) = &def { if let Some(name) = local.name(db) { @@ -95,7 +93,7 @@ pub(super) fn element( 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) { + if let hir::VariantDef::Union(_) = field.parent_def(db) { h |= HlMod::Unsafe; } } @@ -104,9 +102,7 @@ pub(super) fn element( h } - NameRefClass::FieldShorthand { .. } => { - HlTag::Symbol(SymbolKind::Field).into() - } + NameRefClass::FieldShorthand { .. } => SymbolKind::Field.into(), }, None if syntactic_name_ref_highlighting => { highlight_name_ref_by_syntax(name_ref, sema) @@ -114,7 +110,7 @@ pub(super) fn element( None => HlTag::UnresolvedReference.into(), }; if h.tag == HlTag::Symbol(SymbolKind::Module) && is_self { - HlTag::Symbol(SymbolKind::SelfParam).into() + SymbolKind::SelfParam.into() } else { h } @@ -135,7 +131,7 @@ pub(super) fn element( INT_NUMBER | FLOAT_NUMBER => HlTag::NumericLiteral.into(), BYTE => HlTag::ByteLiteral.into(), CHAR => HlTag::CharLiteral.into(), - QUESTION => Highlight::new(HlTag::Operator(HlOperator::Other)) | HlMod::ControlFlow, + QUESTION => HlTag::Operator(HlOperator::Other) | HlMod::ControlFlow, LIFETIME => { let lifetime = element.into_node().and_then(ast::Lifetime::cast).unwrap(); @@ -143,44 +139,31 @@ pub(super) fn element( Some(NameClass::Definition(def)) => highlight_def(db, def) | HlMod::Definition, None => match NameRefClass::classify_lifetime(sema, &lifetime) { Some(NameRefClass::Definition(def)) => highlight_def(db, def), - _ => Highlight::new(HlTag::Symbol(SymbolKind::LifetimeParam)), + _ => SymbolKind::LifetimeParam.into(), }, - _ => Highlight::new(HlTag::Symbol(SymbolKind::LifetimeParam)) | HlMod::Definition, + _ => Highlight::from(SymbolKind::LifetimeParam) | HlMod::Definition, } } p if p.is_punct() => match p { - T![&] if element.parent().and_then(ast::BinExpr::cast).is_some() => { - HlTag::Operator(HlOperator::Bitwise).into() - } + T![&] if parent_matches::(&element) => HlOperator::Bitwise.into(), T![&] => { let h = HlTag::Operator(HlOperator::Other).into(); let is_unsafe = element .parent() .and_then(ast::RefExpr::cast) - .map(|ref_expr| sema.is_unsafe_ref_expr(&ref_expr)) - .unwrap_or(false); + .map_or(false, |ref_expr| sema.is_unsafe_ref_expr(&ref_expr)); if is_unsafe { h | HlMod::Unsafe } else { h } } - T![::] | T![->] | T![=>] | T![..] | T![=] | T![@] | T![.] => { - HlTag::Operator(HlOperator::Other).into() - } - T![!] if element.parent().and_then(ast::MacroCall::cast).is_some() => { - HlTag::Symbol(SymbolKind::Macro).into() - } - T![!] if element.parent().and_then(ast::NeverType::cast).is_some() => { - HlTag::BuiltinType.into() - } - T![!] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => { - HlTag::Operator(HlOperator::Logical).into() - } - T![*] if element.parent().and_then(ast::PtrType::cast).is_some() => { - HlTag::Keyword.into() - } - T![*] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => { + T![::] | T![->] | T![=>] | T![..] | T![=] | T![@] | T![.] => HlOperator::Other.into(), + T![!] if parent_matches::(&element) => SymbolKind::Macro.into(), + T![!] if parent_matches::(&element) => HlTag::BuiltinType.into(), + T![!] if parent_matches::(&element) => HlOperator::Logical.into(), + T![*] if parent_matches::(&element) => HlTag::Keyword.into(), + T![*] if parent_matches::(&element) => { let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?; let expr = prefix_expr.expr()?; @@ -188,12 +171,12 @@ pub(super) fn element( if ty.is_raw_ptr() { HlTag::Operator(HlOperator::Other) | HlMod::Unsafe } else if let Some(ast::PrefixOp::Deref) = prefix_expr.op_kind() { - HlTag::Operator(HlOperator::Other).into() + HlOperator::Other.into() } else { - HlTag::Punctuation(HlPunct::Other).into() + HlPunct::Other.into() } } - T![-] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => { + T![-] if parent_matches::(&element) => { let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?; let expr = prefix_expr.expr()?; @@ -203,41 +186,31 @@ pub(super) fn element( } .into() } - _ if element.parent().and_then(ast::PrefixExpr::cast).is_some() => { - HlTag::Operator(HlOperator::Other).into() - } + _ if parent_matches::(&element) => HlOperator::Other.into(), T![+] | T![-] | T![*] | T![/] | T![+=] | T![-=] | T![*=] | T![/=] - if element.parent().and_then(ast::BinExpr::cast).is_some() => + if parent_matches::(&element) => { - HlTag::Operator(HlOperator::Arithmetic).into() + HlOperator::Arithmetic.into() } T![|] | T![&] | T![!] | T![^] | T![|=] | T![&=] | T![^=] - if element.parent().and_then(ast::BinExpr::cast).is_some() => + if parent_matches::(&element) => { - HlTag::Operator(HlOperator::Bitwise).into() + HlOperator::Bitwise.into() } - T![&&] | T![||] if element.parent().and_then(ast::BinExpr::cast).is_some() => { - HlTag::Operator(HlOperator::Logical).into() + T![&&] | T![||] if parent_matches::(&element) => { + HlOperator::Logical.into() } T![>] | T![<] | T![==] | T![>=] | T![<=] | T![!=] - if element.parent().and_then(ast::BinExpr::cast).is_some() => + if parent_matches::(&element) => { - HlTag::Operator(HlOperator::Comparison).into() - } - _ if element.parent().and_then(ast::BinExpr::cast).is_some() => { - HlTag::Operator(HlOperator::Other).into() + HlOperator::Comparison.into() } - _ if element.parent().and_then(ast::RangeExpr::cast).is_some() => { - HlTag::Operator(HlOperator::Other).into() - } - _ if element.parent().and_then(ast::RangePat::cast).is_some() => { - HlTag::Operator(HlOperator::Other).into() - } - _ if element.parent().and_then(ast::RestPat::cast).is_some() => { - HlTag::Operator(HlOperator::Other).into() - } - _ if element.parent().and_then(ast::Attr::cast).is_some() => HlTag::Attribute.into(), - kind => HlTag::Punctuation(match kind { + _ if parent_matches::(&element) => HlOperator::Other.into(), + _ if parent_matches::(&element) => HlOperator::Other.into(), + _ if parent_matches::(&element) => HlOperator::Other.into(), + _ if parent_matches::(&element) => HlOperator::Other.into(), + _ if parent_matches::(&element) => HlTag::Attribute.into(), + kind => match kind { T!['['] | T![']'] => HlPunct::Bracket, T!['{'] | T!['}'] => HlPunct::Brace, T!['('] | T![')'] => HlPunct::Parenthesis, @@ -247,7 +220,7 @@ pub(super) fn element( T![;] => HlPunct::Semi, T![.] => HlPunct::Dot, _ => HlPunct::Other, - }) + } .into(), }, @@ -303,7 +276,6 @@ pub(super) fn element( hash((name, shadow_count)) } } - fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { match def { Definition::Macro(_) => HlTag::Symbol(SymbolKind::Macro), @@ -319,12 +291,12 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { } match item.container(db) { - AssocItemContainer::Impl(i) => { + hir::AssocItemContainer::Impl(i) => { if i.trait_(db).is_some() { h |= HlMod::Trait; } } - AssocItemContainer::Trait(_t) => { + hir::AssocItemContainer::Trait(_t) => { h |= HlMod::Trait; } } @@ -344,12 +316,12 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { if let Some(item) = konst.as_assoc_item(db) { h |= HlMod::Associated; match item.container(db) { - AssocItemContainer::Impl(i) => { + hir::AssocItemContainer::Impl(i) => { if i.trait_(db).is_some() { h |= HlMod::Trait; } } - AssocItemContainer::Trait(_t) => { + hir::AssocItemContainer::Trait(_t) => { h |= HlMod::Trait; } } @@ -363,12 +335,12 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { if let Some(item) = type_.as_assoc_item(db) { h |= HlMod::Associated; match item.container(db) { - AssocItemContainer::Impl(i) => { + hir::AssocItemContainer::Impl(i) => { if i.trait_(db).is_some() { h |= HlMod::Trait; } } - AssocItemContainer::Trait(_t) => { + hir::AssocItemContainer::Trait(_t) => { h |= HlMod::Trait; } } @@ -427,7 +399,7 @@ fn highlight_method_call( method_call: &ast::MethodCallExpr, ) -> Option { let func = sema.resolve_method_call(&method_call)?; - let mut h = HlTag::Symbol(SymbolKind::Function).into(); + let mut h = SymbolKind::Function.into(); h |= HlMod::Associated; if func.is_unsafe(sema.db) || sema.is_unsafe_method_call(&method_call) { h |= HlMod::Unsafe; @@ -463,20 +435,20 @@ fn highlight_name_by_syntax(name: ast::Name) -> Highlight { }; let tag = match parent.kind() { - STRUCT => HlTag::Symbol(SymbolKind::Struct), - ENUM => HlTag::Symbol(SymbolKind::Enum), - VARIANT => HlTag::Symbol(SymbolKind::Variant), - UNION => HlTag::Symbol(SymbolKind::Union), - TRAIT => HlTag::Symbol(SymbolKind::Trait), - TYPE_ALIAS => HlTag::Symbol(SymbolKind::TypeAlias), - TYPE_PARAM => HlTag::Symbol(SymbolKind::TypeParam), - RECORD_FIELD => HlTag::Symbol(SymbolKind::Field), - MODULE => HlTag::Symbol(SymbolKind::Module), - FN => HlTag::Symbol(SymbolKind::Function), - CONST => HlTag::Symbol(SymbolKind::Const), - STATIC => HlTag::Symbol(SymbolKind::Static), - IDENT_PAT => HlTag::Symbol(SymbolKind::Local), - _ => default, + STRUCT => SymbolKind::Struct, + ENUM => SymbolKind::Enum, + VARIANT => SymbolKind::Variant, + UNION => SymbolKind::Union, + TRAIT => SymbolKind::Trait, + TYPE_ALIAS => SymbolKind::TypeAlias, + TYPE_PARAM => SymbolKind::TypeParam, + RECORD_FIELD => SymbolKind::Field, + MODULE => SymbolKind::Module, + FN => SymbolKind::Function, + CONST => SymbolKind::Const, + STATIC => SymbolKind::Static, + IDENT_PAT => SymbolKind::Local, + _ => return default.into(), }; tag.into() @@ -494,20 +466,15 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics { return ast::MethodCallExpr::cast(parent) .and_then(|it| highlight_method_call(sema, &it)) - .unwrap_or_else(|| HlTag::Symbol(SymbolKind::Function).into()); + .unwrap_or_else(|| SymbolKind::Function.into()); } FIELD_EXPR => { let h = HlTag::Symbol(SymbolKind::Field); let is_union = ast::FieldExpr::cast(parent) - .and_then(|field_expr| { - let field = sema.resolve_field(&field_expr)?; - Some(if let VariantDef::Union(_) = field.parent_def(sema.db) { - true - } else { - false - }) - }) - .unwrap_or(false); + .and_then(|field_expr| sema.resolve_field(&field_expr)) + .map_or(false, |field| { + matches!(field.parent_def(sema.db), hir::VariantDef::Union(_)) + }); if is_union { h | HlMod::Unsafe } else { @@ -524,9 +491,9 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics { // within path, decide whether it is module or adt by checking for uppercase name return if name.text().chars().next().unwrap_or_default().is_uppercase() { - HlTag::Symbol(SymbolKind::Struct) + SymbolKind::Struct } else { - HlTag::Symbol(SymbolKind::Module) + SymbolKind::Module } .into(); } @@ -537,11 +504,11 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics HlTag::Symbol(SymbolKind::Function).into(), + CALL_EXPR => SymbolKind::Function.into(), _ => if name.text().chars().next().unwrap_or_default().is_uppercase() { - HlTag::Symbol(SymbolKind::Struct) + SymbolKind::Struct } else { - HlTag::Symbol(SymbolKind::Const) + SymbolKind::Const } .into(), } @@ -576,6 +543,11 @@ fn parents_match(mut node: NodeOrToken, mut kinds: &[Sy kinds.len() == 0 } +#[inline] +fn parent_matches(element: &SyntaxElement) -> bool { + element.parent().map_or(false, |it| N::can_cast(it.kind())) +} + fn is_child_of_impl(element: &SyntaxElement) -> bool { match element.parent() { Some(e) => e.kind() == IMPL, diff --git a/crates/ide/src/syntax_highlighting/tags.rs b/crates/ide/src/syntax_highlighting/tags.rs index 3bee0ae46..a304b3250 100644 --- a/crates/ide/src/syntax_highlighting/tags.rs +++ b/crates/ide/src/syntax_highlighting/tags.rs @@ -234,6 +234,24 @@ impl From for Highlight { } } +impl From for Highlight { + fn from(op: HlOperator) -> Highlight { + Highlight::new(HlTag::Operator(op)) + } +} + +impl From for Highlight { + fn from(punct: HlPunct) -> Highlight { + Highlight::new(HlTag::Punctuation(punct)) + } +} + +impl From for Highlight { + fn from(sym: SymbolKind) -> Highlight { + Highlight::new(HlTag::Symbol(sym)) + } +} + impl Highlight { pub(crate) fn new(tag: HlTag) -> Highlight { Highlight { tag, mods: HlMods::default() } -- cgit v1.2.3