From e1d6b7f7c48d82c3c03550bc702e64cd7d079c99 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Jul 2020 14:50:00 +0200 Subject: Use dedicated semantic highlight tag for parameters closes #5106 --- crates/ra_hir/src/code_model.rs | 15 ++++++++++++++- crates/ra_ide/src/syntax_highlighting.rs | 5 +++-- crates/ra_ide/src/syntax_highlighting/html.rs | 5 +++-- crates/ra_ide/src/syntax_highlighting/tags.rs | 2 ++ crates/ra_ide/test_data/highlight_doctest.html | 5 +++-- crates/ra_ide/test_data/highlight_injection.html | 7 ++++--- crates/ra_ide/test_data/highlight_strings.html | 5 +++-- crates/ra_ide/test_data/highlight_unsafe.html | 5 +++-- crates/ra_ide/test_data/highlighting.html | 9 +++++---- crates/ra_ide/test_data/rainbow_highlighting.html | 5 +++-- crates/rust-analyzer/src/to_proto.rs | 1 + 11 files changed, 44 insertions(+), 20 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 1b3525011..04fd335fe 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -33,7 +33,10 @@ use hir_ty::{ }; use ra_db::{CrateId, Edition, FileId}; use ra_prof::profile; -use ra_syntax::ast::{self, AttrsOwner, NameOwner}; +use ra_syntax::{ + ast::{self, AttrsOwner, NameOwner}, + AstNode, +}; use rustc_hash::FxHashSet; use crate::{ @@ -955,6 +958,16 @@ pub struct Local { } impl Local { + pub fn is_param(self, db: &dyn HirDatabase) -> bool { + let src = self.source(db); + match src.value { + Either::Left(bind_pat) => { + bind_pat.syntax().ancestors().any(|it| ast::Param::can_cast(it.kind())) + } + Either::Right(_self_param) => true, + } + } + // FIXME: why is this an option? It shouldn't be? pub fn name(self, db: &dyn HirDatabase) -> Option { let body = db.body(self.parent.into()); diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 028b55902..5bb6f9642 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -630,9 +630,10 @@ fn highlight_name(db: &RootDatabase, def: Definition) -> Highlight { }, Definition::SelfType(_) => HighlightTag::SelfType, Definition::TypeParam(_) => HighlightTag::TypeParam, - // FIXME: distinguish between locals and parameters Definition::Local(local) => { - let mut h = Highlight::new(HighlightTag::Local); + let tag = + if local.is_param(db) { HighlightTag::ValueParam } else { HighlightTag::Local }; + let mut h = Highlight::new(tag); if local.is_mut(db) || local.ty(db).is_mutable_reference() { h |= HighlightModifier::Mutable; } diff --git a/crates/ra_ide/src/syntax_highlighting/html.rs b/crates/ra_ide/src/syntax_highlighting/html.rs index 0c74f7370..0be55bca9 100644 --- a/crates/ra_ide/src/syntax_highlighting/html.rs +++ b/crates/ra_ide/src/syntax_highlighting/html.rs @@ -83,14 +83,15 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .bool_literal { color: #BFE6EB; } .macro { color: #94BFF3; } .module { color: #AFD8AF; } +.value_param { color: #DCDCCC; } .variable { color: #DCDCCC; } .format_specifier { color: #CC696B; } .mutable { text-decoration: underline; } -.unresolved_reference { color: #FC5555; } .escape_sequence { color: #94BFF3; } - .keyword { color: #F0DFAF; font-weight: bold; } .keyword.unsafe { color: #BC8383; font-weight: bold; } .control { font-style: italic; } + +.unresolved_reference { color: #FC5555; text-decoration: wavy underline; } "; diff --git a/crates/ra_ide/src/syntax_highlighting/tags.rs b/crates/ra_ide/src/syntax_highlighting/tags.rs index 13d9dd195..719c6ed3c 100644 --- a/crates/ra_ide/src/syntax_highlighting/tags.rs +++ b/crates/ra_ide/src/syntax_highlighting/tags.rs @@ -41,6 +41,7 @@ pub enum HighlightTag { TypeAlias, TypeParam, Union, + ValueParam, Local, UnresolvedReference, FormatSpecifier, @@ -95,6 +96,7 @@ impl HighlightTag { HighlightTag::TypeAlias => "type_alias", HighlightTag::TypeParam => "type_param", HighlightTag::Union => "union", + HighlightTag::ValueParam => "value_param", HighlightTag::Local => "variable", HighlightTag::UnresolvedReference => "unresolved_reference", } diff --git a/crates/ra_ide/test_data/highlight_doctest.html b/crates/ra_ide/test_data/highlight_doctest.html index e8155def7..a77fabb68 100644 --- a/crates/ra_ide/test_data/highlight_doctest.html +++ b/crates/ra_ide/test_data/highlight_doctest.html @@ -24,15 +24,16 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .bool_literal { color: #BFE6EB; } .macro { color: #94BFF3; } .module { color: #AFD8AF; } +.value_param { color: #DCDCCC; } .variable { color: #DCDCCC; } .format_specifier { color: #CC696B; } .mutable { text-decoration: underline; } -.unresolved_reference { color: #FC5555; } .escape_sequence { color: #94BFF3; } - .keyword { color: #F0DFAF; font-weight: bold; } .keyword.unsafe { color: #BC8383; font-weight: bold; } .control { font-style: italic; } + +.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
/// ```
 /// let _ = "early doctests should not go boom";
