Skip to content

Commit 1b02caf

Browse files
bors[bot]jhgg
andauthored
Merge #9734
9734: semantic highlighting: add reference hlmod r=matklad a=jhgg This PR adds the "reference" highlight modifier! I basically went around and looked for `HlMod::Mutable` to find the callsites to add a reference. I think these all make sense! Co-authored-by: Jake Heinz <jh@discordapp.com> Co-authored-by: Jake <jh@discordapp.com>
2 parents 8a84311 + 19b1d50 commit 1b02caf

15 files changed

+81
-44
lines changed

crates/hir/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,6 +1861,14 @@ impl Local {
18611861
matches!(&body[self.pat_id], Pat::Bind { mode: BindingAnnotation::Mutable, .. })
18621862
}
18631863

1864+
pub fn is_ref(self, db: &dyn HirDatabase) -> bool {
1865+
let body = db.body(self.parent);
1866+
matches!(
1867+
&body[self.pat_id],
1868+
Pat::Bind { mode: BindingAnnotation::Ref | BindingAnnotation::RefMut, .. }
1869+
)
1870+
}
1871+
18641872
pub fn parent(self, _db: &dyn HirDatabase) -> DefWithBody {
18651873
self.parent.into()
18661874
}
@@ -2216,6 +2224,10 @@ impl Type {
22162224
matches!(self.ty.kind(&Interner), TyKind::Ref(hir_ty::Mutability::Mut, ..))
22172225
}
22182226

2227+
pub fn is_reference(&self) -> bool {
2228+
matches!(self.ty.kind(&Interner), TyKind::Ref(..))
2229+
}
2230+
22192231
pub fn is_usize(&self) -> bool {
22202232
matches!(self.ty.kind(&Interner), TyKind::Scalar(Scalar::Uint(UintTy::Usize)))
22212233
}

crates/ide/src/syntax_highlighting/highlight.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,14 @@ 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) {
378+
Some(sp) => match sp.access(db) {
379+
hir::Access::Exclusive => {
380380
h |= HlMod::Mutable;
381+
h |= HlMod::Reference;
381382
}
382-
}
383+
hir::Access::Shared => h |= HlMod::Reference,
384+
_ => {}
385+
},
383386
None => h |= HlMod::Static,
384387
}
385388

@@ -488,6 +491,9 @@ fn highlight_def(db: &RootDatabase, krate: Option<hir::Crate>, def: Definition)
488491
if local.is_mut(db) || ty.is_mutable_reference() {
489492
h |= HlMod::Mutable;
490493
}
494+
if local.is_ref(db) || ty.is_reference() {
495+
h |= HlMod::Reference;
496+
}
491497
if ty.as_callable(db).is_some() || ty.impls_fnonce(db) {
492498
h |= HlMod::Callable;
493499
}
@@ -549,8 +555,11 @@ fn highlight_method_call(
549555

550556
if let Some(self_param) = func.self_param(sema.db) {
551557
match self_param.access(sema.db) {
552-
hir::Access::Shared => (),
553-
hir::Access::Exclusive => h |= HlMod::Mutable,
558+
hir::Access::Shared => h |= HlMod::Reference,
559+
hir::Access::Exclusive => {
560+
h |= HlMod::Mutable;
561+
h |= HlMod::Reference;
562+
}
554563
hir::Access::Owned => {
555564
if let Some(receiver_ty) =
556565
method_call.receiver().and_then(|it| sema.type_of_expr(&it))

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)