find references on test decl with identifier name (#1051)

This commit is contained in:
Techatrix 2023-03-12 06:24:54 +00:00 committed by GitHub
parent ab23ff3616
commit a959f161e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 13 deletions

View File

@ -276,7 +276,9 @@ pub fn isSnakeCase(name: []const u8) bool {
pub fn getDeclNameToken(tree: Ast, node: Ast.Node.Index) ?Ast.TokenIndex { pub fn getDeclNameToken(tree: Ast, node: Ast.Node.Index) ?Ast.TokenIndex {
const tags = tree.nodes.items(.tag); const tags = tree.nodes.items(.tag);
const datas = tree.nodes.items(.data);
const main_token = tree.nodes.items(.main_token)[node]; const main_token = tree.nodes.items(.main_token)[node];
return switch (tags[node]) { return switch (tags[node]) {
// regular declaration names. + 1 to mut token because name comes after 'const'/'var' // regular declaration names. + 1 to mut token because name comes after 'const'/'var'
.local_var_decl, .local_var_decl,
@ -307,21 +309,23 @@ pub fn getDeclNameToken(tree: Ast, node: Ast.Node.Index) ?Ast.TokenIndex {
.identifier => main_token, .identifier => main_token,
.error_value => main_token + 2, // 'error'.<main_token +2> .error_value => main_token + 2, // 'error'.<main_token +2>
// lhs of main token is name token, so use `node` - 1 .test_decl => datas[node].lhs,
.test_decl => if (tree.tokens.items(.tag)[main_token + 1] == .string_literal)
return main_token + 1
else
null,
else => null, else => null,
}; };
} }
pub fn getDeclName(tree: Ast, node: Ast.Node.Index) ?[]const u8 { pub fn getDeclName(tree: Ast, node: Ast.Node.Index) ?[]const u8 {
const name = tree.tokenSlice(getDeclNameToken(tree, node) orelse return null); const name_token = getDeclNameToken(tree, node) orelse return null;
return switch (tree.nodes.items(.tag)[node]) { const name = offsets.tokenToSlice(tree, name_token);
.test_decl => name[1 .. name.len - 1],
else => name, if (tree.nodes.items(.tag)[node] == .test_decl and
}; tree.tokens.items(.tag)[name_token] == .string_literal)
{
return name[1 .. name.len - 1];
}
return name;
} }
fn resolveVarDeclAliasInternal(arena: std.mem.Allocator, store: *DocumentStore, node_handle: NodeWithHandle, root: bool) error{OutOfMemory}!?DeclWithHandle { fn resolveVarDeclAliasInternal(arena: std.mem.Allocator, store: *DocumentStore, node_handle: NodeWithHandle, root: bool) error{OutOfMemory}!?DeclWithHandle {

View File

@ -91,12 +91,15 @@ const Builder = struct {
const node_tags = tree.nodes.items(.tag); const node_tags = tree.nodes.items(.tag);
const datas = tree.nodes.items(.data); const datas = tree.nodes.items(.data);
const main_tokens = tree.nodes.items(.main_token); const token_tags = tree.tokens.items(.tag);
const starts = tree.tokens.items(.start); const starts = tree.tokens.items(.start);
switch (node_tags[node]) { switch (node_tags[node]) {
.identifier => { .identifier,
const identifier_token = main_tokens[node]; .test_decl,
=> {
const identifier_token = analysis.getDeclNameToken(tree, node).?;
if (token_tags[identifier_token] != .identifier) return;
const child = (try analysis.lookupSymbolGlobal( const child = (try analysis.lookupSymbolGlobal(
builder.allocator, builder.allocator,

View File

@ -105,6 +105,13 @@ test "references - while continue expression" {
); );
} }
test "references - test with identifier" {
try testReferences(
\\pub fn <0>() bool {}
\\test <0> {}
);
}
test "references - label" { test "references - label" {
if (true) return error.SkipZigTest; // TODO if (true) return error.SkipZigTest; // TODO
try testReferences( try testReferences(