diff --git a/crates/ra_ide/test_data/highlight_injection.html b/crates/ra_ide/test_data/highlight_injection.html
index 1b0349bae..e15234936 100644
--- a/crates/ra_ide/test_data/highlight_injection.html
+++ b/crates/ra_ide/test_data/highlight_injection.html
@@ -24,17 +24,18 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 .bool_literal       { color: #BFE6EB; }
 .macro              { color: #94BFF3; }
 .module             { color: #AFD8AF; }
+.value_param        { color: #DCDCCC; }
 .variable           { color: #DCDCCC; }
 .format_specifier   { color: #CC696B; }
 .mutable            { text-decoration: underline; }
-.unresolved_reference { color: #FC5555; }
 .escape_sequence    { color: #94BFF3; }
-
 .keyword            { color: #F0DFAF; font-weight: bold; }
 .keyword.unsafe     { color: #BC8383; font-weight: bold; }
 .control            { font-style: italic; }
+
+.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/ra_ide/test_data/highlight_strings.html b/crates/ra_ide/test_data/highlight_strings.html
index d184b5691..423813366 100644
--- a/crates/ra_ide/test_data/highlight_strings.html
+++ b/crates/ra_ide/test_data/highlight_strings.html
@@ -24,15 +24,16 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 .bool_literal       { color: #BFE6EB; }
 .macro              { color: #94BFF3; }
 .module             { color: #AFD8AF; }
+.value_param        { color: #DCDCCC; }
 .variable           { color: #DCDCCC; }
 .format_specifier   { color: #CC696B; }
 .mutable            { text-decoration: underline; }
-.unresolved_reference { color: #FC5555; }
 .escape_sequence    { color: #94BFF3; }
-
 .keyword            { color: #F0DFAF; font-weight: bold; }
 .keyword.unsafe     { color: #BC8383; font-weight: bold; }
 .control            { font-style: italic; }
+
+.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
 
 
macro_rules! println {
     ($($arg:tt)*) => ({
diff --git a/crates/ra_ide/test_data/highlight_unsafe.html b/crates/ra_ide/test_data/highlight_unsafe.html
index 6936e949f..7585fbb42 100644
--- a/crates/ra_ide/test_data/highlight_unsafe.html
+++ b/crates/ra_ide/test_data/highlight_unsafe.html
@@ -24,15 +24,16 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 .bool_literal       { color: #BFE6EB; }
 .macro              { color: #94BFF3; }
 .module             { color: #AFD8AF; }
+.value_param        { color: #DCDCCC; }
 .variable           { color: #DCDCCC; }
 .format_specifier   { color: #CC696B; }
 .mutable            { text-decoration: underline; }
-.unresolved_reference { color: #FC5555; }
 .escape_sequence    { color: #94BFF3; }
-
 .keyword            { color: #F0DFAF; font-weight: bold; }
 .keyword.unsafe     { color: #BC8383; font-weight: bold; }
 .control            { font-style: italic; }
+
+.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
 
 
unsafe fn unsafe_fn() {}
 
diff --git a/crates/ra_ide/test_data/highlighting.html b/crates/ra_ide/test_data/highlighting.html
index 8d0b38f95..134743c72 100644
--- a/crates/ra_ide/test_data/highlighting.html
+++ b/crates/ra_ide/test_data/highlighting.html
@@ -24,15 +24,16 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 .bool_literal       { color: #BFE6EB; }
 .macro              { color: #94BFF3; }
 .module             { color: #AFD8AF; }
+.value_param        { color: #DCDCCC; }
 .variable           { color: #DCDCCC; }
 .format_specifier   { color: #CC696B; }
 .mutable            { text-decoration: underline; }
-.unresolved_reference { color: #FC5555; }
 .escape_sequence    { color: #94BFF3; }
-
 .keyword            { color: #F0DFAF; font-weight: bold; }
 .keyword.unsafe     { color: #BC8383; font-weight: bold; }
 .control            { font-style: italic; }
+
+.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
 
 
#[derive(Clone, Debug)]
 struct Foo {
@@ -108,8 +109,8 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 use Option::*;
 
 impl<T> Option<T> {
-    fn and<U>(self, other: Option<U>) -> Option<(T, U)> {
-        match other {
+    fn and<U>(self, other: Option<U>) -> Option<(T, U)> {
+        match other {
             None => unimplemented!(),
             Nope => Nope,
         }
diff --git a/crates/ra_ide/test_data/rainbow_highlighting.html b/crates/ra_ide/test_data/rainbow_highlighting.html
index 9516c7441..84a7686b2 100644
--- a/crates/ra_ide/test_data/rainbow_highlighting.html
+++ b/crates/ra_ide/test_data/rainbow_highlighting.html
@@ -24,15 +24,16 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 .bool_literal       { color: #BFE6EB; }
 .macro              { color: #94BFF3; }
 .module             { color: #AFD8AF; }
+.value_param        { color: #DCDCCC; }
 .variable           { color: #DCDCCC; }
 .format_specifier   { color: #CC696B; }
 .mutable            { text-decoration: underline; }
-.unresolved_reference { color: #FC5555; }
 .escape_sequence    { color: #94BFF3; }
-
 .keyword            { color: #F0DFAF; font-weight: bold; }
 .keyword.unsafe     { color: #BC8383; font-weight: bold; }
 .control            { font-style: italic; }
+
+.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
 
 
fn main() {
     let hello = "hello";
diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs
index f6dff1684..f6d2f4fc4 100644
--- a/crates/rust-analyzer/src/to_proto.rs
+++ b/crates/rust-analyzer/src/to_proto.rs
@@ -309,6 +309,7 @@ fn semantic_token_type_and_modifiers(
         }
         HighlightTag::EnumVariant => semantic_tokens::ENUM_MEMBER,
         HighlightTag::Macro => lsp_types::SemanticTokenType::MACRO,
+        HighlightTag::ValueParam => lsp_types::SemanticTokenType::PARAMETER,
         HighlightTag::Local => lsp_types::SemanticTokenType::VARIABLE,
         HighlightTag::TypeParam => lsp_types::SemanticTokenType::TYPE_PARAMETER,
         HighlightTag::Lifetime => semantic_tokens::LIFETIME,
-- 
cgit v1.2.3