mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-12 05:08:52 +00:00
Highlight Self
as a keyword by default
This commit is contained in:
parent
0bb631bf71
commit
e5bb661b7a
5 changed files with 94 additions and 15 deletions
|
@ -190,12 +190,13 @@ 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
|
||||
// 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::<ast::NameRef>(&token) => return None,
|
||||
T![self] | T![crate] | T![super] | T![Self] if parent_matches::<ast::NameRef>(&token) => {
|
||||
return None
|
||||
}
|
||||
T![self] if parent_matches::<ast::Name>(&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
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
|
||||
<style>
|
||||
body { margin: 0; }
|
||||
pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padding: 0.4em; }
|
||||
|
||||
.lifetime { color: #DFAF8F; font-style: italic; }
|
||||
.label { color: #DFAF8F; font-style: italic; }
|
||||
.comment { color: #7F9F7F; }
|
||||
.documentation { color: #629755; }
|
||||
.intra_doc_link { font-style: italic; }
|
||||
.injected { opacity: 0.65 ; }
|
||||
.struct, .enum { color: #7CB8BB; }
|
||||
.enum_variant { color: #BDE0F3; }
|
||||
.string_literal { color: #CC9393; }
|
||||
.field { color: #94BFF3; }
|
||||
.function { color: #93E0E3; }
|
||||
.function.unsafe { color: #BC8383; }
|
||||
.trait.unsafe { color: #BC8383; }
|
||||
.operator.unsafe { color: #BC8383; }
|
||||
.mutable.unsafe { color: #BC8383; text-decoration: underline; }
|
||||
.keyword.unsafe { color: #BC8383; font-weight: bold; }
|
||||
.parameter { color: #94BFF3; }
|
||||
.text { color: #DCDCCC; }
|
||||
.type { color: #7CB8BB; }
|
||||
.builtin_type { color: #8CD0D3; }
|
||||
.type_param { color: #DFAF8F; }
|
||||
.attribute { color: #94BFF3; }
|
||||
.numeric_literal { color: #BFEBBF; }
|
||||
.bool_literal { color: #BFE6EB; }
|
||||
.macro { color: #94BFF3; }
|
||||
.derive { color: #94BFF3; font-style: italic; }
|
||||
.module { color: #AFD8AF; }
|
||||
.value_param { color: #DCDCCC; }
|
||||
.variable { color: #DCDCCC; }
|
||||
.format_specifier { color: #CC696B; }
|
||||
.mutable { text-decoration: underline; }
|
||||
.escape_sequence { color: #94BFF3; }
|
||||
.keyword { color: #F0DFAF; font-weight: bold; }
|
||||
.control { font-style: italic; }
|
||||
.reference { font-style: italic; font-weight: bold; }
|
||||
|
||||
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
|
||||
</style>
|
||||
<pre><code><span class="keyword">extern</span> <span class="keyword">crate</span> <span class="unresolved_reference">self</span><span class="semicolon">;</span>
|
||||
|
||||
<span class="keyword">use</span> <span class="keyword crate_root">crate</span><span class="semicolon">;</span>
|
||||
<span class="keyword">use</span> <span class="self_keyword crate_root">self</span><span class="semicolon">;</span>
|
||||
<span class="keyword">mod</span> <span class="module declaration">__</span> <span class="brace">{</span>
|
||||
<span class="keyword">use</span> <span class="keyword crate_root">super</span><span class="operator">::</span><span class="punctuation">*</span><span class="semicolon">;</span>
|
||||
<span class="brace">}</span>
|
||||
|
||||
<span class="keyword">struct</span> <span class="struct declaration">__</span> <span class="keyword">where</span> <span class="self_type_keyword">Self</span><span class="colon">:</span><span class="semicolon">;</span>
|
||||
<span class="keyword">fn</span> <span class="function declaration">__</span><span class="parenthesis">(</span><span class="punctuation">_</span><span class="colon">:</span> <span class="unresolved_reference">Self</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span></code></pre>
|
|
@ -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,
|
||||
|
|
|
@ -34,6 +34,10 @@ impl ast::NameRef {
|
|||
pub fn as_tuple_field(&self) -> Option<usize> {
|
||||
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<PathSegmentKind> {
|
||||
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 {
|
||||
|
|
|
@ -1297,7 +1297,7 @@
|
|||
{
|
||||
"id": "selfTypeKeyword",
|
||||
"description": "Style for the self type keyword",
|
||||
"superType": "typeParameter"
|
||||
"superType": "keyword"
|
||||
},
|
||||
{
|
||||
"id": "semicolon",
|
||||
|
|
Loading…
Reference in a new issue