Cleanup highlighting tags

This commit is contained in:
Aleksey Kladov 2020-02-28 12:06:54 +01:00
parent 996e18846d
commit 701cf43606
5 changed files with 170 additions and 103 deletions

View file

@ -177,10 +177,11 @@ fn highlight_element(
} }
}; };
match name_kind { let h = match name_kind {
Some(name_kind) => highlight_name(db, name_kind), Some(name_kind) => highlight_name(db, name_kind),
None => highlight_name_by_syntax(name), None => highlight_name_by_syntax(name),
} };
h | HighlightModifier::Definition
} }
// Highlight references like the definitions they resolve to // Highlight references like the definitions they resolve to
@ -206,12 +207,13 @@ fn highlight_element(
// Simple token-based highlighting // Simple token-based highlighting
COMMENT => HighlightTag::Comment.into(), COMMENT => HighlightTag::Comment.into(),
STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => HighlightTag::LiteralString.into(), STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => HighlightTag::StringLiteral.into(),
ATTR => HighlightTag::Attribute.into(), ATTR => HighlightTag::Attribute.into(),
INT_NUMBER | FLOAT_NUMBER => HighlightTag::LiteralNumeric.into(), INT_NUMBER | FLOAT_NUMBER => HighlightTag::NumericLiteral.into(),
BYTE => HighlightTag::LiteralByte.into(), BYTE => HighlightTag::ByteLiteral.into(),
CHAR => HighlightTag::LiteralChar.into(), CHAR => HighlightTag::CharLiteral.into(),
LIFETIME => HighlightTag::TypeLifetime.into(), // FIXME: set Declaration for decls
LIFETIME => HighlightTag::Lifetime.into(),
k if k.is_keyword() => { k if k.is_keyword() => {
let h = Highlight::new(HighlightTag::Keyword); let h = Highlight::new(HighlightTag::Keyword);
@ -258,17 +260,18 @@ fn highlight_name(db: &RootDatabase, def: NameDefinition) -> Highlight {
hir::ModuleDef::Adt(hir::Adt::Struct(_)) => HighlightTag::Struct, hir::ModuleDef::Adt(hir::Adt::Struct(_)) => HighlightTag::Struct,
hir::ModuleDef::Adt(hir::Adt::Enum(_)) => HighlightTag::Enum, hir::ModuleDef::Adt(hir::Adt::Enum(_)) => HighlightTag::Enum,
hir::ModuleDef::Adt(hir::Adt::Union(_)) => HighlightTag::Union, hir::ModuleDef::Adt(hir::Adt::Union(_)) => HighlightTag::Union,
hir::ModuleDef::EnumVariant(_) => HighlightTag::Constant, hir::ModuleDef::EnumVariant(_) => HighlightTag::EnumVariant,
hir::ModuleDef::Const(_) => HighlightTag::Constant, hir::ModuleDef::Const(_) => HighlightTag::Constant,
hir::ModuleDef::Static(_) => HighlightTag::Constant, hir::ModuleDef::Static(_) => HighlightTag::Static,
hir::ModuleDef::Trait(_) => HighlightTag::Trait, hir::ModuleDef::Trait(_) => HighlightTag::Trait,
hir::ModuleDef::TypeAlias(_) => HighlightTag::TypeAlias, hir::ModuleDef::TypeAlias(_) => HighlightTag::TypeAlias,
hir::ModuleDef::BuiltinType(_) => HighlightTag::BuiltinType, hir::ModuleDef::BuiltinType(_) => HighlightTag::BuiltinType,
}, },
NameDefinition::SelfType(_) => HighlightTag::TypeSelf, NameDefinition::SelfType(_) => HighlightTag::SelfType,
NameDefinition::TypeParam(_) => HighlightTag::TypeParam, NameDefinition::TypeParam(_) => HighlightTag::TypeParam,
// FIXME: distinguish between locals and parameters
NameDefinition::Local(local) => { NameDefinition::Local(local) => {
let mut h = Highlight::new(HighlightTag::Variable); let mut h = Highlight::new(HighlightTag::Local);
if local.is_mut(db) || local.ty(db).is_mutable_reference() { if local.is_mut(db) || local.ty(db).is_mutable_reference() {
h |= HighlightModifier::Mutable; h |= HighlightModifier::Mutable;
} }
@ -289,6 +292,7 @@ fn highlight_name_by_syntax(name: ast::Name) -> Highlight {
match parent.kind() { match parent.kind() {
STRUCT_DEF => HighlightTag::Struct.into(), STRUCT_DEF => HighlightTag::Struct.into(),
ENUM_DEF => HighlightTag::Enum.into(), ENUM_DEF => HighlightTag::Enum.into(),
UNION_KW => HighlightTag::Union.into(),
TRAIT_DEF => HighlightTag::Trait.into(), TRAIT_DEF => HighlightTag::Trait.into(),
TYPE_ALIAS_DEF => HighlightTag::TypeAlias.into(), TYPE_ALIAS_DEF => HighlightTag::TypeAlias.into(),
TYPE_PARAM => HighlightTag::TypeParam.into(), TYPE_PARAM => HighlightTag::TypeParam.into(),
@ -315,7 +319,7 @@ fn highlight_injection(
if let Some(range) = literal.open_quote_text_range() { if let Some(range) = literal.open_quote_text_range() {
acc.push(HighlightedRange { acc.push(HighlightedRange {
range, range,
highlight: HighlightTag::LiteralString.into(), highlight: HighlightTag::StringLiteral.into(),
binding_hash: None, binding_hash: None,
}) })
} }
@ -330,7 +334,7 @@ fn highlight_injection(
if let Some(range) = literal.close_quote_text_range() { if let Some(range) = literal.close_quote_text_range() {
acc.push(HighlightedRange { acc.push(HighlightedRange {
range, range,
highlight: HighlightTag::LiteralString.into(), highlight: HighlightTag::StringLiteral.into(),
binding_hash: None, binding_hash: None,
}) })
} }

View file

@ -14,70 +14,71 @@ pub struct HighlightModifiers(u32);
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum HighlightTag { pub enum HighlightTag {
Struct, Attribute,
Enum,
Union,
Trait,
TypeAlias,
BuiltinType, BuiltinType,
ByteLiteral,
CharLiteral,
Comment,
Constant,
Enum,
EnumVariant,
Field, Field,
Function, Function,
Module,
Constant,
Macro,
Variable,
TypeSelf,
TypeParam,
TypeLifetime,
LiteralByte,
LiteralNumeric,
LiteralChar,
Comment,
LiteralString,
Attribute,
Keyword, Keyword,
Lifetime,
Macro,
Module,
NumericLiteral,
SelfType,
Static,
StringLiteral,
Struct,
Trait,
TypeAlias,
TypeParam,
Union,
Local,
} }
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(u8)] #[repr(u8)]
pub enum HighlightModifier { pub enum HighlightModifier {
Mutable = 0,
Unsafe,
/// Used with keywords like `if` and `break`. /// Used with keywords like `if` and `break`.
Control, Control = 0,
/// `foo` in `fn foo(x: i32)` is a definition, `foo` in `foo(90 + 2)` is
/// not.
Definition,
Mutable,
Unsafe,
} }
impl HighlightTag { impl HighlightTag {
fn as_str(self) -> &'static str { fn as_str(self) -> &'static str {
match self { match self {
HighlightTag::Struct => "struct", HighlightTag::Attribute => "attribute",
HighlightTag::Enum => "enum",
HighlightTag::Union => "union",
HighlightTag::Trait => "trait",
HighlightTag::TypeAlias => "type_alias",
HighlightTag::BuiltinType => "builtin_type", HighlightTag::BuiltinType => "builtin_type",
HighlightTag::ByteLiteral => "byte_literal",
HighlightTag::CharLiteral => "char_literal",
HighlightTag::Comment => "comment",
HighlightTag::Constant => "constant",
HighlightTag::Enum => "enum",
HighlightTag::EnumVariant => "enum_variant",
HighlightTag::Field => "field", HighlightTag::Field => "field",
HighlightTag::Function => "function", HighlightTag::Function => "function",
HighlightTag::Module => "module",
HighlightTag::Constant => "constant",
HighlightTag::Macro => "macro",
HighlightTag::Variable => "variable",
HighlightTag::TypeSelf => "type.self",
HighlightTag::TypeParam => "type.param",
HighlightTag::TypeLifetime => "type.lifetime",
HighlightTag::LiteralByte => "literal.byte",
HighlightTag::LiteralNumeric => "literal.numeric",
HighlightTag::LiteralChar => "literal.char",
HighlightTag::Comment => "comment",
HighlightTag::LiteralString => "string",
HighlightTag::Attribute => "attribute",
HighlightTag::Keyword => "keyword", HighlightTag::Keyword => "keyword",
HighlightTag::Lifetime => "lifetime",
HighlightTag::Macro => "macro",
HighlightTag::Module => "module",
HighlightTag::NumericLiteral => "numeric_literal",
HighlightTag::SelfType => "self_type",
HighlightTag::Static => "static",
HighlightTag::StringLiteral => "string",
HighlightTag::Struct => "struct",
HighlightTag::Trait => "trait",
HighlightTag::TypeAlias => "type_alias",
HighlightTag::TypeParam => "type_param",
HighlightTag::Union => "union",
HighlightTag::Local => "variable",
} }
} }
} }
@ -94,9 +95,10 @@ impl HighlightModifier {
fn as_str(self) -> &'static str { fn as_str(self) -> &'static str {
match self { match self {
HighlightModifier::Control => "control",
HighlightModifier::Definition => "declaration",
HighlightModifier::Mutable => "mutable", HighlightModifier::Mutable => "mutable",
HighlightModifier::Unsafe => "unsafe", HighlightModifier::Unsafe => "unsafe",
HighlightModifier::Control => "control",
} }
} }

View file

@ -20,11 +20,11 @@ use ra_vfs::LineEndings;
use crate::{ use crate::{
req, req,
semantic_tokens::{self, ModifierSet, BUILTIN, CONSTANT, CONTROL, MUTABLE, UNSAFE}, semantic_tokens::{self, ModifierSet, CONSTANT, CONTROL, MUTABLE, UNSAFE},
world::WorldSnapshot, world::WorldSnapshot,
Result, Result,
}; };
use semantic_tokens::ATTRIBUTE; use semantic_tokens::{ATTRIBUTE, BUILTIN_TYPE, ENUM_MEMBER, LIFETIME, TYPE_ALIAS, UNION};
pub trait Conv { pub trait Conv {
type Output; type Output;
@ -316,45 +316,43 @@ impl Conv for Highlight {
fn conv(self) -> Self::Output { fn conv(self) -> Self::Output {
let mut mods = ModifierSet::default(); let mut mods = ModifierSet::default();
let type_ = match self.tag { let type_ = match self.tag {
HighlightTag::Struct HighlightTag::Struct => SemanticTokenType::STRUCT,
| HighlightTag::Enum HighlightTag::Enum => SemanticTokenType::ENUM,
| HighlightTag::Union HighlightTag::Union => UNION,
| HighlightTag::TypeAlias HighlightTag::TypeAlias => TYPE_ALIAS,
| HighlightTag::Trait HighlightTag::Trait => SemanticTokenType::INTERFACE,
| HighlightTag::BuiltinType => SemanticTokenType::TYPE, HighlightTag::BuiltinType => BUILTIN_TYPE,
HighlightTag::SelfType => SemanticTokenType::TYPE,
HighlightTag::Field => SemanticTokenType::MEMBER, HighlightTag::Field => SemanticTokenType::MEMBER,
HighlightTag::Function => SemanticTokenType::FUNCTION, HighlightTag::Function => SemanticTokenType::FUNCTION,
HighlightTag::Module => SemanticTokenType::NAMESPACE, HighlightTag::Module => SemanticTokenType::NAMESPACE,
HighlightTag::Constant => { HighlightTag::Constant => {
mods |= CONSTANT;
mods |= SemanticTokenModifier::STATIC; mods |= SemanticTokenModifier::STATIC;
mods |= SemanticTokenModifier::READONLY; SemanticTokenType::VARIABLE
CONSTANT
} }
HighlightTag::Static => {
mods |= SemanticTokenModifier::STATIC;
SemanticTokenType::VARIABLE
}
HighlightTag::EnumVariant => ENUM_MEMBER,
HighlightTag::Macro => SemanticTokenType::MACRO, HighlightTag::Macro => SemanticTokenType::MACRO,
HighlightTag::Variable => SemanticTokenType::VARIABLE, HighlightTag::Local => SemanticTokenType::VARIABLE,
HighlightTag::TypeSelf => {
mods |= SemanticTokenModifier::REFERENCE;
SemanticTokenType::TYPE
}
HighlightTag::TypeParam => SemanticTokenType::TYPE_PARAMETER, HighlightTag::TypeParam => SemanticTokenType::TYPE_PARAMETER,
HighlightTag::TypeLifetime => { HighlightTag::Lifetime => LIFETIME,
mods |= SemanticTokenModifier::REFERENCE; HighlightTag::ByteLiteral | HighlightTag::NumericLiteral => SemanticTokenType::NUMBER,
SemanticTokenType::LABEL HighlightTag::CharLiteral | HighlightTag::StringLiteral => SemanticTokenType::STRING,
}
HighlightTag::LiteralByte => SemanticTokenType::NUMBER,
HighlightTag::LiteralNumeric => SemanticTokenType::NUMBER,
HighlightTag::LiteralChar => SemanticTokenType::NUMBER,
HighlightTag::Comment => SemanticTokenType::COMMENT, HighlightTag::Comment => SemanticTokenType::COMMENT,
HighlightTag::LiteralString => SemanticTokenType::STRING,
HighlightTag::Attribute => ATTRIBUTE, HighlightTag::Attribute => ATTRIBUTE,
HighlightTag::Keyword => SemanticTokenType::KEYWORD, HighlightTag::Keyword => SemanticTokenType::KEYWORD,
}; };
for modifier in self.modifiers.iter() { for modifier in self.modifiers.iter() {
let modifier = match modifier { let modifier = match modifier {
HighlightModifier::Definition => SemanticTokenModifier::DECLARATION,
HighlightModifier::Control => CONTROL,
HighlightModifier::Mutable => MUTABLE, HighlightModifier::Mutable => MUTABLE,
HighlightModifier::Unsafe => UNSAFE, HighlightModifier::Unsafe => UNSAFE,
HighlightModifier::Control => CONTROL,
}; };
mods |= modifier; mods |= modifier;
} }

View file

@ -5,11 +5,16 @@ use std::ops;
use lsp_types::{Range, SemanticToken, SemanticTokenModifier, SemanticTokenType}; use lsp_types::{Range, SemanticToken, SemanticTokenModifier, SemanticTokenType};
pub(crate) const ATTRIBUTE: SemanticTokenType = SemanticTokenType::new("attribute"); pub(crate) const ATTRIBUTE: SemanticTokenType = SemanticTokenType::new("attribute");
pub(crate) const CONSTANT: SemanticTokenType = SemanticTokenType::new("constant"); pub(crate) const BUILTIN_TYPE: SemanticTokenType = SemanticTokenType::new("builtinType");
pub(crate) const ENUM_MEMBER: SemanticTokenType = SemanticTokenType::new("enumMember");
pub(crate) const LIFETIME: SemanticTokenType = SemanticTokenType::new("lifetime");
pub(crate) const TYPE_ALIAS: SemanticTokenType = SemanticTokenType::new("typeAlias");
pub(crate) const UNION: SemanticTokenType = SemanticTokenType::new("union");
pub(crate) const CONSTANT: SemanticTokenModifier = SemanticTokenModifier::new("constant");
pub(crate) const CONTROL: SemanticTokenModifier = SemanticTokenModifier::new("control");
pub(crate) const MUTABLE: SemanticTokenModifier = SemanticTokenModifier::new("mutable"); pub(crate) const MUTABLE: SemanticTokenModifier = SemanticTokenModifier::new("mutable");
pub(crate) const UNSAFE: SemanticTokenModifier = SemanticTokenModifier::new("unsafe"); pub(crate) const UNSAFE: SemanticTokenModifier = SemanticTokenModifier::new("unsafe");
pub(crate) const CONTROL: SemanticTokenModifier = SemanticTokenModifier::new("control");
pub(crate) const SUPPORTED_TYPES: &[SemanticTokenType] = &[ pub(crate) const SUPPORTED_TYPES: &[SemanticTokenType] = &[
SemanticTokenType::COMMENT, SemanticTokenType::COMMENT,
@ -33,7 +38,11 @@ pub(crate) const SUPPORTED_TYPES: &[SemanticTokenType] = &[
SemanticTokenType::PARAMETER, SemanticTokenType::PARAMETER,
SemanticTokenType::LABEL, SemanticTokenType::LABEL,
ATTRIBUTE, ATTRIBUTE,
CONSTANT, BUILTIN_TYPE,
ENUM_MEMBER,
LIFETIME,
TYPE_ALIAS,
UNION,
]; ];
pub(crate) const SUPPORTED_MODIFIERS: &[SemanticTokenModifier] = &[ pub(crate) const SUPPORTED_MODIFIERS: &[SemanticTokenModifier] = &[
@ -47,6 +56,7 @@ pub(crate) const SUPPORTED_MODIFIERS: &[SemanticTokenModifier] = &[
SemanticTokenModifier::ASYNC, SemanticTokenModifier::ASYNC,
SemanticTokenModifier::VOLATILE, SemanticTokenModifier::VOLATILE,
SemanticTokenModifier::READONLY, SemanticTokenModifier::READONLY,
CONSTANT,
MUTABLE, MUTABLE,
UNSAFE, UNSAFE,
CONTROL, CONTROL,

View file

@ -383,21 +383,42 @@
], ],
"semanticTokenTypes": [ "semanticTokenTypes": [
{ {
"id": "attribute" "id": "attribute",
"description": "Style for attributes"
}, },
{ {
"id": "constant" "id": "builtinType",
"description": "Style for builtin types"
},
{
"id": "lifetime",
"description": "Style for lifetimes"
},
{
"id": "typeAlias",
"description": "Style for type aliases"
},
{
"id": "union",
"description": "Style for C-style untagged unions"
} }
], ],
"semanticTokenModifiers": [ "semanticTokenModifiers": [
{ {
"id": "mutable" "id": "constant",
"description": "Style for compile-time constants"
}, },
{ {
"id": "unsafe" "id": "control",
"description": "Style for control flow keywords"
}, },
{ {
"id": "control" "id": "mutable",
"description": "Style for mutable bindings"
},
{
"id": "unsafe",
"description": "Style for unsafe operations"
} }
], ],
"semanticTokenStyleDefaults": [ "semanticTokenStyleDefaults": [
@ -407,6 +428,50 @@
"meta.attribute" "meta.attribute"
] ]
}, },
{
"selector": "builtinType",
"scope": [
"support.type.primitive"
]
},
{
"selector": "lifetime",
"scope": [
"entity.name.lifetime.rust"
]
},
{
"selector": "typeAlias",
"scope": [
"entity.name.type",
"entity.name.typeAlias"
]
},
{
"selector": "union",
"scope": [
"entity.name.type",
"entity.name.union"
]
},
{
"selector": "keyword.unsafe",
"scope": [
"keyword.other.unsafe"
]
},
{
"selector": "keyword.control",
"scope": [
"keyword.control"
]
},
{
"selector": "variable.constant",
"scope": [
"entity.name.constant"
]
},
{ {
"selector": "*.mutable", "selector": "*.mutable",
"light": { "light": {
@ -418,18 +483,6 @@
"highContrast": { "highContrast": {
"fontStyle": "underline" "fontStyle": "underline"
} }
},
{
"selector": "constant",
"scope": [
"entity.name.constant"
]
},
{
"selector": "keyword.unsafe",
"scope": [
"keyword.other.unsafe"
]
} }
] ]
} }