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 position error when desuger config expr #772

Merged
merged 1 commit into from
Oct 16, 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
37 changes: 28 additions & 9 deletions kclvm/sema/src/pre_process/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,30 @@ impl ConfigNestAttrTransformer {
pub fn walk_config_entry(&mut self, config_entry: &mut Box<ast::Node<ast::ConfigEntry>>) {
if let Some(key) = config_entry.node.key.as_mut() {
if let ast::Expr::Identifier(identifier) = &mut key.node {
// desuger config expr, e.g., desuger
// ```
// foo = Foo {
// bar.baz : xxx
// }
// ```
// to:
// ```
// foo = Foo {
// bar : Bar {
// baz : xxx
// }
// }
// ```
if identifier.names.len() > 1 {
let mut names = identifier.names.clone();
let names = &mut names[1..];
names.reverse();
identifier.names = vec![identifier.names[0].clone()];
key.filename = identifier.names[0].filename.clone();
key.line = identifier.names[0].line;
key.column = identifier.names[0].column;
key.end_line = identifier.names[0].end_line;
key.end_column = identifier.names[0].end_column;

let mut value = config_entry.node.value.clone();
for (i, name) in names.iter().enumerate() {
Expand All @@ -29,11 +48,11 @@ impl ConfigNestAttrTransformer {
let entry_value = ast::ConfigEntry {
key: Some(Box::new(ast::Node::new(
ast::Expr::Identifier(name_node),
key.filename.clone(),
key.line,
key.column,
key.end_line,
key.end_column,
name.filename.clone(),
name.line,
name.column,
name.end_line,
name.end_column,
))),
value: value.clone(),
operation: if is_last_item {
Expand All @@ -47,17 +66,17 @@ impl ConfigNestAttrTransformer {
items: vec![Box::new(ast::Node::new(
entry_value,
config_entry.filename.clone(),
config_entry.line,
config_entry.column,
name.line,
name.column,
config_entry.end_line,
config_entry.end_column,
))],
};
value = Box::new(ast::Node::new(
ast::Expr::Config(config_expr),
value.filename.clone(),
value.line,
value.column,
name.line,
name.column,
value.end_line,
value.end_column,
))
Expand Down
16 changes: 16 additions & 0 deletions kclvm/tools/src/LSP/src/goto_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1031,4 +1031,20 @@ mod tests {
let res = goto_definition(&program, &pos, &prog_scope);
compare_goto_res(res, (&file, 18, 4, 18, 8));
}

#[test]
#[bench_test]
fn config_desuger_def_goto_def() {
let (file, program, prog_scope, _) =
compile_test_file("src/test_data/goto_def_test/goto_def.k");

let pos = KCLPos {
filename: file.clone(),
line: 82,
column: Some(9),
};

let res = goto_definition(&program, &pos, &prog_scope);
compare_goto_res(res, (&file, 81, 6, 81, 10));
}
}
4 changes: 4 additions & 0 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 @@ -77,3 +77,7 @@ items1 = [item | {
"securityContext": {"capabilities": {"add" += [cc] if cc not in (container?.securityContext?.capabilities?.drop or []) else [] for cc in capabilities}}
} for container in item.spec.containers]
} for item in option("items")]

p4 = Person {
n.name: "a"
}
Loading