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 completion for config expr #688

Merged
merged 2 commits into from
Aug 30, 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
28 changes: 27 additions & 1 deletion kclvm/tools/src/LSP/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub(crate) fn get_completion(
pos: &KCLPos,
prog_scope: &ProgramScope,
) -> IndexSet<KCLCompletionItem> {
let expr = inner_most_expr_in_stmt(&stmt.node, pos, None).0;
let (expr, parent) = inner_most_expr_in_stmt(&stmt.node, pos, None);
match expr {
Some(node) => {
let mut items: IndexSet<KCLCompletionItem> = IndexSet::new();
Expand Down Expand Up @@ -233,6 +233,32 @@ pub(crate) fn get_completion(
});
}
}
Expr::Config(config_expr) => match parent {
Some(schema_expr) => {
if let Expr::Schema(schema_expr) = schema_expr.node {
let schema_def =
find_def(stmt, &schema_expr.name.get_end_pos(), prog_scope);
if let Some(schema) = schema_def {
match schema {
Definition::Object(obj) => {
let schema_type = obj.ty.into_schema_type();
items.extend(
schema_type
.attrs
.keys()
.map(|s| KCLCompletionItem {
label: s.to_string(),
})
.collect::<IndexSet<KCLCompletionItem>>(),
);
}
Definition::Scope(_) => {}
}
}
}
}
None => {}
},
_ => {}
}

Expand Down
1 change: 1 addition & 0 deletions kclvm/tools/src/LSP/src/goto_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ pub(crate) fn find_def(
}

let (inner_expr, parent) = inner_most_expr_in_stmt(&node.node, kcl_pos, None);

if let Some(expr) = inner_expr {
if let Expr::Identifier(id) = expr.node {
let id_node = Node::node_with_pos(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ p4 = Person{

}

schema P:
a: int = 1

aaaa = P{}.
21 changes: 19 additions & 2 deletions kclvm/tools/src/LSP/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ fn var_completion_test() {
items.extend(
vec![
"", // generate from error recovery of "pkg."
"subpkg", "math", "Person", "p", "p1", "p2", "p3", "p4",
"subpkg", "math", "Person", "P", "p", "p1", "p2", "p3", "p4", "aaaa",
]
.iter()
.map(|name| KCLCompletionItem {
Expand Down Expand Up @@ -994,7 +994,7 @@ fn dot_completion_test() {

// test completion for literal str builtin function
let pos = KCLPos {
filename: file,
filename: file.clone(),
line: 21,
column: Some(4),
};
Expand All @@ -1007,7 +1007,24 @@ fn dot_completion_test() {
});
}
let expect: CompletionResponse = into_completion_items(&items).into();
items.clear();

assert_eq!(got, expect);

let pos = KCLPos {
filename: file,
line: 30,
column: Some(11),
};

let got = completion(Some('.'), &program, &pos, &prog_scope).unwrap();
items.insert(KCLCompletionItem {
label: "__settings__".to_string(),
});
items.insert(KCLCompletionItem {
label: "a".to_string(),
});
let expect: CompletionResponse = into_completion_items(&items).into();
assert_eq!(got, expect);
}

Expand Down