Skip to content

Commit

Permalink
feat: enhance builtin func complete (#1064)
Browse files Browse the repository at this point in the history
* feat: enchance builtin function completion. 1. only complete builtin function in root scope. 2. Modify the label of the function's completion item to avoid fuzzy matching of function parameters to commonly used characters, such as x,y,z, and migrate function parameter descriptions to completion item detail

Signed-off-by: he1pa <[email protected]>

* feat: complete builtin function in lambda scope

Signed-off-by: he1pa <[email protected]>

---------

Signed-off-by: he1pa <[email protected]>
  • Loading branch information
He1pa authored Feb 21, 2024
1 parent cc38aee commit 88f4c2c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 40 deletions.
2 changes: 1 addition & 1 deletion kclvm/sema/src/resolver/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ fn test_resolve_function_with_default_values() {
let main_scope = scope.main_scope().unwrap();
let func = main_scope.borrow().lookup("is_alpha").unwrap();
assert!(func.borrow().ty.is_func());
let func_ty = func.borrow().ty.into_function_ty();
let func_ty = func.borrow().ty.into_func_type();
assert_eq!(func_ty.params.len(), 3);
assert_eq!(func_ty.params[0].has_default, false);
assert_eq!(func_ty.params[1].has_default, true);
Expand Down
7 changes: 0 additions & 7 deletions kclvm/sema/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,6 @@ impl Type {
_ => None,
}
}

pub fn into_function_ty(&self) -> FunctionType {
match &self.kind {
TypeKind::Function(func) => func.clone(),
_ => panic!("Not a function type"),
}
}
}

unsafe impl Send for TypeKind {}
Expand Down
119 changes: 87 additions & 32 deletions kclvm/tools/src/LSP/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use kclvm_config::modfile::KCL_FILE_EXTENSION;
use kclvm_sema::core::global_state::GlobalState;

use kclvm_error::Position as KCLPos;
use kclvm_sema::builtin::{STANDARD_SYSTEM_MODULES, STRING_MEMBER_FUNCTIONS};
use kclvm_sema::builtin::{BUILTIN_FUNCTIONS, STANDARD_SYSTEM_MODULES, STRING_MEMBER_FUNCTIONS};
use kclvm_sema::core::package::ModuleInfo;
use kclvm_sema::resolver::doc::{parse_doc_string, Doc};
use kclvm_sema::ty::{FunctionType, SchemaType, Type};
Expand Down Expand Up @@ -107,8 +107,52 @@ pub(crate) fn completion(
}));
}

