aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChetan Khilosiya <[email protected]>2021-04-05 19:39:17 +0100
committerChetan Khilosiya <[email protected]>2021-04-08 18:28:26 +0100
commit1735b3ef6a7c7f7b3f5cdecdbf204c85991bcd63 (patch)
tree2f0cb9fd6643eaa6433f452e1aa4021b6bc19ab0
parent5f279d57f0cba600eae8c550654a00b4268812ac (diff)
8279: Added initial implementation for
Operator semantic highlighting.
-rw-r--r--crates/ide/src/lib.rs2
-rw-r--r--crates/ide/src/syntax_highlighting/highlight.rs66
-rw-r--r--crates/ide/src/syntax_highlighting/tags.rs24
-rw-r--r--crates/rust-analyzer/src/semantic_tokens.rs5
-rw-r--r--crates/rust-analyzer/src/to_proto.rs14
5 files changed, 92 insertions, 19 deletions
diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs
index 3f73c0632..b5ae51d28 100644
--- a/crates/ide/src/lib.rs
+++ b/crates/ide/src/lib.rs
@@ -82,7 +82,7 @@ pub use crate::{
82 references::{rename::RenameError, ReferenceSearchResult}, 82 references::{rename::RenameError, ReferenceSearchResult},
83 runnables::{Runnable, RunnableKind, TestId}, 83 runnables::{Runnable, RunnableKind, TestId},
84 syntax_highlighting::{ 84 syntax_highlighting::{
85 tags::{Highlight, HlMod, HlMods, HlPunct, HlTag}, 85 tags::{Highlight, HlMod, HlMods, HlOperator, HlPunct, HlTag},
86 HlRange, 86 HlRange,
87 }, 87 },
88}; 88};
diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs
index 5ccb84714..7734ea301 100644
--- a/crates/ide/src/syntax_highlighting/highlight.rs
+++ b/crates/ide/src/syntax_highlighting/highlight.rs
@@ -12,7 +12,10 @@ use syntax::{
12 SyntaxNode, SyntaxToken, T, 12 SyntaxNode, SyntaxToken, T,
13}; 13};
14 14
15use crate::{syntax_highlighting::tags::HlPunct, Highlight, HlMod, HlTag}; 15use crate::{
16 syntax_highlighting::tags::{HlOperator, HlPunct},
17 Highlight, HlMod, HlTag,
18};
16 19
17pub(super) fn element( 20pub(super) fn element(
18 sema: &Semantics<RootDatabase>, 21 sema: &Semantics<RootDatabase>,
@@ -132,7 +135,7 @@ pub(super) fn element(
132 INT_NUMBER | FLOAT_NUMBER => HlTag::NumericLiteral.into(), 135 INT_NUMBER | FLOAT_NUMBER => HlTag::NumericLiteral.into(),
133 BYTE => HlTag::ByteLiteral.into(), 136 BYTE => HlTag::ByteLiteral.into(),
134 CHAR => HlTag::CharLiteral.into(), 137 CHAR => HlTag::CharLiteral.into(),
135 QUESTION => Highlight::new(HlTag::Operator) | HlMod::ControlFlow, 138 QUESTION => Highlight::new(HlTag::Operator(HlOperator::Other)) | HlMod::ControlFlow,
136 LIFETIME => { 139 LIFETIME => {
137 let lifetime = element.into_node().and_then(ast::Lifetime::cast).unwrap(); 140 let lifetime = element.into_node().and_then(ast::Lifetime::cast).unwrap();
138 141
@@ -146,8 +149,11 @@ pub(super) fn element(
146 } 149 }
147 } 150 }
148 p if p.is_punct() => match p { 151 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 }
149 T![&] => { 155 T![&] => {
150 let h = HlTag::Operator.into(); 156 let h = HlTag::Operator(HlOperator::Other).into();
151 let is_unsafe = element 157 let is_unsafe = element
152 .parent() 158 .parent()
153 .and_then(ast::RefExpr::cast) 159 .and_then(ast::RefExpr::cast)
@@ -159,13 +165,21 @@ pub(super) fn element(
159 h 165 h
160 } 166 }
161 } 167 }
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 }
163 T![!] if element.parent().and_then(ast::MacroCall::cast).is_some() => { 171 T![!] if element.parent().and_then(ast::MacroCall::cast).is_some() => {
172 eprintln!("in macro call: {}", element);
164 HlTag::Symbol(SymbolKind::Macro).into() 173 HlTag::Symbol(SymbolKind::Macro).into()
165 } 174 }
166 T![!] if element.parent().and_then(ast::NeverType::cast).is_some() => { 175 T![!] if element.parent().and_then(ast::NeverType::cast).is_some() => {
176 eprintln!("in never type : {}", element);
167 HlTag::BuiltinType.into() 177 HlTag::BuiltinType.into()
168 } 178 }
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 }
169 T![*] if element.parent().and_then(ast::PtrType::cast).is_some() => { 183 T![*] if element.parent().and_then(ast::PtrType::cast).is_some() => {
170 HlTag::Keyword.into() 184 HlTag::Keyword.into()
171 } 185 }
@@ -175,32 +189,60 @@ pub(super) fn element(
175 let expr = prefix_expr.expr()?; 189 let expr = prefix_expr.expr()?;
176 let ty = sema.type_of_expr(&expr)?; 190 let ty = sema.type_of_expr(&expr)?;
177 if ty.is_raw_ptr() { 191 if ty.is_raw_ptr() {
178 HlTag::Operator | HlMod::Unsafe 192 HlTag::Operator(HlOperator::Other) | HlMod::Unsafe
179 } else if let Some(ast::PrefixOp::Deref) = prefix_expr.op_kind() { 193 } else if let Some(ast::PrefixOp::Deref) = prefix_expr.op_kind() {
180 HlTag::Operator.into() 194 HlTag::Operator(HlOperator::Other).into()
181 } else { 195 } else {
182 HlTag::Punctuation(HlPunct::Other).into() 196 HlTag::Punctuation(HlPunct::Other).into()
183 } 197 }
184 } 198 }
185 T![-] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => { 199 T![-] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => {
200 eprintln!("the - operator: {}", element);
186 let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?; 201 let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?;
187 202
188 let expr = prefix_expr.expr()?; 203 let expr = prefix_expr.expr()?;
189 match expr { 204 match expr {
190 ast::Expr::Literal(_) => HlTag::NumericLiteral, 205 ast::Expr::Literal(_) => HlTag::NumericLiteral,
191 _ => HlTag::Operator, 206 _ => HlTag::Operator(HlOperator::Other),
192 } 207 }
193 .into() 208 .into()
194 } 209 }
195 _ if element.parent().and_then(ast::PrefixExpr::cast).is_some() => { 210 _ 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()
197 } 235 }
198 _ if element.parent().and_then(ast::BinExpr::cast).is_some() => HlTag::Operator.into(),
199 _ if element.parent().and_then(ast::RangeExpr::cast).is_some() => { 236 _ 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()
201 } 245 }
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(),
204 _ if element.parent().and_then(ast::Attr::cast).is_some() => HlTag::Attribute.into(), 246 _ if element.parent().and_then(ast::Attr::cast).is_some() => HlTag::Attribute.into(),
205 kind => HlTag::Punctuation(match kind { 247 kind => HlTag::Punctuation(match kind {
206 T!['['] | T![']'] => HlPunct::Bracket, 248 T!['['] | T![']'] => HlPunct::Bracket,
diff --git a/crates/ide/src/syntax_highlighting/tags.rs b/crates/ide/src/syntax_highlighting/tags.rs
index 1cec991aa..8128d231d 100644
--- a/crates/ide/src/syntax_highlighting/tags.rs
+++ b/crates/ide/src/syntax_highlighting/tags.rs
@@ -28,7 +28,7 @@ pub enum HlTag {
28 FormatSpecifier, 28 FormatSpecifier,
29 Keyword, 29 Keyword,
30 NumericLiteral, 30 NumericLiteral,
31 Operator, 31 Operator(HlOperator),
32 Punctuation(HlPunct), 32 Punctuation(HlPunct),
33 StringLiteral, 33 StringLiteral,
34 UnresolvedReference, 34 UnresolvedReference,
@@ -87,6 +87,20 @@ pub enum HlPunct {
87 Other, 87 Other,
88} 88}
89 89
90#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
91pub enum HlOperator {
92 /// |, &, !, ^, |=, &=, ^=
93 Bitwise,
94 /// +, -, *, /, +=, -=, *=, /=
95 Arithmetic,
96 /// &&, ||, !
97 Logical,
98 /// >, <, ==, >=, <=, !=
99 Comparision,
100 ///
101 Other,
102}
103
90impl HlTag { 104impl HlTag {
91 fn as_str(self) -> &'static str { 105 fn as_str(self) -> &'static str {
92 match self { 106 match self {
@@ -133,7 +147,13 @@ impl HlTag {
133 HlPunct::Other => "punctuation", 147 HlPunct::Other => "punctuation",
134 }, 148 },
135 HlTag::NumericLiteral => "numeric_literal", 149 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 },
137 HlTag::StringLiteral => "string_literal", 157 HlTag::StringLiteral => "string_literal",
138 HlTag::UnresolvedReference => "unresolved_reference", 158 HlTag::UnresolvedReference => "unresolved_reference",
139 HlTag::None => "none", 159 HlTag::None => "none",
diff --git a/crates/rust-analyzer/src/semantic_tokens.rs b/crates/rust-analyzer/src/semantic_tokens.rs
index 2dc8a42f1..adc059817 100644
--- a/crates/rust-analyzer/src/semantic_tokens.rs
+++ b/crates/rust-analyzer/src/semantic_tokens.rs
@@ -39,7 +39,9 @@ macro_rules! define_semantic_token_types {
39 39
40define_semantic_token_types![ 40define_semantic_token_types![
41 (ANGLE, "angle"), 41 (ANGLE, "angle"),
42 (ARITHMETIC, "arithmetic"),
42 (ATTRIBUTE, "attribute"), 43 (ATTRIBUTE, "attribute"),
44 (BITWISE, "bitwise"),
43 (BOOLEAN, "boolean"), 45 (BOOLEAN, "boolean"),
44 (BRACE, "brace"), 46 (BRACE, "brace"),
45 (BRACKET, "bracket"), 47 (BRACKET, "bracket"),
@@ -47,6 +49,7 @@ define_semantic_token_types![
47 (CHAR_LITERAL, "characterLiteral"), 49 (CHAR_LITERAL, "characterLiteral"),
48 (COLON, "colon"), 50 (COLON, "colon"),
49 (COMMA, "comma"), 51 (COMMA, "comma"),
52 (COMPARISION, "comparision"),
50 (CONST_PARAMETER, "constParameter"), 53 (CONST_PARAMETER, "constParameter"),
51 (DOT, "dot"), 54 (DOT, "dot"),
52 (ESCAPE_SEQUENCE, "escapeSequence"), 55 (ESCAPE_SEQUENCE, "escapeSequence"),
@@ -54,6 +57,8 @@ define_semantic_token_types![
54 (GENERIC, "generic"), 57 (GENERIC, "generic"),
55 (LABEL, "label"), 58 (LABEL, "label"),
56 (LIFETIME, "lifetime"), 59 (LIFETIME, "lifetime"),
60 (LOGICAL, "logical"),
61 (OPERATOR, "operator"),
57 (PARENTHESIS, "parenthesis"), 62 (PARENTHESIS, "parenthesis"),
58 (PUNCTUATION, "punctuation"), 63 (PUNCTUATION, "punctuation"),
59 (SELF_KEYWORD, "selfKeyword"), 64 (SELF_KEYWORD, "selfKeyword"),
diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs
index 2ac31d981..df9292f8e 100644
--- a/crates/rust-analyzer/src/to_proto.rs
+++ b/crates/rust-analyzer/src/to_proto.rs
@@ -7,9 +7,9 @@ use std::{
7use ide::{ 7use ide::{
8 Annotation, AnnotationKind, Assist, AssistKind, CallInfo, CompletionItem, CompletionItemKind, 8 Annotation, AnnotationKind, Assist, AssistKind, CallInfo, CompletionItem, CompletionItemKind,
9 CompletionRelevance, Documentation, FileId, FileRange, FileSystemEdit, Fold, FoldKind, 9 CompletionRelevance, Documentation, FileId, FileRange, FileSystemEdit, Fold, FoldKind,
10 Highlight, HlMod, HlPunct, HlRange, HlTag, Indel, InlayHint, InlayKind, InsertTextFormat, 10 Highlight, HlMod, HlOperator, HlPunct, HlRange, HlTag, Indel, InlayHint, InlayKind,
11 Markup, NavigationTarget, ReferenceAccess, RenameError, Runnable, Severity, SourceChange, 11 InsertTextFormat, Markup, NavigationTarget, ReferenceAccess, RenameError, Runnable, Severity,
12 StructureNodeKind, SymbolKind, TextEdit, TextRange, TextSize, 12 SourceChange, StructureNodeKind, SymbolKind, TextEdit, TextRange, TextSize,
13}; 13};
14use itertools::Itertools; 14use itertools::Itertools;
15use serde_json::to_value; 15use serde_json::to_value;
@@ -445,7 +445,13 @@ fn semantic_token_type_and_modifiers(
445 HlTag::FormatSpecifier => semantic_tokens::FORMAT_SPECIFIER, 445 HlTag::FormatSpecifier => semantic_tokens::FORMAT_SPECIFIER,
446 HlTag::Keyword => lsp_types::SemanticTokenType::KEYWORD, 446 HlTag::Keyword => lsp_types::SemanticTokenType::KEYWORD,
447 HlTag::None => semantic_tokens::GENERIC, 447 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 },
449 HlTag::StringLiteral => lsp_types::SemanticTokenType::STRING, 455 HlTag::StringLiteral => lsp_types::SemanticTokenType::STRING,
450 HlTag::UnresolvedReference => semantic_tokens::UNRESOLVED_REFERENCE, 456 HlTag::UnresolvedReference => semantic_tokens::UNRESOLVED_REFERENCE,
451 HlTag::Punctuation(punct) => match punct { 457 HlTag::Punctuation(punct) => match punct {