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

feat: lsp find_def for dict type #683

Merged
merged 2 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 20 additions & 12 deletions kclvm/sema/src/resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,26 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for Resolver<'ctx> {
self.ctx.l_value = true;
let expected_ty = self.walk_identifier_expr(target);
self.ctx.l_value = false;
if let TypeKind::Schema(ty) = &expected_ty.kind {
let obj = self.new_config_expr_context_item(
&ty.name,
expected_ty.clone(),
start.clone(),
end.clone(),
);
let init_stack_depth = self.switch_config_expr_context(Some(obj));
value_ty = self.expr(&assign_stmt.value);
self.clear_config_expr_context(init_stack_depth as usize, false)
} else {
value_ty = self.expr(&assign_stmt.value);
match &expected_ty.kind {
TypeKind::Schema(ty) => {
let obj = self.new_config_expr_context_item(
&ty.name,
expected_ty.clone(),
start.clone(),
end.clone(),
);
let init_stack_depth = self.switch_config_expr_context(Some(obj));
value_ty = self.expr(&assign_stmt.value);
self.clear_config_expr_context(init_stack_depth as usize, false)
}
TypeKind::Dict(_) => {
value_ty = self.expr(&assign_stmt.value);
// update attrs
self.set_type_to_scope(name, value_ty.clone(), target.get_span_pos());
}
_ => {
value_ty = self.expr(&assign_stmt.value);
}
}
self.must_assignable_to(
value_ty.clone(),
Expand Down
50 changes: 31 additions & 19 deletions kclvm/tools/src/LSP/src/goto_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,30 +200,42 @@ pub(crate) fn resolve_var(
_ => {
let name = names[0].clone();
match current_scope.lookup(&name) {
Some(obj) => {
match &obj.borrow().ty.kind {
kclvm_sema::ty::TypeKind::Schema(schema_type) => {
find_attr_in_schema(schema_type, &node_names[1..], scope_map)
Some(obj) => match &obj.borrow().ty.kind {
kclvm_sema::ty::TypeKind::Schema(schema_type) => {
find_attr_in_schema(schema_type, &node_names[1..], scope_map)
}
kclvm_sema::ty::TypeKind::Module(module_ty) => {
match scope_map.get(&pkgpath_without_prefix!(module_ty.pkgpath)) {
Some(scope) => {
return resolve_var(&node_names[1..], &scope.borrow(), scope_map);
}
None => None,
}
kclvm_sema::ty::TypeKind::Module(module_ty) => {
match scope_map.get(&pkgpath_without_prefix!(module_ty.pkgpath)) {
Some(scope) => {
return resolve_var(
&node_names[1..],
&scope.borrow(),
scope_map,
);
}
kclvm_sema::ty::TypeKind::Dict(DictType { attrs, .. }) => {
let key_name = names[1].clone();
match attrs.get(&key_name) {
Some(attr) => {
let start_pos = attr.range.0.clone();
for (_, scope) in scope_map {
match scope.borrow().inner_most(&start_pos) {
Some(inner_most_scope) => {
return resolve_var(
&node_names[1..],
&inner_most_scope,
scope_map,
)
}
None => continue,
}
}
None => None,
None
}
None => None,
}
kclvm_sema::ty::TypeKind::Dict(DictType { attrs: _, .. }) => {
// Todo: find key def in dict
None
}
_ => None,
}
}
_ => None,
},
None => None,
}
}
Expand Down
11 changes: 9 additions & 2 deletions kclvm/tools/src/LSP/src/test_data/goto_def_test/goto_def.k
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ schema Person3:


schema Name:
name: str
name: str

schema Person:
n: Name
Expand Down Expand Up @@ -49,4 +49,11 @@ schema Fib:
value = 1
else:
value = Fib {n = n1}.value + Fib {n = n2}.value
fib8 = Fib {n = 8}.value
fib8 = Fib {n = 8}.value

schema_map: {str: Person} = {
person: p2
person1: p2
}

p3 = schema_map.person.n.name
45 changes: 28 additions & 17 deletions kclvm/tools/src/LSP/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,24 +444,35 @@ fn goto_schema_def_test() {
);
}

// todo
// #[test]
// fn goto_dict_key_def_test() {
// let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
#[test]
fn goto_dict_key_def_test() {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));

// let (file, program, prog_scope, _) =
// compile_test_file("src/test_data/goto_def_test/goto_def.k");
let (file, program, prog_scope, _) =
compile_test_file("src/test_data/goto_def_test/goto_def.k");

// let mut expected_path = path;
// expected_path.push("src/test_data/goto_def_test/pkg/schema_def.k");
let mut expected_path = path;
expected_path.push("src/test_data/goto_def_test/pkg/schema_def.k");

// let pos = KCLPos {
// filename: file,
// line: 30,
// column: Some(11),
// };
// let res = goto_definition(&program, &pos, &prog_scope);
// }
let pos = KCLPos {
filename: file.clone(),
line: 26,
column: Some(24),
};
let res = goto_definition(&program, &pos, &prog_scope);
compare_goto_res(
res,
(&expected_path.to_str().unwrap().to_string(), 8, 4, 8, 8),
);

let pos = KCLPos {
filename: file.clone(),
line: 59,
column: Some(28),
};
let res = goto_definition(&program, &pos, &prog_scope);
compare_goto_res(res, (&file, 18, 4, 18, 8));
}

#[test]
fn goto_schema_attr_def_test() {
Expand Down Expand Up @@ -505,7 +516,7 @@ fn goto_schema_attr_def_test1() {
let res = goto_definition(&program, &pos, &prog_scope);
compare_goto_res(
res,
(&expected_path.to_str().unwrap().to_string(), 18, 1, 18, 5),
(&expected_path.to_str().unwrap().to_string(), 18, 4, 18, 8),
);
}

Expand Down Expand Up @@ -552,7 +563,7 @@ fn test_goto_identifier_names() {
let res = goto_definition(&program, &pos, &prog_scope);
compare_goto_res(
res,
(&expected_path.to_str().unwrap().to_string(), 18, 1, 18, 5),
(&expected_path.to_str().unwrap().to_string(), 18, 4, 18, 8),
);
}

Expand Down
Loading