diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index 7f98aa316fe8..67ad7c63ddf8 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs @@ -736,7 +736,8 @@ fn highlight_method_call( method_call: &ast::MethodCallExpr, ) -> Option { let func = sema.resolve_method_call(&method_call)?; - let mut h = HighlightTag::Method.into(); + let mut h = HighlightTag::Symbol(SymbolKind::Function).into(); + h |= HighlightModifier::Associated; if func.is_unsafe(sema.db) || sema.is_unsafe_method_call(&method_call) { h |= HighlightModifier::Unsafe; } @@ -765,15 +766,13 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { Definition::ModuleDef(def) => match def { hir::ModuleDef::Module(_) => HighlightTag::Symbol(SymbolKind::Module), hir::ModuleDef::Function(func) => { - let mut h = if func.as_assoc_item(db).is_some() { + let mut h = Highlight::new(HighlightTag::Symbol(SymbolKind::Function)); + if func.as_assoc_item(db).is_some() { + h |= HighlightModifier::Associated; if func.self_param(db).is_none() { - Highlight::from(HighlightTag::Method) | HighlightModifier::Static - } else { - HighlightTag::Method.into() + h |= HighlightModifier::Static } - } else { - HighlightTag::Symbol(SymbolKind::Function).into() - }; + } if func.is_unsafe(db) { h |= HighlightModifier::Unsafe; } @@ -783,9 +782,21 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { hir::ModuleDef::Adt(hir::Adt::Enum(_)) => HighlightTag::Symbol(SymbolKind::Enum), hir::ModuleDef::Adt(hir::Adt::Union(_)) => HighlightTag::Symbol(SymbolKind::Union), hir::ModuleDef::EnumVariant(_) => HighlightTag::Symbol(SymbolKind::Variant), - hir::ModuleDef::Const(_) => HighlightTag::Symbol(SymbolKind::Const), + hir::ModuleDef::Const(konst) => { + let mut h = Highlight::new(HighlightTag::Symbol(SymbolKind::Const)); + if konst.as_assoc_item(db).is_some() { + h |= HighlightModifier::Associated + } + return h; + } hir::ModuleDef::Trait(_) => HighlightTag::Symbol(SymbolKind::Trait), - hir::ModuleDef::TypeAlias(_) => HighlightTag::Symbol(SymbolKind::TypeAlias), + hir::ModuleDef::TypeAlias(type_) => { + let mut h = Highlight::new(HighlightTag::Symbol(SymbolKind::TypeAlias)); + if type_.as_assoc_item(db).is_some() { + h |= HighlightModifier::Associated + } + return h; + } hir::ModuleDef::BuiltinType(_) => HighlightTag::BuiltinType, hir::ModuleDef::Static(s) => { let mut h = Highlight::new(HighlightTag::Symbol(SymbolKind::Static)); diff --git a/crates/ide/src/syntax_highlighting/injection.rs b/crates/ide/src/syntax_highlighting/injection.rs index e97d1be1aa86..9eb184c74bff 100644 --- a/crates/ide/src/syntax_highlighting/injection.rs +++ b/crates/ide/src/syntax_highlighting/injection.rs @@ -179,6 +179,5 @@ pub(super) fn highlight_doc_comment( stack.add(comment); } stack.pop_and_inject(None); - stack - .pop_and_inject(Some(Highlight::from(HighlightTag::Generic) | HighlightModifier::Injected)); + stack.pop_and_inject(Some(Highlight::from(HighlightTag::Dummy) | HighlightModifier::Injected)); } diff --git a/crates/ide/src/syntax_highlighting/tags.rs b/crates/ide/src/syntax_highlighting/tags.rs index e0117a6b2a5d..e07cfb43fe6f 100644 --- a/crates/ide/src/syntax_highlighting/tags.rs +++ b/crates/ide/src/syntax_highlighting/tags.rs @@ -33,9 +33,8 @@ pub enum HighlightTag { Operator, UnresolvedReference, - // FIXME: this two are random and don't fit with the others - Method, - Generic, + // For things which don't have proper Tag, but want to use modifiers. + Dummy, } #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] @@ -56,6 +55,8 @@ pub enum HighlightModifier { Callable, /// Used for associated functions Static, + /// Used for items in impls&traits. + Associated, } impl HighlightTag { @@ -89,10 +90,9 @@ impl HighlightTag { HighlightTag::Comment => "comment", HighlightTag::EscapeSequence => "escape_sequence", HighlightTag::FormatSpecifier => "format_specifier", - HighlightTag::Generic => "generic", + HighlightTag::Dummy => "dummy", HighlightTag::Keyword => "keyword", HighlightTag::Punctuation => "punctuation", - HighlightTag::Method => "method", HighlightTag::NumericLiteral => "numeric_literal", HighlightTag::Operator => "operator", HighlightTag::StringLiteral => "string_literal", @@ -133,6 +133,7 @@ impl HighlightModifier { HighlightModifier::Unsafe => "unsafe", HighlightModifier::Callable => "callable", HighlightModifier::Static => "static", + HighlightModifier::Associated => "associated", } } @@ -199,6 +200,10 @@ impl ops::BitOr for Highlight { } impl HighlightModifiers { + pub fn contains(self, m: HighlightModifier) -> bool { + self.0 & m.mask() == m.mask() + } + pub fn iter(self) -> impl Iterator { HighlightModifier::ALL.iter().copied().filter(move |it| self.0 & it.mask() == it.mask()) } 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 6fb606a4729b..cd80d72b7989 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 @@ -40,17 +40,17 @@ 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 920956b51f0b..56aba43e86bb 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html @@ -36,7 +36,7 @@ .unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
/// ```
-/// let _ = "early doctests should not go boom";
+/// let _ = "early doctests should not go boom";
 /// ```
 struct Foo {
     bar: bool,
@@ -50,10 +50,10 @@
     /// # Examples
     ///
     /// ```
-    /// # #![allow(unused_mut)]
-    /// let mut foo: Foo = Foo::new();
+    /// # #![allow(unused_mut)]
+    /// let mut foo: Foo = Foo::new();
     /// ```
-    pub const fn new() -> Foo {
+    pub const fn new() -> Foo {
         Foo { bar: true }
     }
 
@@ -62,32 +62,32 @@
     /// # Examples
     ///
     /// ```
-    /// use x::y;
+    /// use x::y;
     ///
-    /// let foo = Foo::new();
+    /// let foo = Foo::new();
     ///
     /// // calls bar on foo
-    /// assert!(foo.bar());
+    /// assert!(foo.bar());
     ///
-    /// let bar = foo.bar || Foo::bar;
+    /// let bar = foo.bar || Foo::bar;
     ///
     /// /* multi-line
     ///        comment */
     ///
-    /// let multi_line_string = "Foo
+    /// let multi_line_string = "Foo
     ///   bar
     ///          ";
     ///
     /// ```
     ///
     /// ```rust,no_run
-    /// let foobar = Foo::new().bar();
+    /// let foobar = Foo::new().bar();
     /// ```
     ///
     /// ```sh
     /// echo 1
     /// ```
-    pub fn foo(&self) -> bool {
+    pub fn foo(&self) -> bool {
         true
     }
 }
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 31daf2bd0b4d..57c178916f6a 100644
--- a/crates/ide/src/syntax_highlighting/test_data/highlight_injection.html
+++ b/crates/ide/src/syntax_highlighting/test_data/highlight_injection.html
@@ -40,7 +40,7 @@
 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 67ec73f15928..1d05b771310b 100644
--- a/crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html
+++ b/crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html
@@ -45,7 +45,7 @@
 struct HasUnsafeFn;
 
 impl HasUnsafeFn {
-    unsafe fn unsafe_method(&self) {}
+    unsafe fn unsafe_method(&self) {}
 }
 
 struct TypeForStaticMut {
@@ -60,11 +60,11 @@
 }
 
 trait DoTheAutoref {
-    fn calls_autoref(&self);
+    fn calls_autoref(&self);
 }
 
 impl DoTheAutoref for u16 {
-    fn calls_autoref(&self) {}
+    fn calls_autoref(&self) {}
 }
 
 fn main() {
@@ -78,7 +78,7 @@
             Union { b: 0 } => (),
             Union { a } => (),
         }
-        HasUnsafeFn.unsafe_method();
+        HasUnsafeFn.unsafe_method();
 
         // unsafe deref
         let y = *x;
@@ -94,6 +94,6 @@
         let Packed { a: ref _a } = packed;
 
         // unsafe auto ref of packed field
-        packed.a.calls_autoref();
+        packed.a.calls_autoref();
     }
 }
\ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/test_data/highlighting.html b/crates/ide/src/syntax_highlighting/test_data/highlighting.html index 3530a5fdb4b4..11843441802f 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlighting.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlighting.html @@ -65,25 +65,25 @@ } 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 } } @@ -94,15 +94,15 @@ } impl FooCopy { - fn baz(self, f: FooCopy) -> u32 { - f.baz(self) + 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 } } @@ -178,17 +178,17 @@ let mut foo = Foo { x, y: x }; let foo2 = Foo { x, y: x }; - foo.quop(); - foo.qux(); - foo.baz(foo2); + foo.quop(); + foo.qux(); + foo.baz(foo2); let mut copy = FooCopy { x }; - copy.quop(); - copy.qux(); - copy.baz(copy); + copy.quop(); + copy.qux(); + copy.baz(copy); let a = |x| x; - let bar = Foo::baz; + let bar = Foo::baz; let baz = -42; let baz = -baz; @@ -203,7 +203,7 @@ 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, diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 229df47dc6bf..e0561b5a7a8a 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -381,7 +381,13 @@ fn semantic_token_type_and_modifiers( SymbolKind::ValueParam => lsp_types::SemanticTokenType::PARAMETER, SymbolKind::SelfParam => semantic_tokens::SELF_KEYWORD, SymbolKind::Local => lsp_types::SemanticTokenType::VARIABLE, - SymbolKind::Function => lsp_types::SemanticTokenType::FUNCTION, + SymbolKind::Function => { + if highlight.modifiers.contains(HighlightModifier::Associated) { + lsp_types::SemanticTokenType::METHOD + } else { + lsp_types::SemanticTokenType::FUNCTION + } + } SymbolKind::Const => { mods |= semantic_tokens::CONSTANT; mods |= lsp_types::SemanticTokenModifier::STATIC; @@ -400,8 +406,7 @@ fn semantic_token_type_and_modifiers( SymbolKind::Macro => lsp_types::SemanticTokenType::MACRO, }, HighlightTag::BuiltinType => semantic_tokens::BUILTIN_TYPE, - HighlightTag::Generic => semantic_tokens::GENERIC, - HighlightTag::Method => lsp_types::SemanticTokenType::METHOD, + HighlightTag::Dummy => semantic_tokens::GENERIC, HighlightTag::ByteLiteral | HighlightTag::NumericLiteral => { lsp_types::SemanticTokenType::NUMBER } @@ -431,6 +436,7 @@ fn semantic_token_type_and_modifiers( HighlightModifier::Unsafe => semantic_tokens::UNSAFE, HighlightModifier::Callable => semantic_tokens::CALLABLE, HighlightModifier::Static => lsp_types::SemanticTokenModifier::STATIC, + HighlightModifier::Associated => continue, }; mods |= modifier; }