Skip to content

Commit e1d6b7f

Browse files
committed
Use dedicated semantic highlight tag for parameters
closes #5106
1 parent 87ddcba commit e1d6b7f

File tree

11 files changed

+44
-20
lines changed

11 files changed

+44
-20
lines changed

crates/ra_hir/src/code_model.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ use hir_ty::{
3333
};
3434
use ra_db::{CrateId, Edition, FileId};
3535
use ra_prof::profile;
36-
use ra_syntax::ast::{self, AttrsOwner, NameOwner};
36+
use ra_syntax::{
37+
ast::{self, AttrsOwner, NameOwner},
38+
AstNode,
39+
};
3740
use rustc_hash::FxHashSet;
3841

3942
use crate::{
@@ -955,6 +958,16 @@ pub struct Local {
955958
}
956959

957960
impl Local {
961+
pub fn is_param(self, db: &dyn HirDatabase) -> bool {
962+
let src = self.source(db);
963+
match src.value {
964+
Either::Left(bind_pat) => {
965+
bind_pat.syntax().ancestors().any(|it| ast::Param::can_cast(it.kind()))
966+
}
967+
Either::Right(_self_param) => true,
968+
}
969+
}
970+
958971
// FIXME: why is this an option? It shouldn't be?
959972
pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
960973
let body = db.body(self.parent.into());

crates/ra_ide/src/syntax_highlighting.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,9 +630,10 @@ fn highlight_name(db: &RootDatabase, def: Definition) -> Highlight {
630630
},
631631
Definition::SelfType(_) => HighlightTag::SelfType,
632632
Definition::TypeParam(_) => HighlightTag::TypeParam,
633-
// FIXME: distinguish between locals and parameters
634633
Definition::Local(local) => {
635-
let mut h = Highlight::new(HighlightTag::Local);
634+
let tag =
635+
if local.is_param(db) { HighlightTag::ValueParam } else { HighlightTag::Local };
636+
let mut h = Highlight::new(tag);
636637
if local.is_mut(db) || local.ty(db).is_mutable_reference() {
637638
h |= HighlightModifier::Mutable;
638639
}

crates/ra_ide/src/syntax_highlighting/html.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,15 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
8383
.bool_literal { color: #BFE6EB; }
8484
.macro { color: #94BFF3; }
8585
.module { color: #AFD8AF; }
86+
.value_param { color: #DCDCCC; }
8687
.variable { color: #DCDCCC; }
8788
.format_specifier { color: #CC696B; }
8889
.mutable { text-decoration: underline; }
89-
.unresolved_reference { color: #FC5555; }
9090
.escape_sequence { color: #94BFF3; }
91-
9291
.keyword { color: #F0DFAF; font-weight: bold; }
9392
.keyword.unsafe { color: #BC8383; font-weight: bold; }
9493
.control { font-style: italic; }
94+
95+
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
9596
</style>
9697
";

crates/ra_ide/src/syntax_highlighting/tags.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub enum HighlightTag {
4141
TypeAlias,
4242
TypeParam,
4343
Union,
44+
ValueParam,
4445
Local,
4546
UnresolvedReference,
4647
FormatSpecifier,
@@ -95,6 +96,7 @@ impl HighlightTag {
9596
HighlightTag::TypeAlias => "type_alias",
9697
HighlightTag::TypeParam => "type_param",
9798
HighlightTag::Union => "union",
99+
HighlightTag::ValueParam => "value_param",
98100
HighlightTag::Local => "variable",
99101
HighlightTag::UnresolvedReference => "unresolved_reference",
100102
}

crates/ra_ide/test_data/highlight_doctest.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@
2424
.bool_literal { color: #BFE6EB; }
2525
.macro { color: #94BFF3; }
2626
.module { color: #AFD8AF; }
27+
.value_param { color: #DCDCCC; }
2728
.variable { color: #DCDCCC; }
2829
.format_specifier { color: #CC696B; }
2930
.mutable { text-decoration: underline; }
30-
.unresolved_reference { color: #FC5555; }
3131
.escape_sequence { color: #94BFF3; }
32-
3332
.keyword { color: #F0DFAF; font-weight: bold; }
3433
.keyword.unsafe { color: #BC8383; font-weight: bold; }
3534
.control { font-style: italic; }
35+
36+
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
3637
</style>
3738
<pre><code><span class="comment documentation">/// ```</span>
3839
<span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="generic injected"> _ = </span><span class="string_literal injected">"early doctests should not go boom"</span><span class="generic injected">;</span>

crates/ra_ide/test_data/highlight_injection.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,18 @@
2424
.bool_literal { color: #BFE6EB; }
2525
.macro { color: #94BFF3; }
2626
.module { color: #AFD8AF; }
27+
.value_param { color: #DCDCCC; }
2728
.variable { color: #DCDCCC; }
2829
.format_specifier { color: #CC696B; }
2930
.mutable { text-decoration: underline; }
30-
.unresolved_reference { color: #FC5555; }
3131
.escape_sequence { color: #94BFF3; }
32-
3332
.keyword { color: #F0DFAF; font-weight: bold; }
3433
.keyword.unsafe { color: #BC8383; font-weight: bold; }
3534
.control { font-style: italic; }
35+
36+
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
3637
</style>
37-
<pre><code><span class="keyword">fn</span> <span class="function declaration">fixture</span>(<span class="variable declaration">ra_fixture</span>: &<span class="builtin_type">str</span>) {}
38+
<pre><code><span class="keyword">fn</span> <span class="function declaration">fixture</span>(<span class="value_param declaration">ra_fixture</span>: &<span class="builtin_type">str</span>) {}
3839

3940
<span class="keyword">fn</span> <span class="function declaration">main</span>() {
4041
<span class="function">fixture</span>(<span class="string_literal">r#"</span>

crates/ra_ide/test_data/highlight_strings.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@
2424
.bool_literal { color: #BFE6EB; }
2525
.macro { color: #94BFF3; }
2626
.module { color: #AFD8AF; }
27+
.value_param { color: #DCDCCC; }
2728
.variable { color: #DCDCCC; }
2829
.format_specifier { color: #CC696B; }
2930
.mutable { text-decoration: underline; }
30-
.unresolved_reference { color: #FC5555; }
3131
.escape_sequence { color: #94BFF3; }
32-
3332
.keyword { color: #F0DFAF; font-weight: bold; }
3433
.keyword.unsafe { color: #BC8383; font-weight: bold; }
3534
.control { font-style: italic; }
35+
36+
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
3637
</style>
3738
<pre><code><span class="macro">macro_rules!</span> <span class="macro declaration">println</span> {
3839
($($arg:tt)*) =&gt; ({

crates/ra_ide/test_data/highlight_unsafe.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@
2424
.bool_literal { color: #BFE6EB; }
2525
.macro { color: #94BFF3; }
2626
.module { color: #AFD8AF; }
27+
.value_param { color: #DCDCCC; }
2728
.variable { color: #DCDCCC; }
2829
.format_specifier { color: #CC696B; }
2930
.mutable { text-decoration: underline; }
30-
.unresolved_reference { color: #FC5555; }
3131
.escape_sequence { color: #94BFF3; }
32-
3332
.keyword { color: #F0DFAF; font-weight: bold; }
3433
.keyword.unsafe { color: #BC8383; font-weight: bold; }
3534
.control { font-style: italic; }
35+
36+
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
3637
</style>
3738
<pre><code><span class="keyword unsafe">unsafe</span> <span class="keyword">fn</span> <span class="function declaration unsafe">unsafe_fn</span>() {}
3839

crates/ra_ide/test_data/highlighting.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@
2424
.bool_literal { color: #BFE6EB; }
2525
.macro { color: #94BFF3; }
2626
.module { color: #AFD8AF; }
27+
.value_param { color: #DCDCCC; }
2728
.variable { color: #DCDCCC; }
2829
.format_specifier { color: #CC696B; }
2930
.mutable { text-decoration: underline; }
30-
.unresolved_reference { color: #FC5555; }
3131
.escape_sequence { color: #94BFF3; }
32-
3332
.keyword { color: #F0DFAF; font-weight: bold; }
3433
.keyword.unsafe { color: #BC8383; font-weight: bold; }
3534
.control { font-style: italic; }
35+
36+
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
3637
</style>
3738
<pre><code><span class="attribute">#[</span><span class="function attribute">derive</span><span class="attribute">(Clone, Debug)]</span>
3839
<span class="keyword">struct</span> <span class="struct declaration">Foo</span> {
@@ -108,8 +109,8 @@
108109
<span class="keyword">use</span> <span class="enum">Option</span>::*;
109110

110111
<span class="keyword">impl</span>&lt;<span class="type_param declaration">T</span>&gt; <span class="enum">Option</span>&lt;<span class="type_param">T</span>&gt; {
111-
<span class="keyword">fn</span> <span class="function declaration">and</span>&lt;<span class="type_param declaration">U</span>&gt;(<span class="self_keyword">self</span>, <span class="variable declaration">other</span>: <span class="enum">Option</span>&lt;<span class="type_param">U</span>&gt;) -&gt; <span class="enum">Option</span>&lt;(<span class="type_param">T</span>, <span class="type_param">U</span>)&gt; {
112-
<span class="keyword control">match</span> <span class="variable">other</span> {
112+
<span class="keyword">fn</span> <span class="function declaration">and</span>&lt;<span class="type_param declaration">U</span>&gt;(<span class="self_keyword">self</span>, <span class="value_param declaration">other</span>: <span class="enum">Option</span>&lt;<span class="type_param">U</span>&gt;) -&gt; <span class="enum">Option</span>&lt;(<span class="type_param">T</span>, <span class="type_param">U</span>)&gt; {
113+
<span class="keyword control">match</span> <span class="value_param">other</span> {
113114
<span class="enum_variant">None</span> =&gt; <span class="macro">unimplemented!</span>(),
114115
<span class="variable declaration">Nope</span> =&gt; <span class="variable">Nope</span>,
115116
}

crates/ra_ide/test_data/rainbow_highlighting.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@
2424
.bool_literal { color: #BFE6EB; }
2525
.macro { color: #94BFF3; }
2626
.module { color: #AFD8AF; }
27+
.value_param { color: #DCDCCC; }
2728
.variable { color: #DCDCCC; }
2829
.format_specifier { color: #CC696B; }
2930
.mutable { text-decoration: underline; }
30-
.unresolved_reference { color: #FC5555; }
3131
.escape_sequence { color: #94BFF3; }
32-
3332
.keyword { color: #F0DFAF; font-weight: bold; }
3433
.keyword.unsafe { color: #BC8383; font-weight: bold; }
3534
.control { font-style: italic; }
35+
36+
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
3637
</style>
3738
<pre><code><span class="keyword">fn</span> <span class="function declaration">main</span>() {
3839
<span class="keyword">let</span> <span class="variable declaration" data-binding-hash="8121853618659664005" style="color: hsl(261,57%,61%);">hello</span> = <span class="string_literal">"hello"</span>;

crates/rust-analyzer/src/to_proto.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ fn semantic_token_type_and_modifiers(
309309
}
310310
HighlightTag::EnumVariant => semantic_tokens::ENUM_MEMBER,
311311
HighlightTag::Macro => lsp_types::SemanticTokenType::MACRO,
312+
HighlightTag::ValueParam => lsp_types::SemanticTokenType::PARAMETER,
312313
HighlightTag::Local => lsp_types::SemanticTokenType::VARIABLE,
313314
HighlightTag::TypeParam => lsp_types::SemanticTokenType::TYPE_PARAMETER,
314315
HighlightTag::Lifetime => semantic_tokens::LIFETIME,

0 commit comments

Comments
 (0)