From 5c0aee013e93e40ab54d1928e2e15aac81d1aaea Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 5 Mar 2022 23:34:37 +0100 Subject: [PATCH] Fix highlighting of `Self` --- crates/ide/src/syntax_highlighting.rs | 1 + crates/ide/src/syntax_highlighting/highlight.rs | 1 + crates/ide/src/syntax_highlighting/tags.rs | 1 + .../syntax_highlighting/test_data/highlight_general.html | 6 +++--- crates/ide_completion/src/item.rs | 1 + crates/ide_db/src/lib.rs | 1 + crates/rust-analyzer/src/semantic_tokens.rs | 1 + crates/rust-analyzer/src/to_proto.rs | 6 +++++- editors/code/package.json | 5 +++++ 9 files changed, 19 insertions(+), 4 deletions(-) diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index bd6431da815..968aa331164 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs @@ -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. diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index 8ad27b1fdcd..bf532b5bb2a 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs @@ -190,6 +190,7 @@ fn keyword( T![for] if parent_matches::(&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::(&token) => h, // self, crate and super are handled as either a Name or NameRef already, unless they diff --git a/crates/ide/src/syntax_highlighting/tags.rs b/crates/ide/src/syntax_highlighting/tags.rs index bdf484e01f7..cb9d36f8304 100644 --- a/crates/ide/src/syntax_highlighting/tags.rs +++ b/crates/ide/src/syntax_highlighting/tags.rs @@ -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", diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_general.html b/crates/ide/src/syntax_highlighting/test_data/highlight_general.html index 8501e0ffe58..27e3e57065c 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_general.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_general.html @@ -59,11 +59,11 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd x: u32, } -trait Bar where Self: { +trait Bar where Self: { fn bar(&self) -> i32; } -impl Bar for Foo where Self: { +impl Bar for Foo where Self: { fn bar(&self) -> i32 { self.x } @@ -210,7 +210,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd impl Bool { pub const fn to_primitive(self) -> bool { - matches!(self, Self::True) + matches!(self, Self::True) } } const USAGE_OF_BOOL:bool = Bool::True.to_primitive(); diff --git a/crates/ide_completion/src/item.rs b/crates/ide_completion/src/item.rs index acaf17c2551..32b14764a10 100644 --- a/crates/ide_completion/src/item.rs +++ b/crates/ide_completion/src/item.rs @@ -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", diff --git a/crates/ide_db/src/lib.rs b/crates/ide_db/src/lib.rs index 602eaf0de53..3f64c0a1243 100644 --- a/crates/ide_db/src/lib.rs +++ b/crates/ide_db/src/lib.rs @@ -163,6 +163,7 @@ pub enum SymbolKind { Macro, Module, SelfParam, + SelfType, Static, Struct, ToolModule, diff --git a/crates/rust-analyzer/src/semantic_tokens.rs b/crates/rust-analyzer/src/semantic_tokens.rs index c6322327a5e..5fb945ea987 100644 --- a/crates/rust-analyzer/src/semantic_tokens.rs +++ b/crates/rust-analyzer/src/semantic_tokens.rs @@ -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"), diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index f0de166d8b5..4a2b3a1b47b 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -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) { diff --git a/editors/code/package.json b/editors/code/package.json index 1252752a9ae..fb519be4615 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -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 ;",