Skip to content

Commit

Permalink
fix: schema attribute symbol defined in the unification stmt (#1516)
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <xpf6677@163.com>
  • Loading branch information
Peefy committed Jul 23, 2024
1 parent 6385e30 commit 988b907
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 14 deletions.
55 changes: 55 additions & 0 deletions kclvm/sema/src/advanced_resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
42 changes: 28 additions & 14 deletions kclvm/sema/src/namer/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions kclvm/tools/src/LSP/src/goto_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Original file line number Diff line number Diff line change
@@ -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 } }"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
schema Metadata:

schema Object:
metadata: Metadata {}

o = Object {
metadata: {}
}

0 comments on commit 988b907

Please sign in to comment.