Skip to content

Commit 0221b42

Browse files
committed
refactor folding range collection
1 parent f4caa14 commit 0221b42

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

src/features/folding_range.zig

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ const FoldingRange = struct {
1414
};
1515

1616
const Inclusivity = enum {
17+
/// Include the token itself as part of the folding range.
1718
inclusive,
18-
inclusive_ignore_space,
19+
/// Do not include the token itself as part of the folding range.
1920
exclusive,
21+
/// Same as `exclusive` but will also not include any adjacent whitespace.
2022
exclusive_ignore_space,
2123
};
2224

@@ -40,22 +42,28 @@ const Builder = struct {
4042
) error{OutOfMemory}!void {
4143
if (start >= end) return;
4244
if (builder.tree.tokensOnSameLine(start, end)) return;
43-
const start_loc = offsets.tokenToLoc(builder.tree, start);
44-
const end_loc = offsets.tokenToLoc(builder.tree, end);
4545

46-
try builder.locations.append(builder.allocator, .{
47-
.loc = .{
48-
.start = switch (start_reach) {
49-
.inclusive, .inclusive_ignore_space => start_loc.start,
50-
.exclusive => start_loc.end,
51-
.exclusive_ignore_space => std.mem.findNonePos(u8, builder.tree.source, start_loc.end, " \t") orelse builder.tree.source.len,
52-
},
53-
.end = switch (end_reach) {
54-
.inclusive, .inclusive_ignore_space => end_loc.end,
55-
.exclusive => end_loc.start,
56-
.exclusive_ignore_space => std.mem.findLastNone(u8, builder.tree.source[0..end_loc.start], " \t") orelse 0,
57-
},
46+
const start_index = switch (start_reach) {
47+
.inclusive => builder.tree.tokenStart(start),
48+
.exclusive => offsets.tokenToLoc(builder.tree, start).end,
49+
.exclusive_ignore_space => blk: {
50+
const start_index = offsets.tokenToLoc(builder.tree, start).end;
51+
const end_index = builder.tree.tokenStart(end);
52+
break :blk std.mem.findNonePos(u8, builder.tree.source[0..end_index], start_index, " \t") orelse end_index;
5853
},
54+
};
55+
56+
const end_index = switch (end_reach) {
57+
.inclusive => offsets.tokenToLoc(builder.tree, end).end,
58+
.exclusive => builder.tree.tokenStart(end),
59+
.exclusive_ignore_space => std.mem.findLastNone(u8, builder.tree.source[0..builder.tree.tokenStart(end)], " \t") orelse 0,
60+
};
61+
62+
std.debug.assert(start_index <= end_index);
63+
if (start_index == end_index) return;
64+
65+
try builder.locations.append(builder.allocator, .{
66+
.loc = .{ .start = start_index, .end = end_index },
5967
.kind = kind,
6068
});
6169
}
@@ -81,7 +89,7 @@ const Builder = struct {
8189
var mappings = try builder.allocator.alloc(offsets.multiple.IndexToPositionMapping, builder.locations.items.len * 2);
8290
defer builder.allocator.free(mappings);
8391

84-
for (builder.locations.items, result_ranges, 0..) |*folding_range, *result, i| {
92+
for (builder.locations.items, result_ranges, 0..) |folding_range, *result, i| {
8593
mappings[2 * i + 0] = .{ .output = &result.start, .source_index = folding_range.loc.start };
8694
mappings[2 * i + 1] = .{ .output = &result.end, .source_index = folding_range.loc.end };
8795
}
@@ -292,18 +300,17 @@ pub fn generateFoldingRanges(allocator: std.mem.Allocator, tree: *const Ast, enc
292300

293301
var i: usize = 0;
294302
while (std.mem.findPos(u8, tree.source, i, "//#")) |possible_region| {
295-
defer i = possible_region + "//#".len;
303+
i = possible_region + "//#".len;
304+
i = std.mem.findScalarPos(u8, tree.source, i, '\n') orelse tree.source.len;
305+
296306
if (std.mem.startsWith(u8, tree.source[possible_region..], "//#region")) {
297307
try stack.append(allocator, possible_region);
298308
} else if (std.mem.startsWith(u8, tree.source[possible_region..], "//#endregion")) {
299309
const start_index = stack.pop() orelse break; // null means there are more endregions than regions
300-
const end_index = offsets.lineLocAtIndex(tree.source, possible_region).end;
301-
const is_same_line = std.mem.findScalar(u8, tree.source[start_index..end_index], '\n') == null;
302-
if (is_same_line) continue;
303310
try builder.locations.append(allocator, .{
304311
.loc = .{
305312
.start = start_index,
306-
.end = end_index,
313+
.end = i,
307314
},
308315
.kind = .region,
309316
});

0 commit comments

Comments
 (0)