diff options
author | Omer Ben-Amram <[email protected]> | 2019-12-15 10:33:14 +0000 |
---|---|---|
committer | Omer Ben-Amram <[email protected]> | 2019-12-15 10:39:31 +0000 |
commit | 50ecb1e19bc555aa627224d41a5ca243e44296f4 (patch) | |
tree | 1e93f0960bcaad49ea1325bd1d42e851380ca613 /crates/ra_ide/src | |
parent | 3e8f9eb6c45b6bde294c79deea0e5bb02c084ae0 (diff) |
introduce named constants for highlighting tag names.
Diffstat (limited to 'crates/ra_ide/src')
-rw-r--r-- | crates/ra_ide/src/syntax_highlighting.rs | 104 |
1 files changed, 67 insertions, 37 deletions
diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 235e09ffc..d0cefea0f 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs | |||
@@ -16,6 +16,32 @@ use crate::{ | |||
16 | FileId, | 16 | FileId, |
17 | }; | 17 | }; |
18 | 18 | ||
19 | const HIGHLIGHT_TAG_FIELD: &'static str = "field"; | ||
20 | const HIGHLIGHT_TAG_FUNCTION: &'static str = "function"; | ||
21 | const HIGHLIGHT_TAG_MODULE: &'static str = "module"; | ||
22 | const HIGHLIGHT_TAG_TYPE: &'static str = "type"; | ||
23 | const HIGHLIGHT_TAG_CONSTANT: &'static str = "constant"; | ||
24 | const HIGHLIGHT_TAG_MACRO: &'static str = "macro"; | ||
25 | const HIGHLIGHT_TAG_VARIABLE: &'static str = "variable"; | ||
26 | const HIGHLIGHT_TAG_VARIABLE_MUT: &'static str = "variable.mut"; | ||
27 | const HIGHLIGHT_TAG_TEXT: &'static str = "text"; | ||
28 | |||
29 | const HIGHLIGHT_TAG_TYPE_BUILTIN: &'static str = "type.builtin"; | ||
30 | const HIGHLIGHT_TAG_TYPE_SELF: &'static str = "type.self"; | ||
31 | const HIGHLIGHT_TAG_TYPE_PARAM: &'static str = "type.param"; | ||
32 | const HIGHLIGHT_TAG_TYPE_LIFETIME: &'static str = "type.lifetime"; | ||
33 | |||
34 | const HIGHLIGHT_TAG_LITERAL_BYTE: &'static str = "literal.byte"; | ||
35 | const HIGHLIGHT_TAG_LITERAL_NUMERIC: &'static str = "literal.numeric"; | ||
36 | const HIGHLIGHT_TAG_LITERAL_CHAR: &'static str = "literal.char"; | ||
37 | const HIGHLIGHT_TAG_LITERAL_COMMENT: &'static str = "comment"; | ||
38 | const HIGHLIGHT_TAG_LITERAL_STRING: &'static str = "string"; | ||
39 | const HIGHLIGHT_TAG_LITERAL_ATTRIBUTE: &'static str = "attribute"; | ||
40 | |||
41 | const HIGHLIGHT_TAG_KEYWORD_UNSAFE: &'static str = "keyword.unsafe"; | ||
42 | const HIGHLIGHT_TAG_KEYWORD_CONTROL: &'static str = "keyword.control"; | ||
43 | const HIGHLIGHT_TAG_KEYWORD: &'static str = "keyword"; | ||
44 | |||
19 | #[derive(Debug)] | 45 | #[derive(Debug)] |
20 | pub struct HighlightedRange { | 46 | pub struct HighlightedRange { |
21 | pub range: TextRange, | 47 | pub range: TextRange, |
@@ -71,9 +97,9 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa | |||
71 | bindings_shadow_count.clear(); | 97 | bindings_shadow_count.clear(); |
72 | continue; | 98 | continue; |
73 | } | 99 | } |
74 | COMMENT => "comment", | 100 | COMMENT => HIGHLIGHT_TAG_LITERAL_COMMENT, |
75 | STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => "string", | 101 | STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => HIGHLIGHT_TAG_LITERAL_STRING, |
76 | ATTR => "attribute", | 102 | ATTR => HIGHLIGHT_TAG_LITERAL_ATTRIBUTE, |
77 | NAME_REF => { | 103 | NAME_REF => { |
78 | if node.ancestors().any(|it| it.kind() == ATTR) { | 104 | if node.ancestors().any(|it| it.kind() == ATTR) { |
79 | continue; | 105 | continue; |
@@ -90,7 +116,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa | |||
90 | } | 116 | } |
91 | }; | 117 | }; |
92 | 118 | ||
93 | name_kind.map_or("text", |it| highlight_name(db, it)) | 119 | name_kind.map_or(HIGHLIGHT_TAG_TEXT, |it| highlight_name(db, it)) |
94 | } | 120 | } |
95 | NAME => { | 121 | NAME => { |
96 | let name = node.as_node().cloned().and_then(ast::Name::cast).unwrap(); | 122 | let name = node.as_node().cloned().and_then(ast::Name::cast).unwrap(); |
@@ -107,21 +133,25 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa | |||
107 | 133 | ||
108 | match name_kind { | 134 | match name_kind { |
109 | Some(name_kind) => highlight_name(db, name_kind), | 135 | Some(name_kind) => highlight_name(db, name_kind), |
110 | None => name.syntax().parent().map_or("function", |x| match x.kind() { | 136 | None => { |
111 | STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => "type", | 137 | name.syntax().parent().map_or(HIGHLIGHT_TAG_FUNCTION, |x| match x.kind() { |
112 | TYPE_PARAM => "type.param", | 138 | STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => { |
113 | RECORD_FIELD_DEF => "field", | 139 | HIGHLIGHT_TAG_TYPE |
114 | _ => "function", | 140 | } |
115 | }), | 141 | TYPE_PARAM => HIGHLIGHT_TAG_TYPE_PARAM, |
142 | RECORD_FIELD_DEF => HIGHLIGHT_TAG_FIELD, | ||
143 | _ => HIGHLIGHT_TAG_FUNCTION, | ||
144 | }) | ||
145 | } | ||
116 | } | 146 | } |
117 | } | 147 | } |
118 | INT_NUMBER | FLOAT_NUMBER => "literal.numeric", | 148 | INT_NUMBER | FLOAT_NUMBER => HIGHLIGHT_TAG_LITERAL_NUMERIC, |
119 | BYTE => "literal.byte", | 149 | BYTE => HIGHLIGHT_TAG_LITERAL_BYTE, |
120 | CHAR => "literal.char", | 150 | CHAR => HIGHLIGHT_TAG_LITERAL_CHAR, |
121 | LIFETIME => "type.lifetime", | 151 | LIFETIME => HIGHLIGHT_TAG_TYPE_LIFETIME, |
122 | T![unsafe] => "keyword.unsafe", | 152 | T![unsafe] => HIGHLIGHT_TAG_KEYWORD_UNSAFE, |
123 | k if is_control_keyword(k) => "keyword.control", | 153 | k if is_control_keyword(k) => HIGHLIGHT_TAG_KEYWORD_CONTROL, |
124 | k if k.is_keyword() => "keyword", | 154 | k if k.is_keyword() => HIGHLIGHT_TAG_KEYWORD, |
125 | _ => { | 155 | _ => { |
126 | if let Some(macro_call) = node.as_node().cloned().and_then(ast::MacroCall::cast) { | 156 | if let Some(macro_call) = node.as_node().cloned().and_then(ast::MacroCall::cast) { |
127 | if let Some(path) = macro_call.path() { | 157 | if let Some(path) = macro_call.path() { |
@@ -138,7 +168,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa | |||
138 | } | 168 | } |
139 | res.push(HighlightedRange { | 169 | res.push(HighlightedRange { |
140 | range: TextRange::from_to(range_start, range_end), | 170 | range: TextRange::from_to(range_start, range_end), |
141 | tag: "macro", | 171 | tag: HIGHLIGHT_TAG_MACRO, |
142 | binding_hash: None, | 172 | binding_hash: None, |
143 | }) | 173 | }) |
144 | } | 174 | } |
@@ -214,29 +244,29 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo | |||
214 | 244 | ||
215 | fn highlight_name(db: &RootDatabase, name_kind: NameKind) -> &'static str { | 245 | fn highlight_name(db: &RootDatabase, name_kind: NameKind) -> &'static str { |
216 | match name_kind { | 246 | match name_kind { |
217 | Macro(_) => "macro", | 247 | Macro(_) => HIGHLIGHT_TAG_MACRO, |
218 | Field(_) => "field", | 248 | Field(_) => HIGHLIGHT_TAG_FIELD, |
219 | AssocItem(hir::AssocItem::Function(_)) => "function", | 249 | AssocItem(hir::AssocItem::Function(_)) => HIGHLIGHT_TAG_FUNCTION, |
220 | AssocItem(hir::AssocItem::Const(_)) => "constant", | 250 | AssocItem(hir::AssocItem::Const(_)) => HIGHLIGHT_TAG_CONSTANT, |
221 | AssocItem(hir::AssocItem::TypeAlias(_)) => "type", | 251 | AssocItem(hir::AssocItem::TypeAlias(_)) => HIGHLIGHT_TAG_TYPE, |
222 | Def(hir::ModuleDef::Module(_)) => "module", | 252 | Def(hir::ModuleDef::Module(_)) => HIGHLIGHT_TAG_MODULE, |
223 | Def(hir::ModuleDef::Function(_)) => "function", | 253 | Def(hir::ModuleDef::Function(_)) => HIGHLIGHT_TAG_FUNCTION, |
224 | Def(hir::ModuleDef::Adt(_)) => "type", | 254 | Def(hir::ModuleDef::Adt(_)) => HIGHLIGHT_TAG_TYPE, |
225 | Def(hir::ModuleDef::EnumVariant(_)) => "constant", | 255 | Def(hir::ModuleDef::EnumVariant(_)) => HIGHLIGHT_TAG_CONSTANT, |
226 | Def(hir::ModuleDef::Const(_)) => "constant", | 256 | Def(hir::ModuleDef::Const(_)) => HIGHLIGHT_TAG_CONSTANT, |
227 | Def(hir::ModuleDef::Static(_)) => "constant", | 257 | Def(hir::ModuleDef::Static(_)) => HIGHLIGHT_TAG_CONSTANT, |
228 | Def(hir::ModuleDef::Trait(_)) => "type", | 258 | Def(hir::ModuleDef::Trait(_)) => HIGHLIGHT_TAG_TYPE, |
229 | Def(hir::ModuleDef::TypeAlias(_)) => "type", | 259 | Def(hir::ModuleDef::TypeAlias(_)) => HIGHLIGHT_TAG_TYPE, |
230 | Def(hir::ModuleDef::BuiltinType(_)) => "type.builtin", | 260 | Def(hir::ModuleDef::BuiltinType(_)) => HIGHLIGHT_TAG_TYPE_BUILTIN, |
231 | SelfType(_) => "type.self", | 261 | SelfType(_) => HIGHLIGHT_TAG_TYPE_SELF, |
232 | TypeParam(_) => "type.param", | 262 | TypeParam(_) => HIGHLIGHT_TAG_TYPE_PARAM, |
233 | Local(local) => { | 263 | Local(local) => { |
234 | if local.is_mut(db) { | 264 | if local.is_mut(db) { |
235 | "variable.mut" | 265 | HIGHLIGHT_TAG_VARIABLE_MUT |
236 | } else if local.ty(db).is_mutable_reference() { | 266 | } else if local.ty(db).is_mutable_reference() { |
237 | "variable.mut" | 267 | HIGHLIGHT_TAG_VARIABLE_MUT |
238 | } else { | 268 | } else { |
239 | "variable" | 269 | HIGHLIGHT_TAG_VARIABLE |
240 | } | 270 | } |
241 | } | 271 | } |
242 | } | 272 | } |