aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-12-15 11:20:50 +0000
committerGitHub <[email protected]>2019-12-15 11:20:50 +0000
commit6cbd8a4a4bbca8a7656df9f3ef849acebbf9ef9b (patch)
tree74ed89cf2ef03766a15c0ff6ba521ef1b509666c /crates
parent3e8f9eb6c45b6bde294c79deea0e5bb02c084ae0 (diff)
parent9a6d496497df35c0dbd8fa40574d47d0463997dd (diff)
Merge #2564
2564: Introduce named constants for highlighting tag names. r=matklad a=omerbenamram Refers to #2563 . This is just a refactor of all the tag strings to named constants as suggested by @matklad. An enum could _probably_ prevent some future inconsistencies (since strings are still accepted), but I think the constants here are just fine - since the frontend only cares about strings anyways. The frontend doesn't know about about those constants, so we'll still need to be mindful for them there. Note: I didn't touch the `STYLE` const (big css blob), we could probably make it a `format!` string using something like `once_cell::Lazy`, let me know if this is something that needs fixing (since it doesn't seem like a useful API outside of tests). Also - I left those consts private, I assume if they were some kind of API we would have made it into an enum? Co-authored-by: Omer Ben-Amram <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_ide/src/syntax_highlighting.rs100
1 files changed, 64 insertions, 36 deletions
diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs
index 235e09ffc..eb3dd1779 100644
--- a/crates/ra_ide/src/syntax_highlighting.rs
+++ b/crates/ra_ide/src/syntax_highlighting.rs
@@ -16,6 +16,34 @@ use crate::{
16 FileId, 16 FileId,
17}; 17};
18 18
19pub mod tags {
20 pub(crate) const FIELD: &'static str = "field";
21 pub(crate) const FUNCTION: &'static str = "function";
22 pub(crate) const MODULE: &'static str = "module";
23 pub(crate) const TYPE: &'static str = "type";
24 pub(crate) const CONSTANT: &'static str = "constant";
25 pub(crate) const MACRO: &'static str = "macro";
26 pub(crate) const VARIABLE: &'static str = "variable";
27 pub(crate) const VARIABLE_MUT: &'static str = "variable.mut";
28 pub(crate) const TEXT: &'static str = "text";
29
30 pub(crate) const TYPE_BUILTIN: &'static str = "type.builtin";
31 pub(crate) const TYPE_SELF: &'static str = "type.self";
32 pub(crate) const TYPE_PARAM: &'static str = "type.param";
33 pub(crate) const TYPE_LIFETIME: &'static str = "type.lifetime";
34
35 pub(crate) const LITERAL_BYTE: &'static str = "literal.byte";
36 pub(crate) const LITERAL_NUMERIC: &'static str = "literal.numeric";
37 pub(crate) const LITERAL_CHAR: &'static str = "literal.char";
38 pub(crate) const LITERAL_COMMENT: &'static str = "comment";
39 pub(crate) const LITERAL_STRING: &'static str = "string";
40 pub(crate) const LITERAL_ATTRIBUTE: &'static str = "attribute";
41
42 pub(crate) const KEYWORD_UNSAFE: &'static str = "keyword.unsafe";
43 pub(crate) const KEYWORD_CONTROL: &'static str = "keyword.control";
44 pub(crate) const KEYWORD: &'static str = "keyword";
45}
46
19#[derive(Debug)] 47#[derive(Debug)]
20pub struct HighlightedRange { 48pub struct HighlightedRange {
21 pub range: TextRange, 49 pub range: TextRange,
@@ -71,9 +99,9 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
71 bindings_shadow_count.clear(); 99 bindings_shadow_count.clear();
72 continue; 100 continue;
73 } 101 }
74 COMMENT => "comment", 102 COMMENT => tags::LITERAL_COMMENT,
75 STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => "string", 103 STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => tags::LITERAL_STRING,
76 ATTR => "attribute", 104 ATTR => tags::LITERAL_ATTRIBUTE,
77 NAME_REF => { 105 NAME_REF => {
78 if node.ancestors().any(|it| it.kind() == ATTR) { 106 if node.ancestors().any(|it| it.kind() == ATTR) {
79 continue; 107 continue;
@@ -90,7 +118,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
90 } 118 }
91 }; 119 };
92 120
93 name_kind.map_or("text", |it| highlight_name(db, it)) 121 name_kind.map_or(tags::TEXT, |it| highlight_name(db, it))
94 } 122 }
95 NAME => { 123 NAME => {
96 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();
@@ -107,21 +135,21 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
107 135
108 match name_kind { 136 match name_kind {
109 Some(name_kind) => highlight_name(db, name_kind), 137 Some(name_kind) => highlight_name(db, name_kind),
110 None => name.syntax().parent().map_or("function", |x| match x.kind() { 138 None => name.syntax().parent().map_or(tags::FUNCTION, |x| match x.kind() {
111 STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => "type", 139 STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => tags::TYPE,
112 TYPE_PARAM => "type.param", 140 TYPE_PARAM => tags::TYPE_PARAM,
113 RECORD_FIELD_DEF => "field", 141 RECORD_FIELD_DEF => tags::FIELD,
114 _ => "function", 142 _ => tags::FUNCTION,
115 }), 143 }),
116 } 144 }
117 } 145 }
118 INT_NUMBER | FLOAT_NUMBER => "literal.numeric", 146 INT_NUMBER | FLOAT_NUMBER => tags::LITERAL_NUMERIC,
119 BYTE => "literal.byte", 147 BYTE => tags::LITERAL_BYTE,
120 CHAR => "literal.char", 148 CHAR => tags::LITERAL_CHAR,
121 LIFETIME => "type.lifetime", 149 LIFETIME => tags::TYPE_LIFETIME,
122 T![unsafe] => "keyword.unsafe", 150 T![unsafe] => tags::KEYWORD_UNSAFE,
123 k if is_control_keyword(k) => "keyword.control", 151 k if is_control_keyword(k) => tags::KEYWORD_CONTROL,
124 k if k.is_keyword() => "keyword", 152 k if k.is_keyword() => tags::KEYWORD,
125 _ => { 153 _ => {
126 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) {
127 if let Some(path) = macro_call.path() { 155 if let Some(path) = macro_call.path() {
@@ -138,7 +166,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
138 } 166 }
139 res.push(HighlightedRange { 167 res.push(HighlightedRange {
140 range: TextRange::from_to(range_start, range_end), 168 range: TextRange::from_to(range_start, range_end),
141 tag: "macro", 169 tag: tags::MACRO,
142 binding_hash: None, 170 binding_hash: None,
143 }) 171 })
144 } 172 }
@@ -214,29 +242,29 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
214 242
215fn highlight_name(db: &RootDatabase, name_kind: NameKind) -> &'static str { 243fn highlight_name(db: &RootDatabase, name_kind: NameKind) -> &'static str {
216 match name_kind { 244 match name_kind {
217 Macro(_) => "macro", 245 Macro(_) => tags::MACRO,
218 Field(_) => "field", 246 Field(_) => tags::FIELD,
219 AssocItem(hir::AssocItem::Function(_)) => "function", 247 AssocItem(hir::AssocItem::Function(_)) => tags::FUNCTION,
220 AssocItem(hir::AssocItem::Const(_)) => "constant", 248 AssocItem(hir::AssocItem::Const(_)) => tags::CONSTANT,
221 AssocItem(hir::AssocItem::TypeAlias(_)) => "type", 249 AssocItem(hir::AssocItem::TypeAlias(_)) => tags::TYPE,
222 Def(hir::ModuleDef::Module(_)) => "module", 250 Def(hir::ModuleDef::Module(_)) => tags::MODULE,
223 Def(hir::ModuleDef::Function(_)) => "function", 251 Def(hir::ModuleDef::Function(_)) => tags::FUNCTION,
224 Def(hir::ModuleDef::Adt(_)) => "type", 252 Def(hir::ModuleDef::Adt(_)) => tags::TYPE,
225 Def(hir::ModuleDef::EnumVariant(_)) => "constant", 253 Def(hir::ModuleDef::EnumVariant(_)) => tags::CONSTANT,
226 Def(hir::ModuleDef::Const(_)) => "constant", 254 Def(hir::ModuleDef::Const(_)) => tags::CONSTANT,
227 Def(hir::ModuleDef::Static(_)) => "constant", 255 Def(hir::ModuleDef::Static(_)) => tags::CONSTANT,
228 Def(hir::ModuleDef::Trait(_)) => "type", 256 Def(hir::ModuleDef::Trait(_)) => tags::TYPE,
229 Def(hir::ModuleDef::TypeAlias(_)) => "type", 257 Def(hir::ModuleDef::TypeAlias(_)) => tags::TYPE,
230 Def(hir::ModuleDef::BuiltinType(_)) => "type.builtin", 258 Def(hir::ModuleDef::BuiltinType(_)) => tags::TYPE_BUILTIN,
231 SelfType(_) => "type.self", 259 SelfType(_) => tags::TYPE_SELF,
232 TypeParam(_) => "type.param", 260 TypeParam(_) => tags::TYPE_PARAM,
233 Local(local) => { 261 Local(local) => {
234 if local.is_mut(db) { 262 if local.is_mut(db) {
235 "variable.mut" 263 tags::VARIABLE_MUT
236 } else if local.ty(db).is_mutable_reference() { 264 } else if local.ty(db).is_mutable_reference() {
237 "variable.mut" 265 tags::VARIABLE_MUT
238 } else { 266 } else {
239 "variable" 267 tags::VARIABLE
240 } 268 }
241 } 269 }
242 } 270 }