Skip to content

Commit 0d77561

Browse files
authored
unwrap error unions, optionals, and pointers in goto type definition (#2321)
1 parent 2822437 commit 0d77561

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/features/goto.zig

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,14 @@ fn gotoDefinitionSymbol(
3838
.definition => try decl_handle.definitionToken(analyser, true),
3939
.type_definition => blk: {
4040
if (try decl_handle.resolveType(analyser)) |ty| {
41-
if (try ty.typeDefinitionToken()) |token_handle| break :blk token_handle;
41+
var resolved_ty = ty;
42+
while (true) {
43+
resolved_ty =
44+
try analyser.resolveUnwrapErrorUnionType(resolved_ty, .payload) orelse
45+
try analyser.resolveDerefType(resolved_ty) orelse
46+
try analyser.resolveOptionalUnwrap(resolved_ty) orelse break;
47+
}
48+
if (try resolved_ty.typeDefinitionToken()) |token_handle| break :blk token_handle;
4249
}
4350
const type_declaration = try decl_handle.typeDeclarationNode() orelse return null;
4451

tests/lsp_features/definition.zig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,25 @@ test "non labeled break" {
260260
);
261261
}
262262

263+
test "type definition unwraps error unions, optionals, pointers" {
264+
try testDefinition(
265+
\\const S = <tdef>struct</tdef> {};
266+
\\const <>foo: error{}!S = .{};
267+
);
268+
try testDefinition(
269+
\\const S = <tdef>struct</tdef> {};
270+
\\const <>foo: ?S = .{};
271+
);
272+
try testDefinition(
273+
\\const S = <tdef>struct</tdef> {};
274+
\\const <>foo: *const S = &.{};
275+
);
276+
try testDefinition(
277+
\\const S = <tdef>struct</tdef> {};
278+
\\const <>foo: error{}!?*const S = &.{};
279+
);
280+
}
281+
263282
/// - use `<>` to indicate the cursor position
264283
/// - use `<decl>content</decl>` to set the expected range of the declaration
265284
/// - use `<def>content</def>` to set the expected range of the definition

0 commit comments

Comments
 (0)