Use semantic token type boolean for true/false

This distinguishes boolean literals from other builtin constants like
`null`. The type name is copied from rust-analyzer.

<526507fe22/crates/rust-analyzer/src/semantic_tokens.rs (L67)>

Closes #52
This commit is contained in:
oxalica 2023-05-31 19:22:45 +08:00
parent ac641a993a
commit dcd38b96c9
2 changed files with 9 additions and 1 deletions

View file

@ -21,6 +21,7 @@ pub enum HlTag {
AttrField(HlAttrField),
Builtin(BuiltinKind),
Comment,
BoolLiteral,
FloatLiteral,
IntLiteral,
Keyword(HlKeyword),
@ -82,6 +83,10 @@ pub(crate) fn highlight(
Some(node) if node.kind() == SyntaxKind::REF => {
let expr = source_map.expr_for_node(AstPtr::new(&node))?;
if let Some(builtin) = nameres.check_builtin(expr, &module) {
// Special case boolean literals.
if matches!(builtin, "true" | "false") {
return Some(HlTag::BoolLiteral);
}
return Some(HlTag::Builtin(ALL_BUILTINS[builtin].kind));
}
Some(match nameres.get(expr) {
@ -261,7 +266,8 @@ mod tests {
#[test]
fn builtins_global() {
check("$0true", expect!["Builtin(Const)"]);
check("$0true", expect!["BoolLiteral"]);
check("$0null", expect!["Builtin(Const)"]);
check("$0builtins", expect!["Builtin(Attrset)"]);
check("$0map", expect!["Builtin(Function)"]);
}

View file

@ -18,6 +18,7 @@ macro_rules! def_index {
def_index! {
SemanticTokenType, SEMANTIC_TOKEN_TYPES, TokenTypeIdx;
Boolean => SemanticTokenType::new("boolean"),
Comment => SemanticTokenType::COMMENT,
Constant => SemanticTokenType::new("constant"),
Function => SemanticTokenType::FUNCTION,
@ -102,6 +103,7 @@ pub(crate) fn to_semantic_type_and_modifiers(tag: HlTag) -> (TokenTypeIdx, Token
mods.insert(TokenModIdx::Escape);
TokenTypeIdx::String
}
HlTag::BoolLiteral => TokenTypeIdx::Boolean,
HlTag::FloatLiteral | HlTag::IntLiteral => TokenTypeIdx::Number,
HlTag::Keyword(kw) => match kw {
HlKeyword::Conditional => {