Auto merge of #17074 - Veykril:hl, r=Veykril

Add Static and Const highlighting token types
This commit is contained in:
bors 2024-04-15 15:19:56 +00:00
commit b223860c0e
7 changed files with 142 additions and 102 deletions

View file

@ -444,7 +444,6 @@ pub(super) fn highlight_def(
Definition::Variant(_) => Highlight::new(HlTag::Symbol(SymbolKind::Variant)),
Definition::Const(konst) => {
let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Const)) | HlMod::Const;
if let Some(item) = konst.as_assoc_item(db) {
h |= HlMod::Associated;
h |= HlMod::Static;

View file

@ -77,6 +77,7 @@ pub enum HlMod {
Library,
/// Used to differentiate individual elements within macro calls.
Macro,
/// Used to differentiate individual elements within proc-macro calls.
ProcMacro,
/// Mutable binding.
Mutable,
@ -225,8 +226,8 @@ impl HlMod {
HlMod::IntraDocLink,
HlMod::Library,
HlMod::Macro,
HlMod::ProcMacro,
HlMod::Mutable,
HlMod::ProcMacro,
HlMod::Public,
HlMod::Reference,
HlMod::Static,
@ -262,6 +263,7 @@ impl HlMod {
}
fn mask(self) -> u32 {
debug_assert!(Self::ALL.len() <= 32, "HlMod::mask is not enough to cover all variants");
1 << (self as u32)
}
}

View file

@ -218,7 +218,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
<span class="bool_literal">true</span>
<span class="brace">}</span>
<span class="brace">}</span>
<span class="keyword const">const</span> <span class="constant const declaration">USAGE_OF_BOOL</span><span class="colon">:</span><span class="builtin_type">bool</span> <span class="operator">=</span> <span class="enum public">Bool</span><span class="operator">::</span><span class="enum_variant public">True</span><span class="operator">.</span><span class="method consuming public">to_primitive</span><span class="parenthesis">(</span><span class="parenthesis">)</span><span class="semicolon">;</span>
<span class="keyword const">const</span> <span class="constant const declaration">USAGE_OF_BOOL</span><span class="colon">:</span> <span class="builtin_type">bool</span> <span class="operator">=</span> <span class="enum public">Bool</span><span class="operator">::</span><span class="enum_variant public">True</span><span class="operator">.</span><span class="method consuming public">to_primitive</span><span class="parenthesis">(</span><span class="parenthesis">)</span><span class="semicolon">;</span>
<span class="keyword">trait</span> <span class="trait declaration">Baz</span> <span class="brace">{</span>
<span class="keyword">type</span> <span class="type_alias associated declaration static trait">Qux</span><span class="semicolon">;</span>

View file

@ -300,7 +300,7 @@ impl Bool {
true
}
}
const USAGE_OF_BOOL:bool = Bool::True.to_primitive();
const USAGE_OF_BOOL: bool = Bool::True.to_primitive();
trait Baz {
type Qux;

View file

@ -17,15 +17,19 @@ macro_rules! define_semantic_token_types {
}
) => {
$(pub(crate) const $standard: SemanticTokenType = SemanticTokenType::$standard;)*
$(pub(crate) const $custom: SemanticTokenType = SemanticTokenType::new($string);)*
pub(crate) mod types {
use super::SemanticTokenType;
$(pub(crate) const $standard: SemanticTokenType = SemanticTokenType::$standard;)*
$(pub(crate) const $custom: SemanticTokenType = SemanticTokenType::new($string);)*
}
pub(crate) const SUPPORTED_TYPES: &[SemanticTokenType] = &[
$(SemanticTokenType::$standard,)*
$($custom),*
$(self::types::$custom),*
];
pub(crate) fn standard_fallback_type(token: SemanticTokenType) -> Option<SemanticTokenType> {
use self::types::*;
$(
if token == $custom {
None $(.or(Some(SemanticTokenType::$fallback)))?
@ -61,39 +65,41 @@ define_semantic_token_types![
custom {
(ANGLE, "angle"),
(ARITHMETIC, "arithmetic") => OPERATOR,
(ATTRIBUTE, "attribute") => DECORATOR,
(ATTRIBUTE_BRACKET, "attributeBracket") => DECORATOR,
(ATTRIBUTE, "attribute") => DECORATOR,
(BITWISE, "bitwise") => OPERATOR,
(BOOLEAN, "boolean"),
(BRACE, "brace"),
(BRACKET, "bracket"),
(BUILTIN_ATTRIBUTE, "builtinAttribute") => DECORATOR,
(BUILTIN_TYPE, "builtinType"),
(BUILTIN_TYPE, "builtinType") => TYPE,
(CHAR, "character") => STRING,
(COLON, "colon"),
(COMMA, "comma"),
(COMPARISON, "comparison") => OPERATOR,
(CONST_PARAMETER, "constParameter"),
(DERIVE, "derive") => DECORATOR,
(CONST, "const") => VARIABLE,
(DERIVE_HELPER, "deriveHelper") => DECORATOR,
(DERIVE, "derive") => DECORATOR,
(DOT, "dot"),
(ESCAPE_SEQUENCE, "escapeSequence") => STRING,
(INVALID_ESCAPE_SEQUENCE, "invalidEscapeSequence") => STRING,
(FORMAT_SPECIFIER, "formatSpecifier") => STRING,
(GENERIC, "generic") => TYPE_PARAMETER,
(INVALID_ESCAPE_SEQUENCE, "invalidEscapeSequence") => STRING,
(LABEL, "label"),
(LIFETIME, "lifetime"),
(LOGICAL, "logical") => OPERATOR,
(MACRO_BANG, "macroBang") => MACRO,
(PROC_MACRO, "procMacro") => MACRO,
(PARENTHESIS, "parenthesis"),
(PROC_MACRO, "procMacro") => MACRO,
(PUNCTUATION, "punctuation"),
(SELF_KEYWORD, "selfKeyword") => KEYWORD,
(SELF_TYPE_KEYWORD, "selfTypeKeyword") => KEYWORD,
(SEMICOLON, "semicolon"),
(TYPE_ALIAS, "typeAlias"),
(STATIC, "static") => VARIABLE,
(TOOL_MODULE, "toolModule") => DECORATOR,
(UNION, "union"),
(TYPE_ALIAS, "typeAlias") => TYPE,
(UNION, "union") => TYPE,
(UNRESOLVED_REFERENCE, "unresolvedReference"),
}
];
@ -112,13 +118,16 @@ macro_rules! define_semantic_token_modifiers {
}
) => {
pub(crate) mod modifiers {
use super::SemanticTokenModifier;
$(pub(crate) const $standard: SemanticTokenModifier = SemanticTokenModifier::$standard;)*
$(pub(crate) const $custom: SemanticTokenModifier = SemanticTokenModifier::new($string);)*
$(pub(crate) const $standard: SemanticTokenModifier = SemanticTokenModifier::$standard;)*
$(pub(crate) const $custom: SemanticTokenModifier = SemanticTokenModifier::new($string);)*
}
pub(crate) const SUPPORTED_MODIFIERS: &[SemanticTokenModifier] = &[
$(SemanticTokenModifier::$standard,)*
$($custom),*
$(self::modifiers::$custom),*
];
const LAST_STANDARD_MOD: usize = count_tts!($($standard)*);
@ -145,8 +154,8 @@ define_semantic_token_modifiers![
(INTRA_DOC_LINK, "intraDocLink"),
(LIBRARY, "library"),
(MACRO_MODIFIER, "macro"),
(PROC_MACRO_MODIFIER, "proc_macro"),
(MUTABLE, "mutable"),
(PROC_MACRO_MODIFIER, "procMacro"),
(PUBLIC, "public"),
(REFERENCE, "reference"),
(TRAIT_MODIFIER, "trait"),

View file

@ -654,97 +654,99 @@ pub(crate) fn semantic_token_delta(
fn semantic_token_type_and_modifiers(
highlight: Highlight,
) -> (lsp_types::SemanticTokenType, semantic_tokens::ModifierSet) {
use semantic_tokens::{modifiers as mods, types};
let ty = match highlight.tag {
HlTag::Symbol(symbol) => match symbol {
SymbolKind::Attribute => semantic_tokens::DECORATOR,
SymbolKind::Derive => semantic_tokens::DERIVE,
SymbolKind::DeriveHelper => semantic_tokens::DERIVE_HELPER,
SymbolKind::Module => semantic_tokens::NAMESPACE,
SymbolKind::Impl => semantic_tokens::TYPE_ALIAS,
SymbolKind::Field => semantic_tokens::PROPERTY,
SymbolKind::TypeParam => semantic_tokens::TYPE_PARAMETER,
SymbolKind::ConstParam => semantic_tokens::CONST_PARAMETER,
SymbolKind::LifetimeParam => semantic_tokens::LIFETIME,
SymbolKind::Label => semantic_tokens::LABEL,
SymbolKind::ValueParam => semantic_tokens::PARAMETER,
SymbolKind::SelfParam => semantic_tokens::SELF_KEYWORD,
SymbolKind::SelfType => semantic_tokens::SELF_TYPE_KEYWORD,
SymbolKind::Local => semantic_tokens::VARIABLE,
SymbolKind::Method => semantic_tokens::METHOD,
SymbolKind::Function => semantic_tokens::FUNCTION,
SymbolKind::Const => semantic_tokens::VARIABLE,
SymbolKind::Static => semantic_tokens::VARIABLE,
SymbolKind::Struct => semantic_tokens::STRUCT,
SymbolKind::Enum => semantic_tokens::ENUM,
SymbolKind::Variant => semantic_tokens::ENUM_MEMBER,
SymbolKind::Union => semantic_tokens::UNION,
SymbolKind::TypeAlias => semantic_tokens::TYPE_ALIAS,
SymbolKind::Trait => semantic_tokens::INTERFACE,
SymbolKind::TraitAlias => semantic_tokens::INTERFACE,
SymbolKind::Macro => semantic_tokens::MACRO,
SymbolKind::ProcMacro => semantic_tokens::PROC_MACRO,
SymbolKind::BuiltinAttr => semantic_tokens::BUILTIN_ATTRIBUTE,
SymbolKind::ToolModule => semantic_tokens::TOOL_MODULE,
SymbolKind::Attribute => types::DECORATOR,
SymbolKind::Derive => types::DERIVE,
SymbolKind::DeriveHelper => types::DERIVE_HELPER,
SymbolKind::Module => types::NAMESPACE,
SymbolKind::Impl => types::TYPE_ALIAS,
SymbolKind::Field => types::PROPERTY,
SymbolKind::TypeParam => types::TYPE_PARAMETER,
SymbolKind::ConstParam => types::CONST_PARAMETER,
SymbolKind::LifetimeParam => types::LIFETIME,
SymbolKind::Label => types::LABEL,
SymbolKind::ValueParam => types::PARAMETER,
SymbolKind::SelfParam => types::SELF_KEYWORD,
SymbolKind::SelfType => types::SELF_TYPE_KEYWORD,
SymbolKind::Local => types::VARIABLE,
SymbolKind::Method => types::METHOD,
SymbolKind::Function => types::FUNCTION,
SymbolKind::Const => types::CONST,
SymbolKind::Static => types::STATIC,
SymbolKind::Struct => types::STRUCT,
SymbolKind::Enum => types::ENUM,
SymbolKind::Variant => types::ENUM_MEMBER,
SymbolKind::Union => types::UNION,
SymbolKind::TypeAlias => types::TYPE_ALIAS,
SymbolKind::Trait => types::INTERFACE,
SymbolKind::TraitAlias => types::INTERFACE,
SymbolKind::Macro => types::MACRO,
SymbolKind::ProcMacro => types::PROC_MACRO,
SymbolKind::BuiltinAttr => types::BUILTIN_ATTRIBUTE,
SymbolKind::ToolModule => types::TOOL_MODULE,
},
HlTag::AttributeBracket => semantic_tokens::ATTRIBUTE_BRACKET,
HlTag::BoolLiteral => semantic_tokens::BOOLEAN,
HlTag::BuiltinType => semantic_tokens::BUILTIN_TYPE,
HlTag::ByteLiteral | HlTag::NumericLiteral => semantic_tokens::NUMBER,
HlTag::CharLiteral => semantic_tokens::CHAR,
HlTag::Comment => semantic_tokens::COMMENT,
HlTag::EscapeSequence => semantic_tokens::ESCAPE_SEQUENCE,
HlTag::InvalidEscapeSequence => semantic_tokens::INVALID_ESCAPE_SEQUENCE,
HlTag::FormatSpecifier => semantic_tokens::FORMAT_SPECIFIER,
HlTag::Keyword => semantic_tokens::KEYWORD,
HlTag::None => semantic_tokens::GENERIC,
HlTag::AttributeBracket => types::ATTRIBUTE_BRACKET,
HlTag::BoolLiteral => types::BOOLEAN,
HlTag::BuiltinType => types::BUILTIN_TYPE,
HlTag::ByteLiteral | HlTag::NumericLiteral => types::NUMBER,
HlTag::CharLiteral => types::CHAR,
HlTag::Comment => types::COMMENT,
HlTag::EscapeSequence => types::ESCAPE_SEQUENCE,
HlTag::InvalidEscapeSequence => types::INVALID_ESCAPE_SEQUENCE,
HlTag::FormatSpecifier => types::FORMAT_SPECIFIER,
HlTag::Keyword => types::KEYWORD,
HlTag::None => types::GENERIC,
HlTag::Operator(op) => match op {
HlOperator::Bitwise => semantic_tokens::BITWISE,
HlOperator::Arithmetic => semantic_tokens::ARITHMETIC,
HlOperator::Logical => semantic_tokens::LOGICAL,
HlOperator::Comparison => semantic_tokens::COMPARISON,
HlOperator::Other => semantic_tokens::OPERATOR,
HlOperator::Bitwise => types::BITWISE,
HlOperator::Arithmetic => types::ARITHMETIC,
HlOperator::Logical => types::LOGICAL,
HlOperator::Comparison => types::COMPARISON,
HlOperator::Other => types::OPERATOR,
},
HlTag::StringLiteral => semantic_tokens::STRING,
HlTag::UnresolvedReference => semantic_tokens::UNRESOLVED_REFERENCE,
HlTag::StringLiteral => types::STRING,
HlTag::UnresolvedReference => types::UNRESOLVED_REFERENCE,
HlTag::Punctuation(punct) => match punct {
HlPunct::Bracket => semantic_tokens::BRACKET,
HlPunct::Brace => semantic_tokens::BRACE,
HlPunct::Parenthesis => semantic_tokens::PARENTHESIS,
HlPunct::Angle => semantic_tokens::ANGLE,
HlPunct::Comma => semantic_tokens::COMMA,
HlPunct::Dot => semantic_tokens::DOT,
HlPunct::Colon => semantic_tokens::COLON,
HlPunct::Semi => semantic_tokens::SEMICOLON,
HlPunct::Other => semantic_tokens::PUNCTUATION,
HlPunct::MacroBang => semantic_tokens::MACRO_BANG,
HlPunct::Bracket => types::BRACKET,
HlPunct::Brace => types::BRACE,
HlPunct::Parenthesis => types::PARENTHESIS,
HlPunct::Angle => types::ANGLE,
HlPunct::Comma => types::COMMA,
HlPunct::Dot => types::DOT,
HlPunct::Colon => types::COLON,
HlPunct::Semi => types::SEMICOLON,
HlPunct::Other => types::PUNCTUATION,
HlPunct::MacroBang => types::MACRO_BANG,
},
};
let mut mods = semantic_tokens::ModifierSet::default();
for modifier in highlight.mods.iter() {
let modifier = match modifier {
HlMod::Associated => semantic_tokens::ASSOCIATED,
HlMod::Async => semantic_tokens::ASYNC,
HlMod::Attribute => semantic_tokens::ATTRIBUTE_MODIFIER,
HlMod::Callable => semantic_tokens::CALLABLE,
HlMod::Const => semantic_tokens::CONSTANT,
HlMod::Consuming => semantic_tokens::CONSUMING,
HlMod::ControlFlow => semantic_tokens::CONTROL_FLOW,
HlMod::CrateRoot => semantic_tokens::CRATE_ROOT,
HlMod::DefaultLibrary => semantic_tokens::DEFAULT_LIBRARY,
HlMod::Definition => semantic_tokens::DECLARATION,
HlMod::Documentation => semantic_tokens::DOCUMENTATION,
HlMod::Injected => semantic_tokens::INJECTED,
HlMod::IntraDocLink => semantic_tokens::INTRA_DOC_LINK,
HlMod::Library => semantic_tokens::LIBRARY,
HlMod::Macro => semantic_tokens::MACRO_MODIFIER,
HlMod::ProcMacro => semantic_tokens::PROC_MACRO_MODIFIER,
HlMod::Mutable => semantic_tokens::MUTABLE,
HlMod::Public => semantic_tokens::PUBLIC,
HlMod::Reference => semantic_tokens::REFERENCE,
HlMod::Static => semantic_tokens::STATIC,
HlMod::Trait => semantic_tokens::TRAIT_MODIFIER,
HlMod::Unsafe => semantic_tokens::UNSAFE,
HlMod::Associated => mods::ASSOCIATED,
HlMod::Async => mods::ASYNC,
HlMod::Attribute => mods::ATTRIBUTE_MODIFIER,
HlMod::Callable => mods::CALLABLE,
HlMod::Const => mods::CONSTANT,
HlMod::Consuming => mods::CONSUMING,
HlMod::ControlFlow => mods::CONTROL_FLOW,
HlMod::CrateRoot => mods::CRATE_ROOT,
HlMod::DefaultLibrary => mods::DEFAULT_LIBRARY,
HlMod::Definition => mods::DECLARATION,
HlMod::Documentation => mods::DOCUMENTATION,
HlMod::Injected => mods::INJECTED,
HlMod::IntraDocLink => mods::INTRA_DOC_LINK,
HlMod::Library => mods::LIBRARY,
HlMod::Macro => mods::MACRO_MODIFIER,
HlMod::ProcMacro => mods::PROC_MACRO_MODIFIER,
HlMod::Mutable => mods::MUTABLE,
HlMod::Public => mods::PUBLIC,
HlMod::Reference => mods::REFERENCE,
HlMod::Static => mods::STATIC,
HlMod::Trait => mods::TRAIT_MODIFIER,
HlMod::Unsafe => mods::UNSAFE,
};
mods |= modifier;
}

View file

@ -1934,6 +1934,11 @@
"id": "constParameter",
"description": "Style for const generics"
},
{
"id": "const",
"description": "Style for consts",
"superType": "variable"
},
{
"id": "derive",
"description": "Style for derives",
@ -1979,20 +1984,25 @@
"description": "Style for the ! token of macro calls",
"superType": "punctuation"
},
{
"id": "operator",
"description": "Style for operators",
"superType": "punctuation"
},
{
"id": "parenthesis",
"description": "Style for ( or )",
"superType": "punctuation"
},
{
"id": "procMacro",
"description": "Style for proc macro code",
"superType": "macro"
},
{
"id": "punctuation",
"description": "Style for generic punctuation"
},
{
"id": "operator",
"description": "Style for operators",
"superType": "punctuation"
},
{
"id": "selfKeyword",
"description": "Style for the self keyword",
@ -2008,6 +2018,16 @@
"description": "Style for ;",
"superType": "punctuation"
},
{
"id": "static",
"description": "Style for statics",
"superType": "variable"
},
{
"id": "toolModule",
"description": "Style for tool module attributes",
"superType": "decorator"
},
{
"id": "typeAlias",
"description": "Style for type aliases",
@ -2064,10 +2084,18 @@
"id": "library",
"description": "Style for items that are defined outside of the current crate"
},
{
"id": "macro",
"description": "Style for tokens inside of macro calls"
},
{
"id": "mutable",
"description": "Style for mutable locals and statics as well as functions taking `&mut self`"
},
{
"id": "procMacro",
"description": "Style for tokens inside of proc-macro calls"
},
{
"id": "public",
"description": "Style for items that are from the current crate and are `pub`"