Skip to content

Commit

Permalink
distinguishing any in keywords and types in lsp hightlight
Browse files Browse the repository at this point in the history
Signed-off-by: he1pa <[email protected]>
  • Loading branch information
He1pa committed Oct 16, 2024
1 parent a36e4d7 commit 7d81bc3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
15 changes: 13 additions & 2 deletions kclvm/sema/src/advanced_resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
SymbolHintKind, SymbolRef, SymbolSemanticInfo, UnresolvedSymbol, ValueSymbol,
},
},
ty::{Parameter, Type, TypeKind, SCHEMA_MEMBER_FUNCTIONS},
ty::{Parameter, Type, TypeKind, ANY_TYPE_STR, SCHEMA_MEMBER_FUNCTIONS},
};

use super::AdvancedResolver;
Expand Down Expand Up @@ -1721,7 +1721,18 @@ impl<'ctx> AdvancedResolver<'ctx> {
self.ctx.is_type_expr = true;
if let Some(ty_node) = ty_node {
match &ty_node.node {
ast::Type::Any => {}
ast::Type::Any => {
let (start, end) = ty_node.get_span_pos();
let mut type_symbol =
UnresolvedSymbol::new(ANY_TYPE_STR.to_owned(), start, end, None, true);

type_symbol.sema_info.ty = Some(Arc::new(Type::ANY));
self.gs.get_symbols_mut().alloc_unresolved_symbol(
type_symbol,
self.ctx.get_node_key(&ty_node.id),
self.ctx.current_pkgpath.clone().unwrap(),
);
}
ast::Type::Named(identifier) => {
self.walk_identifier(identifier)?;
}
Expand Down
25 changes: 18 additions & 7 deletions kclvm/tools/src/LSP/src/semantic_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::vec;
use kclvm_error::Position;
use kclvm_sema::core::{
global_state::GlobalState,
symbol::{KCLSymbol, SymbolKind},
symbol::{KCLSymbol, SymbolKind, SymbolRef},
};
use kclvm_sema::ty::TypeKind;
use lsp_types::{SemanticToken, SemanticTokenType, SemanticTokens, SemanticTokensResult};
Expand Down Expand Up @@ -34,7 +34,7 @@ pub fn semantic_tokens_full(file: &str, gs: &GlobalState) -> Option<SemanticToke
for symbol_ref in symbols {
if let Some(symbol) = gs.get_symbols().get_symbol(*symbol_ref) {
let (start, end) = symbol.get_range();
match get_kind(symbol_ref.get_kind(), symbol, gs) {
match get_kind(*symbol_ref, symbol, gs) {
Some(kind) => {
kcl_tokens.push(KCLSemanticToken {
start: start.clone(),
Expand All @@ -58,8 +58,8 @@ pub fn semantic_tokens_full(file: &str, gs: &GlobalState) -> Option<SemanticToke
}))
}

pub(crate) fn get_kind(ty: SymbolKind, symbol: &KCLSymbol, gs: &GlobalState) -> Option<u32> {
match ty {
pub(crate) fn get_kind(symbol_ref: SymbolRef, symbol: &KCLSymbol, gs: &GlobalState) -> Option<u32> {
match symbol_ref.get_kind() {
SymbolKind::Schema => Some(type_index(SemanticTokenType::STRUCT)),
SymbolKind::Attribute => Some(type_index(SemanticTokenType::PROPERTY)),
SymbolKind::Package => Some(type_index(SemanticTokenType::NAMESPACE)),
Expand All @@ -78,10 +78,17 @@ pub(crate) fn get_kind(ty: SymbolKind, symbol: &KCLSymbol, gs: &GlobalState) ->
SymbolKind::Rule => Some(type_index(SemanticTokenType::MACRO)),
SymbolKind::Unresolved => match &symbol.get_definition() {
Some(def_ref) => match gs.get_symbols().get_symbol(*def_ref) {
Some(symbol) => get_kind(def_ref.get_kind(), symbol, gs),
Some(symbol) => get_kind(*def_ref, symbol, gs),
None => Some(type_index(SemanticTokenType::VARIABLE)),
},
None => Some(type_index(SemanticTokenType::VARIABLE)),
None => {
let unresolved_symbol = gs.get_symbols().get_unresolved_symbol(symbol_ref).unwrap();
if unresolved_symbol.is_type() {
Some(type_index(SemanticTokenType::TYPE))
} else {
Some(type_index(SemanticTokenType::VARIABLE))
}
}
},
SymbolKind::Expression => None,
SymbolKind::Comment => None,
Expand Down Expand Up @@ -197,7 +204,11 @@ mod tests {
// (0, 4, 4, 8), // func
// (0, 5, 1, 0) // x
// (2, 7, 8, 1) // Manifest
// (1, 5, 4, 0)] // name
// (1, 5, 4, 0) // name
// (2, 0, 3, 0) // aaa
// (0, 5, 3, 4) // any
// (2, 0, 3, 0) // bbb
// (0, 10, 4, 0) // item
insta::assert_snapshot!(format!("{:?}", get));
}
lsp_types::SemanticTokensResult::Partial(_) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: tools/src/LSP/src/semantic_token.rs
expression: "format!(\"{:?}\", get)"
---
[(0, 15, 1, 3), (1, 5, 3, 4), (1, 7, 7, 1), (1, 4, 4, 2), (2, 0, 2, 0), (0, 4, 7, 1), (0, 10, 7, 1), (1, 4, 4, 2), (2, 0, 1, 0), (0, 3, 3, 4), (2, 0, 4, 8), (0, 14, 1, 0), (1, 4, 1, 0), (3, 0, 1, 0), (0, 4, 4, 8), (1, 0, 1, 0), (0, 4, 4, 8), (0, 5, 1, 0), (2, 7, 8, 1), (1, 5, 4, 0)]
[(0, 15, 1, 3), (1, 5, 3, 4), (1, 7, 7, 1), (1, 4, 4, 2), (2, 0, 2, 0), (0, 4, 7, 1), (0, 10, 7, 1), (1, 4, 4, 2), (2, 0, 1, 0), (0, 3, 3, 4), (2, 0, 4, 8), (0, 14, 1, 0), (1, 4, 1, 0), (3, 0, 1, 0), (0, 4, 4, 8), (1, 0, 1, 0), (0, 4, 4, 8), (0, 5, 1, 0), (2, 7, 8, 1), (1, 5, 4, 0), (2, 0, 3, 0), (0, 5, 3, 4), (2, 0, 3, 0), (0, 10, 4, 0)]
3 changes: 3 additions & 0 deletions kclvm/tools/src/LSP/src/test_data/sema_token/sema_token.k
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ b = func(x="123")
schema Manifest:
[name: str] = ""

aaa: any = 1

bbb = any item in [] { True }

0 comments on commit 7d81bc3

Please sign in to comment.