// Complete all usable symbol obj in inner most scope
if let Some(scope) = gs.look_up_scope(pos) {
// Complete builtin functions in root scope and lambda
match scope.get_kind() {
kclvm_sema::core::scope::ScopeKind::Local => {
if let Some(locol_scope) = gs.get_scopes().try_get_local_scope(&scope) {
match locol_scope.get_kind() {
kclvm_sema::core::scope::LocalSymbolScopeKind::Lambda => {
completions.extend(BUILTIN_FUNCTIONS.iter().map(
|(name, ty)| KCLCompletionItem {
label: func_ty_complete_label(
name,
&ty.into_func_type(),
),
detail: Some(
ty.into_func_type().func_signature_str(name),
),
documentation: ty.ty_doc(),
kind: Some(KCLCompletionItemKind::Function),
insert_text: Some(func_ty_complete_insert_text(
name,
&ty.into_func_type(),
)),
},
));
}
_ => {}
}
}
}
kclvm_sema::core::scope::ScopeKind::Root => {
completions.extend(BUILTIN_FUNCTIONS.iter().map(|(name, ty)| {
KCLCompletionItem {
label: func_ty_complete_label(name, &ty.into_func_type()),
detail: Some(ty.into_func_type().func_signature_str(name)),
documentation: ty.ty_doc(),
kind: Some(KCLCompletionItemKind::Function),
insert_text: Some(func_ty_complete_insert_text(
name,
&ty.into_func_type(),
)),
}
}));
}
}

// Complete all usable symbol obj in inner most scope
if let Some(defs) = gs.get_all_defs_in_scope(scope) {
for symbol_ref in defs {
match gs.get_symbols().get_symbol(symbol_ref) {
Expand Down Expand Up @@ -138,9 +182,15 @@ pub(crate) fn completion(
});
}
_ => {
let detail = match &ty.kind {
kclvm_sema::ty::TypeKind::Function(func_ty) => {
func_ty.func_signature_str(&name)
}
_ => ty.ty_str(),
};
completions.insert(KCLCompletionItem {
label: name,
detail: Some(ty.ty_str()),
detail: Some(detail),
documentation: sema_info.doc.clone(),
kind: type_to_item_kind(ty),
insert_text: None,
Expand Down Expand Up @@ -190,14 +240,16 @@ fn completion_dot(
.map(|(name, ty)| KCLCompletionItem {
label: func_ty_complete_label(
name,
&ty.into_function_ty(),
&ty.into_func_type(),
),
detail: Some(
ty.into_func_type().func_signature_str(name),
),
detail: Some(ty.ty_str()),
documentation: ty.ty_doc(),
kind: Some(KCLCompletionItemKind::Function),
insert_text: Some(func_ty_complete_insert_text(
name,
&ty.into_function_ty(),
&ty.into_func_type(),
)),
})
.collect(),
Expand Down Expand Up @@ -639,17 +691,8 @@ fn pkg_real_name(pkg: &String, module: &ModuleInfo) -> String {
pkg.split('.').last().unwrap().to_string()
}

fn func_ty_complete_label(func_name: &String, func_type: &FunctionType) -> String {
format!(
"{}({})",
func_name,
func_type
.params
.iter()
.map(|param| param.name.clone())
.collect::<Vec<String>>()
.join(", "),
)
fn func_ty_complete_label(func_name: &String, _func_type: &FunctionType) -> String {
format!("{}(…)", func_name,)
}

fn func_ty_complete_insert_text(func_name: &String, func_type: &FunctionType) -> String {
Expand Down Expand Up @@ -715,7 +758,7 @@ pub(crate) fn into_completion_items(items: &IndexSet<KCLCompletionItem>) -> Vec<
mod tests {
use indexmap::IndexSet;
use kclvm_error::Position as KCLPos;
use kclvm_sema::builtin::{MATH_FUNCTION_TYPES, STRING_MEMBER_FUNCTIONS};
use kclvm_sema::builtin::{BUILTIN_FUNCTIONS, MATH_FUNCTION_TYPES, STRING_MEMBER_FUNCTIONS};
use lsp_types::{CompletionItem, CompletionItemKind, CompletionResponse, InsertTextFormat};
use proc_macro_crate::bench_test;

Expand Down Expand Up @@ -746,11 +789,20 @@ mod tests {
CompletionResponse::List(_) => panic!("test failed"),
};

let mut expected_labels: Vec<&str> = vec![
let mut expected_labels: Vec<String> = vec![
"", // generate from error recovery of "pkg."
"subpkg", "math", "Person", "Person{}", "P", "P{}", "p", "p1", "p2", "p3", "p4",
"aaaa",
];
]
.iter()
.map(|s| s.to_string())
.collect();

expected_labels.extend(
BUILTIN_FUNCTIONS
.iter()
.map(|(name, func)| func_ty_complete_label(name, &func.into_func_type())),
);
got_labels.sort();
expected_labels.sort();

Expand All @@ -769,7 +821,10 @@ mod tests {
CompletionResponse::List(_) => panic!("test failed"),
};

expected_labels = vec!["", "age", "math", "name", "subpkg"];
expected_labels = vec!["", "age", "math", "name", "subpkg"]
.iter()
.map(|s| s.to_string())
.collect();
got_labels.sort();
expected_labels.sort();
assert_eq!(got_labels, expected_labels);
Expand Down Expand Up @@ -811,7 +866,7 @@ mod tests {
};
let expected_labels: Vec<String> = STRING_MEMBER_FUNCTIONS
.iter()
.map(|(name, ty)| func_ty_complete_label(name, &ty.into_function_ty()))
.map(|(name, ty)| func_ty_complete_label(name, &ty.into_func_type()))
.collect();
assert_eq!(got_labels, expected_labels);

Expand All @@ -824,7 +879,7 @@ mod tests {
};
let expected_insert_text: Vec<String> = STRING_MEMBER_FUNCTIONS
.iter()
.map(|(name, ty)| func_ty_complete_insert_text(name, &ty.into_function_ty()))
.map(|(name, ty)| func_ty_complete_insert_text(name, &ty.into_func_type()))
.collect();
assert_eq!(got_insert_text, expected_insert_text);

Expand Down Expand Up @@ -872,7 +927,7 @@ mod tests {
};
let expected_labels: Vec<String> = MATH_FUNCTION_TYPES
.iter()
.map(|(name, ty)| func_ty_complete_label(name, &ty.into_function_ty()))
.map(|(name, ty)| func_ty_complete_label(name, &ty.into_func_type()))
.collect();
assert_eq!(got_labels, expected_labels);

Expand All @@ -891,7 +946,7 @@ mod tests {

let expected_labels: Vec<String> = STRING_MEMBER_FUNCTIONS
.iter()
.map(|(name, ty)| func_ty_complete_label(name, &ty.into_function_ty()))
.map(|(name, ty)| func_ty_complete_label(name, &ty.into_func_type()))
.collect();
assert_eq!(got_labels, expected_labels);

Expand Down Expand Up @@ -947,7 +1002,7 @@ mod tests {
};
let expected_labels: Vec<String> = STRING_MEMBER_FUNCTIONS
.iter()
.map(|(name, ty)| func_ty_complete_label(name, &ty.into_function_ty()))
.map(|(name, ty)| func_ty_complete_label(name, &ty.into_func_type()))
.collect();
assert_eq!(got_labels, expected_labels);

Expand Down Expand Up @@ -995,7 +1050,7 @@ mod tests {
};
let expected_labels: Vec<String> = MATH_FUNCTION_TYPES
.iter()
.map(|(name, ty)| func_ty_complete_label(name, &ty.into_function_ty()))
.map(|(name, ty)| func_ty_complete_label(name, &ty.into_func_type()))
.collect();
assert_eq!(got_labels, expected_labels);

Expand All @@ -1008,7 +1063,7 @@ mod tests {
};
let expected_insert_text: Vec<String> = MATH_FUNCTION_TYPES
.iter()
.map(|(name, ty)| func_ty_complete_insert_text(name, &ty.into_function_ty()))
.map(|(name, ty)| func_ty_complete_insert_text(name, &ty.into_func_type()))
.collect();
assert_eq!(got_insert_text, expected_insert_text);

Expand All @@ -1027,7 +1082,7 @@ mod tests {

let expected_labels: Vec<String> = STRING_MEMBER_FUNCTIONS
.iter()
.map(|(name, ty)| func_ty_complete_label(name, &ty.into_function_ty()))
.map(|(name, ty)| func_ty_complete_label(name, &ty.into_func_type()))
.collect();
assert_eq!(got_labels, expected_labels);

Expand All @@ -1040,7 +1095,7 @@ mod tests {
};
let expected_insert_text: Vec<String> = STRING_MEMBER_FUNCTIONS
.iter()
.map(|(name, ty)| func_ty_complete_insert_text(name, &ty.into_function_ty()))
.map(|(name, ty)| func_ty_complete_insert_text(name, &ty.into_func_type()))
.collect();
assert_eq!(got_insert_text, expected_insert_text);

Expand Down Expand Up @@ -1346,7 +1401,7 @@ mod tests {

let expected_labels: Vec<String> = STRING_MEMBER_FUNCTIONS
.iter()
.map(|(name, ty)| func_ty_complete_label(name, &ty.into_function_ty()))
.map(|(name, ty)| func_ty_complete_label(name, &ty.into_func_type()))
.collect();
assert_eq!(got_labels, expected_labels);

Expand All @@ -1359,7 +1414,7 @@ mod tests {
};
let expected_insert_text: Vec<String> = STRING_MEMBER_FUNCTIONS
.iter()
.map(|(name, ty)| func_ty_complete_insert_text(name, &ty.into_function_ty()))
.map(|(name, ty)| func_ty_complete_insert_text(name, &ty.into_func_type()))
.collect();
assert_eq!(got_insert_text, expected_insert_text);

Expand Down

0 comments on commit 88f4c2c

Please sign in to comment.