Skip to content

Commit 2baef17

Browse files
committed
semantic highlighting: add reference hlmod
1 parent 14f00ad commit 2baef17

15 files changed

+68
-38
lines changed

crates/hir/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,6 +1840,11 @@ impl Local {
18401840
matches!(&body[self.pat_id], Pat::Bind { mode: BindingAnnotation::Mutable, .. })
18411841
}
18421842

1843+
pub fn is_ref(self, db: &dyn HirDatabase) -> bool {
1844+
let body = db.body(self.parent);
1845+
matches!(&body[self.pat_id], Pat::Bind { mode: BindingAnnotation::Ref, .. })
1846+
}
1847+
18431848
pub fn parent(self, _db: &dyn HirDatabase) -> DefWithBody {
18441849
self.parent.into()
18451850
}
@@ -2195,6 +2200,10 @@ impl Type {
21952200
matches!(self.ty.kind(&Interner), TyKind::Ref(hir_ty::Mutability::Mut, ..))
21962201
}
21972202

2203+
pub fn is_reference(&self) -> bool {
2204+
matches!(self.ty.kind(&Interner), TyKind::Ref(hir_ty::Mutability::Not, ..))
2205+
}
2206+
21982207
pub fn is_usize(&self) -> bool {
21992208
matches!(self.ty.kind(&Interner), TyKind::Scalar(Scalar::Uint(UintTy::Usize)))
22002209
}

