Skip to content

Commit a3241ac

Browse files
committed
more document symbol fixes
1 parent 210ebbd commit a3241ac

File tree

2 files changed

+56
-12
lines changed

2 files changed

+56
-12
lines changed

src/features/document_symbol.zig

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,33 +112,40 @@ fn callback(ctx: *Context, tree: *const Ast, node: Ast.Node.Index) error{OutOfMe
112112
.container_field_align,
113113
.container_field,
114114
=> blk: {
115-
const kind: types.SymbolKind, const is_struct = switch (tree.nodeTag(ctx.parent_container)) {
116-
.root => .{ .Field, true },
115+
const container_kind = switch (tree.nodeTag(ctx.parent_container)) {
116+
.root => .keyword_struct,
117117
.container_decl,
118118
.container_decl_trailing,
119119
.container_decl_arg,
120120
.container_decl_arg_trailing,
121121
.container_decl_two,
122122
.container_decl_two_trailing,
123-
=> switch (tree.tokenTag(tree.nodeMainToken(ctx.parent_container))) {
124-
.keyword_struct => .{ .Field, true },
125-
.keyword_union => .{ .Field, false },
126-
.keyword_enum => .{ .EnumMember, false },
127-
.keyword_opaque => break :blk null,
128-
else => unreachable,
129-
},
123+
=> tree.tokenTag(tree.nodeMainToken(ctx.parent_container)),
130124
.tagged_union,
131125
.tagged_union_trailing,
132126
.tagged_union_enum_tag,
133127
.tagged_union_enum_tag_trailing,
134128
.tagged_union_two,
135129
.tagged_union_two_trailing,
136-
=> .{ .Field, false },
130+
=> .keyword_union,
137131
else => unreachable,
138132
};
139133

140-
const container_field = tree.fullContainerField(node).?;
141-
if (is_struct and container_field.ast.tuple_like) break :blk null;
134+
const kind: types.SymbolKind = switch (container_kind) {
135+
.keyword_struct => .Field,
136+
.keyword_union => .Field,
137+
.keyword_enum => .EnumMember,
138+
.keyword_opaque => break :blk null,
139+
else => unreachable,
140+
};
141+
142+
var container_field = tree.fullContainerField(node).?;
143+
switch (container_kind) {
144+
.keyword_struct => {},
145+
.keyword_enum, .keyword_union => container_field.convertToNonTupleLike(tree),
146+
else => unreachable,
147+
}
148+
if (container_field.ast.tuple_like) break :blk null;
142149

143150
const decl_name_token = container_field.ast.main_token;
144151

tests/lsp_features/document_symbol.zig

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ test "tuple" {
3939
);
4040
}
4141

42+
test "union" {
43+
try testDocumentSymbol(
44+
\\const U = union {
45+
\\ alpha: u32,
46+
\\ beta,
47+
\\};
48+
,
49+
\\Constant U
50+
\\ Field alpha
51+
\\ Field beta
52+
);
53+
}
54+
4255
test "enum" {
4356
try testDocumentSymbol(
4457
\\const E = enum {
@@ -52,6 +65,30 @@ test "enum" {
5265
);
5366
}
5467

68+
test "invalid tuple-like container" {
69+
try testDocumentSymbol(
70+
\\const E = enum {
71+
\\ '=',
72+
\\};
73+
,
74+
\\Constant E
75+
);
76+
try testDocumentSymbol(
77+
\\const U = union {
78+
\\ '=',
79+
\\};
80+
,
81+
\\Constant U
82+
);
83+
try testDocumentSymbol(
84+
\\const U = union(enum) {
85+
\\ '=',
86+
\\};
87+
,
88+
\\Constant U
89+
);
90+
}
91+
5592
test "test decl" {
5693
try testDocumentSymbol(
5794
\\test foo {}

0 commit comments

Comments
 (0)