Skip to content

Commit 6cbd8a4

Browse files
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]>
2 parents 3e8f9eb + 9a6d496 commit 6cbd8a4

File tree

1 file changed

+64
-36
lines changed

1 file changed

+64
-36
lines changed

crates/ra_ide/src/syntax_highlighting.rs

Lines changed: 64 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,34 @@ use crate::{
1616
FileId,
1717
};
1818

19+
pub 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+
1947
#[derive(Debug)]
2048
pub struct HighlightedRange {
2149
pub range: TextRange,
@@ -71,9 +99,9 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
7199
bindings_shadow_count.clear();
72100
continue;
73101
}
74-
COMMENT => "comment",
75-
STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => "string",
76-
ATTR => "attribute",
102+
COMMENT => tags::LITERAL_COMMENT,
103+
STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => tags::LITERAL_STRING,
104+
ATTR => tags::LITERAL_ATTRIBUTE,
77105
NAME_REF => {
78106
if node.ancestors().any(|it| it.kind() == ATTR) {
79107
continue;
@@ -90,7 +118,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
90118
}
91119
};
92120

93-
name_kind.map_or("text", |it| highlight_name(db, it))
121+
name_kind.map_or(tags::TEXT, |it| highlight_name(db, it))
94122
}
95123
NAME => {
96124
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
107135

108136
match name_kind {
109137
Some(name_kind) => highlight_name(db, name_kind),
110-
None => name.syntax().parent().map_or("function", |x| match x.kind() {
111-
STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => "type",
112-
TYPE_PARAM => "type.param",
113-
RECORD_FIELD_DEF => "field",
114-
_ => "function",
138+
None => name.syntax().parent().map_or(tags::FUNCTION, |x| match x.kind() {
139+
STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => tags::TYPE,
140+
TYPE_PARAM => tags::TYPE_PARAM,
141+
RECORD_FIELD_DEF => tags::FIELD,
142+
_ => tags::FUNCTION,
115143
}),
116144
}
117145
}
118-
INT_NUMBER | FLOAT_NUMBER => "literal.numeric",
119-
BYTE => "literal.byte",
120-
CHAR => "literal.char",
121-
LIFETIME => "type.lifetime",
122-
T![unsafe] => "keyword.unsafe",
123-
k if is_control_keyword(k) => "keyword.control",
124-
k if k.is_keyword() => "keyword",
146+
INT_NUMBER | FLOAT_NUMBER => tags::LITERAL_NUMERIC,
147+
BYTE => tags::LITERAL_BYTE,
148+
CHAR => tags::LITERAL_CHAR,
149+
LIFETIME => tags::TYPE_LIFETIME,
150+
T![unsafe] => tags::KEYWORD_UNSAFE,
151+
k if is_control_keyword(k) => tags::KEYWORD_CONTROL,
152+
k if k.is_keyword() => tags::KEYWORD,
125153
_ => {
126154
if let Some(macro_call) = node.as_node().cloned().and_then(ast::MacroCall::cast) {
127155
if let Some(path) = macro_call.path() {
@@ -138,7 +166,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
138166
}
139167
res.push(HighlightedRange {
140168
range: TextRange::from_to(range_start, range_end),
141-
tag: "macro",
169+
tag: tags::MACRO,
142170
binding_hash: None,
143171
})
144172
}
@@ -214,29 +242,29 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
214242

215243
fn highlight_name(db: &RootDatabase, name_kind: NameKind) -> &'static str {
216244
match name_kind {
217-
Macro(_) => "macro",
218-
Field(_) => "field",
219-
AssocItem(hir::AssocItem::Function(_)) => "function",
220-
AssocItem(hir::AssocItem::Const(_)) => "constant",
221-
AssocItem(hir::AssocItem::TypeAlias(_)) => "type",
222-
Def(hir::ModuleDef::Module(_)) => "module",
223-
Def(hir::ModuleDef::Function(_)) => "function",
224-
Def(hir::ModuleDef::Adt(_)) => "type",
225-
Def(hir::ModuleDef::EnumVariant(_)) => "constant",
226-
Def(hir::ModuleDef::Const(_)) => "constant",
227-
Def(hir::ModuleDef::Static(_)) => "constant",
228-
Def(hir::ModuleDef::Trait(_)) => "type",
229-
Def(hir::ModuleDef::TypeAlias(_)) => "type",
230-
Def(hir::ModuleDef::BuiltinType(_)) => "type.builtin",
231-
SelfType(_) => "type.self",
232-
TypeParam(_) => "type.param",
245+
Macro(_) => tags::MACRO,
246+
Field(_) => tags::FIELD,
247+
AssocItem(hir::AssocItem::Function(_)) => tags::FUNCTION,
248+
AssocItem(hir::AssocItem::Const(_)) => tags::CONSTANT,
249+
AssocItem(hir::AssocItem::TypeAlias(_)) => tags::TYPE,
250+
Def(hir::ModuleDef::Module(_)) => tags::MODULE,
251+
Def(hir::ModuleDef::Function(_)) => tags::FUNCTION,
252+
Def(hir::ModuleDef::Adt(_)) => tags::TYPE,
253+
Def(hir::ModuleDef::EnumVariant(_)) => tags::CONSTANT,
254+
Def(hir::ModuleDef::Const(_)) => tags::CONSTANT,
255+
Def(hir::ModuleDef::Static(_)) => tags::CONSTANT,
256+
Def(hir::ModuleDef::Trait(_)) => tags::TYPE,
257+
Def(hir::ModuleDef::TypeAlias(_)) => tags::TYPE,
258+
Def(hir::ModuleDef::BuiltinType(_)) => tags::TYPE_BUILTIN,
259+
SelfType(_) => tags::TYPE_SELF,
260+
TypeParam(_) => tags::TYPE_PARAM,
233261
Local(local) => {
234262
if local.is_mut(db) {
235-
"variable.mut"
263+
tags::VARIABLE_MUT
236264
} else if local.ty(db).is_mutable_reference() {
237-
"variable.mut"
265+
tags::VARIABLE_MUT
238266
} else {
239-
"variable"
267+
tags::VARIABLE
240268
}
241269
}
242270
}

0 commit comments

Comments
 (0)