Fix highlighting of Self

This commit is contained in:
Lukas Wirth 2022-03-05 23:34:37 +01:00
parent c0d6471143
commit 5c0aee013e
9 changed files with 19 additions and 4 deletions

View file

@ -118,6 +118,7 @@ pub struct HlRange {
// parameter:: Emitted for non-self function parameters.
// property:: Emitted for struct and union fields.
// selfKeyword:: Emitted for the self function parameter and self path-specifier.
// selfTypeKeyword:: Emitted for the Self type parameter.
// toolModule:: Emitted for tool modules.
// typeParameter:: Emitted for type parameters.
// unresolvedReference:: Emitted for unresolved references, names that rust-analyzer can't find the definition of.

View file

@ -190,6 +190,7 @@ fn keyword(
T![for] if parent_matches::<ast::ForExpr>(&token) => h | HlMod::ControlFlow,
T![unsafe] => h | HlMod::Unsafe,
T![true] | T![false] => HlTag::BoolLiteral.into(),
T![Self] => return Some(HlTag::Symbol(SymbolKind::SelfType).into()),
// crate is handled just as a token if it's in an `extern crate`
T![crate] if parent_matches::<ast::ExternCrate>(&token) => h,
// self, crate and super are handled as either a Name or NameRef already, unless they

View file

@ -141,6 +141,7 @@ fn as_str(self) -> &'static str {
SymbolKind::Macro => "macro",
SymbolKind::Module => "module",
SymbolKind::SelfParam => "self_keyword",
SymbolKind::SelfType => "self_type_keyword",
SymbolKind::Static => "static",
SymbolKind::Struct => "struct",
SymbolKind::ToolModule => "tool_module",

View file

@ -59,11 +59,11 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
<span class="field declaration">x</span><span class="colon">:</span> <span class="builtin_type">u32</span><span class="comma">,</span>
<span class="brace">}</span>
<span class="keyword">trait</span> <span class="trait declaration">Bar</span> <span class="keyword">where</span> <span class="keyword">Self</span><span class="colon">:</span> <span class="brace">{</span>
<span class="keyword">trait</span> <span class="trait declaration">Bar</span> <span class="keyword">where</span> <span class="self_type_keyword">Self</span><span class="colon">:</span> <span class="brace">{</span>
<span class="keyword">fn</span> <span class="function associated declaration reference trait">bar</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">i32</span><span class="semicolon">;</span>
<span class="brace">}</span>
<span class="keyword">impl</span> <span class="trait">Bar</span> <span class="keyword">for</span> <span class="struct">Foo</span> <span class="keyword">where</span> <span class="keyword">Self</span><span class="colon">:</span> <span class="brace">{</span>
<span class="keyword">impl</span> <span class="trait">Bar</span> <span class="keyword">for</span> <span class="struct">Foo</span> <span class="keyword">where</span> <span class="self_type_keyword">Self</span><span class="colon">:</span> <span class="brace">{</span>
<span class="keyword">fn</span> <span class="function associated declaration reference trait">bar</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">i32</span> <span class="brace">{</span>
<span class="self_keyword reference">self</span><span class="operator">.</span><span class="field">x</span>
<span class="brace">}</span>
@ -210,7 +210,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
<span class="keyword">impl</span> <span class="enum public">Bool</span> <span class="brace">{</span>
<span class="keyword">pub</span> <span class="keyword">const</span> <span class="keyword">fn</span> <span class="function associated consuming declaration public">to_primitive</span><span class="parenthesis">(</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>
<span class="unresolved_reference">matches</span><span class="macro_bang">!</span><span class="parenthesis">(</span><span class="keyword">self</span><span class="comma">,</span> <span class="keyword">Self</span><span class="colon">:</span><span class="colon">:</span>True<span class="parenthesis">)</span>
<span class="unresolved_reference">matches</span><span class="macro_bang">!</span><span class="parenthesis">(</span><span class="keyword">self</span><span class="comma">,</span> <span class="self_type_keyword">Self</span><span class="colon">:</span><span class="colon">:</span>True<span class="parenthesis">)</span>
<span class="brace">}</span>
<span class="brace">}</span>
<span class="keyword">const</span> <span class="constant declaration">USAGE_OF_BOOL</span><span class="colon">:</span><span class="builtin_type">bool</span> <span class="operator">=</span> <span class="enum public">Bool</span><span class="operator">::</span><span class="enum_variant public">True</span><span class="operator">.</span><span class="function associated consuming public">to_primitive</span><span class="parenthesis">(</span><span class="parenthesis">)</span><span class="semicolon">;</span>

View file

@ -260,6 +260,7 @@ pub(crate) fn tag(&self) -> &'static str {
SymbolKind::Macro => "ma",
SymbolKind::Module => "md",
SymbolKind::SelfParam => "sp",
SymbolKind::SelfType => "sy",
SymbolKind::Static => "sc",
SymbolKind::Struct => "st",
SymbolKind::ToolModule => "tm",

View file

@ -163,6 +163,7 @@ pub enum SymbolKind {
Macro,
Module,
SelfParam,
SelfType,
Static,
Struct,
ToolModule,

View file

@ -66,6 +66,7 @@ macro_rules! define_semantic_token_types {
(PARENTHESIS, "parenthesis"),
(PUNCTUATION, "punctuation"),
(SELF_KEYWORD, "selfKeyword"),
(SELF_TYPE_KEYWORD, "selfTypeKeyword"),
(SEMICOLON, "semicolon"),
(TYPE_ALIAS, "typeAlias"),
(TOOL_MODULE, "toolModule"),

View file

@ -55,7 +55,9 @@ pub(crate) fn symbol_kind(symbol_kind: SymbolKind) -> lsp_types::SymbolKind {
| SymbolKind::Attribute
| SymbolKind::Derive => lsp_types::SymbolKind::FUNCTION,
SymbolKind::Module | SymbolKind::ToolModule => lsp_types::SymbolKind::MODULE,
SymbolKind::TypeAlias | SymbolKind::TypeParam => lsp_types::SymbolKind::TYPE_PARAMETER,
SymbolKind::TypeAlias | SymbolKind::TypeParam | SymbolKind::SelfType => {
lsp_types::SymbolKind::TYPE_PARAMETER
}
SymbolKind::Field => lsp_types::SymbolKind::FIELD,
SymbolKind::Static => lsp_types::SymbolKind::CONSTANT,
SymbolKind::Const => lsp_types::SymbolKind::CONSTANT,
@ -124,6 +126,7 @@ pub(crate) fn completion_item_kind(
SymbolKind::Macro => lsp_types::CompletionItemKind::FUNCTION,
SymbolKind::Module => lsp_types::CompletionItemKind::MODULE,
SymbolKind::SelfParam => lsp_types::CompletionItemKind::VALUE,
SymbolKind::SelfType => lsp_types::CompletionItemKind::TYPE_PARAMETER,
SymbolKind::Static => lsp_types::CompletionItemKind::VALUE,
SymbolKind::Struct => lsp_types::CompletionItemKind::STRUCT,
SymbolKind::Trait => lsp_types::CompletionItemKind::INTERFACE,
@ -483,6 +486,7 @@ fn semantic_token_type_and_modifiers(
SymbolKind::Label => semantic_tokens::LABEL,
SymbolKind::ValueParam => lsp_types::SemanticTokenType::PARAMETER,
SymbolKind::SelfParam => semantic_tokens::SELF_KEYWORD,
SymbolKind::SelfType => semantic_tokens::SELF_TYPE_KEYWORD,
SymbolKind::Local => lsp_types::SemanticTokenType::VARIABLE,
SymbolKind::Function => {
if highlight.mods.contains(HlMod::Associated) {

View file

@ -1294,6 +1294,11 @@
"description": "Style for the self keyword",
"superType": "keyword"
},
{
"id": "selfTypeKeyword",
"description": "Style for the self type keyword",
"superType": "typeParameter"
},
{
"id": "semicolon",
"description": "Style for ;",