aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_ide/src/lib.rs4
-rw-r--r--crates/ra_ide/src/syntax_highlighting.rs107
-rw-r--r--crates/ra_ide/src/syntax_highlighting/highlight.rs163
-rw-r--r--crates/ra_ide/src/syntax_highlighting/highlight_tag.rs43
-rw-r--r--crates/rust-analyzer/src/caps.rs7
-rw-r--r--crates/rust-analyzer/src/conv.rs111
-rw-r--r--crates/rust-analyzer/src/main_loop/handlers.rs22
-rw-r--r--crates/rust-analyzer/src/semantic_tokens.rs36
-rw-r--r--editors/code/package.json19
9 files changed, 332 insertions, 180 deletions
diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs
index d74d32453..d509de14e 100644
--- a/crates/ra_ide/src/lib.rs
+++ b/crates/ra_ide/src/lib.rs
@@ -74,7 +74,9 @@ pub use crate::{
74 runnables::{Runnable, RunnableKind, TestId}, 74 runnables::{Runnable, RunnableKind, TestId},
75 source_change::{FileSystemEdit, SourceChange, SourceFileEdit}, 75 source_change::{FileSystemEdit, SourceChange, SourceFileEdit},
76 ssr::SsrError, 76 ssr::SsrError,
77 syntax_highlighting::{HighlightTag, HighlightedRange}, 77 syntax_highlighting::{
78 Highlight, HighlightModifier, HighlightModifiers, HighlightTag, HighlightedRange,
79 },
78}; 80};
79 81
80pub use hir::Documentation; 82pub use hir::Documentation;
diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs
index d422930bf..18980dc20 100644
--- a/crates/ra_ide/src/syntax_highlighting.rs
+++ b/crates/ra_ide/src/syntax_highlighting.rs
@@ -1,6 +1,6 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3mod highlight_tag; 3mod highlight;
4 4
5use hir::{Name, Semantics}; 5use hir::{Name, Semantics};
6use ra_db::SourceDatabase; 6use ra_db::SourceDatabase;
@@ -17,12 +17,12 @@ use rustc_hash::FxHashMap;
17 17
18use crate::{references::classify_name_ref, FileId}; 18use crate::{references::classify_name_ref, FileId};
19 19
20pub use highlight_tag::HighlightTag; 20pub use highlight::{Highlight, HighlightModifier, HighlightModifiers, HighlightTag};
21 21
22#[derive(Debug)] 22#[derive(Debug)]
23pub struct HighlightedRange { 23pub struct HighlightedRange {
24 pub range: TextRange, 24 pub range: TextRange,
25 pub tag: HighlightTag, 25 pub highlight: Highlight,
26 pub binding_hash: Option<u64>, 26 pub binding_hash: Option<u64>,
27} 27}
28 28
@@ -79,33 +79,33 @@ pub(crate) fn highlight(
79 if let Some(range) = highlight_macro(node) { 79 if let Some(range) = highlight_macro(node) {
80 res.push(HighlightedRange { 80 res.push(HighlightedRange {
81 range, 81 range,
82 tag: HighlightTag::MACRO, 82 highlight: HighlightTag::Macro.into(),
83 binding_hash: None, 83 binding_hash: None,
84 }); 84 });
85 } 85 }
86 } 86 }
87 _ if in_macro_call.is_some() => { 87 _ if in_macro_call.is_some() => {
88 if let Some(token) = node.as_token() { 88 if let Some(token) = node.as_token() {
89 if let Some((tag, binding_hash)) = highlight_token_tree( 89 if let Some((highlight, binding_hash)) = highlight_token_tree(
90 &sema, 90 &sema,
91 &mut bindings_shadow_count, 91 &mut bindings_shadow_count,
92 token.clone(), 92 token.clone(),
93 ) { 93 ) {
94 res.push(HighlightedRange { 94 res.push(HighlightedRange {
95 range: node.text_range(), 95 range: node.text_range(),
96 tag, 96 highlight,
97 binding_hash, 97 binding_hash,
98 }); 98 });
99 } 99 }
100 } 100 }
101 } 101 }
102 _ => { 102 _ => {
103 if let Some((tag, binding_hash)) = 103 if let Some((highlight, binding_hash)) =
104 highlight_node(&sema, &mut bindings_shadow_count, node.clone()) 104 highlight_node(&sema, &mut bindings_shadow_count, node.clone())
105 { 105 {
106 res.push(HighlightedRange { 106 res.push(HighlightedRange {
107 range: node.text_range(), 107 range: node.text_range(),
108 tag, 108 highlight,
109 binding_hash, 109 binding_hash,
110 }); 110 });
111 } 111 }
@@ -150,7 +150,7 @@ fn highlight_token_tree(
150 sema: &Semantics<RootDatabase>, 150 sema: &Semantics<RootDatabase>,
151 bindings_shadow_count: &mut FxHashMap<Name, u32>, 151 bindings_shadow_count: &mut FxHashMap<Name, u32>,
152 token: SyntaxToken, 152 token: SyntaxToken,
153) -> Option<(HighlightTag, Option<u64>)> { 153) -> Option<(Highlight, Option<u64>)> {
154 if token.parent().kind() != TOKEN_TREE { 154 if token.parent().kind() != TOKEN_TREE {
155 return None; 155 return None;
156 } 156 }
@@ -171,19 +171,21 @@ fn highlight_node(
171 sema: &Semantics<RootDatabase>, 171 sema: &Semantics<RootDatabase>,
172 bindings_shadow_count: &mut FxHashMap<Name, u32>, 172 bindings_shadow_count: &mut FxHashMap<Name, u32>,
173 node: SyntaxElement, 173 node: SyntaxElement,
174) -> Option<(HighlightTag, Option<u64>)> { 174) -> Option<(Highlight, Option<u64>)> {
175 let db = sema.db; 175 let db = sema.db;
176 let mut binding_hash = None; 176 let mut binding_hash = None;
177 let tag = match node.kind() { 177 let highlight: Highlight = match node.kind() {
178 FN_DEF => { 178 FN_DEF => {
179 bindings_shadow_count.clear(); 179 bindings_shadow_count.clear();
180 return None; 180 return None;
181 } 181 }
182 COMMENT => HighlightTag::LITERAL_COMMENT, 182 COMMENT => HighlightTag::Comment.into(),
183 STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => HighlightTag::LITERAL_STRING, 183 STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => HighlightTag::LiteralString.into(),
184 ATTR => HighlightTag::LITERAL_ATTRIBUTE, 184 ATTR => HighlightTag::Attribute.into(),
185 // Special-case field init shorthand 185 // Special-case field init shorthand
186 NAME_REF if node.parent().and_then(ast::RecordField::cast).is_some() => HighlightTag::FIELD, 186 NAME_REF if node.parent().and_then(ast::RecordField::cast).is_some() => {
187 HighlightTag::Field.into()
188 }
187 NAME_REF if node.ancestors().any(|it| it.kind() == ATTR) => return None, 189 NAME_REF if node.ancestors().any(|it| it.kind() == ATTR) => return None,
188 NAME_REF => { 190 NAME_REF => {
189 let name_ref = node.as_node().cloned().and_then(ast::NameRef::cast).unwrap(); 191 let name_ref = node.as_node().cloned().and_then(ast::NameRef::cast).unwrap();
@@ -217,26 +219,30 @@ fn highlight_node(
217 219
218 match name_kind { 220 match name_kind {
219 Some(name_kind) => highlight_name(db, name_kind), 221 Some(name_kind) => highlight_name(db, name_kind),
220 None => name.syntax().parent().map_or(HighlightTag::FUNCTION, |x| match x.kind() { 222 None => name.syntax().parent().map_or(HighlightTag::Function.into(), |x| {
221 STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => HighlightTag::TYPE, 223 match x.kind() {
222 TYPE_PARAM => HighlightTag::TYPE_PARAM, 224 STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => {
223 RECORD_FIELD_DEF => HighlightTag::FIELD, 225 HighlightTag::Type.into()
224 _ => HighlightTag::FUNCTION, 226 }
227 TYPE_PARAM => HighlightTag::TypeParam.into(),
228 RECORD_FIELD_DEF => HighlightTag::Field.into(),
229 _ => HighlightTag::Function.into(),
230 }
225 }), 231 }),
226 } 232 }
227 } 233 }
228 INT_NUMBER | FLOAT_NUMBER => HighlightTag::LITERAL_NUMERIC, 234 INT_NUMBER | FLOAT_NUMBER => HighlightTag::LiteralNumeric.into(),
229 BYTE => HighlightTag::LITERAL_BYTE, 235 BYTE => HighlightTag::LiteralByte.into(),
230 CHAR => HighlightTag::LITERAL_CHAR, 236 CHAR => HighlightTag::LiteralChar.into(),
231 LIFETIME => HighlightTag::TYPE_LIFETIME, 237 LIFETIME => HighlightTag::TypeLifetime.into(),
232 T![unsafe] => HighlightTag::KEYWORD_UNSAFE, 238 T![unsafe] => HighlightTag::Keyword | HighlightModifier::Unsafe,
233 k if is_control_keyword(k) => HighlightTag::KEYWORD_CONTROL, 239 k if is_control_keyword(k) => HighlightTag::Keyword | HighlightModifier::Control,
234 k if k.is_keyword() => HighlightTag::KEYWORD, 240 k if k.is_keyword() => HighlightTag::Keyword.into(),
235 241
236 _ => return None, 242 _ => return None,
237 }; 243 };
238 244
239 return Some((tag, binding_hash)); 245 return Some((highlight, binding_hash));
240 246
241 fn calc_binding_hash(name: &Name, shadow_count: u32) -> u64 { 247 fn calc_binding_hash(name: &Name, shadow_count: u32) -> u64 {
242 fn hash<T: std::hash::Hash + std::fmt::Debug>(x: T) -> u64 { 248 fn hash<T: std::hash::Hash + std::fmt::Debug>(x: T) -> u64 {
@@ -293,7 +299,11 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
293 if ranges.is_empty() { 299 if ranges.is_empty() {
294 buf.push_str(&text); 300 buf.push_str(&text);
295 } else { 301 } else {
296 let classes = ranges.iter().map(|x| x.tag.to_string()).collect::<Vec<_>>().join(" "); 302 let classes = ranges
303 .iter()
304 .map(|it| it.highlight.to_string().replace('.', " "))
305 .collect::<Vec<_>>()
306 .join(" ");
297 let binding_hash = ranges.first().and_then(|x| x.binding_hash); 307 let binding_hash = ranges.first().and_then(|x| x.binding_hash);
298 let color = match (rainbow, binding_hash) { 308 let color = match (rainbow, binding_hash) {
299 (true, Some(hash)) => format!( 309 (true, Some(hash)) => format!(
@@ -310,29 +320,32 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
310 buf 320 buf
311} 321}
312 322
313fn highlight_name(db: &RootDatabase, def: NameDefinition) -> HighlightTag { 323fn highlight_name(db: &RootDatabase, def: NameDefinition) -> Highlight {
314 match def { 324 match def {
315 NameDefinition::Macro(_) => HighlightTag::MACRO, 325 NameDefinition::Macro(_) => HighlightTag::Macro,
316 NameDefinition::StructField(_) => HighlightTag::FIELD, 326 NameDefinition::StructField(_) => HighlightTag::Field,
317 NameDefinition::ModuleDef(hir::ModuleDef::Module(_)) => HighlightTag::MODULE, 327 NameDefinition::ModuleDef(hir::ModuleDef::Module(_)) => HighlightTag::Module,
318 NameDefinition::ModuleDef(hir::ModuleDef::Function(_)) => HighlightTag::FUNCTION, 328 NameDefinition::ModuleDef(hir::ModuleDef::Function(_)) => HighlightTag::Function,
319 NameDefinition::ModuleDef(hir::ModuleDef::Adt(_)) => HighlightTag::TYPE, 329 NameDefinition::ModuleDef(hir::ModuleDef::Adt(_)) => HighlightTag::Type,
320 NameDefinition::ModuleDef(hir::ModuleDef::EnumVariant(_)) => HighlightTag::CONSTANT, 330 NameDefinition::ModuleDef(hir::ModuleDef::EnumVariant(_)) => HighlightTag::Constant,
321 NameDefinition::ModuleDef(hir::ModuleDef::Const(_)) => HighlightTag::CONSTANT, 331 NameDefinition::ModuleDef(hir::ModuleDef::Const(_)) => HighlightTag::Constant,
322 NameDefinition::ModuleDef(hir::ModuleDef::Static(_)) => HighlightTag::CONSTANT, 332 NameDefinition::ModuleDef(hir::ModuleDef::Static(_)) => HighlightTag::Constant,
323 NameDefinition::ModuleDef(hir::ModuleDef::Trait(_)) => HighlightTag::TYPE, 333 NameDefinition::ModuleDef(hir::ModuleDef::Trait(_)) => HighlightTag::Type,
324 NameDefinition::ModuleDef(hir::ModuleDef::TypeAlias(_)) => HighlightTag::TYPE, 334 NameDefinition::ModuleDef(hir::ModuleDef::TypeAlias(_)) => HighlightTag::Type,
325 NameDefinition::ModuleDef(hir::ModuleDef::BuiltinType(_)) => HighlightTag::TYPE_BUILTIN, 335 NameDefinition::ModuleDef(hir::ModuleDef::BuiltinType(_)) => {
326 NameDefinition::SelfType(_) => HighlightTag::TYPE_SELF, 336 return HighlightTag::Type | HighlightModifier::Builtin
327 NameDefinition::TypeParam(_) => HighlightTag::TYPE_PARAM, 337 }
338 NameDefinition::SelfType(_) => HighlightTag::TypeSelf,
339 NameDefinition::TypeParam(_) => HighlightTag::TypeParam,
328 NameDefinition::Local(local) => { 340 NameDefinition::Local(local) => {
341 let mut h = Highlight::new(HighlightTag::Variable);
329 if local.is_mut(db) || local.ty(db).is_mutable_reference() { 342 if local.is_mut(db) || local.ty(db).is_mutable_reference() {
330 HighlightTag::VARIABLE_MUT 343 h |= HighlightModifier::Mutable;
331 } else {
332 HighlightTag::VARIABLE
333 } 344 }
345 return h;
334 } 346 }
335 } 347 }
348 .into()
336} 349}
337 350
338//FIXME: like, real html escaping 351//FIXME: like, real html escaping
@@ -498,6 +511,6 @@ fn bar() {
498 }) 511 })
499 .unwrap(); 512 .unwrap();
500 513
501 assert_eq!(&highlights[0].tag.to_string(), "field"); 514 assert_eq!(&highlights[0].highlight.to_string(), "field");
502 } 515 }
503} 516}
diff --git a/crates/ra_ide/src/syntax_highlighting/highlight.rs b/crates/ra_ide/src/syntax_highlighting/highlight.rs
new file mode 100644
index 000000000..383c74c98
--- /dev/null
+++ b/crates/ra_ide/src/syntax_highlighting/highlight.rs
@@ -0,0 +1,163 @@
1//! Defines token tags we use for syntax highlighting.
2//! A tag is not unlike a CSS class.
3
4use std::{fmt, ops};
5
6#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
7pub struct Highlight {
8 pub tag: HighlightTag,
9 pub modifiers: HighlightModifiers,
10}
11
12#[derive(Default, Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
13pub struct HighlightModifiers(u32);
14
15#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
16pub enum HighlightTag {
17 Field,
18 Function,
19 Module,
20 Constant,
21 Macro,
22 Variable,
23
24 Type,
25 TypeSelf,
26 TypeParam,
27 TypeLifetime,
28
29 LiteralByte,
30 LiteralNumeric,
31 LiteralChar,
32
33 Comment,
34 LiteralString,
35 Attribute,
36
37 Keyword,
38}
39
40#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
41#[repr(u8)]
42pub enum HighlightModifier {
43 Mutable = 0,
44 Unsafe,
45 /// Used with keywords like `if` and `break`.
46 Control,
47 Builtin,
48}
49
50impl HighlightTag {
51 fn as_str(self) -> &'static str {
52 match self {
53 HighlightTag::Field => "field",
54 HighlightTag::Function => "function",
55 HighlightTag::Module => "module",
56 HighlightTag::Constant => "constant",
57 HighlightTag::Macro => "macro",
58 HighlightTag::Variable => "variable",
59 HighlightTag::Type => "type",
60 HighlightTag::TypeSelf => "type.self",
61 HighlightTag::TypeParam => "type.param",
62 HighlightTag::TypeLifetime => "type.lifetime",
63 HighlightTag::LiteralByte => "literal.byte",
64 HighlightTag::LiteralNumeric => "literal.numeric",
65 HighlightTag::LiteralChar => "literal.char",
66 HighlightTag::Comment => "comment",
67 HighlightTag::LiteralString => "string",
68 HighlightTag::Attribute => "attribute",
69 HighlightTag::Keyword => "keyword",
70 }
71 }
72}
73
74impl fmt::Display for HighlightTag {
75 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76 fmt::Display::fmt(self.as_str(), f)
77 }
78}
79
80impl HighlightModifier {
81 const ALL: &'static [HighlightModifier] = &[
82 HighlightModifier::Mutable,
83 HighlightModifier::Unsafe,
84 HighlightModifier::Control,
85 HighlightModifier::Builtin,
86 ];
87
88 fn as_str(self) -> &'static str {
89 match self {
90 HighlightModifier::Mutable => "mutable",
91 HighlightModifier::Unsafe => "unsafe",
92 HighlightModifier::Control => "control",
93 HighlightModifier::Builtin => "builtin",
94 }
95 }
96
97 fn mask(self) -> u32 {
98 1 << (self as u32)
99 }
100}
101
102impl fmt::Display for HighlightModifier {
103 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104 fmt::Display::fmt(self.as_str(), f)
105 }
106}
107
108impl fmt::Display for Highlight {
109 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110 write!(f, "{}", self.tag)?;
111 for modifier in self.modifiers.iter() {
112 write!(f, ".{}", modifier)?
113 }
114 Ok(())
115 }
116}
117
118impl From<HighlightTag> for Highlight {
119 fn from(tag: HighlightTag) -> Highlight {
120 Highlight::new(tag)
121 }
122}
123
124impl Highlight {
125 pub(crate) fn new(tag: HighlightTag) -> Highlight {
126 Highlight { tag, modifiers: HighlightModifiers::default() }
127 }
128}
129
130impl ops::BitOr<HighlightModifier> for HighlightTag {
131 type Output = Highlight;
132
133 fn bitor(self, rhs: HighlightModifier) -> Highlight {
134 Highlight::new(self) | rhs
135 }
136}
137
138impl ops::BitOrAssign<HighlightModifier> for HighlightModifiers {
139 fn bitor_assign(&mut self, rhs: HighlightModifier) {
140 self.0 |= rhs.mask();
141 }
142}
143
144impl ops::BitOrAssign<HighlightModifier> for Highlight {
145 fn bitor_assign(&mut self, rhs: HighlightModifier) {
146 self.modifiers |= rhs;
147 }
148}
149
150impl ops::BitOr<HighlightModifier> for Highlight {
151 type Output = Highlight;
152
153 fn bitor(mut self, rhs: HighlightModifier) -> Highlight {
154 self |= rhs;
155 self
156 }
157}
158
159impl HighlightModifiers {
160 pub fn iter(self) -> impl Iterator<Item = HighlightModifier> {
161 HighlightModifier::ALL.iter().copied().filter(move |it| self.0 & it.mask() == it.mask())
162 }
163}
diff --git a/crates/ra_ide/src/syntax_highlighting/highlight_tag.rs b/crates/ra_ide/src/syntax_highlighting/highlight_tag.rs
deleted file mode 100644
index af1ac07b3..000000000
--- a/crates/ra_ide/src/syntax_highlighting/highlight_tag.rs
+++ /dev/null
@@ -1,43 +0,0 @@
1//! Defines token tags we use for syntax highlighting.
2//! A tag is not unlike a CSS class.
3
4use std::fmt;
5
6#[derive(Debug, PartialEq, Eq, Clone, Copy)]
7pub struct HighlightTag(&'static str);
8
9impl fmt::Display for HighlightTag {
10 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11 fmt::Display::fmt(self.0, f)
12 }
13}
14
15#[rustfmt::skip]
16impl HighlightTag {
17 pub const FIELD: HighlightTag = HighlightTag("field");
18 pub const FUNCTION: HighlightTag = HighlightTag("function");
19 pub const MODULE: HighlightTag = HighlightTag("module");
20 pub const CONSTANT: HighlightTag = HighlightTag("constant");
21 pub const MACRO: HighlightTag = HighlightTag("macro");
22
23 pub const VARIABLE: HighlightTag = HighlightTag("variable");
24 pub const VARIABLE_MUT: HighlightTag = HighlightTag("variable.mut");
25
26 pub const TYPE: HighlightTag = HighlightTag("type");
27 pub const TYPE_BUILTIN: HighlightTag = HighlightTag("type.builtin");
28 pub const TYPE_SELF: HighlightTag = HighlightTag("type.self");
29 pub const TYPE_PARAM: HighlightTag = HighlightTag("type.param");
30 pub const TYPE_LIFETIME: HighlightTag = HighlightTag("type.lifetime");
31
32 pub const LITERAL_BYTE: HighlightTag = HighlightTag("literal.byte");
33 pub const LITERAL_NUMERIC: HighlightTag = HighlightTag("literal.numeric");
34 pub const LITERAL_CHAR: HighlightTag = HighlightTag("literal.char");
35
36 pub const LITERAL_COMMENT: HighlightTag = HighlightTag("comment");
37 pub const LITERAL_STRING: HighlightTag = HighlightTag("string");
38 pub const LITERAL_ATTRIBUTE: HighlightTag = HighlightTag("attribute");
39
40 pub const KEYWORD: HighlightTag = HighlightTag("keyword");
41 pub const KEYWORD_UNSAFE: HighlightTag = HighlightTag("keyword.unsafe");
42 pub const KEYWORD_CONTROL: HighlightTag = HighlightTag("keyword.control");
43}
diff --git a/crates/rust-analyzer/src/caps.rs b/crates/rust-analyzer/src/caps.rs
index db82eeb1c..759bceb32 100644
--- a/crates/rust-analyzer/src/caps.rs
+++ b/crates/rust-analyzer/src/caps.rs
@@ -63,11 +63,8 @@ pub fn server_capabilities() -> ServerCapabilities {
63 semantic_tokens_provider: Some( 63 semantic_tokens_provider: Some(
64 SemanticTokensOptions { 64 SemanticTokensOptions {
65 legend: SemanticTokensLegend { 65 legend: SemanticTokensLegend {
66 token_types: semantic_tokens::supported_token_types().iter().cloned().collect(), 66 token_types: semantic_tokens::SUPPORTED_TYPES.iter().cloned().collect(),
67 token_modifiers: semantic_tokens::supported_token_modifiers() 67 token_modifiers: semantic_tokens::SUPPORTED_MODIFIERS.iter().cloned().collect(),
68 .iter()
69 .cloned()
70 .collect(),
71 }, 68 },
72 69
73 document_provider: Some(SemanticTokensDocumentProvider::Bool(true)), 70 document_provider: Some(SemanticTokensDocumentProvider::Bool(true)),
diff --git a/crates/rust-analyzer/src/conv.rs b/crates/rust-analyzer/src/conv.rs
index 5596967bd..86851c1f1 100644
--- a/crates/rust-analyzer/src/conv.rs
+++ b/crates/rust-analyzer/src/conv.rs
@@ -10,14 +10,21 @@ use lsp_types::{
10}; 10};
11use ra_ide::{ 11use ra_ide::{
12 translate_offset_with_edit, CompletionItem, CompletionItemKind, FileId, FilePosition, 12 translate_offset_with_edit, CompletionItem, CompletionItemKind, FileId, FilePosition,
13 FileRange, FileSystemEdit, Fold, FoldKind, HighlightTag, InsertTextFormat, LineCol, LineIndex, 13 FileRange, FileSystemEdit, Fold, FoldKind, Highlight, HighlightModifier, HighlightTag,
14 NavigationTarget, RangeInfo, ReferenceAccess, Severity, SourceChange, SourceFileEdit, 14 InsertTextFormat, LineCol, LineIndex, NavigationTarget, RangeInfo, ReferenceAccess, Severity,
15 SourceChange, SourceFileEdit,
15}; 16};
16use ra_syntax::{SyntaxKind, TextRange, TextUnit}; 17use ra_syntax::{SyntaxKind, TextRange, TextUnit};
17use ra_text_edit::{AtomTextEdit, TextEdit}; 18use ra_text_edit::{AtomTextEdit, TextEdit};
18use ra_vfs::LineEndings; 19use ra_vfs::LineEndings;
19 20
20use crate::{req, semantic_tokens, world::WorldSnapshot, Result}; 21use crate::{
22 req,
23 semantic_tokens::{self, ModifierSet, BUILTIN, CONTROL, MUTABLE, UNSAFE},
24 world::WorldSnapshot,
25 Result,
26};
27use semantic_tokens::ATTRIBUTE;
21 28
22pub trait Conv { 29pub trait Conv {
23 type Output; 30 type Output;
@@ -303,74 +310,52 @@ impl ConvWith<&FoldConvCtx<'_>> for Fold {
303 } 310 }
304} 311}
305 312
306impl Conv for HighlightTag { 313impl Conv for Highlight {
307 type Output = (SemanticTokenType, Vec<SemanticTokenModifier>); 314 type Output = (u32, u32);
308
309 fn conv(self) -> (SemanticTokenType, Vec<SemanticTokenModifier>) {
310 let token_type: SemanticTokenType = match self {
311 HighlightTag::FIELD => SemanticTokenType::MEMBER,
312 HighlightTag::FUNCTION => SemanticTokenType::FUNCTION,
313 HighlightTag::MODULE => SemanticTokenType::NAMESPACE,
314 HighlightTag::CONSTANT => {
315 return (
316 SemanticTokenType::VARIABLE,
317 vec![SemanticTokenModifier::STATIC, SemanticTokenModifier::READONLY],
318 )
319 }
320 HighlightTag::MACRO => SemanticTokenType::MACRO,
321
322 HighlightTag::VARIABLE => {
323 return (SemanticTokenType::VARIABLE, vec![SemanticTokenModifier::READONLY])
324 }
325 HighlightTag::VARIABLE_MUT => SemanticTokenType::VARIABLE,
326 315
327 HighlightTag::TYPE => SemanticTokenType::TYPE, 316 fn conv(self) -> Self::Output {
328 HighlightTag::TYPE_BUILTIN => SemanticTokenType::TYPE, 317 let mut mods = ModifierSet::default();
329 HighlightTag::TYPE_SELF => { 318 let type_ = match self.tag {
330 return (SemanticTokenType::TYPE, vec![SemanticTokenModifier::REFERENCE]) 319 HighlightTag::Field => SemanticTokenType::MEMBER,
320 HighlightTag::Function => SemanticTokenType::FUNCTION,
321 HighlightTag::Module => SemanticTokenType::NAMESPACE,
322 HighlightTag::Constant => {
323 mods |= SemanticTokenModifier::STATIC;
324 mods |= SemanticTokenModifier::READONLY;
325 SemanticTokenType::VARIABLE
331 } 326 }
332 HighlightTag::TYPE_PARAM => SemanticTokenType::TYPE_PARAMETER, 327 HighlightTag::Macro => SemanticTokenType::MACRO,
333 HighlightTag::TYPE_LIFETIME => { 328 HighlightTag::Variable => SemanticTokenType::VARIABLE,
334 return (SemanticTokenType::LABEL, vec![SemanticTokenModifier::REFERENCE]) 329 HighlightTag::Type => SemanticTokenType::TYPE,
330 HighlightTag::TypeSelf => {
331 mods |= SemanticTokenModifier::REFERENCE;
332 SemanticTokenType::TYPE
335 } 333 }
336 334 HighlightTag::TypeParam => SemanticTokenType::TYPE_PARAMETER,
337 HighlightTag::LITERAL_BYTE => SemanticTokenType::NUMBER, 335 HighlightTag::TypeLifetime => {
338 HighlightTag::LITERAL_NUMERIC => SemanticTokenType::NUMBER, 336 mods |= SemanticTokenModifier::REFERENCE;
339 HighlightTag::LITERAL_CHAR => SemanticTokenType::NUMBER, 337 SemanticTokenType::LABEL
340
341 HighlightTag::LITERAL_COMMENT => {
342 return (SemanticTokenType::COMMENT, vec![SemanticTokenModifier::DOCUMENTATION])
343 } 338 }
344 339 HighlightTag::LiteralByte => SemanticTokenType::NUMBER,
345 HighlightTag::LITERAL_STRING => SemanticTokenType::STRING, 340 HighlightTag::LiteralNumeric => SemanticTokenType::NUMBER,
346 HighlightTag::LITERAL_ATTRIBUTE => SemanticTokenType::KEYWORD, 341 HighlightTag::LiteralChar => SemanticTokenType::NUMBER,
347 342 HighlightTag::Comment => SemanticTokenType::COMMENT,
348 HighlightTag::KEYWORD => SemanticTokenType::KEYWORD, 343 HighlightTag::LiteralString => SemanticTokenType::STRING,
349 HighlightTag::KEYWORD_UNSAFE => SemanticTokenType::KEYWORD, 344 HighlightTag::Attribute => ATTRIBUTE,
350 HighlightTag::KEYWORD_CONTROL => SemanticTokenType::KEYWORD, 345 HighlightTag::Keyword => SemanticTokenType::KEYWORD,
351 unknown => panic!("Unknown semantic token: {}", unknown),
352 }; 346 };
353 347
354 (token_type, vec![]) 348 for modifier in self.modifiers.iter() {
355 } 349 let modifier = match modifier {
356} 350 HighlightModifier::Mutable => MUTABLE,
357 351 HighlightModifier::Unsafe => UNSAFE,
358impl Conv for (SemanticTokenType, Vec<SemanticTokenModifier>) { 352 HighlightModifier::Control => CONTROL,
359 type Output = (u32, u32); 353 HighlightModifier::Builtin => BUILTIN,
360 354 };
361 fn conv(self) -> Self::Output { 355 mods |= modifier;
362 let token_index =
363 semantic_tokens::supported_token_types().iter().position(|it| *it == self.0).unwrap();
364 let mut token_modifier_bitset = 0;
365 for modifier in self.1.iter() {
366 let modifier_index = semantic_tokens::supported_token_modifiers()
367 .iter()
368 .position(|it| it == modifier)
369 .unwrap();
370 token_modifier_bitset |= 1 << modifier_index;
371 } 356 }
372 357
373 (token_index as u32, token_modifier_bitset as u32) 358 (semantic_tokens::type_index(type_), mods.0)
374 } 359 }
375} 360}
376 361
diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs
index e9f1c4f4b..9ed53169c 100644
--- a/crates/rust-analyzer/src/main_loop/handlers.rs
+++ b/crates/rust-analyzer/src/main_loop/handlers.rs
@@ -16,9 +16,9 @@ use lsp_types::{
16 CodeAction, CodeActionOrCommand, CodeActionResponse, CodeLens, Command, CompletionItem, 16 CodeAction, CodeActionOrCommand, CodeActionResponse, CodeLens, Command, CompletionItem,
17 Diagnostic, DocumentFormattingParams, DocumentHighlight, DocumentSymbol, FoldingRange, 17 Diagnostic, DocumentFormattingParams, DocumentHighlight, DocumentSymbol, FoldingRange,
18 FoldingRangeParams, Hover, HoverContents, Location, MarkupContent, MarkupKind, Position, 18 FoldingRangeParams, Hover, HoverContents, Location, MarkupContent, MarkupKind, Position,
19 PrepareRenameResponse, Range, RenameParams, SemanticTokenModifier, SemanticTokenType, 19 PrepareRenameResponse, Range, RenameParams, SemanticTokens, SemanticTokensParams,
20 SemanticTokens, SemanticTokensParams, SemanticTokensRangeParams, SemanticTokensRangeResult, 20 SemanticTokensRangeParams, SemanticTokensRangeResult, SemanticTokensResult, SymbolInformation,
21 SemanticTokensResult, SymbolInformation, TextDocumentIdentifier, TextEdit, WorkspaceEdit, 21 TextDocumentIdentifier, TextEdit, WorkspaceEdit,
22}; 22};
23use ra_ide::{ 23use ra_ide::{
24 AssistId, FileId, FilePosition, FileRange, Query, RangeInfo, Runnable, RunnableKind, 24 AssistId, FileId, FilePosition, FileRange, Query, RangeInfo, Runnable, RunnableKind,
@@ -954,7 +954,7 @@ fn highlight(world: &WorldSnapshot, file_id: FileId) -> Result<Vec<Decoration>>
954 .into_iter() 954 .into_iter()
955 .map(|h| Decoration { 955 .map(|h| Decoration {
956 range: h.range.conv_with(&line_index), 956 range: h.range.conv_with(&line_index),
957 tag: h.tag.to_string(), 957 tag: h.highlight.to_string(),
958 binding_hash: h.binding_hash.map(|x| x.to_string()), 958 binding_hash: h.binding_hash.map(|x| x.to_string()),
959 }) 959 })
960 .collect(); 960 .collect();
@@ -1082,10 +1082,9 @@ pub fn handle_semantic_tokens(
1082 1082
1083 let mut builder = SemanticTokensBuilder::default(); 1083 let mut builder = SemanticTokensBuilder::default();
1084 1084
1085 for h in world.analysis().highlight(file_id)?.into_iter() { 1085 for highlight_range in world.analysis().highlight(file_id)?.into_iter() {
1086 let type_and_modifiers: (SemanticTokenType, Vec<SemanticTokenModifier>) = h.tag.conv(); 1086 let (token_type, token_modifiers) = highlight_range.highlight.conv();
1087 let (token_type, token_modifiers) = type_and_modifiers.conv(); 1087 builder.push(highlight_range.range.conv_with(&line_index), token_type, token_modifiers);
1088 builder.push(h.range.conv_with(&line_index), token_type, token_modifiers);
1089 } 1088 }
1090 1089
1091 let tokens = SemanticTokens { data: builder.build(), ..Default::default() }; 1090 let tokens = SemanticTokens { data: builder.build(), ..Default::default() };
@@ -1104,10 +1103,9 @@ pub fn handle_semantic_tokens_range(
1104 1103
1105 let mut builder = SemanticTokensBuilder::default(); 1104 let mut builder = SemanticTokensBuilder::default();
1106 1105
1107 for h in world.analysis().highlight_range(frange)?.into_iter() { 1106 for highlight_range in world.analysis().highlight_range(frange)?.into_iter() {
1108 let type_and_modifiers: (SemanticTokenType, Vec<SemanticTokenModifier>) = h.tag.conv(); 1107 let (token_type, token_modifiers) = highlight_range.highlight.conv();
1109 let (token_type, token_modifiers) = type_and_modifiers.conv(); 1108 builder.push(highlight_range.range.conv_with(&line_index), token_type, token_modifiers);
1110 builder.push(h.range.conv_with(&line_index), token_type, token_modifiers);
1111 } 1109 }
1112 1110
1113 let tokens = SemanticTokens { data: builder.build(), ..Default::default() }; 1111 let tokens = SemanticTokens { data: builder.build(), ..Default::default() };
diff --git a/crates/rust-analyzer/src/semantic_tokens.rs b/crates/rust-analyzer/src/semantic_tokens.rs
index e6a8eb146..bf21dc68e 100644
--- a/crates/rust-analyzer/src/semantic_tokens.rs
+++ b/crates/rust-analyzer/src/semantic_tokens.rs
@@ -1,8 +1,17 @@
1//! Semantic Tokens helpers 1//! Semantic Tokens helpers
2 2
3use std::ops;
4
3use lsp_types::{Range, SemanticToken, SemanticTokenModifier, SemanticTokenType}; 5use lsp_types::{Range, SemanticToken, SemanticTokenModifier, SemanticTokenType};
4 6
5const SUPPORTED_TYPES: &[SemanticTokenType] = &[ 7pub(crate) const ATTRIBUTE: SemanticTokenType = SemanticTokenType::new("attribute");
8
9pub(crate) const MUTABLE: SemanticTokenModifier = SemanticTokenModifier::new("mutable");
10pub(crate) const UNSAFE: SemanticTokenModifier = SemanticTokenModifier::new("unsafe");
11pub(crate) const CONTROL: SemanticTokenModifier = SemanticTokenModifier::new("control");
12pub(crate) const BUILTIN: SemanticTokenModifier = SemanticTokenModifier::new("builtin");
13
14pub(crate) const SUPPORTED_TYPES: &[SemanticTokenType] = &[
6 SemanticTokenType::COMMENT, 15 SemanticTokenType::COMMENT,
7 SemanticTokenType::KEYWORD, 16 SemanticTokenType::KEYWORD,
8 SemanticTokenType::STRING, 17 SemanticTokenType::STRING,
@@ -23,9 +32,10 @@ const SUPPORTED_TYPES: &[SemanticTokenType] = &[
23 SemanticTokenType::VARIABLE, 32 SemanticTokenType::VARIABLE,
24 SemanticTokenType::PARAMETER, 33 SemanticTokenType::PARAMETER,
25 SemanticTokenType::LABEL, 34 SemanticTokenType::LABEL,
35 ATTRIBUTE,
26]; 36];
27 37
28const SUPPORTED_MODIFIERS: &[SemanticTokenModifier] = &[ 38pub(crate) const SUPPORTED_MODIFIERS: &[SemanticTokenModifier] = &[
29 SemanticTokenModifier::DOCUMENTATION, 39 SemanticTokenModifier::DOCUMENTATION,
30 SemanticTokenModifier::DECLARATION, 40 SemanticTokenModifier::DECLARATION,
31 SemanticTokenModifier::DEFINITION, 41 SemanticTokenModifier::DEFINITION,
@@ -36,16 +46,20 @@ const SUPPORTED_MODIFIERS: &[SemanticTokenModifier] = &[
36 SemanticTokenModifier::ASYNC, 46 SemanticTokenModifier::ASYNC,
37 SemanticTokenModifier::VOLATILE, 47 SemanticTokenModifier::VOLATILE,
38 SemanticTokenModifier::READONLY, 48 SemanticTokenModifier::READONLY,
49 MUTABLE,
50 UNSAFE,
51 CONTROL,
52 BUILTIN,
39]; 53];
40 54
41/// Token types that the server supports 55#[derive(Default)]
42pub(crate) fn supported_token_types() -> &'static [SemanticTokenType] { 56pub(crate) struct ModifierSet(pub(crate) u32);
43 SUPPORTED_TYPES
44}
45 57
46/// Token modifiers that the server supports 58impl ops::BitOrAssign<SemanticTokenModifier> for ModifierSet {
47pub(crate) fn supported_token_modifiers() -> &'static [SemanticTokenModifier] { 59 fn bitor_assign(&mut self, rhs: SemanticTokenModifier) {
48 SUPPORTED_MODIFIERS 60 let idx = SUPPORTED_MODIFIERS.iter().position(|it| it == &rhs).unwrap();
61 self.0 |= 1 << idx;
62 }
49} 63}
50 64
51/// Tokens are encoded relative to each other. 65/// Tokens are encoded relative to each other.
@@ -92,3 +106,7 @@ impl SemanticTokensBuilder {
92 self.data 106 self.data
93 } 107 }
94} 108}
109
110pub fn type_index(type_: SemanticTokenType) -> u32 {
111 SUPPORTED_TYPES.iter().position(|it| *it == type_).unwrap() as u32
112}
diff --git a/editors/code/package.json b/editors/code/package.json
index c5f9d50ac..3fbf3f0bb 100644
--- a/editors/code/package.json
+++ b/editors/code/package.json
@@ -380,6 +380,25 @@
380 } 380 }
381 } 381 }
382 ], 382 ],
383 "semanticTokenTypes": [
384 {
385 "id": "attribute"
386 }
387 ],
388 "semanticTokenModifiers": [
389 {
390 "id": "mutable"
391 },
392 {
393 "id": "unsafe"
394 },
395 {
396 "id": "control"
397 },
398 {
399 "id": "builtin"
400 }
401 ],
383 "semanticTokenStyleDefaults": [ 402 "semanticTokenStyleDefaults": [
384 { 403 {
385 "selector": "*.mutable", 404 "selector": "*.mutable",