diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 5960b3cc1c..099b488a15 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -1840,6 +1840,11 @@ impl Local { matches!(&body[self.pat_id], Pat::Bind { mode: BindingAnnotation::Mutable, .. }) } + pub fn is_ref(self, db: &dyn HirDatabase) -> bool { + let body = db.body(self.parent); + matches!(&body[self.pat_id], Pat::Bind { mode: BindingAnnotation::Ref, .. }) + } + pub fn parent(self, _db: &dyn HirDatabase) -> DefWithBody { self.parent.into() } @@ -2195,6 +2200,10 @@ impl Type { matches!(self.ty.kind(&Interner), TyKind::Ref(hir_ty::Mutability::Mut, ..)) } + pub fn is_reference(&self) -> bool { + matches!(self.ty.kind(&Interner), TyKind::Ref(hir_ty::Mutability::Not, ..)) + } + pub fn is_usize(&self) -> bool { matches!(self.ty.kind(&Interner), TyKind::Scalar(Scalar::Uint(UintTy::Usize))) } diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index 6ace8ce83c..23bc85e333 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs @@ -375,11 +375,11 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) if let Some(item) = func.as_assoc_item(db) { h |= HlMod::Associated; match func.self_param(db) { - Some(sp) => { - if let hir::Access::Exclusive = sp.access(db) { - h |= HlMod::Mutable; - } - } + Some(sp) => match sp.access(db) { + hir::Access::Exclusive => h |= HlMod::Mutable, + hir::Access::Shared => h |= HlMod::Reference, + _ => {} + }, None => h |= HlMod::Static, } @@ -460,6 +460,8 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) if s.is_mut(db) { h |= HlMod::Mutable; h |= HlMod::Unsafe; + } else { + h |= HlMod::Reference; } h @@ -491,6 +493,9 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) if ty.as_callable(db).is_some() || ty.impls_fnonce(db) { h |= HlMod::Callable; } + if local.is_ref(db) || ty.is_reference() { + h |= HlMod::Reference; + } h } Definition::Label(_) => Highlight::new(HlTag::Symbol(SymbolKind::Label)), @@ -549,7 +554,7 @@ fn highlight_method_call( if let Some(self_param) = func.self_param(sema.db) { match self_param.access(sema.db) { - hir::Access::Shared => (), + hir::Access::Shared => h |= HlMod::Reference, hir::Access::Exclusive => h |= HlMod::Mutable, hir::Access::Owned => { if let Some(receiver_ty) = diff --git a/crates/ide/src/syntax_highlighting/html.rs b/crates/ide/src/syntax_highlighting/html.rs index 21376a7ae2..6064ef4517 100644 --- a/crates/ide/src/syntax_highlighting/html.rs +++ b/crates/ide/src/syntax_highlighting/html.rs @@ -87,6 +87,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .keyword { color: #F0DFAF; font-weight: bold; } .keyword.unsafe { color: #BC8383; font-weight: bold; } .control { font-style: italic; } +.reference { font-style: italic; font-weight: bold; } .unresolved_reference { color: #FC5555; text-decoration: wavy underline; } diff --git a/crates/ide/src/syntax_highlighting/tags.rs b/crates/ide/src/syntax_highlighting/tags.rs index 82c5b609eb..95c6a3d5af 100644 --- a/crates/ide/src/syntax_highlighting/tags.rs +++ b/crates/ide/src/syntax_highlighting/tags.rs @@ -64,6 +64,8 @@ pub enum HlMod { IntraDocLink, /// Mutable binding. Mutable, + /// Immutable reference. + Reference, /// Used for associated functions. Static, /// Used for items in traits and trait impls. @@ -194,6 +196,7 @@ impl HlMod { HlMod::Injected, HlMod::IntraDocLink, HlMod::Mutable, + HlMod::Reference, HlMod::Static, HlMod::Trait, HlMod::Async, @@ -214,6 +217,7 @@ impl HlMod { HlMod::Injected => "injected", HlMod::IntraDocLink => "intra_doc_link", HlMod::Mutable => "mutable", + HlMod::Reference => "reference", HlMod::Static => "static", HlMod::Trait => "trait", HlMod::Async => "async", 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 9326d9d453..a5d2ba31ee 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 @@ -35,6 +35,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .keyword { color: #F0DFAF; font-weight: bold; } .keyword.unsafe { color: #BC8383; font-weight: bold; } .control { font-style: italic; } +.reference { font-style: italic; font-weight: bold; } .unresolved_reference { color: #FC5555; text-decoration: wavy underline; } @@ -44,16 +45,16 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd impl foo { pub fn is_static() {} - pub fn is_not_static(&self) {} + pub fn is_not_static(&self) {} } trait t { fn t_is_static() {} - fn t_is_not_static(&self) {} + fn t_is_not_static(&self) {} } impl t for foo { pub fn is_static() {} - pub fn is_not_static(&self) {} + pub fn is_not_static(&self) {} } \ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html index 4ae702ba5d..982850dc7e 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html @@ -35,6 +35,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .keyword { color: #F0DFAF; font-weight: bold; } .keyword.unsafe { color: #BC8383; font-weight: bold; } .control { font-style: italic; } +.reference { font-style: italic; font-weight: bold; } .unresolved_reference { color: #FC5555; text-decoration: wavy underline; } @@ -94,7 +95,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd /// /* multi-line /// comment */ /// - /// let multi_line_string = "Foo + /// let multi_line_string = "Foo /// bar\n /// "; /// @@ -107,7 +108,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd /// ```sh /// echo 1 /// ``` - pub fn foo(&self) -> bool { + pub fn foo(&self) -> bool { true } } diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_extern_crate.html b/crates/ide/src/syntax_highlighting/test_data/highlight_extern_crate.html index 76d80886d1..e49de797b3 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_extern_crate.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_extern_crate.html @@ -35,6 +35,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .keyword { color: #F0DFAF; font-weight: bold; } .keyword.unsafe { color: #BC8383; font-weight: bold; } .control { font-style: italic; } +.reference { font-style: italic; font-weight: bold; } .unresolved_reference { color: #FC5555; text-decoration: wavy underline; } 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 50df376ae4..0c1aa1fe11 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_injection.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_injection.html @@ -35,10 +35,11 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .keyword { color: #F0DFAF; font-weight: bold; } .keyword.unsafe { color: #BC8383; font-weight: bold; } .control { font-style: italic; } +.reference { font-style: italic; font-weight: bold; } .unresolved_reference { color: #FC5555; text-decoration: wavy underline; } -
fn fixture(ra_fixture: &str) {}
+
fn fixture(ra_fixture: &str) {}
 
 fn main() {
     fixture(r#"
diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html b/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html
index b409a67659..e519b22007 100644
--- a/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html
+++ b/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html
@@ -35,6 +35,7 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 .keyword            { color: #F0DFAF; font-weight: bold; }
 .keyword.unsafe     { color: #BC8383; font-weight: bold; }
 .control            { font-style: italic; }
+.reference          { font-style: italic; font-weight: bold; }
 
 .unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
 
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 bf745f4056..987f06a23a 100644
--- a/crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html
+++ b/crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html
@@ -35,6 +35,7 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 .keyword            { color: #F0DFAF; font-weight: bold; }
 .keyword.unsafe     { color: #BC8383; font-weight: bold; }
 .control            { font-style: italic; }
+.reference          { font-style: italic; font-weight: bold; }
 
 .unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
 
@@ -48,7 +49,7 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 struct HasUnsafeFn;
 
 impl HasUnsafeFn {
-    unsafe fn unsafe_method(&self) {}
+    unsafe fn unsafe_method(&self) {}
 }
 
 struct TypeForStaticMut {
@@ -68,11 +69,11 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 fn require_unsafe_trait<T: UnsafeTrait>(_: T) {}
 
 trait DoTheAutoref {
-    fn calls_autoref(&self);
+    fn calls_autoref(&self);
 }
 
 impl DoTheAutoref for u16 {
-    fn calls_autoref(&self) {}
+    fn calls_autoref(&self) {}
 }
 
 fn main() {
@@ -86,7 +87,7 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
             Union { b: 0 } => (),
             Union { a } => (),
         }
-        HasUnsafeFn.unsafe_method();
+        HasUnsafeFn.unsafe_method();
 
         // unsafe deref
         let y = *x;
@@ -96,12 +97,12 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 
         // unsafe ref of packed fields
         let packed = Packed { a: 0 };
-        let a = &packed.a;
-        let ref a = packed.a;
+        let a = &packed.a;
+        let ref a = packed.a;
         let Packed { ref a } = packed;
-        let Packed { a: ref _a } = packed;
+        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 42416afa25..c566acca64 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlighting.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlighting.html @@ -35,6 +35,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .keyword { color: #F0DFAF; font-weight: bold; } .keyword.unsafe { color: #BC8383; font-weight: bold; } .control { font-style: italic; } +.reference { font-style: italic; font-weight: bold; } .unresolved_reference { color: #FC5555; text-decoration: wavy underline; } @@ -68,12 +69,12 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd } trait Bar where Self: { - fn bar(&self) -> i32; + fn bar(&self) -> i32; } impl Bar for Foo where Self: { - fn bar(&self) -> i32 { - self.x + fn bar(&self) -> i32 { + self.x } } @@ -86,8 +87,8 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd self.x = 0; } - fn quop(&self) -> i32 { - self.x + fn quop(&self) -> i32 { + self.x } } @@ -105,8 +106,8 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd self.x = 0; } - fn quop(&self) -> u32 { - self.x + fn quop(&self) -> u32 { + self.x } } @@ -197,20 +198,20 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd let mut x = 42; x += 1; let y = &mut x; - let z = &y; + let z = &y; - let Foo { x: z, y } = Foo { x: z, y }; + let Foo { x: z, y } = Foo { x: z, y }; y; let mut foo = Foo { x, y: x }; let foo2 = Foo { x, y: x }; - foo.quop(); + foo.quop(); foo.qux(); foo.baz(foo2); let mut copy = FooCopy { x }; - copy.quop(); + copy.quop(); copy.qux(); copy.baz(copy); diff --git a/crates/ide/src/syntax_highlighting/test_data/injection.html b/crates/ide/src/syntax_highlighting/test_data/injection.html index 082837328f..afb32746f1 100644 --- a/crates/ide/src/syntax_highlighting/test_data/injection.html +++ b/crates/ide/src/syntax_highlighting/test_data/injection.html @@ -35,10 +35,11 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .keyword { color: #F0DFAF; font-weight: bold; } .keyword.unsafe { color: #BC8383; font-weight: bold; } .control { font-style: italic; } +.reference { font-style: italic; font-weight: bold; } .unresolved_reference { color: #FC5555; text-decoration: wavy underline; } -
fn f(ra_fixture: &str) {}
+
fn f(ra_fixture: &str) {}
 fn main() {
     f(r"
 fn foo() {
diff --git a/crates/ide/src/syntax_highlighting/test_data/rainbow_highlighting.html b/crates/ide/src/syntax_highlighting/test_data/rainbow_highlighting.html
index 763917714c..f0c96412f1 100644
--- a/crates/ide/src/syntax_highlighting/test_data/rainbow_highlighting.html
+++ b/crates/ide/src/syntax_highlighting/test_data/rainbow_highlighting.html
@@ -35,18 +35,19 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 .keyword            { color: #F0DFAF; font-weight: bold; }
 .keyword.unsafe     { color: #BC8383; font-weight: bold; }
 .control            { font-style: italic; }
+.reference          { font-style: italic; font-weight: bold; }
 
 .unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
 
 
fn main() {
-    let hello = "hello";
-    let x = hello.to_string();
-    let y = hello.to_string();
+    let hello = "hello";
+    let x = hello.to_string();
+    let y = hello.to_string();
 
-    let x = "other color please!";
-    let y = x.to_string();
+    let x = "other color please!";
+    let y = x.to_string();
 }
 
 fn bar() {
-    let mut hello = "hello";
+    let mut hello = "hello";
 }
\ No newline at end of file diff --git a/crates/rust-analyzer/src/semantic_tokens.rs b/crates/rust-analyzer/src/semantic_tokens.rs index 8535e8a135..f2ed98f2a4 100644 --- a/crates/rust-analyzer/src/semantic_tokens.rs +++ b/crates/rust-analyzer/src/semantic_tokens.rs @@ -100,6 +100,7 @@ define_semantic_token_modifiers![ (TRAIT_MODIFIER, "trait"), (CALLABLE, "callable"), (INTRA_DOC_LINK, "intraDocLink"), + (REFERENCE, "reference"), ]; #[derive(Default)] diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 906259b098..8dcfff5c71 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -533,6 +533,7 @@ fn semantic_token_type_and_modifiers( HlMod::Injected => semantic_tokens::INJECTED, HlMod::ControlFlow => semantic_tokens::CONTROL_FLOW, HlMod::Mutable => semantic_tokens::MUTABLE, + HlMod::Reference => semantic_tokens::REFERENCE, HlMod::Consuming => semantic_tokens::CONSUMING, HlMod::Async => semantic_tokens::ASYNC, HlMod::Library => semantic_tokens::LIBRARY,