From 50ecb1e19bc555aa627224d41a5ca243e44296f4 Mon Sep 17 00:00:00 2001 From: Omer Ben-Amram Date: Sun, 15 Dec 2019 12:33:14 +0200 Subject: introduce named constants for highlighting tag names. --- crates/ra_ide/src/syntax_highlighting.rs | 104 ++++++++++++++++++++----------- 1 file 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::{ 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)] pub struct HighlightedRange { pub range: TextRange, @@ -71,9 +97,9 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec "comment", - STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => "string", - ATTR => "attribute", + COMMENT => HIGHLIGHT_TAG_LITERAL_COMMENT, + STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => HIGHLIGHT_TAG_LITERAL_STRING, + ATTR => HIGHLIGHT_TAG_LITERAL_ATTRIBUTE, NAME_REF => { if node.ancestors().any(|it| it.kind() == ATTR) { continue; @@ -90,7 +116,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec { 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 highlight_name(db, name_kind), - None => name.syntax().parent().map_or("function", |x| match x.kind() { - STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => "type", - TYPE_PARAM => "type.param", - RECORD_FIELD_DEF => "field", - _ => "function", - }), + None => { + name.syntax().parent().map_or(HIGHLIGHT_TAG_FUNCTION, |x| match x.kind() { + STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => { + HIGHLIGHT_TAG_TYPE + } + TYPE_PARAM => HIGHLIGHT_TAG_TYPE_PARAM, + RECORD_FIELD_DEF => HIGHLIGHT_TAG_FIELD, + _ => HIGHLIGHT_TAG_FUNCTION, + }) + } } } - INT_NUMBER | FLOAT_NUMBER => "literal.numeric", - BYTE => "literal.byte", - CHAR => "literal.char", - LIFETIME => "type.lifetime", - T![unsafe] => "keyword.unsafe", - k if is_control_keyword(k) => "keyword.control", - k if k.is_keyword() => "keyword", + INT_NUMBER | FLOAT_NUMBER => HIGHLIGHT_TAG_LITERAL_NUMERIC, + BYTE => HIGHLIGHT_TAG_LITERAL_BYTE, + CHAR => HIGHLIGHT_TAG_LITERAL_CHAR, + LIFETIME => HIGHLIGHT_TAG_TYPE_LIFETIME, + T![unsafe] => HIGHLIGHT_TAG_KEYWORD_UNSAFE, + k if is_control_keyword(k) => HIGHLIGHT_TAG_KEYWORD_CONTROL, + 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(path) = macro_call.path() { @@ -138,7 +168,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec &'static str { match name_kind { - Macro(_) => "macro", - Field(_) => "field", - AssocItem(hir::AssocItem::Function(_)) => "function", - AssocItem(hir::AssocItem::Const(_)) => "constant", - AssocItem(hir::AssocItem::TypeAlias(_)) => "type", - Def(hir::ModuleDef::Module(_)) => "module", - Def(hir::ModuleDef::Function(_)) => "function", - Def(hir::ModuleDef::Adt(_)) => "type", - Def(hir::ModuleDef::EnumVariant(_)) => "constant", - Def(hir::ModuleDef::Const(_)) => "constant", - Def(hir::ModuleDef::Static(_)) => "constant", - Def(hir::ModuleDef::Trait(_)) => "type", - Def(hir::ModuleDef::TypeAlias(_)) => "type", - Def(hir::ModuleDef::BuiltinType(_)) => "type.builtin", - SelfType(_) => "type.self", - TypeParam(_) => "type.param", + Macro(_) => HIGHLIGHT_TAG_MACRO, + Field(_) => HIGHLIGHT_TAG_FIELD, + AssocItem(hir::AssocItem::Function(_)) => HIGHLIGHT_TAG_FUNCTION, + AssocItem(hir::AssocItem::Const(_)) => HIGHLIGHT_TAG_CONSTANT, + AssocItem(hir::AssocItem::TypeAlias(_)) => HIGHLIGHT_TAG_TYPE, + Def(hir::ModuleDef::Module(_)) => HIGHLIGHT_TAG_MODULE, + Def(hir::ModuleDef::Function(_)) => HIGHLIGHT_TAG_FUNCTION, + Def(hir::ModuleDef::Adt(_)) => HIGHLIGHT_TAG_TYPE, + Def(hir::ModuleDef::EnumVariant(_)) => HIGHLIGHT_TAG_CONSTANT, + Def(hir::ModuleDef::Const(_)) => HIGHLIGHT_TAG_CONSTANT, + Def(hir::ModuleDef::Static(_)) => HIGHLIGHT_TAG_CONSTANT, + Def(hir::ModuleDef::Trait(_)) => HIGHLIGHT_TAG_TYPE, + Def(hir::ModuleDef::TypeAlias(_)) => HIGHLIGHT_TAG_TYPE, + Def(hir::ModuleDef::BuiltinType(_)) => HIGHLIGHT_TAG_TYPE_BUILTIN, + SelfType(_) => HIGHLIGHT_TAG_TYPE_SELF, + TypeParam(_) => HIGHLIGHT_TAG_TYPE_PARAM, Local(local) => { if local.is_mut(db) { - "variable.mut" + HIGHLIGHT_TAG_VARIABLE_MUT } else if local.ty(db).is_mutable_reference() { - "variable.mut" + HIGHLIGHT_TAG_VARIABLE_MUT } else { - "variable" + HIGHLIGHT_TAG_VARIABLE } } } -- cgit v1.2.3 From 9a6d496497df35c0dbd8fa40574d47d0463997dd Mon Sep 17 00:00:00 2001 From: Omer Ben-Amram Date: Sun, 15 Dec 2019 13:18:37 +0200 Subject: use a module instead of prefixed consts. --- crates/ra_ide/src/syntax_highlighting.rs | 130 +++++++++++++++---------------- 1 file 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::{ 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"; +pub mod tags { + pub(crate) const FIELD: &'static str = "field"; + pub(crate) const FUNCTION: &'static str = "function"; + pub(crate) const MODULE: &'static str = "module"; + pub(crate) const TYPE: &'static str = "type"; + pub(crate) const CONSTANT: &'static str = "constant"; + pub(crate) const MACRO: &'static str = "macro"; + pub(crate) const VARIABLE: &'static str = "variable"; + pub(crate) const VARIABLE_MUT: &'static str = "variable.mut"; + pub(crate) const TEXT: &'static str = "text"; + + pub(crate) const TYPE_BUILTIN: &'static str = "type.builtin"; + pub(crate) const TYPE_SELF: &'static str = "type.self"; + pub(crate) const TYPE_PARAM: &'static str = "type.param"; + pub(crate) const TYPE_LIFETIME: &'static str = "type.lifetime"; + + pub(crate) const LITERAL_BYTE: &'static str = "literal.byte"; + pub(crate) const LITERAL_NUMERIC: &'static str = "literal.numeric"; + pub(crate) const LITERAL_CHAR: &'static str = "literal.char"; + pub(crate) const LITERAL_COMMENT: &'static str = "comment"; + pub(crate) const LITERAL_STRING: &'static str = "string"; + pub(crate) const LITERAL_ATTRIBUTE: &'static str = "attribute"; + + pub(crate) const KEYWORD_UNSAFE: &'static str = "keyword.unsafe"; + pub(crate) const KEYWORD_CONTROL: &'static str = "keyword.control"; + pub(crate) const KEYWORD: &'static str = "keyword"; +} #[derive(Debug)] pub struct HighlightedRange { @@ -97,9 +99,9 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec HIGHLIGHT_TAG_LITERAL_COMMENT, - STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => HIGHLIGHT_TAG_LITERAL_STRING, - ATTR => HIGHLIGHT_TAG_LITERAL_ATTRIBUTE, + COMMENT => tags::LITERAL_COMMENT, + STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => tags::LITERAL_STRING, + ATTR => tags::LITERAL_ATTRIBUTE, NAME_REF => { if node.ancestors().any(|it| it.kind() == ATTR) { continue; @@ -116,7 +118,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec { 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 highlight_name(db, name_kind), - None => { - name.syntax().parent().map_or(HIGHLIGHT_TAG_FUNCTION, |x| match x.kind() { - STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => { - HIGHLIGHT_TAG_TYPE - } - TYPE_PARAM => HIGHLIGHT_TAG_TYPE_PARAM, - RECORD_FIELD_DEF => HIGHLIGHT_TAG_FIELD, - _ => HIGHLIGHT_TAG_FUNCTION, - }) - } + None => name.syntax().parent().map_or(tags::FUNCTION, |x| match x.kind() { + STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => tags::TYPE, + TYPE_PARAM => tags::TYPE_PARAM, + RECORD_FIELD_DEF => tags::FIELD, + _ => tags::FUNCTION, + }), } } - INT_NUMBER | FLOAT_NUMBER => HIGHLIGHT_TAG_LITERAL_NUMERIC, - BYTE => HIGHLIGHT_TAG_LITERAL_BYTE, - CHAR => HIGHLIGHT_TAG_LITERAL_CHAR, - LIFETIME => HIGHLIGHT_TAG_TYPE_LIFETIME, - T![unsafe] => HIGHLIGHT_TAG_KEYWORD_UNSAFE, - k if is_control_keyword(k) => HIGHLIGHT_TAG_KEYWORD_CONTROL, - k if k.is_keyword() => HIGHLIGHT_TAG_KEYWORD, + INT_NUMBER | FLOAT_NUMBER => tags::LITERAL_NUMERIC, + BYTE => tags::LITERAL_BYTE, + CHAR => tags::LITERAL_CHAR, + LIFETIME => tags::TYPE_LIFETIME, + T![unsafe] => tags::KEYWORD_UNSAFE, + k if is_control_keyword(k) => tags::KEYWORD_CONTROL, + k if k.is_keyword() => tags::KEYWORD, _ => { if let Some(macro_call) = node.as_node().cloned().and_then(ast::MacroCall::cast) { if let Some(path) = macro_call.path() { @@ -168,7 +166,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec &'static str { match name_kind { - Macro(_) => HIGHLIGHT_TAG_MACRO, - Field(_) => HIGHLIGHT_TAG_FIELD, - AssocItem(hir::AssocItem::Function(_)) => HIGHLIGHT_TAG_FUNCTION, - AssocItem(hir::AssocItem::Const(_)) => HIGHLIGHT_TAG_CONSTANT, - AssocItem(hir::AssocItem::TypeAlias(_)) => HIGHLIGHT_TAG_TYPE, - Def(hir::ModuleDef::Module(_)) => HIGHLIGHT_TAG_MODULE, - Def(hir::ModuleDef::Function(_)) => HIGHLIGHT_TAG_FUNCTION, - Def(hir::ModuleDef::Adt(_)) => HIGHLIGHT_TAG_TYPE, - Def(hir::ModuleDef::EnumVariant(_)) => HIGHLIGHT_TAG_CONSTANT, - Def(hir::ModuleDef::Const(_)) => HIGHLIGHT_TAG_CONSTANT, - Def(hir::ModuleDef::Static(_)) => HIGHLIGHT_TAG_CONSTANT, - Def(hir::ModuleDef::Trait(_)) => HIGHLIGHT_TAG_TYPE, - Def(hir::ModuleDef::TypeAlias(_)) => HIGHLIGHT_TAG_TYPE, - Def(hir::ModuleDef::BuiltinType(_)) => HIGHLIGHT_TAG_TYPE_BUILTIN, - SelfType(_) => HIGHLIGHT_TAG_TYPE_SELF, - TypeParam(_) => HIGHLIGHT_TAG_TYPE_PARAM, + Macro(_) => tags::MACRO, + Field(_) => tags::FIELD, + AssocItem(hir::AssocItem::Function(_)) => tags::FUNCTION, + AssocItem(hir::AssocItem::Const(_)) => tags::CONSTANT, + AssocItem(hir::AssocItem::TypeAlias(_)) => tags::TYPE, + Def(hir::ModuleDef::Module(_)) => tags::MODULE, + Def(hir::ModuleDef::Function(_)) => tags::FUNCTION, + Def(hir::ModuleDef::Adt(_)) => tags::TYPE, + Def(hir::ModuleDef::EnumVariant(_)) => tags::CONSTANT, + Def(hir::ModuleDef::Const(_)) => tags::CONSTANT, + Def(hir::ModuleDef::Static(_)) => tags::CONSTANT, + Def(hir::ModuleDef::Trait(_)) => tags::TYPE, + Def(hir::ModuleDef::TypeAlias(_)) => tags::TYPE, + Def(hir::ModuleDef::BuiltinType(_)) => tags::TYPE_BUILTIN, + SelfType(_) => tags::TYPE_SELF, + TypeParam(_) => tags::TYPE_PARAM, Local(local) => { if local.is_mut(db) { - HIGHLIGHT_TAG_VARIABLE_MUT + tags::VARIABLE_MUT } else if local.ty(db).is_mutable_reference() { - HIGHLIGHT_TAG_VARIABLE_MUT + tags::VARIABLE_MUT } else { - HIGHLIGHT_TAG_VARIABLE + tags::VARIABLE } } } -- cgit v1.2.3