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/ide/src/syntax_highlighting') 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