diff --git a/kclvm/sema/src/advanced_resolver/node.rs b/kclvm/sema/src/advanced_resolver/node.rs index ce8bb7eda..cf04c73b3 100644 --- a/kclvm/sema/src/advanced_resolver/node.rs +++ b/kclvm/sema/src/advanced_resolver/node.rs @@ -52,6 +52,61 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { ) -> Self::Result { self.ctx.maybe_def = true; self.walk_identifier_expr(&unification_stmt.target)?; + // Set schema attribute if it is in the schema stmt. + if let Some(parent_scope) = self.ctx.scopes.last() { + if let Some(parent_scope) = self.gs.get_scopes().get_scope(&parent_scope) { + let mut doc = None; + if let Some(schema_symbol) = parent_scope.get_owner() { + let schema_symbol = self + .gs + .get_symbols() + .get_symbol(schema_symbol) + .ok_or(anyhow!("schema_symbol not found"))?; + if let Some(schema_ty) = schema_symbol.get_sema_info().ty.clone() { + if !unification_stmt.target.node.names.is_empty() { + let schema_ty = schema_ty.into_schema_type(); + if let Some(attr) = schema_ty + .attrs + .get(&unification_stmt.target.node.names[0].node) + { + doc = attr.doc.clone() + } + let attr_symbol = self + .gs + .get_symbols() + .symbols_info + .node_symbol_map + .get( + &self + .ctx + .get_node_key(&unification_stmt.target.node.names[0].id), + ) + .cloned(); + if let Some(attr_symbol) = attr_symbol { + if let Some(symbol) = self + .gs + .get_symbols_mut() + .attributes + .get_mut(attr_symbol.get_id()) + { + symbol.sema_info = SymbolSemanticInfo { + ty: self + .ctx + .node_ty_map + .borrow() + .get(&self.ctx.get_node_key( + &unification_stmt.target.node.names[0].id, + )) + .map(|ty| ty.clone()), + doc, + }; + } + } + } + } + }; + } + } self.ctx.maybe_def = false; self.walk_schema_expr(&unification_stmt.value.node)?; Ok(None) diff --git a/kclvm/sema/src/namer/node.rs b/kclvm/sema/src/namer/node.rs index e07af9545..39f2c79ef 100644 --- a/kclvm/sema/src/namer/node.rs +++ b/kclvm/sema/src/namer/node.rs @@ -59,27 +59,41 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for Namer<'ctx> { .get_fully_qualified_name(owner) .unwrap(); let value_name = unification_stmt.target.node.get_name(); - let value_fully_qualified_name = owner_fully_qualified_name + "." + &value_name; - if !self - .ctx - .value_fully_qualified_name_set - .contains(&value_fully_qualified_name) - { - let value_ref = self.gs.get_symbols_mut().alloc_value_symbol( - ValueSymbol::new(value_name, start_pos, end_pos, Some(owner), true), - self.ctx.get_node_key(&unification_stmt.target.id), + if self.gs.get_symbols().get_schema_symbol(owner).is_some() { + let attribute_ref = self.gs.get_symbols_mut().alloc_attribute_symbol( + AttributeSymbol::new(value_name, start_pos, end_pos, owner, false), + self.ctx + .get_node_key(&unification_stmt.target.node.names[0].id), self.ctx .current_package_info .clone() .unwrap() .fully_qualified_name, ); - self.ctx - .value_fully_qualified_name_set - .insert(value_fully_qualified_name); - Some(vec![value_ref]) + Some(vec![attribute_ref]) } else { - None + let value_fully_qualified_name = owner_fully_qualified_name + "." + &value_name; + if !self + .ctx + .value_fully_qualified_name_set + .contains(&value_fully_qualified_name) + { + let value_ref = self.gs.get_symbols_mut().alloc_value_symbol( + ValueSymbol::new(value_name, start_pos, end_pos, Some(owner), true), + self.ctx.get_node_key(&unification_stmt.target.id), + self.ctx + .current_package_info + .clone() + .unwrap() + .fully_qualified_name, + ); + self.ctx + .value_fully_qualified_name_set + .insert(value_fully_qualified_name); + Some(vec![value_ref]) + } else { + None + } } } else { None diff --git a/kclvm/tools/src/LSP/src/goto_def.rs b/kclvm/tools/src/LSP/src/goto_def.rs index c6d613c1f..b7fc0b76d 100644 --- a/kclvm/tools/src/LSP/src/goto_def.rs +++ b/kclvm/tools/src/LSP/src/goto_def.rs @@ -455,4 +455,11 @@ mod tests { 4, 12 ); + + goto_def_test_snapshot!( + goto_unification_schema_attr_test, + "src/test_data/goto_def_test/goto_unification_schema_attr_test/goto_unification_schema_attr_test.k", + 7, + 7 + ); } diff --git a/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__goto_def__tests__goto_unification_schema_attr_test.snap b/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__goto_def__tests__goto_unification_schema_attr_test.snap new file mode 100644 index 000000000..7b968d1db --- /dev/null +++ b/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__goto_def__tests__goto_unification_schema_attr_test.snap @@ -0,0 +1,5 @@ +--- +source: tools/src/LSP/src/goto_def.rs +expression: "format!(\"{:?}\", { fmt_resp(& res) })" +--- +"path: \"src/test_data/goto_def_test/goto_unification_schema_attr_test/goto_unification_schema_attr_test.k\", range: Range { start: Position { line: 3, character: 4 }, end: Position { line: 3, character: 12 } }" diff --git a/kclvm/tools/src/LSP/src/test_data/goto_def_test/goto_unification_schema_attr_test/goto_unification_schema_attr_test.k b/kclvm/tools/src/LSP/src/test_data/goto_def_test/goto_unification_schema_attr_test/goto_unification_schema_attr_test.k new file mode 100644 index 000000000..a862bb026 --- /dev/null +++ b/kclvm/tools/src/LSP/src/test_data/goto_def_test/goto_unification_schema_attr_test/goto_unification_schema_attr_test.k @@ -0,0 +1,8 @@ +schema Metadata: + +schema Object: + metadata: Metadata {} + +o = Object { + metadata: {} +}