Skip to content

Commit c93621a

Browse files
committed
improve trigramstore declarations
- resolve symbol kind - include test declarations
1 parent 8bd33cc commit c93621a

File tree

2 files changed

+53
-19
lines changed

2 files changed

+53
-19
lines changed

src/TrigramStore.zig

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,22 @@ pub const Trigram = [3]u8;
1313
pub const Declaration = struct {
1414
pub const Index = enum(u32) { _ };
1515

16+
pub const Kind = enum {
17+
variable,
18+
constant,
19+
function,
20+
test_function,
21+
};
22+
23+
/// Either `.identifier` or `.string_literal`.
1624
name: Ast.TokenIndex,
25+
kind: Kind,
1726
};
1827

1928
has_filter: bool,
2029
filter_buckets: std.ArrayListUnmanaged(CuckooFilter.Bucket),
2130
trigram_to_declarations: std.AutoArrayHashMapUnmanaged(Trigram, std.ArrayListUnmanaged(Declaration.Index)),
2231
declarations: std.MultiArrayList(Declaration),
23-
names: std.ArrayListUnmanaged(u8),
2432

2533
pub fn init(
2634
allocator: std.mem.Allocator,
@@ -31,7 +39,6 @@ pub fn init(
3139
.filter_buckets = .empty,
3240
.trigram_to_declarations = .empty,
3341
.declarations = .empty,
34-
.names = .empty,
3542
};
3643
errdefer store.deinit(allocator);
3744

@@ -45,7 +52,6 @@ pub fn init(
4552
const old_in_function = context.in_function;
4653
defer context.in_function = old_in_function;
4754

48-
var name_token_maybe: ?Ast.TokenIndex = null;
4955
switch (cb_tree.nodeTag(node)) {
5056
.fn_proto,
5157
.fn_proto_multi,
@@ -57,7 +63,12 @@ pub fn init(
5763
const fn_token = cb_tree.nodeMainToken(node);
5864
if (cb_tree.tokenTag(fn_token + 1) != .identifier) break :skip;
5965

60-
name_token_maybe = fn_token + 1;
66+
try context.store.appendDeclaration(
67+
context.allocator,
68+
offsets.identifierTokenToNameSlice(cb_tree, fn_token + 1),
69+
fn_token + 1,
70+
.function,
71+
);
6172
},
6273
.root => unreachable,
6374
.container_decl,
@@ -80,17 +91,34 @@ pub fn init(
8091
.aligned_var_decl,
8192
=> skip: {
8293
if (context.in_function) break :skip;
83-
name_token_maybe = cb_tree.nodeMainToken(node) + 1;
94+
95+
const main_token = cb_tree.nodeMainToken(node);
96+
97+
const kind: Declaration.Kind = switch (cb_tree.tokenTag(main_token)) {
98+
.keyword_var => .variable,
99+
.keyword_const => .constant,
100+
else => unreachable,
101+
};
102+
103+
try context.store.appendDeclaration(
104+
context.allocator,
105+
offsets.identifierTokenToNameSlice(cb_tree, main_token + 1),
106+
main_token + 1,
107+
kind,
108+
);
84109
},
85-
else => {},
86-
}
87110

88-
if (name_token_maybe) |name_token| {
89-
try context.store.appendDeclaration(
90-
context.allocator,
91-
cb_tree,
92-
name_token,
93-
);
111+
.test_decl => skip: {
112+
const test_name_token, const test_name = ast.testDeclNameAndToken(cb_tree, node) orelse break :skip;
113+
114+
try context.store.appendDeclaration(
115+
context.allocator,
116+
test_name,
117+
test_name_token,
118+
.test_function,
119+
);
120+
},
121+
else => {},
94122
}
95123

96124
try ast.iterateChildren(cb_tree, node, context, Error, callback);
@@ -149,23 +177,21 @@ pub fn deinit(store: *TrigramStore, allocator: std.mem.Allocator) void {
149177
}
150178
store.trigram_to_declarations.deinit(allocator);
151179
store.declarations.deinit(allocator);
152-
store.names.deinit(allocator);
153180
store.* = undefined;
154181
}
155182

156-
/// Caller must not submit name.len < 3.
157183
fn appendDeclaration(
158184
store: *TrigramStore,
159185
allocator: std.mem.Allocator,
160-
tree: Ast,
186+
name: []const u8,
161187
name_token: Ast.TokenIndex,
188+
kind: Declaration.Kind,
162189
) error{OutOfMemory}!void {
163-
const loc = offsets.identifierTokenToNameLoc(tree, name_token);
164-
const name = offsets.locToSlice(tree.source, loc);
165190
if (name.len < 3) return;
166191

167192
try store.declarations.append(allocator, .{
168193
.name = name_token,
194+
.kind = kind,
169195
});
170196

171197
for (0..name.len - 2) |index| {

src/features/workspace_symbols.zig

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,16 @@ pub fn handler(server: *Server, arena: std.mem.Allocator, request: types.Workspa
5252

5353
const slice = trigram_store.declarations.slice();
5454
const names = slice.items(.name);
55+
const kinds = slice.items(.kind);
5556

5657
var last_index: usize = 0;
5758
var last_position: offsets.Position = .{ .line = 0, .character = 0 };
5859

5960
try symbols.ensureUnusedCapacity(arena, declaration_buffer.items.len);
6061
for (declaration_buffer.items) |declaration| {
6162
const name_token = names[@intFromEnum(declaration)];
63+
const kind = kinds[@intFromEnum(declaration)];
64+
6265
const loc = offsets.identifierTokenToNameLoc(handle.tree, name_token);
6366
const name = offsets.identifierTokenToNameSlice(handle.tree, name_token);
6467

@@ -69,7 +72,12 @@ pub fn handler(server: *Server, arena: std.mem.Allocator, request: types.Workspa
6972

7073
symbols.appendAssumeCapacity(.{
7174
.name = name,
72-
.kind = .Variable,
75+
.kind = switch (kind) {
76+
.variable => .Variable,
77+
.constant => .Constant,
78+
.function => .Function,
79+
.test_function => .Method, // there is no SymbolKind that represents a tests,
80+
},
7381
.location = .{
7482
.Location = .{
7583
.uri = handle.uri,

0 commit comments

Comments
 (0)