Skip to content

Commit a93d9c5

Browse files
committed
move workspace symbols implementation into separate file
1 parent 7321d23 commit a93d9c5

File tree

2 files changed

+77
-63
lines changed

2 files changed

+77
-63
lines changed

src/Server.zig

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ const goto = @import("features/goto.zig");
3434
const hover_handler = @import("features/hover.zig");
3535
const selection_range = @import("features/selection_range.zig");
3636
const diagnostics_gen = @import("features/diagnostics.zig");
37-
const TrigramStore = @import("TrigramStore.zig");
3837

3938
const BuildOnSave = diagnostics_gen.BuildOnSave;
4039
const BuildOnSaveSupport = build_runner_shared.BuildOnSaveSupport;
@@ -1517,68 +1516,7 @@ fn selectionRangeHandler(server: *Server, arena: std.mem.Allocator, request: typ
15171516
}
15181517

15191518
fn workspaceSymbolHandler(server: *Server, arena: std.mem.Allocator, request: types.WorkspaceSymbolParams) Error!lsp.ResultType("workspace/symbol") {
1520-
if (request.query.len < 3) return null;
1521-
1522-
const handles = try server.document_store.loadTrigramStores();
1523-
defer server.document_store.allocator.free(handles);
1524-
1525-
var symbols: std.ArrayListUnmanaged(types.WorkspaceSymbol) = .empty;
1526-
var declaration_buffer: std.ArrayListUnmanaged(TrigramStore.Declaration.Index) = .empty;
1527-
1528-
for (handles) |handle| {
1529-
const trigram_store = handle.getTrigramStoreCached();
1530-
1531-
declaration_buffer.clearRetainingCapacity();
1532-
try trigram_store.declarationsForQuery(arena, request.query, &declaration_buffer);
1533-
1534-
const SortContext = struct {
1535-
names: []const std.zig.Ast.TokenIndex,
1536-
fn lessThan(ctx: @This(), lhs: TrigramStore.Declaration.Index, rhs: TrigramStore.Declaration.Index) bool {
1537-
return ctx.names[@intFromEnum(lhs)] < ctx.names[@intFromEnum(rhs)];
1538-
}
1539-
};
1540-
1541-
std.mem.sortUnstable(
1542-
TrigramStore.Declaration.Index,
1543-
declaration_buffer.items,
1544-
SortContext{ .names = trigram_store.declarations.items(.name) },
1545-
SortContext.lessThan,
1546-
);
1547-
1548-
const slice = trigram_store.declarations.slice();
1549-
const names = slice.items(.name);
1550-
1551-
var last_index: usize = 0;
1552-
var last_position: offsets.Position = .{ .line = 0, .character = 0 };
1553-
1554-
try symbols.ensureUnusedCapacity(arena, declaration_buffer.items.len);
1555-
for (declaration_buffer.items) |declaration| {
1556-
const name_token = names[@intFromEnum(declaration)];
1557-
const loc = offsets.identifierTokenToNameLoc(handle.tree, name_token);
1558-
const name = offsets.identifierTokenToNameSlice(handle.tree, name_token);
1559-
1560-
const start_position = offsets.advancePosition(handle.tree.source, last_position, last_index, loc.start, server.offset_encoding);
1561-
const end_position = offsets.advancePosition(handle.tree.source, start_position, loc.start, loc.end, server.offset_encoding);
1562-
last_index = loc.end;
1563-
last_position = end_position;
1564-
1565-
symbols.appendAssumeCapacity(.{
1566-
.name = name,
1567-
.kind = .Variable,
1568-
.location = .{
1569-
.Location = .{
1570-
.uri = handle.uri,
1571-
.range = .{
1572-
.start = start_position,
1573-
.end = end_position,
1574-
},
1575-
},
1576-
},
1577-
});
1578-
}
1579-
}
1580-
1581-
return .{ .array_of_WorkspaceSymbol = symbols.items };
1519+
return try @import("features/workspace_symbols.zig").handler(server, arena, request);
15821520
}
15831521

15841522
const HandledRequestParams = union(enum) {

src/features/workspace_symbols.zig

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//! Implementation of [`workspace/symbol`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspace_symbol)
2+
3+
const std = @import("std");
4+
5+
const lsp = @import("lsp");
6+
const types = lsp.types;
7+
8+
const DocumentStore = @import("../DocumentStore.zig");
9+
const offsets = @import("../offsets.zig");
10+
const Server = @import("../Server.zig");
11+
const TrigramStore = @import("../TrigramStore.zig");
12+
13+
pub fn handler(server: *Server, arena: std.mem.Allocator, request: types.WorkspaceSymbolParams) error{OutOfMemory}!lsp.ResultType("workspace/symbol") {
14+
if (request.query.len < 3) return null;
15+
16+
const handles = try server.document_store.loadTrigramStores();
17+
defer server.document_store.allocator.free(handles);
18+
19+
var symbols: std.ArrayListUnmanaged(lsp.types.WorkspaceSymbol) = .empty;
20+
var declaration_buffer: std.ArrayListUnmanaged(TrigramStore.Declaration.Index) = .empty;
21+
22+
for (handles) |handle| {
23+
const trigram_store = handle.getTrigramStoreCached();
24+
25+
declaration_buffer.clearRetainingCapacity();
26+
try trigram_store.declarationsForQuery(arena, request.query, &declaration_buffer);
27+
28+
const SortContext = struct {
29+
names: []const std.zig.Ast.TokenIndex,
30+
fn lessThan(ctx: @This(), lhs: TrigramStore.Declaration.Index, rhs: TrigramStore.Declaration.Index) bool {
31+
return ctx.names[@intFromEnum(lhs)] < ctx.names[@intFromEnum(rhs)];
32+
}
33+
};
34+
35+
std.mem.sortUnstable(
36+
TrigramStore.Declaration.Index,
37+
declaration_buffer.items,
38+
SortContext{ .names = trigram_store.declarations.items(.name) },
39+
SortContext.lessThan,
40+
);
41+
42+
const slice = trigram_store.declarations.slice();
43+
const names = slice.items(.name);
44+
45+
var last_index: usize = 0;
46+
var last_position: offsets.Position = .{ .line = 0, .character = 0 };
47+
48+
try symbols.ensureUnusedCapacity(arena, declaration_buffer.items.len);
49+
for (declaration_buffer.items) |declaration| {
50+
const name_token = names[@intFromEnum(declaration)];
51+
const loc = offsets.identifierTokenToNameLoc(handle.tree, name_token);
52+
const name = offsets.identifierTokenToNameSlice(handle.tree, name_token);
53+
54+
const start_position = offsets.advancePosition(handle.tree.source, last_position, last_index, loc.start, server.offset_encoding);
55+
const end_position = offsets.advancePosition(handle.tree.source, start_position, loc.start, loc.end, server.offset_encoding);
56+
last_index = loc.end;
57+
last_position = end_position;
58+
59+
symbols.appendAssumeCapacity(.{
60+
.name = name,
61+
.kind = .Variable,
62+
.location = .{
63+
.Location = .{
64+
.uri = handle.uri,
65+
.range = .{
66+
.start = start_position,
67+
.end = end_position,
68+
},
69+
},
70+
},
71+
});
72+
}
73+
}
74+
75+
return .{ .array_of_WorkspaceSymbol = symbols.items };
76+
}

0 commit comments

Comments
 (0)