]> git.lizzy.rs Git - rust.git/commitdiff
Semantic Highlighting: Emit mutable modifier for 'self' when applicable
authorLukas Wirth <lukastw97@gmail.com>
Mon, 13 Jul 2020 19:39:01 +0000 (21:39 +0200)
committerLukas Wirth <lukastw97@gmail.com>
Mon, 13 Jul 2020 19:39:01 +0000 (21:39 +0200)
crates/ra_ide/src/syntax_highlighting.rs
crates/ra_ide/src/syntax_highlighting/tests.rs
crates/ra_ide/test_data/highlighting.html

index 5bb6f96422cbe58178a5061abc5005dffd8acf82..b3236e8210772d0bea77504703ef7ab66f424990 100644 (file)
@@ -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 hash<T: std::hash::Hash + std::fmt::Debug>(x: T) -> u64 {
     }
 }
 
-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,
index aa7c887d674fc87dcdcbbdc93e6a7e2ae353437a..87a6e2523b885bfb6456e287e6557d7cc19b758d 100644 (file)
@@ -25,6 +25,16 @@ fn bar(&self) -> i32 {
     }
 }
 
+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 {
index 134743c72c8af4e2d9413964c191a5c297a9637d..553811a2f2e347cb1e1f80359f8038b0be2f6c84 100644 (file)
@@ -51,6 +51,16 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
     }
 }
 
+<span class="keyword">impl</span> <span class="struct">Foo</span> {
+    <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> {
+        <span class="self_keyword">self</span>.<span class="field">x</span>
+    }
+
+    <span class="keyword">fn</span> <span class="function declaration">qux</span>(&<span class="keyword">mut</span> <span class="self_keyword mutable">self</span>) {
+        <span class="self_keyword mutable">self</span>.<span class="field">x</span> = <span class="numeric_literal">0</span>;
+    }
+}
+
 <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>;
 
 <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> {