crates/ide/src/syntax_highlighting/highlight.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,11 @@ fn highlight_def(db: &RootDatabase, krate: Option<hir::Crate>, def: Definition)
375375
if let Some(item) = func.as_assoc_item(db) {
376376
h |= HlMod::Associated;
377377
match func.self_param(db) {
378-
Some(sp) => {
379-
if let hir::Access::Exclusive = sp.access(db) {
380-
h |= HlMod::Mutable;
381-
}
382-
}
378+
Some(sp) => match sp.access(db) {
379+
hir::Access::Exclusive => h |= HlMod::Mutable,
380+
hir::Access::Shared => h |= HlMod::Reference,
381+
_ => {}
382+
},
383383
None => h |= HlMod::Static,
384384
}
385385

@@ -460,6 +460,8 @@ fn highlight_def(db: &RootDatabase, krate: Option<hir::Crate>, def: Definition)
460460
if s.is_mut(db) {
461461
h |= HlMod::Mutable;
462462
h |= HlMod::Unsafe;
463+
} else {
464+
h |= HlMod::Reference;
463465
}
464466

465467
h
@@ -491,6 +493,9 @@ fn highlight_def(db: &RootDatabase, krate: Option<hir::Crate>, def: Definition)
491493
if ty.as_callable(db).is_some() || ty.impls_fnonce(db) {
492494
h |= HlMod::Callable;
493495
}
496+
if local.is_ref(db) || ty.is_reference() {
497+
h |= HlMod::Reference;
498+
}
494499
h
495500
}
496501
Definition::Label(_) => Highlight::new(HlTag::Symbol(SymbolKind::Label)),
@@ -549,7 +554,7 @@ fn highlight_method_call(
549554

550555
if let Some(self_param) = func.self_param(sema.db) {
551556
match self_param.access(sema.db) {
552-
hir::Access::Shared => (),
557+
hir::Access::Shared => h |= HlMod::Reference,
553558
hir::Access::Exclusive => h |= HlMod::Mutable,
554559
hir::Access::Owned => {
555560
if let Some(receiver_ty) =

crates/ide/src/syntax_highlighting/html.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
8787
.keyword { color: #F0DFAF; font-weight: bold; }
8888
.keyword.unsafe { color: #BC8383; font-weight: bold; }
8989
.control { font-style: italic; }
90+
.reference { font-style: italic; font-weight: bold; }
9091
9192
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
9293
</style>

crates/ide/src/syntax_highlighting/tags.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ pub enum HlMod {
6464
IntraDocLink,
6565
/// Mutable binding.
6666
Mutable,
67+
/// Immutable reference.
68+
Reference,
6769
/// Used for associated functions.
6870
Static,
6971
/// Used for items in traits and trait impls.
@@ -194,6 +196,7 @@ impl HlMod {
194196
HlMod::Injected,
195197
HlMod::IntraDocLink,
196198
HlMod::Mutable,
199+
HlMod::Reference,
197200
HlMod::Static,
198201
HlMod::Trait,
199202
HlMod::Async,
@@ -214,6 +217,7 @@ impl HlMod {
214217
HlMod::Injected => "injected",
215218
HlMod::IntraDocLink => "intra_doc_link",
216219
HlMod::Mutable => "mutable",
220+
HlMod::Reference => "reference",
217221
HlMod::Static => "static",
218222
HlMod::Trait => "trait",
219223
HlMod::Async => "async",

crates/ide/src/syntax_highlighting/test_data/highlight_assoc_functions.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
.keyword { color: #F0DFAF; font-weight: bold; }
3636
.keyword.unsafe { color: #BC8383; font-weight: bold; }
3737
.control { font-style: italic; }
38+
.reference { font-style: italic; font-weight: bold; }
3839

3940
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
4041
</style>
@@ -44,16 +45,16 @@
4445

4546
<span class="keyword">impl</span> <span class="struct">foo</span> <span class="brace">{</span>
4647
<span class="keyword">pub</span> <span class="keyword">fn</span> <span class="function associated declaration static public">is_static</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span>
47-
<span class="keyword">pub</span> <span class="keyword">fn</span> <span class="function associated declaration public">is_not_static</span><span class="parenthesis">(</span><span class="operator">&</span><span class="self_keyword declaration">self</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span>
48+
<span class="keyword">pub</span> <span class="keyword">fn</span> <span class="function associated declaration reference public">is_not_static</span><span class="parenthesis">(</span><span class="operator">&</span><span class="self_keyword declaration reference">self</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span>
4849
<span class="brace">}</span>
4950

5051
<span class="keyword">trait</span> <span class="trait declaration">t</span> <span class="brace">{</span>
5152
<span class="keyword">fn</span> <span class="function associated declaration static trait">t_is_static</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span>
52-
<span class="keyword">fn</span> <span class="function associated declaration trait">t_is_not_static</span><span class="parenthesis">(</span><span class="operator">&</span><span class="self_keyword declaration">self</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span>
53+
<span class="keyword">fn</span> <span class="function associated declaration reference trait">t_is_not_static</span><span class="parenthesis">(</span><span class="operator">&</span><span class="self_keyword declaration reference">self</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span>
5354
<span class="brace">}</span>
5455

5556
<span class="keyword">impl</span> <span class="trait">t</span> <span class="keyword">for</span> <span class="struct">foo</span> <span class="brace">{</span>
5657
<span class="keyword">pub</span> <span class="keyword">fn</span> <span class="function associated declaration static trait public">is_static</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span>
57-
<span class="keyword">pub</span> <span class="keyword">fn</span> <span class="function associated declaration trait public">is_not_static</span><span class="parenthesis">(</span><span class="operator">&</span><span class="self_keyword declaration">self</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span>
58+
<span class="keyword">pub</span> <span class="keyword">fn</span> <span class="function associated declaration reference trait public">is_not_static</span><span class="parenthesis">(</span><span class="operator">&</span><span class="self_keyword declaration reference">self</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span>
5859
<span class="brace">}</span>
5960
</code></pre>

crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
.keyword { color: #F0DFAF; font-weight: bold; }
3636
.keyword.unsafe { color: #BC8383; font-weight: bold; }
3737
.control { font-style: italic; }
38+
.reference { font-style: italic; font-weight: bold; }
3839

3940
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
4041
</style>
@@ -94,7 +95,7 @@
9495
<span class="comment documentation">/// </span><span class="comment injected">/* multi-line</span>
9596
<span class="comment documentation">/// </span><span class="comment injected"> comment */</span>
9697
<span class="comment documentation">///</span>
97-
<span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="none injected"> </span><span class="variable declaration injected">multi_line_string</span><span class="none injected"> </span><span class="operator injected">=</span><span class="none injected"> </span><span class="string_literal injected">"Foo</span>
98+
<span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="none injected"> </span><span class="variable declaration injected reference">multi_line_string</span><span class="none injected"> </span><span class="operator injected">=</span><span class="none injected"> </span><span class="string_literal injected">"Foo</span>
9899
<span class="comment documentation">/// </span><span class="string_literal injected"> bar</span><span class="escape_sequence injected">\n</span>
99100
<span class="comment documentation">/// </span><span class="string_literal injected"> "</span><span class="semicolon injected">;</span>
100101
<span class="comment documentation">///</span>
@@ -107,7 +108,7 @@
107108
<span class="comment documentation">/// ```sh</span>
108109
<span class="comment documentation">/// echo 1</span>
109110
<span class="comment documentation">/// ```</span>
110-
<span class="keyword">pub</span> <span class="keyword">fn</span> <span class="function associated declaration public">foo</span><span class="parenthesis">(</span><span class="operator">&</span><span class="self_keyword declaration">self</span><span class="parenthesis">)</span> <span class="operator">-&gt;</span> <span class="builtin_type">bool</span> <span class="brace">{</span>
111+
<span class="keyword">pub</span> <span class="keyword">fn</span> <span class="function associated declaration reference public">foo</span><span class="parenthesis">(</span><span class="operator">&</span><span class="self_keyword declaration reference">self</span><span class="parenthesis">)</span> <span class="operator">-&gt;</span> <span class="builtin_type">bool</span> <span class="brace">{</span>
111112
<span class="bool_literal">true</span>
112113
<span class="brace">}</span>
113114
<span class="brace">}</span>

crates/ide/src/syntax_highlighting/test_data/highlight_extern_crate.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
.keyword { color: #F0DFAF; font-weight: bold; }
3636
.keyword.unsafe { color: #BC8383; font-weight: bold; }
3737
.control { font-style: italic; }
38+
.reference { font-style: italic; font-weight: bold; }
3839

3940
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
4041
</style>

crates/ide/src/syntax_highlighting/test_data/highlight_injection.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@
3535
.keyword { color: #F0DFAF; font-weight: bold; }
3636
.keyword.unsafe { color: #BC8383; font-weight: bold; }
3737
.control { font-style: italic; }
38+
.reference { font-style: italic; font-weight: bold; }
3839

3940
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
4041
</style>
41-
<pre><code><span class="keyword">fn</span> <span class="function declaration">fixture</span><span class="parenthesis">(</span><span class="value_param declaration">ra_fixture</span><span class="colon">:</span> <span class="operator">&</span><span class="builtin_type">str</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span>
42+
<pre><code><span class="keyword">fn</span> <span class="function declaration">fixture</span><span class="parenthesis">(</span><span class="value_param declaration reference">ra_fixture</span><span class="colon">:</span> <span class="operator">&</span><span class="builtin_type">str</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span>
4243

4344
<span class="keyword">fn</span> <span class="function declaration">main</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span>
4445
<span class="function">fixture</span><span class="parenthesis">(</span><span class="string_literal">r#"</span>

crates/ide/src/syntax_highlighting/test_data/highlight_strings.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
.keyword { color: #F0DFAF; font-weight: bold; }
3636
.keyword.unsafe { color: #BC8383; font-weight: bold; }
3737
.control { font-style: italic; }
38+
.reference { font-style: italic; font-weight: bold; }
3839

3940
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
4041
</style>

0 commit comments

Comments
 (0)