From f269fe71569984dea7738926d164f284552196ed Mon Sep 17 00:00:00 2001 From: Chetan Khilosiya Date: Mon, 29 Mar 2021 22:57:05 +0530 Subject: 8024: Added the trait modifier for methods method in impls and method calls will have trait modifier. --- crates/ide/src/syntax_highlighting/highlight.rs | 19 ++++++++++++++++++- crates/ide/src/syntax_highlighting/tags.rs | 4 ++++ crates/rust-analyzer/src/semantic_tokens.rs | 1 + crates/rust-analyzer/src/to_proto.rs | 1 + 4 files changed, 24 insertions(+), 1 deletion(-) (limited to 'crates') diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index b0cfdd8b7..26118929b 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, Semantics, VariantDef}; +use hir::{AsAssocItem, AssocItemContainer, Semantics, VariantDef}; use ide_db::{ defs::{Definition, NameClass, NameRefClass}, RootDatabase, SymbolKind, @@ -275,6 +275,19 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { hir::ModuleDef::Module(_) => HlTag::Symbol(SymbolKind::Module), hir::ModuleDef::Function(func) => { let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Function)); + if let Some(item) = func.as_assoc_item(db) { + match item.container(db) { + AssocItemContainer::Impl(i) => { + if i.target_trait(db).is_some() { + h |= HlMod::Trait; + } + } + AssocItemContainer::Trait(_t) => { + h |= HlMod::Trait; + } + } + } + if func.as_assoc_item(db).is_some() { h |= HlMod::Associated; if func.self_param(db).is_none() { @@ -362,6 +375,10 @@ fn highlight_method_call( if func.is_unsafe(sema.db) || sema.is_unsafe_method_call(&method_call) { h |= HlMod::Unsafe; } + if let Some(_t) = func.as_assoc_item(sema.db)?.containing_trait(sema.db) { + h |= HlMod::Trait + } + if let Some(self_param) = func.self_param(sema.db) { match self_param.access(sema.db) { hir::Access::Shared => (), diff --git a/crates/ide/src/syntax_highlighting/tags.rs b/crates/ide/src/syntax_highlighting/tags.rs index 93db79b89..04540813c 100644 --- a/crates/ide/src/syntax_highlighting/tags.rs +++ b/crates/ide/src/syntax_highlighting/tags.rs @@ -58,6 +58,8 @@ pub enum HlMod { Associated, /// Used for intra doc links in doc injection. IntraDocLink, + /// Used for trait items in impls. + Trait, /// Keep this last! Unsafe, @@ -158,6 +160,7 @@ impl HlMod { HlMod::Callable, HlMod::Static, HlMod::Associated, + HlMod::Trait, HlMod::Unsafe, ]; @@ -174,6 +177,7 @@ impl HlMod { HlMod::IntraDocLink => "intra_doc_link", HlMod::Mutable => "mutable", HlMod::Static => "static", + HlMod::Trait => "trait", HlMod::Unsafe => "unsafe", } } diff --git a/crates/rust-analyzer/src/semantic_tokens.rs b/crates/rust-analyzer/src/semantic_tokens.rs index a3c5e9ccf..2dc8a42f1 100644 --- a/crates/rust-analyzer/src/semantic_tokens.rs +++ b/crates/rust-analyzer/src/semantic_tokens.rs @@ -88,6 +88,7 @@ define_semantic_token_modifiers![ (CONSUMING, "consuming"), (UNSAFE, "unsafe"), (ATTRIBUTE_MODIFIER, "attribute"), + (TRAIT_MODIFIER, "trait"), (CALLABLE, "callable"), (INTRA_DOC_LINK, "intraDocLink"), ]; diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index e297a72e6..c3820944b 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -474,6 +474,7 @@ fn semantic_token_type_and_modifiers( HlMod::Callable => semantic_tokens::CALLABLE, HlMod::Static => lsp_types::SemanticTokenModifier::STATIC, HlMod::IntraDocLink => semantic_tokens::INTRA_DOC_LINK, + HlMod::Trait => semantic_tokens::TRAIT_MODIFIER, HlMod::Associated => continue, }; mods |= modifier; -- cgit v1.2.3 From 56f624532aec04a3a1cccf48ff62c490f52826a0 Mon Sep 17 00:00:00 2001 From: Chetan Khilosiya Date: Wed, 31 Mar 2021 00:03:01 +0530 Subject: 8024: Updated the implementation for trait modifier. Fixed the test cases. --- crates/ide/src/syntax_highlighting/highlight.rs | 28 +++++++++++++++------- crates/ide/src/syntax_highlighting/tags.rs | 2 +- .../test_data/highlight_assoc_functions.html | 8 +++---- .../test_data/highlight_injection.html | 2 +- .../test_data/highlight_unsafe.html | 6 ++--- .../test_data/highlighting.html | 4 ++-- 6 files changed, 30 insertions(+), 20 deletions(-) (limited to 'crates') diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index 26118929b..e218b3dc8 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs @@ -276,6 +276,11 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { hir::ModuleDef::Function(func) => { let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Function)); if let Some(item) = func.as_assoc_item(db) { + h |= HlMod::Associated; + if func.self_param(db).is_none() { + h |= HlMod::Static + } + match item.container(db) { AssocItemContainer::Impl(i) => { if i.target_trait(db).is_some() { @@ -288,12 +293,6 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { } } - if func.as_assoc_item(db).is_some() { - h |= HlMod::Associated; - if func.self_param(db).is_none() { - h |= HlMod::Static - } - } if func.is_unsafe(db) { h |= HlMod::Unsafe; } @@ -305,9 +304,20 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { hir::ModuleDef::Variant(_) => HlTag::Symbol(SymbolKind::Variant), hir::ModuleDef::Const(konst) => { let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Const)); - if konst.as_assoc_item(db).is_some() { - h |= HlMod::Associated + if let Some(item) = konst.as_assoc_item(db) { + h |= HlMod::Associated; + match item.container(db) { + AssocItemContainer::Impl(i) => { + if i.target_trait(db).is_some() { + h |= HlMod::Trait; + } + } + AssocItemContainer::Trait(_t) => { + h |= HlMod::Trait; + } + } } + return h; } hir::ModuleDef::Trait(_) => HlTag::Symbol(SymbolKind::Trait), @@ -375,7 +385,7 @@ fn highlight_method_call( if func.is_unsafe(sema.db) || sema.is_unsafe_method_call(&method_call) { h |= HlMod::Unsafe; } - if let Some(_t) = func.as_assoc_item(sema.db)?.containing_trait(sema.db) { + if func.as_assoc_item(sema.db).and_then(|it| it.containing_trait(sema.db)).is_some() { h |= HlMod::Trait } diff --git a/crates/ide/src/syntax_highlighting/tags.rs b/crates/ide/src/syntax_highlighting/tags.rs index 04540813c..1cec991aa 100644 --- a/crates/ide/src/syntax_highlighting/tags.rs +++ b/crates/ide/src/syntax_highlighting/tags.rs @@ -58,7 +58,7 @@ pub enum HlMod { Associated, /// Used for intra doc links in doc injection. IntraDocLink, - /// Used for trait items in impls. + /// Used for items in traits and trait impls. Trait, /// Keep this last! 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 4635ea927..8cde3906c 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 @@ -47,12 +47,12 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd } 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_injection.html b/crates/ide/src/syntax_highlighting/test_data/highlight_injection.html index 9215ddd9e..7c6694a27 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 6a6555208..72910421d 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html @@ -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() { @@ -96,6 +96,6 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd 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 1eaa7b75b..973173254 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlighting.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlighting.html @@ -67,11 +67,11 @@ 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 } } -- cgit v1.2.3 From d7dcd41801b319f64f3ca2ed22735ab70092e491 Mon Sep 17 00:00:00 2001 From: Chetan Khilosiya Date: Wed, 31 Mar 2021 00:30:01 +0530 Subject: 8024: Added test case for highlighting trait items. --- .../test_data/highlight_trait_items.html | 67 ++++++++++++++++++++++ crates/ide/src/syntax_highlighting/tests.rs | 37 ++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 crates/ide/src/syntax_highlighting/test_data/highlight_trait_items.html (limited to 'crates') diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_trait_items.html b/crates/ide/src/syntax_highlighting/test_data/highlight_trait_items.html new file mode 100644 index 000000000..85274a61f --- /dev/null +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_trait_items.html @@ -0,0 +1,67 @@ + + +
struct Cat;
+
+trait Animal {
+    const SOUND: &'static str;
+
+    fn make_sound(&self) {
+        println!("{}", Self::SOUND);
+    }
+}
+
+impl Animal for Cat {
+    const SOUND: &'static str = "Meow!";
+}
+
+impl Cat {
+    const SPEED: u32 = 2;
+
+    fn run() {}
+}
+
+fn main() {
+    let cat = Cat;
+    cat.make_sound();
+    let _sound = Cat::SOUND;
+    let _speed = Cat::SPEED;
+    Cat::run();
+}
+
\ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index 369ae0972..7b3b5caf9 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs @@ -659,6 +659,43 @@ fn foo() { ); } +#[test] +fn test_highlight_trait_items() { + check_highlighting( + r#" +struct Cat; + +trait Animal { + const SOUND: &'static str; + + fn make_sound(&self) { + println!("{}", Self::SOUND); + } +} + +impl Animal for Cat { + const SOUND: &'static str = "Meow!"; +} + +impl Cat { + const SPEED: u32 = 2; + + fn run() {} +} + +fn main() { + let cat = Cat; + cat.make_sound(); + let _sound = Cat::SOUND; + let _speed = Cat::SPEED; + Cat::run(); +} +"#, + expect_file!["./test_data/highlight_trait_items.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. -- cgit v1.2.3 From d0fcd5c5e054efd64585fc3f3eff55fe7bfa1ce2 Mon Sep 17 00:00:00 2001 From: Chetan Khilosiya Date: Wed, 31 Mar 2021 00:47:37 +0530 Subject: 8024: Fix for function name change. --- crates/ide/src/syntax_highlighting/highlight.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index e218b3dc8..5ccb84714 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs @@ -283,7 +283,7 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { match item.container(db) { AssocItemContainer::Impl(i) => { - if i.target_trait(db).is_some() { + if i.trait_(db).is_some() { h |= HlMod::Trait; } } @@ -308,7 +308,7 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { h |= HlMod::Associated; match item.container(db) { AssocItemContainer::Impl(i) => { - if i.target_trait(db).is_some() { + if i.trait_(db).is_some() { h |= HlMod::Trait; } } -- cgit v1.2.3 From 518c9c75481d4dcf6b1e7768fb48990dc74b7bf5 Mon Sep 17 00:00:00 2001 From: Chetan Khilosiya Date: Wed, 31 Mar 2021 01:46:01 +0530 Subject: Revert "8024: Added test case for highlighting trait items." This reverts commit d7dcd41801b319f64f3ca2ed22735ab70092e491. --- .../test_data/highlight_trait_items.html | 67 ---------------------- crates/ide/src/syntax_highlighting/tests.rs | 37 ------------ 2 files changed, 104 deletions(-) delete mode 100644 crates/ide/src/syntax_highlighting/test_data/highlight_trait_items.html (limited to 'crates') diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_trait_items.html b/crates/ide/src/syntax_highlighting/test_data/highlight_trait_items.html deleted file mode 100644 index 85274a61f..000000000 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_trait_items.html +++ /dev/null @@ -1,67 +0,0 @@ - - -
struct Cat;
-
-trait Animal {
-    const SOUND: &'static str;
-
-    fn make_sound(&self) {
-        println!("{}", Self::SOUND);
-    }
-}
-
-impl Animal for Cat {
-    const SOUND: &'static str = "Meow!";
-}
-
-impl Cat {
-    const SPEED: u32 = 2;
-
-    fn run() {}
-}
-
-fn main() {
-    let cat = Cat;
-    cat.make_sound();
-    let _sound = Cat::SOUND;
-    let _speed = Cat::SPEED;
-    Cat::run();
-}
-
\ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index 7b3b5caf9..369ae0972 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs @@ -659,43 +659,6 @@ fn foo() { ); } -#[test] -fn test_highlight_trait_items() { - check_highlighting( - r#" -struct Cat; - -trait Animal { - const SOUND: &'static str; - - fn make_sound(&self) { - println!("{}", Self::SOUND); - } -} - -impl Animal for Cat { - const SOUND: &'static str = "Meow!"; -} - -impl Cat { - const SPEED: u32 = 2; - - fn run() {} -} - -fn main() { - let cat = Cat; - cat.make_sound(); - let _sound = Cat::SOUND; - let _speed = Cat::SPEED; - Cat::run(); -} -"#, - expect_file!["./test_data/highlight_trait_items.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. -- cgit v1.2.3