Skip to content

Commit

Permalink
fix: fix schema scope variables sema info and comments symbols panic
Browse files Browse the repository at this point in the history
Signed-off-by: he1pa <18012015693@163.com>
  • Loading branch information
He1pa committed Jan 29, 2024
1 parent 09aa50f commit 337af62
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
14 changes: 14 additions & 0 deletions kclvm/sema/src/advanced_resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,20 @@ impl<'ctx> AdvancedResolver<'ctx> {
doc: None,
};
}

if self.ctx.maybe_def && identifier.node.names.len() > 0 {
let cur_scope = *self.ctx.scopes.last().unwrap();
match cur_scope.kind {
crate::core::scope::ScopeKind::Local => {
self.gs.get_scopes_mut().add_def_to_scope(
cur_scope,
identifier.node.names.last().unwrap().node.clone(),
identifier_symbol,
);
}
crate::core::scope::ScopeKind::Root => {}
}
}
identifier_symbol
} else {
self.resolve_names(&identifier.node.names, self.ctx.maybe_def)?
Expand Down
2 changes: 1 addition & 1 deletion kclvm/sema/src/core/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ impl SymbolData {
self.symbols_info
.symbol_node_map
.insert(symbol_ref, node_key);
self.exprs.get_mut(symbol_id).unwrap().id = Some(symbol_ref);
self.comments.get_mut(symbol_id).unwrap().id = Some(symbol_ref);
Some(symbol_ref)
}

Expand Down
21 changes: 21 additions & 0 deletions kclvm/tools/src/LSP/src/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub(crate) fn hover(
},
_ => {}
},
kclvm_sema::core::symbol::SymbolKind::Expression => return None,
_ => {
let ty_str = match &obj.get_sema_info().ty {
Some(ty) => ty.ty_str(),
Expand Down Expand Up @@ -448,4 +449,24 @@ mod tests {
_ => unreachable!("test error"),
}
}

#[test]
#[bench_test]
fn schema_scope_variable_hover() {
let (file, program, _, _, gs) = compile_test_file("src/test_data/hover_test/fib.k");
let pos = KCLPos {
filename: file.clone(),
line: 3,
column: Some(11),
};
let got = hover(&program, &pos, &gs).unwrap();
match got.contents {
lsp_types::HoverContents::Scalar(marked_string) => {
if let MarkedString::String(s) = marked_string {
assert_eq!(s, "n1: int");
}
}
_ => unreachable!("test error"),
}
}
}
16 changes: 8 additions & 8 deletions kclvm/tools/src/LSP/src/test_data/hover_test/fib.k
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ schema Fib:
n1 = n - 1
n2 = n1 - 1
n: int
value: int
# value: int

if n <= 1:
value = 1
elif n == 2:
value = 1
else:
value = Fib {n = n1}.value + Fib {n = n2}.value
# if n <= 1:
# value = 1
# elif n == 2:
# value = 1
# else:
# value = Fib {n = n1}.value + Fib {n = n2}.value

fib8 = Fib {n = 8}.value
# fib8 = Fib {n = 8}.value

0 comments on commit 337af62

Please sign in to comment.