diff --git a/kclvm/sema/src/advanced_resolver/node.rs b/kclvm/sema/src/advanced_resolver/node.rs index e1f4970a8..2aa28ea9d 100644 --- a/kclvm/sema/src/advanced_resolver/node.rs +++ b/kclvm/sema/src/advanced_resolver/node.rs @@ -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)? diff --git a/kclvm/sema/src/core/symbol.rs b/kclvm/sema/src/core/symbol.rs index fa842283c..3d406bb7d 100644 --- a/kclvm/sema/src/core/symbol.rs +++ b/kclvm/sema/src/core/symbol.rs @@ -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) } diff --git a/kclvm/tools/src/LSP/src/hover.rs b/kclvm/tools/src/LSP/src/hover.rs index 4aa1de38e..867eb264d 100644 --- a/kclvm/tools/src/LSP/src/hover.rs +++ b/kclvm/tools/src/LSP/src/hover.rs @@ -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(), @@ -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"), + } + } }