aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2020-07-13 20:39:01 +0100
committerLukas Wirth <[email protected]>2020-07-13 20:39:01 +0100
commit91b35d882776d7ae8891b3aecf9879164ef183b5 (patch)
tree54ee29e69913743b4f31f2b4d3eb4fb6c6463721 /crates/ra_ide
parent853440775d72974585ca3fe39f9688a4d4302dd3 (diff)
Semantic Highlighting: Emit mutable modifier for 'self' when applicable
Diffstat (limited to 'crates/ra_ide')
-rw-r--r--crates/ra_ide/src/syntax_highlighting.rs27
-rw-r--r--crates/ra_ide/src/syntax_highlighting/tests.rs10
-rw-r--r--crates/ra_ide/test_data/highlighting.html10
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(
566 | T![return] 566 | T![return]
567 | T![while] 567 | T![while]
568 | T![in] => h | HighlightModifier::ControlFlow, 568 | T![in] => h | HighlightModifier::ControlFlow,
569 T![for] if !is_child_of_impl(element) => h | HighlightModifier::ControlFlow, 569 T![for] if !is_child_of_impl(&element) => h | HighlightModifier::ControlFlow,
570 T![unsafe] => h | HighlightModifier::Unsafe, 570 T![unsafe] => h | HighlightModifier::Unsafe,
571 T![true] | T![false] => HighlightTag::BoolLiteral.into(), 571 T![true] | T![false] => HighlightTag::BoolLiteral.into(),
572 T![self] => HighlightTag::SelfKeyword.into(), 572 T![self] => {
573 let self_param_is_mut = element
574 .parent()
575 .and_then(ast::SelfParam::cast)
576 .and_then(|p| p.mut_token())
577 .is_some();
578 // closure to enforce lazyness
579 let self_path = || {
580 sema.resolve_path(&element.parent()?.parent().and_then(ast::Path::cast)?)
581 };
582 if self_param_is_mut
583 || matches!(self_path(),
584 Some(hir::PathResolution::Local(local))
585 if local.is_self(db)
586 && (local.is_mut(db) || local.ty(db).is_mutable_reference())
587 )
588 {
589 HighlightTag::SelfKeyword | HighlightModifier::Mutable
590 } else {
591 HighlightTag::SelfKeyword.into()
592 }
593 }
573 _ => h, 594 _ => h,
574 } 595 }
575 } 596 }
@@ -592,7 +613,7 @@ fn highlight_element(
592 } 613 }
593} 614}
594 615
595fn is_child_of_impl(element: SyntaxElement) -> bool { 616fn is_child_of_impl(element: &SyntaxElement) -> bool {
596 match element.parent() { 617 match element.parent() {
597 Some(e) => e.kind() == IMPL_DEF, 618 Some(e) => e.kind() == IMPL_DEF,
598 _ => false, 619 _ => 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 {
25 } 25 }
26} 26}
27 27
28impl Foo {
29 fn baz(mut self) -> i32 {
30 self.x
31 }
32
33 fn qux(&mut self) {
34 self.x = 0;
35 }
36}
37
28static mut STATIC_MUT: i32 = 0; 38static mut STATIC_MUT: i32 = 0;
29 39
30fn foo<'a, T>() -> T { 40fn 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
51 } 51 }
52} 52}
53 53
54<span class="keyword">impl</span> <span class="struct">Foo</span> {
55 <span class="keyword">fn</span> <span class="function declaration">baz</span>(<span class="keyword">mut</span> <span class="self_keyword mutable">self</span>) -&gt; <span class="builtin_type">i32</span> {
56 <span class="self_keyword">self</span>.<span class="field">x</span>
57 }
58
59 <span class="keyword">fn</span> <span class="function declaration">qux</span>(&<span class="keyword">mut</span> <span class="self_keyword mutable">self</span>) {
60 <span class="self_keyword mutable">self</span>.<span class="field">x</span> = <span class="numeric_literal">0</span>;
61 }
62}
63
54<span class="keyword">static</span> <span class="keyword">mut</span> <span class="static declaration mutable">STATIC_MUT</span>: <span class="builtin_type">i32</span> = <span class="numeric_literal">0</span>; 64<span class="keyword">static</span> <span class="keyword">mut</span> <span class="static declaration mutable">STATIC_MUT</span>: <span class="builtin_type">i32</span> = <span class="numeric_literal">0</span>;
55 65
56<span class="keyword">fn</span> <span class="function declaration">foo</span>&lt;<span class="lifetime declaration">'a</span>, <span class="type_param declaration">T</span>&gt;() -&gt; <span class="type_param">T</span> { 66<span class="keyword">fn</span> <span class="function declaration">foo</span>&lt;<span class="lifetime declaration">'a</span>, <span class="type_param declaration">T</span>&gt;() -&gt; <span class="type_param">T</span> {