Skip to content

Commit 6435c3c

Browse files
8279: Added initial implementation for
Operator semantic highlighting.
1 parent 87e56eb commit 6435c3c

File tree

5 files changed

+92
-19
lines changed

5 files changed

+92
-19
lines changed

crates/ide/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub use crate::{
8282
references::{rename::RenameError, ReferenceSearchResult},
8383
runnables::{Runnable, RunnableKind, TestId},
8484
syntax_highlighting::{
85-
tags::{Highlight, HlMod, HlMods, HlPunct, HlTag},
85+
tags::{Highlight, HlMod, HlMods, HlOperator, HlPunct, HlTag},
8686
HlRange,
8787
},
8888
};

crates/ide/src/syntax_highlighting/highlight.rs

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ use syntax::{
1212
SyntaxNode, SyntaxToken, T,
1313
};
1414

15-
use crate::{syntax_highlighting::tags::HlPunct, Highlight, HlMod, HlTag};
15+
use crate::{
16+
syntax_highlighting::tags::{HlOperator, HlPunct},
17+
Highlight, HlMod, HlTag,
18+
};
1619

1720
pub(super) fn element(
1821
sema: &Semantics<RootDatabase>,
@@ -132,7 +135,7 @@ pub(super) fn element(
132135
INT_NUMBER | FLOAT_NUMBER => HlTag::NumericLiteral.into(),
133136
BYTE => HlTag::ByteLiteral.into(),
134137
CHAR => HlTag::CharLiteral.into(),
135-
QUESTION => Highlight::new(HlTag::Operator) | HlMod::ControlFlow,
138+
QUESTION => Highlight::new(HlTag::Operator(HlOperator::Other)) | HlMod::ControlFlow,
136139
LIFETIME => {
137140
let lifetime = element.into_node().and_then(ast::Lifetime::cast).unwrap();
138141

@@ -146,8 +149,11 @@ pub(super) fn element(
146149
}
147150
}
148151
p if p.is_punct() => match p {
152+
T![&] if element.parent().and_then(ast::BinExpr::cast).is_some() => {
153+
HlTag::Operator(HlOperator::Bitwise).into()
154+
}
149155
T![&] => {
150-
let h = HlTag::Operator.into();
156+
let h = HlTag::Operator(HlOperator::Other).into();
151157
let is_unsafe = element
152158
.parent()
153159
.and_then(ast::RefExpr::cast)
@@ -159,13 +165,21 @@ pub(super) fn element(
159165
h
160166
}
161167
}
162-
T![::] | T![->] | T![=>] | T![..] | T![=] | T![@] | T![.] => HlTag::Operator.into(),
168+
T![::] | T![->] | T![=>] | T![..] | T![=] | T![@] | T![.] => {
169+
HlTag::Operator(HlOperator::Other).into()
170+
}
163171
T![!] if element.parent().and_then(ast::MacroCall::cast).is_some() => {
172+
eprintln!("in macro call: {}", element);
164173
HlTag::Symbol(SymbolKind::Macro).into()
165174
}
166175
T![!] if element.parent().and_then(ast::NeverType::cast).is_some() => {
176+
eprintln!("in never type : {}", element);
167177
HlTag::BuiltinType.into()
168178
}
179+
T![!] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => {
180+
eprintln!("pre expr for : {}", element);
181+
HlTag::Operator(HlOperator::Bitwise).into()
182+
}
169183
T![*] if element.parent().and_then(ast::PtrType::cast).is_some() => {
170184
HlTag::Keyword.into()
171185
}
@@ -175,32 +189,60 @@ pub(super) fn element(
175189
let expr = prefix_expr.expr()?;
176190
let ty = sema.type_of_expr(&expr)?;
177191
if ty.is_raw_ptr() {
178-
HlTag::Operator | HlMod::Unsafe
192+
HlTag::Operator(HlOperator::Other) | HlMod::Unsafe
179193
} else if let Some(ast::PrefixOp::Deref) = prefix_expr.op_kind() {
180-
HlTag::Operator.into()
194+
HlTag::Operator(HlOperator::Other).into()
181195
} else {
182196
HlTag::Punctuation(HlPunct::Other).into()
183197
}
184198
}
185199
T![-] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => {
200+
eprintln!("the - operator: {}", element);
186201
let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?;
187202

188203
let expr = prefix_expr.expr()?;
189204
match expr {
190205
ast::Expr::Literal(_) => HlTag::NumericLiteral,
191-
_ => HlTag::Operator,
206+
_ => HlTag::Operator(HlOperator::Other),
192207
}
193208
.into()
194209
}
195210
_ if element.parent().and_then(ast::PrefixExpr::cast).is_some() => {
196-
HlTag::Operator.into()
211+
eprintln!("the prefix expr block: {}", element);
212+
HlTag::Operator(HlOperator::Other).into()
213+
}
214+
T![+] | T![-] | T![*] | T![/] | T![+=] | T![-=] | T![*=] | T![/=]
215+
if element.parent().and_then(ast::BinExpr::cast).is_some() =>
216+
{
217+
HlTag::Operator(HlOperator::Arithmetic).into()
218+
}
219+
T![|] | T![&] | T![!] | T![^] | T![|=] | T![&=] | T![^=]
220+
if element.parent().and_then(ast::BinExpr::cast).is_some() =>
221+
{
222+
HlTag::Operator(HlOperator::Bitwise).into()
223+
}
224+
T![&&] | T![||] if element.parent().and_then(ast::BinExpr::cast).is_some() => {
225+
HlTag::Operator(HlOperator::Logical).into()
226+
}
227+
T![>] | T![<] | T![==] | T![>=] | T![<=] | T![!=]
228+
if element.parent().and_then(ast::BinExpr::cast).is_some() =>
229+
{
230+
HlTag::Operator(HlOperator::Comparision).into()
231+
}
232+
_ if element.parent().and_then(ast::BinExpr::cast).is_some() => {
233+
eprintln!("the bin expr : {}", element);
234+
HlTag::Operator(HlOperator::Other).into()
197235
}
198-
_ if element.parent().and_then(ast::BinExpr::cast).is_some() => HlTag::Operator.into(),
199236
_ if element.parent().and_then(ast::RangeExpr::cast).is_some() => {
200-
HlTag::Operator.into()
237+
eprintln!("the range expr block: {}", element);
238+
HlTag::Operator(HlOperator::Other).into()
239+
}
240+
_ if element.parent().and_then(ast::RangePat::cast).is_some() => {
241+
HlTag::Operator(HlOperator::Other).into()
242+
}
243+
_ if element.parent().and_then(ast::RestPat::cast).is_some() => {
244+
HlTag::Operator(HlOperator::Other).into()
201245
}
202-
_ if element.parent().and_then(ast::RangePat::cast).is_some() => HlTag::Operator.into(),
203-
_ if element.parent().and_then(ast::RestPat::cast).is_some() => HlTag::Operator.into(),
204246
_ if element.parent().and_then(ast::Attr::cast).is_some() => HlTag::Attribute.into(),
205247
kind => HlTag::Punctuation(match kind {
206248
T!['['] | T![']'] => HlPunct::Bracket,

crates/ide/src/syntax_highlighting/tags.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub enum HlTag {
2828
FormatSpecifier,
2929
Keyword,
3030
NumericLiteral,
31-
Operator,
31+
Operator(HlOperator),
3232
Punctuation(HlPunct),
3333
StringLiteral,
3434
UnresolvedReference,
@@ -87,6 +87,20 @@ pub enum HlPunct {
8787
Other,
8888
}
8989

90+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
91+
pub enum HlOperator {
92+
/// |, &, !, ^, |=, &=, ^=
93+
Bitwise,
94+
/// +, -, *, /, +=, -=, *=, /=
95+
Arithmetic,
96+
/// &&, ||, !
97+
Logical,
98+
/// >, <, ==, >=, <=, !=
99+
Comparision,
100+
///
101+
Other,
102+
}
103+
90104
impl HlTag {
91105
fn as_str(self) -> &'static str {
92106
match self {
@@ -133,7 +147,13 @@ impl HlTag {
133147
HlPunct::Other => "punctuation",
134148
},
135149
HlTag::NumericLiteral => "numeric_literal",
136-
HlTag::Operator => "operator",
150+
HlTag::Operator(op) => match op {
151+
HlOperator::Bitwise => "bitwise",
152+
HlOperator::Arithmetic => "arithmetic",
153+
HlOperator::Logical => "logical",
154+
HlOperator::Comparision => "comparision",
155+
HlOperator::Other => "operator",
156+
},
137157
HlTag::StringLiteral => "string_literal",
138158
HlTag::UnresolvedReference => "unresolved_reference",
139159
HlTag::None => "none",

crates/rust-analyzer/src/semantic_tokens.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,26 @@ macro_rules! define_semantic_token_types {
3939

4040
define_semantic_token_types![
4141
(ANGLE, "angle"),
42+
(ARITHMETIC, "arithmetic"),
4243
(ATTRIBUTE, "attribute"),
44+
(BITWISE, "bitwise"),
4345
(BOOLEAN, "boolean"),
4446
(BRACE, "brace"),
4547
(BRACKET, "bracket"),
4648
(BUILTIN_TYPE, "builtinType"),
4749
(CHAR_LITERAL, "characterLiteral"),
4850
(COLON, "colon"),
4951
(COMMA, "comma"),
52+
(COMPARISION, "comparision"),
5053
(CONST_PARAMETER, "constParameter"),
5154
(DOT, "dot"),
5255
(ESCAPE_SEQUENCE, "escapeSequence"),
5356
(FORMAT_SPECIFIER, "formatSpecifier"),
5457
(GENERIC, "generic"),
5558
(LABEL, "label"),
5659
(LIFETIME, "lifetime"),
60+
(LOGICAL, "logical"),
61+
(OPERATOR, "operator"),
5762
(PARENTHESIS, "parenthesis"),
5863
(PUNCTUATION, "punctuation"),
5964
(SELF_KEYWORD, "selfKeyword"),

crates/rust-analyzer/src/to_proto.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use std::{
77
use ide::{
88
Annotation, AnnotationKind, Assist, AssistKind, CallInfo, CompletionItem, CompletionItemKind,
99
CompletionRelevance, Documentation, FileId, FileRange, FileSystemEdit, Fold, FoldKind,
10-
Highlight, HlMod, HlPunct, HlRange, HlTag, Indel, InlayHint, InlayKind, InsertTextFormat,
11-
Markup, NavigationTarget, ReferenceAccess, RenameError, Runnable, Severity, SourceChange,
12-
StructureNodeKind, SymbolKind, TextEdit, TextRange, TextSize,
10+
Highlight, HlMod, HlOperator, HlPunct, HlRange, HlTag, Indel, InlayHint, InlayKind,
11+
InsertTextFormat, Markup, NavigationTarget, ReferenceAccess, RenameError, Runnable, Severity,
12+
SourceChange, StructureNodeKind, SymbolKind, TextEdit, TextRange, TextSize,
1313
};
1414
use itertools::Itertools;
1515
use serde_json::to_value;
@@ -445,7 +445,13 @@ fn semantic_token_type_and_modifiers(
445445
HlTag::FormatSpecifier => semantic_tokens::FORMAT_SPECIFIER,
446446
HlTag::Keyword => lsp_types::SemanticTokenType::KEYWORD,
447447
HlTag::None => semantic_tokens::GENERIC,
448-
HlTag::Operator => lsp_types::SemanticTokenType::OPERATOR,
448+
HlTag::Operator(op) => match op {
449+
HlOperator::Bitwise => semantic_tokens::BITWISE,
450+
HlOperator::Arithmetic => semantic_tokens::ARITHMETIC,
451+
HlOperator::Logical => semantic_tokens::LOGICAL,
452+
HlOperator::Comparision => semantic_tokens::COMPARISION,
453+
HlOperator::Other => semantic_tokens::OPERATOR,
454+
},
449455
HlTag::StringLiteral => lsp_types::SemanticTokenType::STRING,
450456
HlTag::UnresolvedReference => semantic_tokens::UNRESOLVED_REFERENCE,
451457
HlTag::Punctuation(punct) => match punct {

0 commit comments

Comments
 (0)