Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix sema info about config entry left key and right value #1541

Merged
merged 9 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
He1pa marked this conversation as resolved.
Show resolved Hide resolved
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
16 changes: 8 additions & 8 deletions kclvm/sema/src/advanced_resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ 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,
He1pa marked this conversation as resolved.
Show resolved Hide resolved
}

impl<'ctx> Context<'ctx> {
Expand Down Expand Up @@ -113,7 +113,7 @@ 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,
},
};
// Scan all scehma symbol
Expand Down Expand Up @@ -1407,7 +1407,7 @@ mod tests {
.replace("/", &std::path::MAIN_SEPARATOR.to_string()),
17_u64,
26_u64,
5_usize,
10_usize,
),
// __main__.Main schema expr scope
(
Expand All @@ -1425,7 +1425,7 @@ mod tests {
.replace("/", &std::path::MAIN_SEPARATOR.to_string()),
30,
20,
5,
7,
),
// pkg.Person schema expr scope
(
Expand All @@ -1443,7 +1443,7 @@ mod tests {
.replace("/", &std::path::MAIN_SEPARATOR.to_string()),
34,
17,
5,
6,
),
// __main__ package scope
(
Expand All @@ -1461,7 +1461,7 @@ mod tests {
.replace("/", &std::path::MAIN_SEPARATOR.to_string()),
15,
11,
4,
6,
),
// import_test.a.Name expr scope
(
Expand All @@ -1479,7 +1479,7 @@ mod tests {
.replace("/", &std::path::MAIN_SEPARATOR.to_string()),
12,
21,
4,
8,
),
];

Expand Down
101 changes: 71 additions & 30 deletions kclvm/sema/src/advanced_resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,6 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> {
if has_check {
self.leave_scope();
}

self.leave_scope();

Ok(Some(schema_symbol))
Expand Down Expand Up @@ -1043,7 +1042,7 @@ impl<'ctx> AdvancedResolver<'ctx> {
cur_scope,
self.get_current_module_info(),
maybe_def,
self.ctx.look_up_in_owner,
!self.ctx.in_schema_config_r_value,
);
if first_symbol.is_none() {
// Maybe import package symbol
Expand Down Expand Up @@ -1083,29 +1082,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 +1282,7 @@ impl<'ctx> AdvancedResolver<'ctx> {
cur_scope,
self.get_current_module_info(),
true,
self.ctx.look_up_in_owner,
!self.ctx.in_schema_config_r_value,
);
match first_symbol {
Some(symbol_ref) => {
Expand Down Expand Up @@ -1767,7 +1798,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 +1816,36 @@ 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.enter_local_scope(
&self.ctx.current_filename.as_ref().unwrap().clone(),
key.get_pos(),
key.get_end_pos(),
LocalSymbolScopeKind::SchemaConfigLeftKey,
He1pa marked this conversation as resolved.
Show resolved Hide resolved
);
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.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,
He1pa marked this conversation as resolved.
Show resolved Hide resolved
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
2 changes: 1 addition & 1 deletion kclvm/tools/src/LSP/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dashmap = "5.1.0"
log = "0.4.14"
im-rc = "15.0.0"
rustc_lexer = "0.1.0"
clap ={ version = "4.3.0", features = ["string"] }
clap = { version = "4.3.0", features = ["string"] }
maplit = "1.0.2"

kclvm-tools = { path = "../../../tools" }
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"]
Loading
Loading