Skip to content

Commit b95b77f

Browse files
bors[bot]matklad
andauthored
Merge #4034
4034: Add semantic tag for unresolved references r=matklad a=matklad This is a quick way to implement unresolved reference diagnostics. For example, adding to VS Code config "editor.tokenColorCustomizationsExperimental": { "unresolvedReference": "#FF0000" }, will highlight all unresolved refs in red. Co-authored-by: Aleksey Kladov <[email protected]>
2 parents b949500 + ca61356 commit b95b77f

File tree

7 files changed

+34
-21
lines changed

7 files changed

+34
-21
lines changed

crates/ra_ide/src/snapshots/highlighting.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@
5050
<span class="keyword">fn</span> <span class="function declaration">main</span>() {
5151
<span class="macro">println!</span>(<span class="string_literal">"Hello, {}!"</span>, <span class="numeric_literal">92</span>);
5252

53-
<span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable declaration mutable">vec</span> = Vec::new();
53+
<span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable declaration mutable">vec</span> = <span class="unresolved_reference">Vec</span>::<span class="unresolved_reference">new</span>();
5454
<span class="keyword control">if</span> <span class="keyword">true</span> {
5555
<span class="keyword">let</span> <span class="variable declaration">x</span> = <span class="numeric_literal">92</span>;
56-
<span class="variable mutable">vec</span>.push(<span class="struct">Foo</span> { <span class="field">x</span>, <span class="field">y</span>: <span class="numeric_literal">1</span> });
56+
<span class="variable mutable">vec</span>.<span class="unresolved_reference">push</span>(<span class="struct">Foo</span> { <span class="field">x</span>, <span class="field">y</span>: <span class="numeric_literal">1</span> });
5757
}
58-
<span class="keyword unsafe">unsafe</span> { <span class="variable mutable">vec</span>.set_len(<span class="numeric_literal">0</span>); }
58+
<span class="keyword unsafe">unsafe</span> { <span class="variable mutable">vec</span>.<span class="unresolved_reference">set_len</span>(<span class="numeric_literal">0</span>); }
5959

6060
<span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable declaration mutable">x</span> = <span class="numeric_literal">42</span>;
6161
<span class="keyword">let</span> <span class="variable declaration mutable">y</span> = &<span class="keyword">mut</span> <span class="variable mutable">x</span>;

crates/ra_ide/src/snapshots/rainbow_highlighting.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
</style>
2929
<pre><code><span class="keyword">fn</span> <span class="function declaration">main</span>() {
3030
<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>;
31-
<span class="keyword">let</span> <span class="variable declaration" data-binding-hash="2705725358298919760" style="color: hsl(17,51%,74%);">x</span> = <span class="variable" data-binding-hash="8121853618659664005" style="color: hsl(261,57%,61%);">hello</span>.to_string();
32-
<span class="keyword">let</span> <span class="variable declaration" data-binding-hash="3365759661443752373" style="color: hsl(127,76%,66%);">y</span> = <span class="variable" data-binding-hash="8121853618659664005" style="color: hsl(261,57%,61%);">hello</span>.to_string();
31+
<span class="keyword">let</span> <span class="variable declaration" data-binding-hash="2705725358298919760" style="color: hsl(17,51%,74%);">x</span> = <span class="variable" data-binding-hash="8121853618659664005" style="color: hsl(261,57%,61%);">hello</span>.<span class="unresolved_reference">to_string</span>();
32+
<span class="keyword">let</span> <span class="variable declaration" data-binding-hash="3365759661443752373" style="color: hsl(127,76%,66%);">y</span> = <span class="variable" data-binding-hash="8121853618659664005" style="color: hsl(261,57%,61%);">hello</span>.<span class="unresolved_reference">to_string</span>();
3333

3434
<span class="keyword">let</span> <span class="variable declaration" data-binding-hash="794745962933817518" style="color: hsl(19,74%,76%);">x</span> = <span class="string_literal">"other color please!"</span>;
35-
<span class="keyword">let</span> <span class="variable declaration" data-binding-hash="6717528807933952652" style="color: hsl(85,49%,84%);">y</span> = <span class="variable" data-binding-hash="794745962933817518" style="color: hsl(19,74%,76%);">x</span>.to_string();
35+
<span class="keyword">let</span> <span class="variable declaration" data-binding-hash="6717528807933952652" style="color: hsl(85,49%,84%);">y</span> = <span class="variable" data-binding-hash="794745962933817518" style="color: hsl(19,74%,76%);">x</span>.<span class="unresolved_reference">to_string</span>();
3636
}
3737

3838
<span class="keyword">fn</span> <span class="function declaration">bar</span>() {

crates/ra_ide/src/syntax_highlighting.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -239,20 +239,21 @@ fn highlight_element(
239239
NAME_REF if element.ancestors().any(|it| it.kind() == ATTR) => return None,
240240
NAME_REF => {
241241
let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap();
242-
let name_kind = classify_name_ref(sema, &name_ref)?;
243-
244-
match name_kind {
245-
NameRefClass::Definition(def) => {
246-
if let Definition::Local(local) = &def {
247-
if let Some(name) = local.name(db) {
248-
let shadow_count =
249-
bindings_shadow_count.entry(name.clone()).or_default();
250-
binding_hash = Some(calc_binding_hash(&name, *shadow_count))
251-
}
252-
};
253-
highlight_name(db, def)
254-
}
255-
NameRefClass::FieldShorthand { .. } => HighlightTag::Field.into(),
242+
match classify_name_ref(sema, &name_ref) {
243+
Some(name_kind) => match name_kind {
244+
NameRefClass::Definition(def) => {
245+
if let Definition::Local(local) = &def {
246+
if let Some(name) = local.name(db) {
247+
let shadow_count =
248+
bindings_shadow_count.entry(name.clone()).or_default();
249+
binding_hash = Some(calc_binding_hash(&name, *shadow_count))
250+
}
251+
};
252+
highlight_name(db, def)
253+
}
254+
NameRefClass::FieldShorthand { .. } => HighlightTag::Field.into(),
255+
},
256+
None => HighlightTag::UnresolvedReference.into(),
256257
}
257258
}
258259

crates/ra_ide/src/syntax_highlighting/tags.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub enum HighlightTag {
3838
TypeParam,
3939
Union,
4040
Local,
41+
UnresolvedReference,
4142
}
4243

4344
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
@@ -79,6 +80,7 @@ impl HighlightTag {
7980
HighlightTag::TypeParam => "type_param",
8081
HighlightTag::Union => "union",
8182
HighlightTag::Local => "variable",
83+
HighlightTag::UnresolvedReference => "unresolved_reference",
8284
}
8385
}
8486
}

crates/rust-analyzer/src/conv.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ use crate::{
2424
world::WorldSnapshot,
2525
Result,
2626
};
27-
use semantic_tokens::{ATTRIBUTE, BUILTIN_TYPE, ENUM_MEMBER, LIFETIME, TYPE_ALIAS, UNION};
27+
use semantic_tokens::{
28+
ATTRIBUTE, BUILTIN_TYPE, ENUM_MEMBER, LIFETIME, TYPE_ALIAS, UNION, UNRESOLVED_REFERENCE,
29+
};
2830

2931
pub trait Conv {
3032
type Output;
@@ -373,6 +375,7 @@ impl Conv for Highlight {
373375
HighlightTag::Comment => SemanticTokenType::COMMENT,
374376
HighlightTag::Attribute => ATTRIBUTE,
375377
HighlightTag::Keyword => SemanticTokenType::KEYWORD,
378+
HighlightTag::UnresolvedReference => UNRESOLVED_REFERENCE,
376379
};
377380

378381
for modifier in self.modifiers.iter() {

crates/rust-analyzer/src/semantic_tokens.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ pub(crate) const ENUM_MEMBER: SemanticTokenType = SemanticTokenType::new("enumMe
1010
pub(crate) const LIFETIME: SemanticTokenType = SemanticTokenType::new("lifetime");
1111
pub(crate) const TYPE_ALIAS: SemanticTokenType = SemanticTokenType::new("typeAlias");
1212
pub(crate) const UNION: SemanticTokenType = SemanticTokenType::new("union");
13+
pub(crate) const UNRESOLVED_REFERENCE: SemanticTokenType =
14+
SemanticTokenType::new("unresolvedReference");
1315

1416
pub(crate) const CONSTANT: SemanticTokenModifier = SemanticTokenModifier::new("constant");
1517
pub(crate) const CONTROL_FLOW: SemanticTokenModifier = SemanticTokenModifier::new("controlFlow");
@@ -43,6 +45,7 @@ pub(crate) const SUPPORTED_TYPES: &[SemanticTokenType] = &[
4345
LIFETIME,
4446
TYPE_ALIAS,
4547
UNION,
48+
UNRESOLVED_REFERENCE,
4649
];
4750

4851
pub(crate) const SUPPORTED_MODIFIERS: &[SemanticTokenModifier] = &[

editors/code/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,10 @@
517517
"id": "union",
518518
"description": "Style for C-style untagged unions",
519519
"superType": "type"
520+
},
521+
{
522+
"id": "unresolvedReference",
523+
"description": "Style for names which can not be resolved due to compilation errors"
520524
}
521525
],
522526
"semanticTokenModifiers": [

0 commit comments

Comments
 (0)