Skip to content

Commit 61cb222

Browse files
committed
move Loc to Position conversion out of TrigramStore
1 parent fc80102 commit 61cb222

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

src/DocumentStore.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ pub const Handle = struct {
346346
if (self.getStatus().has_trigram_store) return self.impl.trigram_store;
347347
return try self.getLazy(TrigramStore, "trigram_store", struct {
348348
fn create(handle: *Handle, allocator: std.mem.Allocator) error{OutOfMemory}!TrigramStore {
349-
return try .init(allocator, handle.tree, .@"utf-16"); // TODO
349+
return try .init(allocator, handle.tree);
350350
}
351351
});
352352
}

src/Server.zig

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,6 +1986,8 @@ fn workspaceSymbolHandler(server: *Server, arena: std.mem.Allocator, request: ty
19861986

19871987
var symbols: std.ArrayListUnmanaged(types.WorkspaceSymbol) = .empty;
19881988
var declaration_buffer: std.ArrayListUnmanaged(TrigramStore.Declaration.Index) = .empty;
1989+
var loc_buffer: std.ArrayListUnmanaged(offsets.Loc) = .empty;
1990+
var range_buffer: std.ArrayListUnmanaged(offsets.Range) = .empty;
19891991

19901992
for (handles) |handle| {
19911993
const trigram_store = handle.getTrigramStoreCached();
@@ -1995,12 +1997,25 @@ fn workspaceSymbolHandler(server: *Server, arena: std.mem.Allocator, request: ty
19951997

19961998
const slice = trigram_store.declarations.slice();
19971999
const names = slice.items(.name);
1998-
const ranges = slice.items(.range);
2000+
const locs = slice.items(.loc);
2001+
2002+
{
2003+
// Convert `offsets.Loc` to `offsets.Range`
2004+
2005+
try loc_buffer.resize(arena, declaration_buffer.items.len);
2006+
try range_buffer.resize(arena, declaration_buffer.items.len);
2007+
2008+
for (declaration_buffer.items, loc_buffer.items) |declaration, *loc| {
2009+
const small_loc = locs[@intFromEnum(declaration)];
2010+
loc.* = .{ .start = small_loc.start, .end = small_loc.end };
2011+
}
2012+
2013+
try offsets.multiple.locToRange(arena, handle.tree.source, loc_buffer.items, range_buffer.items, server.offset_encoding);
2014+
}
19992015

20002016
try symbols.ensureUnusedCapacity(arena, declaration_buffer.items.len);
2001-
for (declaration_buffer.items) |declaration| {
2017+
for (declaration_buffer.items, range_buffer.items) |declaration, range| {
20022018
const name = names[@intFromEnum(declaration)];
2003-
const range = ranges[@intFromEnum(declaration)];
20042019
symbols.appendAssumeCapacity(.{
20052020
.name = trigram_store.names.items[name.start..name.end],
20062021
.kind = .Variable,

src/TrigramStore.zig

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ pub const TrigramStore = @This();
1414
pub const Trigram = [3]u8;
1515

1616
pub const NameSlice = struct { start: u32, end: u32 };
17+
pub const Loc = struct { start: u32, end: u32 };
1718

1819
pub const Declaration = struct {
1920
pub const Index = enum(u32) { _ };
2021

2122
name: NameSlice,
22-
range: offsets.Range,
23+
loc: Loc,
2324
};
2425

2526
has_filter: bool,
@@ -31,7 +32,6 @@ names: std.ArrayListUnmanaged(u8),
3132
pub fn init(
3233
allocator: std.mem.Allocator,
3334
tree: Ast,
34-
encoding: offsets.Encoding,
3535
) error{OutOfMemory}!TrigramStore {
3636
var store: TrigramStore = .{
3737
.has_filter = false,
@@ -46,7 +46,6 @@ pub fn init(
4646
allocator: std.mem.Allocator,
4747
store: *TrigramStore,
4848
in_function: bool,
49-
encoding: offsets.Encoding,
5049

5150
const Error = error{OutOfMemory};
5251
fn callback(context: *@This(), cb_tree: Ast, node: Ast.Node.Index) Error!void {
@@ -84,10 +83,12 @@ pub fn init(
8483
const name = cb_tree.tokenSlice(token);
8584

8685
if (name.len >= 3) {
86+
const loc = offsets.tokenToLoc(cb_tree, token);
87+
8788
try context.store.appendDeclaration(
8889
context.allocator,
8990
name,
90-
offsets.tokenToRange(cb_tree, token, context.encoding),
91+
.{ .start = @intCast(loc.start), .end = @intCast(loc.end) },
9192
);
9293
}
9394
}
@@ -104,7 +105,6 @@ pub fn init(
104105
.allocator = allocator,
105106
.store = &store,
106107
.in_function = false,
107-
.encoding = encoding,
108108
};
109109
try ast.iterateChildren(tree, .root, &context, Context.Error, Context.callback);
110110

@@ -162,7 +162,7 @@ fn appendDeclaration(
162162
store: *TrigramStore,
163163
allocator: std.mem.Allocator,
164164
name: []const u8,
165-
range: offsets.Range,
165+
loc: Loc,
166166
) error{OutOfMemory}!void {
167167
assert(name.len >= 3);
168168

@@ -177,7 +177,7 @@ fn appendDeclaration(
177177

178178
try store.declarations.append(allocator, .{
179179
.name = name_slice,
180-
.range = range,
180+
.loc = loc,
181181
});
182182

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

0 commit comments

Comments
 (0)