Skip to content

Commit

Permalink
fix: fix sema info about config entry left key and right value
Browse files Browse the repository at this point in the history
  • Loading branch information
He1pa committed Aug 5, 2024
1 parent 96b6ae8 commit 4c83c95
Show file tree
Hide file tree
Showing 15 changed files with 200 additions and 48 deletions.
10 changes: 6 additions & 4 deletions kclvm/loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ pub enum ScopeKind {
Lambda,
SchemaDef,
SchemaConfig,
Value,
SchemaConfigRightValue,
SchemaConfigLeftKey,
Check,
Callable,
}
Expand Down Expand Up @@ -254,9 +255,10 @@ impl From<LocalSymbolScopeKind> for ScopeKind {
LocalSymbolScopeKind::Lambda => ScopeKind::Lambda,
LocalSymbolScopeKind::SchemaDef => ScopeKind::SchemaDef,
LocalSymbolScopeKind::SchemaConfig => ScopeKind::SchemaConfig,
LocalSymbolScopeKind::Value => ScopeKind::Value,
LocalSymbolScopeKind::SchemaConfigRightValue => ScopeKind::SchemaConfigRightValue,
LocalSymbolScopeKind::Check => ScopeKind::Check,
LocalSymbolScopeKind::Callable => ScopeKind::Callable,
LocalSymbolScopeKind::SchemaConfigLeftKey => ScopeKind::SchemaConfigLeftKey,
}
}
}
Expand All @@ -278,8 +280,8 @@ fn collect_scope_info(
kclvm_sema::core::scope::ScopeKind::Local => {
match scope_data.try_get_local_scope(&scope_ref) {
Some(local) => match local.get_kind() {
LocalSymbolScopeKind::SchemaConfig | LocalSymbolScopeKind::Check => true,
_ => false,
LocalSymbolScopeKind::SchemaConfigRightValue => false,
_ => true,
},
None => false,
}
Expand Down
9 changes: 6 additions & 3 deletions kclvm/sema/src/advanced_resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ pub struct Context<'ctx> {
// which means advanced resolver will will create the corresponding
// ValueSymbol instead of an UnresolvedSymbol
maybe_def: bool,
// whether lookup def in scope owner, default true, only in schema attr value is false
look_up_in_owner: bool,
// whether in schema config right value, affect lookup def
in_schema_config_r_value: bool,
// whether in schema def, affect lookup def
in_schema_def: bool,
}

impl<'ctx> Context<'ctx> {
Expand Down Expand Up @@ -113,7 +115,8 @@ impl<'ctx> AdvancedResolver<'ctx> {
end_pos: Position::dummy_pos(),
cur_node: AstIndex::default(),
maybe_def: false,
look_up_in_owner: true,
in_schema_config_r_value: false,
in_schema_def: false,
},
};
// Scan all scehma symbol
Expand Down
105 changes: 75 additions & 30 deletions kclvm/sema/src/advanced_resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> {
end.clone(),
LocalSymbolScopeKind::SchemaDef,
);
self.ctx.in_schema_def = true;
let cur_scope = *self.ctx.scopes.last().unwrap();
self.gs
.get_scopes_mut()
Expand Down Expand Up @@ -407,7 +408,7 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> {
if has_check {
self.leave_scope();
}

self.ctx.in_schema_def = false;
self.leave_scope();

Ok(Some(schema_symbol))
Expand Down Expand Up @@ -1043,7 +1044,7 @@ impl<'ctx> AdvancedResolver<'ctx> {
cur_scope,
self.get_current_module_info(),
maybe_def,
self.ctx.look_up_in_owner,
self.ctx.in_schema_def || !self.ctx.in_schema_config_r_value,
);
if first_symbol.is_none() {
// Maybe import package symbol
Expand Down Expand Up @@ -1083,29 +1084,61 @@ impl<'ctx> AdvancedResolver<'ctx> {
match first_symbol {
Some(symbol_ref) => {
let (start_pos, end_pos): Range = first_name.get_span_pos();
let (def_start_pos, def_end_pos) = self
let def_symbol = self
.gs
.get_symbols()
.get_symbol(symbol_ref)
.ok_or(anyhow!("first name symbol not found"))?
.get_range();
.ok_or(anyhow!("first name symbol not found"))?;
let (def_start_pos, def_end_pos) = def_symbol.get_range();

let cur_scope = *self.ctx.scopes.last().unwrap();
let ast_id = first_name.id.clone();
let mut first_unresolved = UnresolvedSymbol::new(
first_name.node.clone(),
start_pos.clone(),
end_pos.clone(),
None,
);
let name = def_symbol.get_name();
first_unresolved.def = Some(symbol_ref);
let first_unresolved_ref = self.gs.get_symbols_mut().alloc_unresolved_symbol(
first_unresolved,
self.ctx.get_node_key(&ast_id),
self.ctx.current_pkgpath.clone().unwrap(),
);

match cur_scope.get_kind() {
crate::core::scope::ScopeKind::Local => {
let local_scope = self
.gs
.get_scopes()
.try_get_local_scope(&cur_scope)
.unwrap();
match local_scope.get_kind() {
LocalSymbolScopeKind::SchemaConfigLeftKey => {
if let crate::core::symbol::SymbolKind::Attribute =
symbol_ref.get_kind()
{
let parent = local_scope.parent;
self.gs.get_scopes_mut().add_def_to_scope(
parent,
name,
first_unresolved_ref,
);
}
}
_ => {}
}
}
_ => {}
}

// Get an unresolved symbol
if def_start_pos != start_pos || def_end_pos != end_pos {
let ast_id = first_name.id.clone();
let mut first_unresolved =
UnresolvedSymbol::new(first_name.node.clone(), start_pos, end_pos, None);
first_unresolved.def = Some(symbol_ref);
let first_unresolved_ref = self.gs.get_symbols_mut().alloc_unresolved_symbol(
first_unresolved,
self.ctx.get_node_key(&ast_id),
self.ctx.current_pkgpath.clone().unwrap(),
);
let cur_scope = *self.ctx.scopes.last().unwrap();
self.gs
.get_scopes_mut()
.add_ref_to_scope(cur_scope, first_unresolved_ref);
}

if names.len() > 1 {
let mut parent_ty = match self
.ctx
Expand Down Expand Up @@ -1251,7 +1284,7 @@ impl<'ctx> AdvancedResolver<'ctx> {
cur_scope,
self.get_current_module_info(),
true,
self.ctx.look_up_in_owner,
self.ctx.in_schema_def || !self.ctx.in_schema_config_r_value,
);
match first_symbol {
Some(symbol_ref) => {
Expand Down Expand Up @@ -1767,7 +1800,7 @@ impl<'ctx> AdvancedResolver<'ctx> {
let schema_symbol = self.ctx.schema_symbol_stack.last().unwrap_or(&None).clone();
let kind = match &schema_symbol {
Some(_) => LocalSymbolScopeKind::SchemaConfig,
None => LocalSymbolScopeKind::Value,
None => LocalSymbolScopeKind::SchemaConfigRightValue,
};

self.enter_local_scope(
Expand All @@ -1785,26 +1818,38 @@ impl<'ctx> AdvancedResolver<'ctx> {
}

for entry in entries.iter() {
if let Some(key) = &entry.node.key {
self.ctx.maybe_def = true;
self.ctx.look_up_in_owner = true;
self.expr(key)?;
self.ctx.look_up_in_owner = false;
self.ctx.maybe_def = false;
}

let (start, end) = entry.node.value.get_span_pos();

self.enter_local_scope(
&self.ctx.current_filename.as_ref().unwrap().clone(),
start,
end,
LocalSymbolScopeKind::Value,
LocalSymbolScopeKind::SchemaConfigRightValue,
);
self.ctx.look_up_in_owner = false;
self.ctx.in_schema_config_r_value = true;
self.expr(&entry.node.value)?;
self.ctx.look_up_in_owner = true;
self.ctx.in_schema_config_r_value = false;
self.leave_scope();

if let Some(key) = &entry.node.key {
self.ctx.maybe_def = true;
self.ctx.in_schema_config_r_value = false;
self.enter_local_scope(
&self.ctx.current_filename.as_ref().unwrap().clone(),
key.get_pos(),
key.get_end_pos(),
LocalSymbolScopeKind::SchemaConfigLeftKey,
);
if let Some(owner) = schema_symbol {
let cur_scope = self.ctx.scopes.last().unwrap();
self.gs
.get_scopes_mut()
.set_owner_to_scope(*cur_scope, owner);
}
self.expr(key)?;
self.leave_scope();
self.ctx.in_schema_config_r_value = true;
self.ctx.maybe_def = false;
}
}
self.leave_scope();
Ok(())
Expand Down
13 changes: 6 additions & 7 deletions kclvm/sema/src/core/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ impl GlobalState {
local: bool,
get_def_from_owner: bool,
) -> Option<SymbolRef> {
match self.scopes.get_scope(&scope_ref)?.look_up_def(
let scope = self.scopes.get_scope(&scope_ref)?;
match scope.look_up_def(
name,
&self.scopes,
&self.symbols,
Expand Down Expand Up @@ -187,9 +188,8 @@ impl GlobalState {
let get_def_from_owner = match scope_ref.kind {
ScopeKind::Local => match scopes.try_get_local_scope(&scope_ref) {
Some(local) => match local.kind {
super::scope::LocalSymbolScopeKind::SchemaConfig
| super::scope::LocalSymbolScopeKind::Check => true,
_ => false,
super::scope::LocalSymbolScopeKind::SchemaConfigRightValue => false,
_ => true,
},
None => true,
},
Expand Down Expand Up @@ -228,9 +228,8 @@ impl GlobalState {
let get_def_from_owner = match scope_ref.kind {
ScopeKind::Local => match scopes.try_get_local_scope(&scope_ref) {
Some(local) => match local.kind {
super::scope::LocalSymbolScopeKind::SchemaConfig
| super::scope::LocalSymbolScopeKind::Check => true,
_ => false,
super::scope::LocalSymbolScopeKind::SchemaConfigRightValue => false,
_ => true,
},
None => false,
},
Expand Down
11 changes: 7 additions & 4 deletions kclvm/sema/src/core/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,8 @@ pub enum LocalSymbolScopeKind {
Lambda,
SchemaDef,
SchemaConfig,
Value,
SchemaConfigRightValue,
SchemaConfigLeftKey,
Check,
Callable,
}
Expand Down Expand Up @@ -549,13 +550,15 @@ impl Scope for LocalSymbolScope {
// #Root[
// b = "bar"
// foo = Foo #SchemaConfig[{
// bar: #Value[b]
// #SchemaConfigLeftKey[bar]: #SchemaConfigRightValue[b]
// }]
// ]
// ````
// At position of `bar`, the scope kind is SchemaConfig, only get the definition of bar.
// At position of `bar`, the scope kind is SchemaConfigLeftKey, only get the definition of bar.
// At position of seconde `b`, the scope is the child scope of SchemaConfig, need to recursively find the definition of `b`` at a higher level
if self.kind == LocalSymbolScopeKind::SchemaConfig && !recursive {
if self.kind == LocalSymbolScopeKind::SchemaConfig && !recursive
|| self.kind == LocalSymbolScopeKind::SchemaConfigLeftKey
{
return all_defs_map;
} else {
for def_ref in self.defs.values() {
Expand Down
24 changes: 24 additions & 0 deletions kclvm/tools/src/LSP/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2064,4 +2064,28 @@ mod tests {
11,
None
);

completion_label_test_snapshot!(
schema_arg_1,
"src/test_data/completion_test/schema_def/schema_def.k",
10,
22,
None
);

completion_label_test_snapshot!(
schema_arg_2,
"src/test_data/completion_test/schema_def/schema_def.k",
12,
5,
None
);

completion_label_test_snapshot!(
schema_arg_3,
"src/test_data/completion_test/schema_def/schema_def.k",
13,
8,
None
);
}
21 changes: 21 additions & 0 deletions kclvm/tools/src/LSP/src/goto_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,4 +469,25 @@ mod tests {
8,
11
);

goto_def_test_snapshot!(
goto_attr_in_schema_def_1,
"src/test_data/goto_def_test/goto_attr_in_schema_def/goto_attr_in_schema_def.k",
9,
14
);

goto_def_test_snapshot!(
goto_attr_in_schema_def_2,
"src/test_data/goto_def_test/goto_attr_in_schema_def/goto_attr_in_schema_def.k",
10,
14
);

goto_def_test_snapshot!(
goto_attr_in_schema_def_3,
"src/test_data/goto_def_test/goto_attr_in_schema_def/goto_attr_in_schema_def.k",
11,
14
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: tools/src/LSP/src/completion.rs
expression: "format!(\"{:?}\", got_labels)"
---
["Config", "Config{}", "Container", "Container{}", "Server", "Server(inputConfig){}", "config", "inputConfig", "mainContainer"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: tools/src/LSP/src/completion.rs
expression: "format!(\"{:?}\", got_labels)"
---
["Config", "Config{}", "Container", "Container{}", "Server", "Server(inputConfig){}", "config", "inputConfig", "mainContainer"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: tools/src/LSP/src/completion.rs
expression: "format!(\"{:?}\", got_labels)"
---
["Config", "Config{}", "Container", "Container{}", "Server", "Server(inputConfig){}", "config", "inputConfig", "mainContainer"]
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_attr_in_schema_def/goto_attr_in_schema_def.k\", range: Range { start: Position { line: 5, character: 0 }, end: Position { line: 5, character: 5 } }"
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_attr_in_schema_def/goto_attr_in_schema_def.k\", range: Range { start: Position { line: 8, character: 4 }, end: Position { line: 8, character: 9 } }"
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_attr_in_schema_def/goto_attr_in_schema_def.k\", range: Range { start: Position { line: 9, character: 4 }, end: Position { line: 9, character: 9 } }"
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
schema Container:
name: str


schema Config:
mainContainer: Container
image: str

schema Server[inputConfig: Config]:
config: Config =
mainContainer: {str:}

if
Loading

0 comments on commit 4c83c95

Please sign in to comment.