diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index bf532b5bb2..43a3fb2d71 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs @@ -190,12 +190,13 @@ 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 + // self, crate, super and `Self` are handled as either a Name or NameRef already, unless they // are inside unmapped token trees - T![self] | T![crate] | T![super] if parent_matches::(&token) => return None, + T![self] | T![crate] | T![super] | T![Self] if parent_matches::(&token) => { + return None + } T![self] if parent_matches::(&token) => return None, T![ref] => match token.parent().and_then(ast::IdentPat::cast) { Some(ident) if sema.is_unsafe_ident_pat(&ident) => h | HlMod::Unsafe, @@ -270,12 +271,13 @@ fn highlight_name_ref( } NameRefClass::FieldShorthand { .. } => SymbolKind::Field.into(), }; - if name_ref.self_token().is_some() { - h.tag = HlTag::Symbol(SymbolKind::SelfParam); - } - if name_ref.crate_token().is_some() || name_ref.super_token().is_some() { - h.tag = HlTag::Keyword; - } + + h.tag = match name_ref.token_kind() { + T![Self] => HlTag::Symbol(SymbolKind::SelfType), + T![self] => HlTag::Symbol(SymbolKind::SelfParam), + T![super] | T![crate] => HlTag::Keyword, + _ => h.tag, + }; h } diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_keywords.html b/crates/ide/src/syntax_highlighting/test_data/highlight_keywords.html new file mode 100644 index 0000000000..7cdeca8e29 --- /dev/null +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_keywords.html @@ -0,0 +1,53 @@ + + +
extern crate self;
+
+use crate;
+use self;
+mod __ {
+    use super::*;
+}
+
+struct __ where Self:;
+fn __(_: Self) {}
\ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index c14e3330e3..599def0341 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs @@ -334,6 +334,26 @@ where ); } +#[test] +fn test_keyword_highlighting() { + check_highlighting( + r#" +extern crate self; + +use crate; +use self; +mod __ { + use super::*; +} + +struct __ where Self:; +fn __(_: Self) {} +"#, + expect_file!["./test_data/highlight_keywords.html"], + false, + ); +} + #[test] fn test_string_highlighting() { // The format string detection is based on macro-expansion, diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs index 4a3abcb3a9..333ee35d63 100644 --- a/crates/syntax/src/ast/node_ext.rs +++ b/crates/syntax/src/ast/node_ext.rs @@ -34,6 +34,10 @@ impl ast::NameRef { pub fn as_tuple_field(&self) -> Option { self.text().parse().ok() } + + pub fn token_kind(&self) -> SyntaxKind { + self.syntax().first_token().map_or(SyntaxKind::ERROR, |it| it.kind()) + } } fn text_of_first_token(node: &SyntaxNode) -> TokenText<'_> { @@ -215,11 +219,11 @@ impl ast::PathSegment { pub fn kind(&self) -> Option { let res = if let Some(name_ref) = self.name_ref() { - match name_ref.syntax().first_token().map(|it| it.kind()) { - Some(T![Self]) => PathSegmentKind::SelfTypeKw, - Some(T![self]) => PathSegmentKind::SelfKw, - Some(T![super]) => PathSegmentKind::SuperKw, - Some(T![crate]) => PathSegmentKind::CrateKw, + match name_ref.token_kind() { + T![Self] => PathSegmentKind::SelfTypeKw, + T![self] => PathSegmentKind::SelfKw, + T![super] => PathSegmentKind::SuperKw, + T![crate] => PathSegmentKind::CrateKw, _ => PathSegmentKind::Name(name_ref), } } else { diff --git a/editors/code/package.json b/editors/code/package.json index fb519be461..4843ea8421 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -1297,7 +1297,7 @@ { "id": "selfTypeKeyword", "description": "Style for the self type keyword", - "superType": "typeParameter" + "superType": "keyword" }, { "id": "semicolon",