From 3baa526fb07184ce9804a06c8e0251971eea3b49 Mon Sep 17 00:00:00 2001 From: Anatol Liu Date: Wed, 4 Nov 2020 20:08:46 -0800 Subject: Add static semantic token modifier for associated functions with no &self --- crates/hir/src/code_model.rs | 4 ++ crates/ide/src/syntax_highlighting.rs | 15 ++++++ crates/ide/src/syntax_highlighting/tags.rs | 4 ++ .../test_data/highlight_assoc_functions.html | 56 ++++++++++++++++++++++ .../test_data/highlight_doctest.html | 2 +- .../test_data/highlight_injection.html | 2 +- crates/ide/src/syntax_highlighting/tests.rs | 28 +++++++++++ crates/rust-analyzer/src/to_proto.rs | 1 + 8 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 crates/ide/src/syntax_highlighting/test_data/highlight_assoc_functions.html diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index 30a5e4580..afb849b4d 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -787,6 +787,10 @@ impl Function { pub fn has_body(self, db: &dyn HirDatabase) -> bool { db.function_data(self.id).has_body } + + pub fn source(self, db: &dyn HirDatabase) -> InFile { + self.id.lookup(db.upcast()).source(db.upcast()) + } } // Note: logically, this belongs to `hir_ty`, but we are not using it there yet. diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index efcc8ecfe..c28ff849a 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs @@ -746,6 +746,21 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { if func.is_unsafe(db) { h |= HighlightModifier::Unsafe; } + if let None = func.self_param(db) { + // if enclosing IMPL or TRAIT exists, this is a static method + let fn_parent_kind = func + .source(db) + .value + .syntax() + .parent() + .and_then(|s| s.parent()) + .and_then(|s| Some(s.kind())); + if let Some(SyntaxKind::IMPL) = fn_parent_kind { + h |= HighlightModifier::Static; + } else if let Some(SyntaxKind::TRAIT) = fn_parent_kind { + h |= HighlightModifier::Static; + } + } return h; } hir::ModuleDef::Adt(hir::Adt::Struct(_)) => HighlightTag::Struct, diff --git a/crates/ide/src/syntax_highlighting/tags.rs b/crates/ide/src/syntax_highlighting/tags.rs index e8f78ad52..65e0671a5 100644 --- a/crates/ide/src/syntax_highlighting/tags.rs +++ b/crates/ide/src/syntax_highlighting/tags.rs @@ -65,6 +65,8 @@ pub enum HighlightModifier { Consuming, Unsafe, Callable, + /// Used for associated functions + Static, } impl HighlightTag { @@ -124,6 +126,7 @@ impl HighlightModifier { HighlightModifier::Consuming, HighlightModifier::Unsafe, HighlightModifier::Callable, + HighlightModifier::Static, ]; fn as_str(self) -> &'static str { @@ -137,6 +140,7 @@ impl HighlightModifier { HighlightModifier::Consuming => "consuming", HighlightModifier::Unsafe => "unsafe", HighlightModifier::Callable => "callable", + HighlightModifier::Static => "static", } } 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 new file mode 100644 index 000000000..cd80d72b7 --- /dev/null +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_assoc_functions.html @@ -0,0 +1,56 @@ + + +
fn not_static() {}
+
+struct foo {}
+
+impl foo {
+    pub fn is_static() {}
+    pub fn is_not_static(&self) {}
+}
+
+trait t {
+    fn t_is_static() {}
+    fn t_is_not_static(&self) {}
+}
+
+impl t for foo {
+    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 6322d404f..65fba8b02 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html @@ -53,7 +53,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 } } 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 18addd00d..57c178916 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 @@ 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/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index 2b667b0d4..5c22e2fce 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs @@ -513,6 +513,34 @@ fn test_extern_crate() { ); } +#[test] +fn test_associated_function() { + check_highlighting( + r#" +fn not_static() {} + +struct foo {} + +impl foo { + pub fn is_static() {} + pub fn is_not_static(&self) {} +} + +trait t { + fn t_is_static() {} + fn t_is_not_static(&self) {} +} + +impl t for foo { + pub fn is_static() {} + pub fn is_not_static(&self) {} +} + "#, + expect_file!["./test_data/highlight_assoc_functions.html"], + false, + ) +} + /// Highlights the code given by the `ra_fixture` argument, renders the /// result as HTML, and compares it with the HTML file given as `snapshot`. /// Note that the `snapshot` file is overwritten by the rendered HTML. diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 92b7c7b68..f8ecd8e83 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -426,6 +426,7 @@ fn semantic_token_type_and_modifiers( HighlightModifier::Consuming => semantic_tokens::CONSUMING, HighlightModifier::Unsafe => semantic_tokens::UNSAFE, HighlightModifier::Callable => semantic_tokens::CALLABLE, + HighlightModifier::Static => lsp_types::SemanticTokenModifier::STATIC, }; mods |= modifier; } -- cgit v1.2.3