diff options
Diffstat (limited to 'crates/ra_ide/src')
-rw-r--r-- | crates/ra_ide/src/syntax_highlighting.rs | 130 |
1 files changed, 64 insertions, 66 deletions
diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index d0cefea0f..eb3dd1779 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs | |||
@@ -16,31 +16,33 @@ use crate::{ | |||
16 | FileId, | 16 | FileId, |
17 | }; | 17 | }; |
18 | 18 | ||
19 | const HIGHLIGHT_TAG_FIELD: &'static str = "field"; | 19 | pub mod tags { |
20 | const HIGHLIGHT_TAG_FUNCTION: &'static str = "function"; | 20 | pub(crate) const FIELD: &'static str = "field"; |
21 | const HIGHLIGHT_TAG_MODULE: &'static str = "module"; | 21 | pub(crate) const FUNCTION: &'static str = "function"; |
22 | const HIGHLIGHT_TAG_TYPE: &'static str = "type"; | 22 | pub(crate) const MODULE: &'static str = "module"; |
23 | const HIGHLIGHT_TAG_CONSTANT: &'static str = "constant"; | 23 | pub(crate) const TYPE: &'static str = "type"; |
24 | const HIGHLIGHT_TAG_MACRO: &'static str = "macro"; | 24 | pub(crate) const CONSTANT: &'static str = "constant"; |
25 | const HIGHLIGHT_TAG_VARIABLE: &'static str = "variable"; | 25 | pub(crate) const MACRO: &'static str = "macro"; |
26 | const HIGHLIGHT_TAG_VARIABLE_MUT: &'static str = "variable.mut"; | 26 | pub(crate) const VARIABLE: &'static str = "variable"; |
27 | const HIGHLIGHT_TAG_TEXT: &'static str = "text"; | 27 | pub(crate) const VARIABLE_MUT: &'static str = "variable.mut"; |
28 | 28 | pub(crate) const TEXT: &'static str = "text"; | |
29 | const HIGHLIGHT_TAG_TYPE_BUILTIN: &'static str = "type.builtin"; | 29 | |
30 | const HIGHLIGHT_TAG_TYPE_SELF: &'static str = "type.self"; | 30 | pub(crate) const TYPE_BUILTIN: &'static str = "type.builtin"; |
31 | const HIGHLIGHT_TAG_TYPE_PARAM: &'static str = "type.param"; | 31 | pub(crate) const TYPE_SELF: &'static str = "type.self"; |
32 | const HIGHLIGHT_TAG_TYPE_LIFETIME: &'static str = "type.lifetime"; | 32 | pub(crate) const TYPE_PARAM: &'static str = "type.param"; |
33 | 33 | pub(crate) const TYPE_LIFETIME: &'static str = "type.lifetime"; | |
34 | const HIGHLIGHT_TAG_LITERAL_BYTE: &'static str = "literal.byte"; | 34 | |
35 | const HIGHLIGHT_TAG_LITERAL_NUMERIC: &'static str = "literal.numeric"; | 35 | pub(crate) const LITERAL_BYTE: &'static str = "literal.byte"; |
36 | const HIGHLIGHT_TAG_LITERAL_CHAR: &'static str = "literal.char"; | 36 | pub(crate) const LITERAL_NUMERIC: &'static str = "literal.numeric"; |
37 | const HIGHLIGHT_TAG_LITERAL_COMMENT: &'static str = "comment"; | 37 | pub(crate) const LITERAL_CHAR: &'static str = "literal.char"; |
38 | const HIGHLIGHT_TAG_LITERAL_STRING: &'static str = "string"; | 38 | pub(crate) const LITERAL_COMMENT: &'static str = "comment"; |
39 | const HIGHLIGHT_TAG_LITERAL_ATTRIBUTE: &'static str = "attribute"; | 39 | pub(crate) const LITERAL_STRING: &'static str = "string"; |
40 | 40 | pub(crate) const LITERAL_ATTRIBUTE: &'static str = "attribute"; | |
41 | const HIGHLIGHT_TAG_KEYWORD_UNSAFE: &'static str = "keyword.unsafe"; | 41 | |
42 | const HIGHLIGHT_TAG_KEYWORD_CONTROL: &'static str = "keyword.control"; | 42 | pub(crate) const KEYWORD_UNSAFE: &'static str = "keyword.unsafe"; |
43 | const HIGHLIGHT_TAG_KEYWORD: &'static str = "keyword"; | 43 | pub(crate) const KEYWORD_CONTROL: &'static str = "keyword.control"; |
44 | pub(crate) const KEYWORD: &'static str = "keyword"; | ||
45 | } | ||
44 | 46 | ||
45 | #[derive(Debug)] | 47 | #[derive(Debug)] |
46 | pub struct HighlightedRange { | 48 | pub struct HighlightedRange { |
@@ -97,9 +99,9 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa | |||
97 | bindings_shadow_count.clear(); | 99 | bindings_shadow_count.clear(); |
98 | continue; | 100 | continue; |
99 | } | 101 | } |
100 | COMMENT => HIGHLIGHT_TAG_LITERAL_COMMENT, | 102 | COMMENT => tags::LITERAL_COMMENT, |
101 | STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => HIGHLIGHT_TAG_LITERAL_STRING, | 103 | STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => tags::LITERAL_STRING, |
102 | ATTR => HIGHLIGHT_TAG_LITERAL_ATTRIBUTE, | 104 | ATTR => tags::LITERAL_ATTRIBUTE, |
103 | NAME_REF => { | 105 | NAME_REF => { |
104 | if node.ancestors().any(|it| it.kind() == ATTR) { | 106 | if node.ancestors().any(|it| it.kind() == ATTR) { |
105 | continue; | 107 | continue; |
@@ -116,7 +118,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa | |||
116 | } | 118 | } |
117 | }; | 119 | }; |
118 | 120 | ||
119 | name_kind.map_or(HIGHLIGHT_TAG_TEXT, |it| highlight_name(db, it)) | 121 | name_kind.map_or(tags::TEXT, |it| highlight_name(db, it)) |
120 | } | 122 | } |
121 | NAME => { | 123 | NAME => { |
122 | let name = node.as_node().cloned().and_then(ast::Name::cast).unwrap(); | 124 | let name = node.as_node().cloned().and_then(ast::Name::cast).unwrap(); |
@@ -133,25 +135,21 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa | |||
133 | 135 | ||
134 | match name_kind { | 136 | match name_kind { |
135 | Some(name_kind) => highlight_name(db, name_kind), | 137 | Some(name_kind) => highlight_name(db, name_kind), |
136 | None => { | 138 | None => name.syntax().parent().map_or(tags::FUNCTION, |x| match x.kind() { |
137 | name.syntax().parent().map_or(HIGHLIGHT_TAG_FUNCTION, |x| match x.kind() { | 139 | STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => tags::TYPE, |
138 | STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => { | 140 | TYPE_PARAM => tags::TYPE_PARAM, |
139 | HIGHLIGHT_TAG_TYPE | 141 | RECORD_FIELD_DEF => tags::FIELD, |
140 | } | 142 | _ => tags::FUNCTION, |
141 | TYPE_PARAM => HIGHLIGHT_TAG_TYPE_PARAM, | 143 | }), |
142 | RECORD_FIELD_DEF => HIGHLIGHT_TAG_FIELD, | ||
143 | _ => HIGHLIGHT_TAG_FUNCTION, | ||
144 | }) | ||
145 | } | ||
146 | } | 144 | } |
147 | } | 145 | } |
148 | INT_NUMBER | FLOAT_NUMBER => HIGHLIGHT_TAG_LITERAL_NUMERIC, | 146 | INT_NUMBER | FLOAT_NUMBER => tags::LITERAL_NUMERIC, |
149 | BYTE => HIGHLIGHT_TAG_LITERAL_BYTE, | 147 | BYTE => tags::LITERAL_BYTE, |
150 | CHAR => HIGHLIGHT_TAG_LITERAL_CHAR, | 148 | CHAR => tags::LITERAL_CHAR, |
151 | LIFETIME => HIGHLIGHT_TAG_TYPE_LIFETIME, | 149 | LIFETIME => tags::TYPE_LIFETIME, |
152 | T![unsafe] => HIGHLIGHT_TAG_KEYWORD_UNSAFE, | 150 | T![unsafe] => tags::KEYWORD_UNSAFE, |
153 | k if is_control_keyword(k) => HIGHLIGHT_TAG_KEYWORD_CONTROL, | 151 | k if is_control_keyword(k) => tags::KEYWORD_CONTROL, |
154 | k if k.is_keyword() => HIGHLIGHT_TAG_KEYWORD, | 152 | k if k.is_keyword() => tags::KEYWORD, |
155 | _ => { | 153 | _ => { |
156 | if let Some(macro_call) = node.as_node().cloned().and_then(ast::MacroCall::cast) { | 154 | if let Some(macro_call) = node.as_node().cloned().and_then(ast::MacroCall::cast) { |
157 | if let Some(path) = macro_call.path() { | 155 | if let Some(path) = macro_call.path() { |
@@ -168,7 +166,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa | |||
168 | } | 166 | } |
169 | res.push(HighlightedRange { | 167 | res.push(HighlightedRange { |
170 | range: TextRange::from_to(range_start, range_end), | 168 | range: TextRange::from_to(range_start, range_end), |
171 | tag: HIGHLIGHT_TAG_MACRO, | 169 | tag: tags::MACRO, |
172 | binding_hash: None, | 170 | binding_hash: None, |
173 | }) | 171 | }) |
174 | } | 172 | } |
@@ -244,29 +242,29 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo | |||
244 | 242 | ||
245 | fn highlight_name(db: &RootDatabase, name_kind: NameKind) -> &'static str { | 243 | fn highlight_name(db: &RootDatabase, name_kind: NameKind) -> &'static str { |
246 | match name_kind { | 244 | match name_kind { |
247 | Macro(_) => HIGHLIGHT_TAG_MACRO, | 245 | Macro(_) => tags::MACRO, |
248 | Field(_) => HIGHLIGHT_TAG_FIELD, | 246 | Field(_) => tags::FIELD, |
249 | AssocItem(hir::AssocItem::Function(_)) => HIGHLIGHT_TAG_FUNCTION, | 247 | AssocItem(hir::AssocItem::Function(_)) => tags::FUNCTION, |
250 | AssocItem(hir::AssocItem::Const(_)) => HIGHLIGHT_TAG_CONSTANT, | 248 | AssocItem(hir::AssocItem::Const(_)) => tags::CONSTANT, |
251 | AssocItem(hir::AssocItem::TypeAlias(_)) => HIGHLIGHT_TAG_TYPE, | 249 | AssocItem(hir::AssocItem::TypeAlias(_)) => tags::TYPE, |
252 | Def(hir::ModuleDef::Module(_)) => HIGHLIGHT_TAG_MODULE, | 250 | Def(hir::ModuleDef::Module(_)) => tags::MODULE, |
253 | Def(hir::ModuleDef::Function(_)) => HIGHLIGHT_TAG_FUNCTION, | 251 | Def(hir::ModuleDef::Function(_)) => tags::FUNCTION, |
254 | Def(hir::ModuleDef::Adt(_)) => HIGHLIGHT_TAG_TYPE, | 252 | Def(hir::ModuleDef::Adt(_)) => tags::TYPE, |
255 | Def(hir::ModuleDef::EnumVariant(_)) => HIGHLIGHT_TAG_CONSTANT, | 253 | Def(hir::ModuleDef::EnumVariant(_)) => tags::CONSTANT, |
256 | Def(hir::ModuleDef::Const(_)) => HIGHLIGHT_TAG_CONSTANT, | 254 | Def(hir::ModuleDef::Const(_)) => tags::CONSTANT, |
257 | Def(hir::ModuleDef::Static(_)) => HIGHLIGHT_TAG_CONSTANT, | 255 | Def(hir::ModuleDef::Static(_)) => tags::CONSTANT, |
258 | Def(hir::ModuleDef::Trait(_)) => HIGHLIGHT_TAG_TYPE, | 256 | Def(hir::ModuleDef::Trait(_)) => tags::TYPE, |
259 | Def(hir::ModuleDef::TypeAlias(_)) => HIGHLIGHT_TAG_TYPE, | 257 | Def(hir::ModuleDef::TypeAlias(_)) => tags::TYPE, |
260 | Def(hir::ModuleDef::BuiltinType(_)) => HIGHLIGHT_TAG_TYPE_BUILTIN, | 258 | Def(hir::ModuleDef::BuiltinType(_)) => tags::TYPE_BUILTIN, |
261 | SelfType(_) => HIGHLIGHT_TAG_TYPE_SELF, | 259 | SelfType(_) => tags::TYPE_SELF, |
262 | TypeParam(_) => HIGHLIGHT_TAG_TYPE_PARAM, | 260 | TypeParam(_) => tags::TYPE_PARAM, |
263 | Local(local) => { | 261 | Local(local) => { |
264 | if local.is_mut(db) { | 262 | if local.is_mut(db) { |
265 | HIGHLIGHT_TAG_VARIABLE_MUT | 263 | tags::VARIABLE_MUT |
266 | } else if local.ty(db).is_mutable_reference() { | 264 | } else if local.ty(db).is_mutable_reference() { |
267 | HIGHLIGHT_TAG_VARIABLE_MUT | 265 | tags::VARIABLE_MUT |
268 | } else { | 266 | } else { |
269 | HIGHLIGHT_TAG_VARIABLE | 267 | tags::VARIABLE |
270 | } | 268 | } |
271 | } | 269 | } |
272 | } | 270 | } |