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 goto attr def from attr def itself, add test cases on goto-def and find-refs #765

Merged
merged 3 commits into from
Oct 13, 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
47 changes: 47 additions & 0 deletions kclvm/tools/src/LSP/src/find_refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,51 @@ mod tests {
Err(_) => assert!(false, "file not found"),
}
}

#[test]
fn find_refs_from_schema_attr_test() {
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let mut path = root.clone();
path.push("src/test_data/find_refs_test/main.k");
let path = path.to_str().unwrap();
match lsp_types::Url::from_file_path(path) {
Ok(url) => {
let def_loc = Location {
uri: url.clone(),
range: Range {
start: Position::new(5, 4),
end: Position::new(5, 8),
},
};
let expect = vec![
Location {
uri: url.clone(),
range: Range {
start: Position::new(5, 4),
end: Position::new(5, 8),
},
},
Location {
uri: url.clone(),
range: Range {
start: Position::new(12, 8),
end: Position::new(12, 12),
},
},
];
check_locations_match(
expect,
find_refs(
None,
setup_word_index_map(path),
def_loc,
"name".to_string(),
path.to_string(),
logger,
),
);
}
Err(_) => assert!(false, "file not found"),
}
}
}
18 changes: 16 additions & 2 deletions kclvm/tools/src/LSP/src/goto_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1003,8 +1003,6 @@ mod tests {
#[test]
#[bench_test]
fn complex_select_goto_def() {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));

let (file, program, prog_scope, _) =
compile_test_file("src/test_data/goto_def_test/goto_def.k");

Expand All @@ -1017,4 +1015,20 @@ mod tests {
let res = goto_definition(&program, &pos, &prog_scope);
compare_goto_res(res, (&file, 43, 4, 43, 9));
}

#[test]
#[bench_test]
fn schema_attribute_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: 19,
column: Some(5),
};

let res = goto_definition(&program, &pos, &prog_scope);
compare_goto_res(res, (&file, 18, 4, 18, 8));
}
}
18 changes: 18 additions & 0 deletions kclvm/tools/src/LSP/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,24 @@ pub(crate) fn inner_most_expr_in_stmt(
(None, schema_def)
}
Stmt::SchemaAttr(schema_attr_expr) => {
walk_if_contains!(
Node::node_with_pos(
Expr::Identifier(Identifier {
names: vec![*schema_attr_expr.name.clone()],
pkgpath: "".to_string(),
ctx: kclvm_ast::ast::ExprContext::Load,
}),
(
schema_attr_expr.name.filename.clone(),
schema_attr_expr.name.line,
schema_attr_expr.name.column,
schema_attr_expr.name.end_line,
schema_attr_expr.name.end_column,
),
),
pos,
schema_def
);
if schema_attr_expr.ty.contains_pos(pos) {
return (
build_identifier_from_ty_string(&schema_attr_expr.ty, pos),
Expand Down
Loading