Skip to content

Commit

Permalink
Fix goto attr def from attr def itself, add test cases on goto-def an…
Browse files Browse the repository at this point in the history
…d find-refs (#765)

* add test case on attribute def

Signed-off-by: xiarui.xr <[email protected]>

* fix find refs from schema attr def

Signed-off-by: xiarui.xr <[email protected]>

* chore: code clean

Signed-off-by: xiarui.xr <[email protected]>

---------

Signed-off-by: xiarui.xr <[email protected]>
  • Loading branch information
amyXia1994 authored Oct 13, 2023
1 parent 61a6640 commit cabe3bf
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
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

0 comments on commit cabe3bf

Please sign in to comment.