Skip to content

Commit

Permalink
bugfix: str type var builtin function hover
Browse files Browse the repository at this point in the history
Signed-off-by: He1pa <[email protected]>
  • Loading branch information
He1pa committed Oct 11, 2023
1 parent c7c851b commit 6e0b09b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
26 changes: 25 additions & 1 deletion kclvm/tools/src/LSP/src/goto_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use kclvm_ast::ast::{Expr, Identifier, ImportStmt, Node, Program, Stmt};
use kclvm_compiler::pkgpath_without_prefix;
use kclvm_error::Position as KCLPos;

use kclvm_sema::builtin::get_system_member_function_ty;
use kclvm_sema::builtin::{get_system_member_function_ty, STRING_MEMBER_FUNCTIONS};
use kclvm_sema::resolver::scope::{
builtin_scope, ProgramScope, Scope, ScopeObject, ScopeObjectKind,
};
Expand Down Expand Up @@ -334,6 +334,30 @@ pub(crate) fn resolve_var(
None => None,
}
}
kclvm_sema::ty::TypeKind::Str => {
if names.len() == 2 {
let func_name_node = node_names[1].clone();
let func_name = func_name_node.node.clone();
match STRING_MEMBER_FUNCTIONS.get(&func_name) {
Some(ty) => match &ty.kind {
kclvm_sema::ty::TypeKind::Function(func_ty) => {
return Some(Definition::Object(ScopeObject {
name: func_name,
start: func_name_node.get_pos(),
end: func_name_node.get_end_pos(),
ty: Rc::new(ty.clone()),
kind: ScopeObjectKind::FunctionCall,
doc: Some(func_ty.doc.clone()),
}))
}
// unreachable
_ => {}
},
None => {}
}
}
None
}
_ => None,
},
None => None,
Expand Down
27 changes: 27 additions & 0 deletions kclvm/tools/src/LSP/src/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,4 +377,31 @@ mod tests {
_ => unreachable!("test error"),
}
}

#[test]
#[bench_test]
fn str_var_func_hover() {
let (file, program, prog_scope, _) = compile_test_file("src/test_data/hover_test/hover.k");
let pos = KCLPos {
filename: file.clone(),
line: 28,
column: Some(12),
};
let got = hover(&program, &pos, &prog_scope).unwrap();
match got.contents {
lsp_types::HoverContents::Array(vec) => {
assert_eq!(vec.len(), 3);
if let MarkedString::String(s) = vec[0].clone() {
assert_eq!(s, "str\n\n");
}
if let MarkedString::String(s) = vec[1].clone() {
assert_eq!(s, "fn capitalize() -> str");
}
if let MarkedString::String(s) = vec[2].clone() {
assert_eq!(s, "Return a copy of the string with its first character capitalized and the rest lowercased.");
}
}
_ => unreachable!("test error"),
}
}
}
3 changes: 3 additions & 0 deletions kclvm/tools/src/LSP/src/test_data/hover_test/hover.k
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ abdc = base64.encode("1")
abcd = "a".count()

print(1)

a = "".capitalize()
b = a.capitalize()

0 comments on commit 6e0b09b

Please sign in to comment.