Skip to content

Commit

Permalink
fix: fix wrong dot and assign complete after str ty
Browse files Browse the repository at this point in the history
Signed-off-by: he1pa <[email protected]>
  • Loading branch information
He1pa committed Dec 6, 2023
1 parent 98ebeac commit 058f334
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 8 deletions.
26 changes: 23 additions & 3 deletions kclvm/tools/src/LSP/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ fn completion_dot(
}

// look_up_exact_symbol
let mut def = find_def_with_gs(&pre_pos, gs, true);
let mut def = find_def_with_gs(&pre_pos, gs, true, true);
if def.is_none() {
// look_up_closest_symbol
def = find_def_with_gs(pos, gs, false);
def = find_def_with_gs(pos, gs, false, true);
}
match def {
Some(def_ref) => {
Expand Down Expand Up @@ -270,7 +270,7 @@ fn completion_dot(
/// Now, just completion for schema attr value
fn completion_assign(pos: &KCLPos, gs: &GlobalState) -> Option<lsp_types::CompletionResponse> {
let mut items = IndexSet::new();
if let Some(symbol_ref) = find_def_with_gs(pos, gs, false) {
if let Some(symbol_ref) = find_def_with_gs(pos, gs, false, true) {
if let Some(symbol) = gs.get_symbols().get_symbol(symbol_ref) {
if let Some(def) = symbol.get_definition() {
match def.get_kind() {
Expand Down Expand Up @@ -1016,6 +1016,14 @@ mod tests {
};
let expected_labels: Vec<&str> = vec![" subpkg.Person1{}"];
assert_eq!(got_labels, expected_labels);

let pos = KCLPos {
filename: file.to_owned(),
line: 31,
column: Some(10),
};
let got = completion(Some(':'), &program, &pos, &gs);
assert!(got.is_none());
}

#[test]
Expand Down Expand Up @@ -1187,6 +1195,18 @@ mod tests {
CompletionResponse::Array(arr) => assert!(arr.is_empty()),
CompletionResponse::List(_) => panic!("test failed"),
};

// not complete inside literal str
let pos = KCLPos {
filename: file.to_owned(),
line: 4,
column: Some(4),
};
let got = completion(Some('.'), &program, &pos, &gs).unwrap();
match got {
CompletionResponse::Array(arr) => assert!(arr.is_empty()),
CompletionResponse::List(_) => panic!("test failed"),
};
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion kclvm/tools/src/LSP/src/find_refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub(crate) fn find_refs<F: Fn(String) -> Result<(), anyhow::Error>>(
gs: &GlobalState,
module_cache: Option<KCLModuleCache>,
) -> Result<Vec<Location>, String> {
let def = find_def_with_gs(kcl_pos, gs, true);
let def = find_def_with_gs(kcl_pos, gs, true, true);
match def {
Some(def_ref) => match gs.get_symbols().get_symbol(def_ref) {
Some(obj) => {
Expand Down
15 changes: 13 additions & 2 deletions kclvm/tools/src/LSP/src/goto_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub(crate) fn goto_definition_with_gs(
gs: &GlobalState,
) -> Option<lsp_types::GotoDefinitionResponse> {
let mut res = IndexSet::new();
let def = find_def_with_gs(kcl_pos, gs, true);
let def = find_def_with_gs(kcl_pos, gs, true, true);
match def {
Some(def_ref) => match gs.get_symbols().get_symbol(def_ref) {
Some(def) => match def_ref.get_kind() {
Expand Down Expand Up @@ -52,6 +52,7 @@ pub(crate) fn find_def_with_gs(
kcl_pos: &KCLPos,
gs: &GlobalState,
exact: bool,
within_same_line: bool,
) -> Option<SymbolRef> {
if exact {
match gs.look_up_exact_symbol(kcl_pos) {
Expand All @@ -64,7 +65,17 @@ pub(crate) fn find_def_with_gs(
} else {
match gs.look_up_closest_symbol(kcl_pos) {
Some(symbol_ref) => match gs.get_symbols().get_symbol(symbol_ref) {
Some(symbol) => symbol.get_definition(),
Some(symbol) => {
if within_same_line {
if symbol.get_range().0.line == kcl_pos.line {
symbol.get_definition()
} else {
None
}
} else {
symbol.get_definition()
}
}
None => None,
},
None => None,
Expand Down
2 changes: 1 addition & 1 deletion kclvm/tools/src/LSP/src/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub(crate) fn hover(
gs: &GlobalState,
) -> Option<lsp_types::Hover> {
let mut docs: Vec<String> = vec![];
let def = find_def_with_gs(kcl_pos, gs, true);
let def = find_def_with_gs(kcl_pos, gs, true, true);
match def {
Some(def_ref) => match gs.get_symbols().get_symbol(def_ref) {
Some(obj) => match def_ref.get_kind() {
Expand Down
2 changes: 1 addition & 1 deletion kclvm/tools/src/LSP/src/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ pub fn rename_symbol(
) {
let kcl_pos = kcl_pos(&p, loc.range.start);
if let Some(symbol_ref) =
find_def_with_gs(&kcl_pos, &gs, true)
find_def_with_gs(&kcl_pos, &gs, true, true)
{
if let Some(symbol_def) =
gs.get_symbols().get_symbol(symbol_ref)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ p: Person2{

g # complete `subpkg.Person1{}`
}

schema Person3:
name: str
check:
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
a = "aaa"
"aaa"

# a

0 comments on commit 058f334

Please sign in to comment.