Skip to content

Commit a7b090e

Browse files
committed
fix paren and bracket semantic token
1 parent bc83e9b commit a7b090e

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

crates/emmylua_ls/src/handlers/semantic_token/build_semantic_tokens.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::{
22
SEMANTIC_TOKEN_MODIFIERS, SEMANTIC_TOKEN_TYPES, semantic_token_builder::SemanticBuilder,
33
};
44
use crate::handlers::semantic_token::function_string_highlight::fun_string_highlight;
5+
use crate::handlers::semantic_token::semantic_token_builder::CustomSemanticTokenType;
56
use crate::util::parse_desc;
67
use crate::{context::ClientId, handlers::semantic_token::language_injector::inject_language};
78
use emmylua_code_analysis::{
@@ -116,15 +117,26 @@ fn build_tokens_semantic_token(
116117
| LuaTokenKind::TkAssign => {
117118
builder.push(token, SemanticTokenType::OPERATOR);
118119
}
119-
// 冒号不标记为运算符,让VS Code使用默认的分隔符高亮
120-
LuaTokenKind::TkColon
121-
| LuaTokenKind::TkLeftBrace
122-
| LuaTokenKind::TkRightBrace
123-
| LuaTokenKind::TkLeftBracket
120+
LuaTokenKind::TkLeftBrace | LuaTokenKind::TkRightBrace => {
121+
builder.push(token, SemanticTokenType::OPERATOR);
122+
}
123+
LuaTokenKind::TkColon => {
124+
if let Some(parent) = token.parent()
125+
&& parent.kind() != LuaSyntaxKind::IndexExpr.into()
126+
{
127+
builder.push(token, SemanticTokenType::OPERATOR);
128+
}
129+
}
130+
// delimiter
131+
LuaTokenKind::TkLeftBracket
124132
| LuaTokenKind::TkRightBracket
125133
| LuaTokenKind::TkLeftParen
126134
| LuaTokenKind::TkRightParen => {
127-
builder.push(token, SemanticTokenType::OPERATOR);
135+
if client_id.is_neovim() {
136+
builder.push(token, CustomSemanticTokenType::DELIMITER);
137+
} else {
138+
builder.push(token, SemanticTokenType::OPERATOR);
139+
}
128140
}
129141
LuaTokenKind::TkTrue | LuaTokenKind::TkFalse | LuaTokenKind::TkNil => {
130142
builder.push_with_modifier(

crates/emmylua_ls/src/handlers/semantic_token/semantic_token_builder.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ use std::{
77
vec::Vec,
88
};
99

10+
pub struct CustomSemanticTokenType;
11+
impl CustomSemanticTokenType {
12+
// neovim supports custom semantic token types, we add a custom type for delimiter
13+
pub const DELIMITER: SemanticTokenType = SemanticTokenType::new("delimiter");
14+
}
15+
1016
pub const SEMANTIC_TOKEN_TYPES: &[SemanticTokenType] = &[
1117
SemanticTokenType::NAMESPACE,
1218
SemanticTokenType::TYPE,
@@ -31,6 +37,8 @@ pub const SEMANTIC_TOKEN_TYPES: &[SemanticTokenType] = &[
3137
SemanticTokenType::REGEXP,
3238
SemanticTokenType::OPERATOR,
3339
SemanticTokenType::DECORATOR,
40+
// Custom types
41+
CustomSemanticTokenType::DELIMITER,
3442
];
3543

3644
pub const SEMANTIC_TOKEN_MODIFIERS: &[SemanticTokenModifier] = &[

crates/emmylua_ls/src/handlers/test/semantic_token_test.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,6 @@ mod tests {
7979
token_type: SemanticTokenType::VARIABLE,
8080
token_modifier: HashSet::from([SemanticTokenModifier::READONLY]),
8181
},
82-
VirtualSemanticToken {
83-
line: 4,
84-
start: 28,
85-
length: 1,
86-
token_type: SemanticTokenType::OPERATOR,
87-
token_modifier: HashSet::new(),
88-
},
8982
VirtualSemanticToken {
9083
line: 4,
9184
start: 29,
@@ -149,13 +142,6 @@ mod tests {
149142
token_type: SemanticTokenType::OPERATOR,
150143
token_modifier: HashSet::from([SemanticTokenModifier::DOCUMENTATION]),
151144
},
152-
VirtualSemanticToken {
153-
line: 4,
154-
start: 50,
155-
length: 1,
156-
token_type: SemanticTokenType::OPERATOR,
157-
token_modifier: HashSet::new(),
158-
},
159145
VirtualSemanticToken {
160146
line: 4,
161147
start: 51,

0 commit comments

Comments
 (0)