mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
Add semantic tag for unresolved references
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.
This commit is contained in:
parent
9b16ae5149
commit
ca61356b01
7 changed files with 34 additions and 21 deletions
|
@ -50,12 +50,12 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
|
||||||
<span class="keyword">fn</span> <span class="function declaration">main</span>() {
|
<span class="keyword">fn</span> <span class="function declaration">main</span>() {
|
||||||
<span class="macro">println!</span>(<span class="string_literal">"Hello, {}!"</span>, <span class="numeric_literal">92</span>);
|
<span class="macro">println!</span>(<span class="string_literal">"Hello, {}!"</span>, <span class="numeric_literal">92</span>);
|
||||||
|
|
||||||
<span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable declaration mutable">vec</span> = Vec::new();
|
<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>();
|
||||||
<span class="keyword control">if</span> <span class="keyword">true</span> {
|
<span class="keyword control">if</span> <span class="keyword">true</span> {
|
||||||
<span class="keyword">let</span> <span class="variable declaration">x</span> = <span class="numeric_literal">92</span>;
|
<span class="keyword">let</span> <span class="variable declaration">x</span> = <span class="numeric_literal">92</span>;
|
||||||
<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> });
|
<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> });
|
||||||
}
|
}
|
||||||
<span class="keyword unsafe">unsafe</span> { <span class="variable mutable">vec</span>.set_len(<span class="numeric_literal">0</span>); }
|
<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>); }
|
||||||
|
|
||||||
<span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable declaration mutable">x</span> = <span class="numeric_literal">42</span>;
|
<span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable declaration mutable">x</span> = <span class="numeric_literal">42</span>;
|
||||||
<span class="keyword">let</span> <span class="variable declaration mutable">y</span> = &<span class="keyword">mut</span> <span class="variable mutable">x</span>;
|
<span class="keyword">let</span> <span class="variable declaration mutable">y</span> = &<span class="keyword">mut</span> <span class="variable mutable">x</span>;
|
||||||
|
|
|
@ -28,11 +28,11 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
|
||||||
</style>
|
</style>
|
||||||
<pre><code><span class="keyword">fn</span> <span class="function declaration">main</span>() {
|
<pre><code><span class="keyword">fn</span> <span class="function declaration">main</span>() {
|
||||||
<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>;
|
<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>;
|
||||||
<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();
|
<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>();
|
||||||
<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();
|
<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>();
|
||||||
|
|
||||||
<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>;
|
<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>;
|
||||||
<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();
|
<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>();
|
||||||
}
|
}
|
||||||
|
|
||||||
<span class="keyword">fn</span> <span class="function declaration">bar</span>() {
|
<span class="keyword">fn</span> <span class="function declaration">bar</span>() {
|
||||||
|
|
|
@ -239,9 +239,8 @@ fn highlight_element(
|
||||||
NAME_REF if element.ancestors().any(|it| it.kind() == ATTR) => return None,
|
NAME_REF if element.ancestors().any(|it| it.kind() == ATTR) => return None,
|
||||||
NAME_REF => {
|
NAME_REF => {
|
||||||
let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap();
|
let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap();
|
||||||
let name_kind = classify_name_ref(sema, &name_ref)?;
|
match classify_name_ref(sema, &name_ref) {
|
||||||
|
Some(name_kind) => match name_kind {
|
||||||
match name_kind {
|
|
||||||
NameRefClass::Definition(def) => {
|
NameRefClass::Definition(def) => {
|
||||||
if let Definition::Local(local) = &def {
|
if let Definition::Local(local) = &def {
|
||||||
if let Some(name) = local.name(db) {
|
if let Some(name) = local.name(db) {
|
||||||
|
@ -253,6 +252,8 @@ fn highlight_element(
|
||||||
highlight_name(db, def)
|
highlight_name(db, def)
|
||||||
}
|
}
|
||||||
NameRefClass::FieldShorthand { .. } => HighlightTag::Field.into(),
|
NameRefClass::FieldShorthand { .. } => HighlightTag::Field.into(),
|
||||||
|
},
|
||||||
|
None => HighlightTag::UnresolvedReference.into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,6 +38,7 @@ pub enum HighlightTag {
|
||||||
TypeParam,
|
TypeParam,
|
||||||
Union,
|
Union,
|
||||||
Local,
|
Local,
|
||||||
|
UnresolvedReference,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||||
|
@ -79,6 +80,7 @@ impl HighlightTag {
|
||||||
HighlightTag::TypeParam => "type_param",
|
HighlightTag::TypeParam => "type_param",
|
||||||
HighlightTag::Union => "union",
|
HighlightTag::Union => "union",
|
||||||
HighlightTag::Local => "variable",
|
HighlightTag::Local => "variable",
|
||||||
|
HighlightTag::UnresolvedReference => "unresolved_reference",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,9 @@ use crate::{
|
||||||
world::WorldSnapshot,
|
world::WorldSnapshot,
|
||||||
Result,
|
Result,
|
||||||
};
|
};
|
||||||
use semantic_tokens::{ATTRIBUTE, BUILTIN_TYPE, ENUM_MEMBER, LIFETIME, TYPE_ALIAS, UNION};
|
use semantic_tokens::{
|
||||||
|
ATTRIBUTE, BUILTIN_TYPE, ENUM_MEMBER, LIFETIME, TYPE_ALIAS, UNION, UNRESOLVED_REFERENCE,
|
||||||
|
};
|
||||||
|
|
||||||
pub trait Conv {
|
pub trait Conv {
|
||||||
type Output;
|
type Output;
|
||||||
|
@ -373,6 +375,7 @@ impl Conv for Highlight {
|
||||||
HighlightTag::Comment => SemanticTokenType::COMMENT,
|
HighlightTag::Comment => SemanticTokenType::COMMENT,
|
||||||
HighlightTag::Attribute => ATTRIBUTE,
|
HighlightTag::Attribute => ATTRIBUTE,
|
||||||
HighlightTag::Keyword => SemanticTokenType::KEYWORD,
|
HighlightTag::Keyword => SemanticTokenType::KEYWORD,
|
||||||
|
HighlightTag::UnresolvedReference => UNRESOLVED_REFERENCE,
|
||||||
};
|
};
|
||||||
|
|
||||||
for modifier in self.modifiers.iter() {
|
for modifier in self.modifiers.iter() {
|
||||||
|
|
|
@ -10,6 +10,8 @@ pub(crate) const ENUM_MEMBER: SemanticTokenType = SemanticTokenType::new("enumMe
|
||||||
pub(crate) const LIFETIME: SemanticTokenType = SemanticTokenType::new("lifetime");
|
pub(crate) const LIFETIME: SemanticTokenType = SemanticTokenType::new("lifetime");
|
||||||
pub(crate) const TYPE_ALIAS: SemanticTokenType = SemanticTokenType::new("typeAlias");
|
pub(crate) const TYPE_ALIAS: SemanticTokenType = SemanticTokenType::new("typeAlias");
|
||||||
pub(crate) const UNION: SemanticTokenType = SemanticTokenType::new("union");
|
pub(crate) const UNION: SemanticTokenType = SemanticTokenType::new("union");
|
||||||
|
pub(crate) const UNRESOLVED_REFERENCE: SemanticTokenType =
|
||||||
|
SemanticTokenType::new("unresolvedReference");
|
||||||
|
|
||||||
pub(crate) const CONSTANT: SemanticTokenModifier = SemanticTokenModifier::new("constant");
|
pub(crate) const CONSTANT: SemanticTokenModifier = SemanticTokenModifier::new("constant");
|
||||||
pub(crate) const CONTROL_FLOW: SemanticTokenModifier = SemanticTokenModifier::new("controlFlow");
|
pub(crate) const CONTROL_FLOW: SemanticTokenModifier = SemanticTokenModifier::new("controlFlow");
|
||||||
|
@ -43,6 +45,7 @@ pub(crate) const SUPPORTED_TYPES: &[SemanticTokenType] = &[
|
||||||
LIFETIME,
|
LIFETIME,
|
||||||
TYPE_ALIAS,
|
TYPE_ALIAS,
|
||||||
UNION,
|
UNION,
|
||||||
|
UNRESOLVED_REFERENCE,
|
||||||
];
|
];
|
||||||
|
|
||||||
pub(crate) const SUPPORTED_MODIFIERS: &[SemanticTokenModifier] = &[
|
pub(crate) const SUPPORTED_MODIFIERS: &[SemanticTokenModifier] = &[
|
||||||
|
|
|
@ -517,6 +517,10 @@
|
||||||
"id": "union",
|
"id": "union",
|
||||||
"description": "Style for C-style untagged unions",
|
"description": "Style for C-style untagged unions",
|
||||||
"superType": "type"
|
"superType": "type"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "unresolvedReference",
|
||||||
|
"description": "Style for names which can not be resolved due to compilation errors"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"semanticTokenModifiers": [
|
"semanticTokenModifiers": [
|
||||||
|
|
Loading…
Reference in a new issue