mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-15 14:43:58 +00:00
introduce named constants for highlighting tag names.
This commit is contained in:
parent
3e8f9eb6c4
commit
50ecb1e19b
1 changed files with 67 additions and 37 deletions
|
@ -16,6 +16,32 @@ use crate::{
|
||||||
FileId,
|
FileId,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const HIGHLIGHT_TAG_FIELD: &'static str = "field";
|
||||||
|
const HIGHLIGHT_TAG_FUNCTION: &'static str = "function";
|
||||||
|
const HIGHLIGHT_TAG_MODULE: &'static str = "module";
|
||||||
|
const HIGHLIGHT_TAG_TYPE: &'static str = "type";
|
||||||
|
const HIGHLIGHT_TAG_CONSTANT: &'static str = "constant";
|
||||||
|
const HIGHLIGHT_TAG_MACRO: &'static str = "macro";
|
||||||
|
const HIGHLIGHT_TAG_VARIABLE: &'static str = "variable";
|
||||||
|
const HIGHLIGHT_TAG_VARIABLE_MUT: &'static str = "variable.mut";
|
||||||
|
const HIGHLIGHT_TAG_TEXT: &'static str = "text";
|
||||||
|
|
||||||
|
const HIGHLIGHT_TAG_TYPE_BUILTIN: &'static str = "type.builtin";
|
||||||
|
const HIGHLIGHT_TAG_TYPE_SELF: &'static str = "type.self";
|
||||||
|
const HIGHLIGHT_TAG_TYPE_PARAM: &'static str = "type.param";
|
||||||
|
const HIGHLIGHT_TAG_TYPE_LIFETIME: &'static str = "type.lifetime";
|
||||||
|
|
||||||
|
const HIGHLIGHT_TAG_LITERAL_BYTE: &'static str = "literal.byte";
|
||||||
|
const HIGHLIGHT_TAG_LITERAL_NUMERIC: &'static str = "literal.numeric";
|
||||||
|
const HIGHLIGHT_TAG_LITERAL_CHAR: &'static str = "literal.char";
|
||||||
|
const HIGHLIGHT_TAG_LITERAL_COMMENT: &'static str = "comment";
|
||||||
|
const HIGHLIGHT_TAG_LITERAL_STRING: &'static str = "string";
|
||||||
|
const HIGHLIGHT_TAG_LITERAL_ATTRIBUTE: &'static str = "attribute";
|
||||||
|
|
||||||
|
const HIGHLIGHT_TAG_KEYWORD_UNSAFE: &'static str = "keyword.unsafe";
|
||||||
|
const HIGHLIGHT_TAG_KEYWORD_CONTROL: &'static str = "keyword.control";
|
||||||
|
const HIGHLIGHT_TAG_KEYWORD: &'static str = "keyword";
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct HighlightedRange {
|
pub struct HighlightedRange {
|
||||||
pub range: TextRange,
|
pub range: TextRange,
|
||||||
|
@ -71,9 +97,9 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
|
||||||
bindings_shadow_count.clear();
|
bindings_shadow_count.clear();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
COMMENT => "comment",
|
COMMENT => HIGHLIGHT_TAG_LITERAL_COMMENT,
|
||||||
STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => "string",
|
STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => HIGHLIGHT_TAG_LITERAL_STRING,
|
||||||
ATTR => "attribute",
|
ATTR => HIGHLIGHT_TAG_LITERAL_ATTRIBUTE,
|
||||||
NAME_REF => {
|
NAME_REF => {
|
||||||
if node.ancestors().any(|it| it.kind() == ATTR) {
|
if node.ancestors().any(|it| it.kind() == ATTR) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -90,7 +116,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
name_kind.map_or("text", |it| highlight_name(db, it))
|
name_kind.map_or(HIGHLIGHT_TAG_TEXT, |it| highlight_name(db, it))
|
||||||
}
|
}
|
||||||
NAME => {
|
NAME => {
|
||||||
let name = node.as_node().cloned().and_then(ast::Name::cast).unwrap();
|
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
|
||||||
|
|
||||||
match name_kind {
|
match name_kind {
|
||||||
Some(name_kind) => highlight_name(db, name_kind),
|
Some(name_kind) => highlight_name(db, name_kind),
|
||||||
None => name.syntax().parent().map_or("function", |x| match x.kind() {
|
None => {
|
||||||
STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => "type",
|
name.syntax().parent().map_or(HIGHLIGHT_TAG_FUNCTION, |x| match x.kind() {
|
||||||
TYPE_PARAM => "type.param",
|
STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => {
|
||||||
RECORD_FIELD_DEF => "field",
|
HIGHLIGHT_TAG_TYPE
|
||||||
_ => "function",
|
}
|
||||||
}),
|
TYPE_PARAM => HIGHLIGHT_TAG_TYPE_PARAM,
|
||||||
|
RECORD_FIELD_DEF => HIGHLIGHT_TAG_FIELD,
|
||||||
|
_ => HIGHLIGHT_TAG_FUNCTION,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
INT_NUMBER | FLOAT_NUMBER => "literal.numeric",
|
INT_NUMBER | FLOAT_NUMBER => HIGHLIGHT_TAG_LITERAL_NUMERIC,
|
||||||
BYTE => "literal.byte",
|
BYTE => HIGHLIGHT_TAG_LITERAL_BYTE,
|
||||||
CHAR => "literal.char",
|
CHAR => HIGHLIGHT_TAG_LITERAL_CHAR,
|
||||||
LIFETIME => "type.lifetime",
|
LIFETIME => HIGHLIGHT_TAG_TYPE_LIFETIME,
|
||||||
T![unsafe] => "keyword.unsafe",
|
T![unsafe] => HIGHLIGHT_TAG_KEYWORD_UNSAFE,
|
||||||
k if is_control_keyword(k) => "keyword.control",
|
k if is_control_keyword(k) => HIGHLIGHT_TAG_KEYWORD_CONTROL,
|
||||||
k if k.is_keyword() => "keyword",
|
k if k.is_keyword() => HIGHLIGHT_TAG_KEYWORD,
|
||||||
_ => {
|
_ => {
|
||||||
if let Some(macro_call) = node.as_node().cloned().and_then(ast::MacroCall::cast) {
|
if let Some(macro_call) = node.as_node().cloned().and_then(ast::MacroCall::cast) {
|
||||||
if let Some(path) = macro_call.path() {
|
if let Some(path) = macro_call.path() {
|
||||||
|
@ -138,7 +168,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
|
||||||
}
|
}
|
||||||
res.push(HighlightedRange {
|
res.push(HighlightedRange {
|
||||||
range: TextRange::from_to(range_start, range_end),
|
range: TextRange::from_to(range_start, range_end),
|
||||||
tag: "macro",
|
tag: HIGHLIGHT_TAG_MACRO,
|
||||||
binding_hash: None,
|
binding_hash: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -214,29 +244,29 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
|
||||||
|
|
||||||
fn highlight_name(db: &RootDatabase, name_kind: NameKind) -> &'static str {
|
fn highlight_name(db: &RootDatabase, name_kind: NameKind) -> &'static str {
|
||||||
match name_kind {
|
match name_kind {
|
||||||
Macro(_) => "macro",
|
Macro(_) => HIGHLIGHT_TAG_MACRO,
|
||||||
Field(_) => "field",
|
Field(_) => HIGHLIGHT_TAG_FIELD,
|
||||||
AssocItem(hir::AssocItem::Function(_)) => "function",
|
AssocItem(hir::AssocItem::Function(_)) => HIGHLIGHT_TAG_FUNCTION,
|
||||||
AssocItem(hir::AssocItem::Const(_)) => "constant",
|
AssocItem(hir::AssocItem::Const(_)) => HIGHLIGHT_TAG_CONSTANT,
|
||||||
AssocItem(hir::AssocItem::TypeAlias(_)) => "type",
|
AssocItem(hir::AssocItem::TypeAlias(_)) => HIGHLIGHT_TAG_TYPE,
|
||||||
Def(hir::ModuleDef::Module(_)) => "module",
|
Def(hir::ModuleDef::Module(_)) => HIGHLIGHT_TAG_MODULE,
|
||||||
Def(hir::ModuleDef::Function(_)) => "function",
|
Def(hir::ModuleDef::Function(_)) => HIGHLIGHT_TAG_FUNCTION,
|
||||||
Def(hir::ModuleDef::Adt(_)) => "type",
|
Def(hir::ModuleDef::Adt(_)) => HIGHLIGHT_TAG_TYPE,
|
||||||
Def(hir::ModuleDef::EnumVariant(_)) => "constant",
|
Def(hir::ModuleDef::EnumVariant(_)) => HIGHLIGHT_TAG_CONSTANT,
|
||||||
Def(hir::ModuleDef::Const(_)) => "constant",
|
Def(hir::ModuleDef::Const(_)) => HIGHLIGHT_TAG_CONSTANT,
|
||||||
Def(hir::ModuleDef::Static(_)) => "constant",
|
Def(hir::ModuleDef::Static(_)) => HIGHLIGHT_TAG_CONSTANT,
|
||||||
Def(hir::ModuleDef::Trait(_)) => "type",
|
Def(hir::ModuleDef::Trait(_)) => HIGHLIGHT_TAG_TYPE,
|
||||||
Def(hir::ModuleDef::TypeAlias(_)) => "type",
|
Def(hir::ModuleDef::TypeAlias(_)) => HIGHLIGHT_TAG_TYPE,
|
||||||
Def(hir::ModuleDef::BuiltinType(_)) => "type.builtin",
|
Def(hir::ModuleDef::BuiltinType(_)) => HIGHLIGHT_TAG_TYPE_BUILTIN,
|
||||||
SelfType(_) => "type.self",
|
SelfType(_) => HIGHLIGHT_TAG_TYPE_SELF,
|
||||||
TypeParam(_) => "type.param",
|
TypeParam(_) => HIGHLIGHT_TAG_TYPE_PARAM,
|
||||||
Local(local) => {
|
Local(local) => {
|
||||||
if local.is_mut(db) {
|
if local.is_mut(db) {
|
||||||
"variable.mut"
|
HIGHLIGHT_TAG_VARIABLE_MUT
|
||||||
} else if local.ty(db).is_mutable_reference() {
|
} else if local.ty(db).is_mutable_reference() {
|
||||||
"variable.mut"
|
HIGHLIGHT_TAG_VARIABLE_MUT
|
||||||
} else {
|
} else {
|
||||||
"variable"
|
HIGHLIGHT_TAG_VARIABLE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue