Skip to content

Commit

Permalink
fix: fix lsp complete in comment after str type var
Browse files Browse the repository at this point in the history
Signed-off-by: he1pa <18012015693@163.com>
  • Loading branch information
He1pa committed Jan 17, 2024
1 parent 5fa77a8 commit 13f20c8
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 4 deletions.
18 changes: 15 additions & 3 deletions kclvm/sema/src/advanced_resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use crate::{
core::{
scope::LocalSymbolScopeKind,
symbol::{
ExpressionSymbol, KCLSymbolSemanticInfo, SymbolRef, UnresolvedSymbol, ValueSymbol,
CommentSymbol, ExpressionSymbol, KCLSymbolSemanticInfo, SymbolRef, UnresolvedSymbol,
ValueSymbol,
},
},
ty::{Type, SCHEMA_MEMBER_FUNCTIONS},
Expand All @@ -27,6 +28,13 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> {
for stmt in module.body.iter() {
self.stmt(&stmt);
}
for comment in module.comments.iter() {
let (start, end) = comment.get_span_pos();
self.ctx.start_pos = start;
self.ctx.end_pos = end;
self.ctx.cur_node = comment.id.clone();
self.walk_comment(&comment.node);
}
None
}

Expand Down Expand Up @@ -729,8 +737,12 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> {
None
}

fn walk_comment(&mut self, _comment: &'ctx ast::Comment) -> Self::Result {
None
fn walk_comment(&mut self, comment: &'ctx ast::Comment) -> Self::Result {
let (start, end) = (self.ctx.start_pos.clone(), self.ctx.end_pos.clone());
let comment_symbol = CommentSymbol::new(start, end, comment.text.clone());
self.gs
.get_symbols_mut()
.alloc_comment_symbol(comment_symbol, self.ctx.get_node_key(&self.ctx.cur_node))
}

fn walk_missing_expr(&mut self, _missing_expr: &'ctx ast::MissingExpr) -> Self::Result {
Expand Down
21 changes: 21 additions & 0 deletions kclvm/sema/src/core/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,27 @@ impl GlobalState {
},
);
}

for (index, symbol) in self.symbols.comments.iter() {
let symbol_ref = SymbolRef {
kind: SymbolKind::Comment,
id: index,
};
let filename = symbol.start.filename.clone();
if !file_sema_map.contains_key(&filename) {
file_sema_map.insert(filename.clone(), FileSemanticInfo::new(filename.clone()));
}
let file_sema_info = file_sema_map.get_mut(&filename).unwrap();
file_sema_info.symbols.push(symbol_ref);
file_sema_info.symbol_locs.insert(
symbol_ref,
CachedLocation {
line: symbol.start.line,
column: symbol.start.column.unwrap_or(0),
},
);
}

// remove dummy file
file_sema_map.remove("");

Expand Down
140 changes: 140 additions & 0 deletions kclvm/sema/src/core/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub struct KCLSymbolData {
pub(crate) unresolved: Arena<UnresolvedSymbol>,
pub(crate) rules: Arena<RuleSymbol>,
pub(crate) exprs: Arena<ExpressionSymbol>,
pub(crate) comments: Arena<CommentSymbol>,

pub(crate) symbols_info: SymbolDB,
}
Expand Down Expand Up @@ -161,6 +162,10 @@ impl KCLSymbolData {
.exprs
.get(id.get_id())
.map(|symbol| symbol as &KCLSymbol),
SymbolKind::Comment => self
.comments
.get(id.get_id())
.map(|symbol| symbol as &KCLSymbol),
}
}

Expand Down Expand Up @@ -214,6 +219,12 @@ impl KCLSymbolData {
symbol
});
}
SymbolKind::Comment => {
self.comments.get_mut(id.get_id()).map(|symbol| {
symbol.sema_info.ty = Some(ty);
symbol
});
}
}
}

Expand Down Expand Up @@ -618,6 +629,26 @@ impl KCLSymbolData {
self.exprs.get_mut(symbol_id).unwrap().id = Some(symbol_ref);
Some(symbol_ref)
}

pub fn alloc_comment_symbol(
&mut self,
comment: CommentSymbol,
node_key: NodeKey,
) -> Option<SymbolRef> {
let symbol_id = self.comments.insert(comment);
let symbol_ref = SymbolRef {
id: symbol_id,
kind: SymbolKind::Comment,
};
self.symbols_info
.node_symbol_map
.insert(node_key.clone(), symbol_ref);
self.symbols_info
.symbol_ref_map
.insert(symbol_ref, node_key);
self.exprs.get_mut(symbol_id).unwrap().id = Some(symbol_ref);
Some(symbol_ref)
}
}
#[allow(unused)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand All @@ -630,6 +661,7 @@ pub enum SymbolKind {
Unresolved,
Rule,
Expression,
Comment,
}
#[allow(unused)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down Expand Up @@ -1725,3 +1757,111 @@ impl ExpressionSymbol {
}
}
}

#[derive(Debug, Clone)]
pub struct CommentSymbol {
pub(crate) id: Option<SymbolRef>,
pub(crate) start: Position,
pub(crate) end: Position,
pub(crate) content: String,
pub(crate) sema_info: KCLSymbolSemanticInfo,
}

impl Symbol for CommentSymbol {
type SymbolData = KCLSymbolData;
type SemanticInfo = KCLSymbolSemanticInfo;

fn get_sema_info(&self) -> &Self::SemanticInfo {
&self.sema_info
}

fn is_global(&self) -> bool {
true
}

fn get_range(&self) -> Range {
(self.start.clone(), self.end.clone())
}

fn get_owner(&self) -> Option<SymbolRef> {
None
}

fn get_definition(&self) -> Option<SymbolRef> {
self.id
}

fn get_name(&self) -> String {
self.name()
}

fn get_id(&self) -> Option<SymbolRef> {
self.id.clone()
}

fn get_attribute(
&self,
_name: &str,
_data: &Self::SymbolData,
_module_info: Option<&ModuleInfo>,
) -> Option<SymbolRef> {
None
}

fn has_attribute(
&self,
_name: &str,
_data: &Self::SymbolData,
_module_info: Option<&ModuleInfo>,
) -> bool {
false
}

fn get_all_attributes(
&self,
_data: &Self::SymbolData,
_module_info: Option<&ModuleInfo>,
) -> Vec<SymbolRef> {
vec![]
}

fn simple_dump(&self) -> String {
let mut output = "{\n".to_string();
output.push_str("\"kind\": \"CommentSymbol\",\n");
output.push_str(&format!(
"\"range\": \"{}:{}",
self.start.filename, self.start.line
));
if let Some(start_col) = self.start.column {
output.push_str(&format!(":{}", start_col));
}

output.push_str(&format!(" to {}", self.end.line));
if let Some(end_col) = self.end.column {
output.push_str(&format!(":{}", end_col));
}
output.push_str(&format!("content :{}", self.name()));
output.push_str("\"\n}");
output
}

fn full_dump(&self, _data: &Self::SymbolData) -> Option<String> {
Some(self.simple_dump())
}
}

impl CommentSymbol {
pub fn new(start: Position, end: Position, content: String) -> Self {
Self {
id: None,
start,
end,
content,
sema_info: KCLSymbolSemanticInfo::default(),
}
}

pub fn name(&self) -> String {
format!("# {}", self.content)
}
}
21 changes: 21 additions & 0 deletions kclvm/tools/src/LSP/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1467,4 +1467,25 @@ mod tests {
CompletionResponse::List(_) => panic!("test failed"),
}
}

#[test]
fn comment_completion() {
let (file, program, _, _, gs) =
compile_test_file("src/test_data/completion_test/dot/lit_str/lit_str.k");

let pos = KCLPos {
filename: file.to_owned(),
line: 4,
column: Some(4),
};

let got = completion(Some('.'), &program, &pos, &gs).unwrap();

match &got {
CompletionResponse::Array(arr) => {
assert_eq!(arr.len(), 0)
}
CompletionResponse::List(_) => panic!("test failed"),
};
}
}
1 change: 1 addition & 0 deletions kclvm/tools/src/LSP/src/document_symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ fn symbol_kind_to_document_symbol_kind(kind: KCLSymbolKind) -> Option<SymbolKind
KCLSymbolKind::Unresolved => Some(SymbolKind::NULL),
KCLSymbolKind::Rule => Some(SymbolKind::FUNCTION),
KCLSymbolKind::Expression => None,
KCLSymbolKind::Comment => None,
}
}

Expand Down
2 changes: 2 additions & 0 deletions kclvm/tools/src/LSP/src/semantic_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub const LEGEND_TYPE: &[SemanticTokenType] = &[
SemanticTokenType::NAMESPACE,
SemanticTokenType::TYPE,
SemanticTokenType::MACRO,
SemanticTokenType::COMMENT,
];

pub(crate) struct KCLSemanticToken {
Expand Down Expand Up @@ -39,6 +40,7 @@ pub(crate) fn semantic_tokens_full(file: &str, gs: &GlobalState) -> Option<Seman
type_index(SemanticTokenType::VARIABLE)
}
SymbolKind::Rule => type_index(SemanticTokenType::MACRO),
SymbolKind::Comment => type_index(SemanticTokenType::COMMENT),
SymbolKind::Expression => unreachable!(),
};
kcl_tokens.push(KCLSemanticToken {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
a = "aaa"
"aaa"
a
a
# a

0 comments on commit 13f20c8

Please sign in to comment.