From 91b35d882776d7ae8891b3aecf9879164ef183b5 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 13 Jul 2020 21:39:01 +0200 Subject: Semantic Highlighting: Emit mutable modifier for 'self' when applicable --- crates/ra_ide/src/syntax_highlighting.rs | 27 +++++++++++++++++++++++--- crates/ra_ide/src/syntax_highlighting/tests.rs | 10 ++++++++++ crates/ra_ide/test_data/highlighting.html | 10 ++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 5bb6f9642..b3236e821 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -566,10 +566,31 @@ fn highlight_element( | T![return] | T![while] | T![in] => h | HighlightModifier::ControlFlow, - T![for] if !is_child_of_impl(element) => h | HighlightModifier::ControlFlow, + T![for] if !is_child_of_impl(&element) => h | HighlightModifier::ControlFlow, T![unsafe] => h | HighlightModifier::Unsafe, T![true] | T![false] => HighlightTag::BoolLiteral.into(), - T![self] => HighlightTag::SelfKeyword.into(), + T![self] => { + let self_param_is_mut = element + .parent() + .and_then(ast::SelfParam::cast) + .and_then(|p| p.mut_token()) + .is_some(); + // closure to enforce lazyness + let self_path = || { + sema.resolve_path(&element.parent()?.parent().and_then(ast::Path::cast)?) + }; + if self_param_is_mut + || matches!(self_path(), + Some(hir::PathResolution::Local(local)) + if local.is_self(db) + && (local.is_mut(db) || local.ty(db).is_mutable_reference()) + ) + { + HighlightTag::SelfKeyword | HighlightModifier::Mutable + } else { + HighlightTag::SelfKeyword.into() + } + } _ => h, } } @@ -592,7 +613,7 @@ fn highlight_element( } } -fn is_child_of_impl(element: SyntaxElement) -> bool { +fn is_child_of_impl(element: &SyntaxElement) -> bool { match element.parent() { Some(e) => e.kind() == IMPL_DEF, _ => false, diff --git a/crates/ra_ide/src/syntax_highlighting/tests.rs b/crates/ra_ide/src/syntax_highlighting/tests.rs index aa7c887d6..87a6e2523 100644 --- a/crates/ra_ide/src/syntax_highlighting/tests.rs +++ b/crates/ra_ide/src/syntax_highlighting/tests.rs @@ -25,6 +25,16 @@ impl Bar for Foo { } } +impl Foo { + fn baz(mut self) -> i32 { + self.x + } + + fn qux(&mut self) { + self.x = 0; + } +} + static mut STATIC_MUT: i32 = 0; fn foo<'a, T>() -> T { diff --git a/crates/ra_ide/test_data/highlighting.html b/crates/ra_ide/test_data/highlighting.html index 134743c72..553811a2f 100644 --- a/crates/ra_ide/test_data/highlighting.html +++ b/crates/ra_ide/test_data/highlighting.html @@ -51,6 +51,16 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd } } +impl Foo { + fn baz(mut self) -> i32 { + self.x + } + + fn qux(&mut self) { + self.x = 0; + } +} + static mut STATIC_MUT: i32 = 0; fn foo<'a, T>() -> T { -- cgit v1.2.3