Skip to content

Commit

Permalink
avoid generating folding range with overlapping lines
Browse files Browse the repository at this point in the history
fixes #1131
  • Loading branch information
Techatrix committed Feb 29, 2024
1 parent f48dc6a commit 20ca8f5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
21 changes: 17 additions & 4 deletions src/features/folding_range.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ const FoldingRange = struct {
kind: ?types.FoldingRangeKind = null,
};

const Inclusivity = enum { inclusive, exclusive };
const Inclusivity = enum {
inclusive,
inclusive_ignore_space,
exclusive,
exclusive_ignore_space,
};

const Builder = struct {
allocator: std.mem.Allocator,
Expand All @@ -38,8 +43,16 @@ const Builder = struct {

try builder.locations.append(builder.allocator, .{
.loc = .{
.start = if (start_reach == .exclusive) start_loc.end else start_loc.start,
.end = if (end_reach == .exclusive) end_loc.start else end_loc.end,
.start = switch (start_reach) {
.inclusive, .inclusive_ignore_space => start_loc.start,
.exclusive => start_loc.end,
.exclusive_ignore_space => std.mem.indexOfNonePos(u8, builder.tree.source, start_loc.end, " \t") orelse builder.tree.source.len,
},
.end = switch (end_reach) {
.inclusive, .inclusive_ignore_space => end_loc.end,
.exclusive => end_loc.start,
.exclusive_ignore_space => std.mem.lastIndexOfNone(u8, builder.tree.source[0..end_loc.start], " \t") orelse 0,
},
},
.kind = kind,
});
Expand Down Expand Up @@ -196,7 +209,7 @@ pub fn generateFoldingRanges(allocator: std.mem.Allocator, tree: Ast, encoding:
.block,
.block_semicolon,
=> {
try builder.addNode(null, node, .exclusive, .exclusive);
try builder.addNode(null, node, .exclusive, .exclusive_ignore_space);
},
.@"switch",
.switch_comma,
Expand Down
35 changes: 28 additions & 7 deletions tests/lsp_features/folding_range.zig
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ test "if" {
\\
\\};
, &.{
.{ .startLine = 0, .startCharacter = 24, .endLine = 2, .endCharacter = 0 },
.{ .startLine = 0, .startCharacter = 24, .endLine = 1, .endCharacter = 0 },
});
try testFoldingRange(
\\const foo = if (false) {
Expand All @@ -77,8 +77,8 @@ test "if" {
\\
\\};
, &.{
.{ .startLine = 0, .startCharacter = 24, .endLine = 2, .endCharacter = 0 },
.{ .startLine = 2, .startCharacter = 8, .endLine = 4, .endCharacter = 0 },
.{ .startLine = 0, .startCharacter = 24, .endLine = 1, .endCharacter = 0 },
.{ .startLine = 2, .startCharacter = 8, .endLine = 3, .endCharacter = 0 },
});
}

Expand All @@ -88,14 +88,35 @@ test "for/while" {
\\
\\};
, &.{
.{ .startLine = 0, .startCharacter = 26, .endLine = 2, .endCharacter = 0 },
.{ .startLine = 0, .startCharacter = 26, .endLine = 1, .endCharacter = 0 },
});
try testFoldingRange(
\\const foo = for ("") |_| {
\\ return;
\\} else {
\\
\\};
, &.{
.{ .startLine = 0, .startCharacter = 26, .endLine = 1, .endCharacter = 11 },
.{ .startLine = 2, .startCharacter = 8, .endLine = 3, .endCharacter = 0 },
});

try testFoldingRange(
\\const foo = while (true) {
\\ //
\\};
, &.{
.{ .startLine = 0, .startCharacter = 26, .endLine = 1, .endCharacter = 6 },
});
try testFoldingRange(
\\const foo = while (true) {
\\
\\} else {
\\ //
\\};
, &.{
.{ .startLine = 0, .startCharacter = 26, .endLine = 2, .endCharacter = 0 },
.{ .startLine = 0, .startCharacter = 26, .endLine = 1, .endCharacter = 0 },
.{ .startLine = 2, .startCharacter = 8, .endLine = 3, .endCharacter = 6 },
});
}

Expand Down Expand Up @@ -138,7 +159,7 @@ test "function" {
\\ return 1 + 1;
\\}
, &.{
.{ .startLine = 0, .startCharacter = 15, .endLine = 2, .endCharacter = 0 },
.{ .startLine = 0, .startCharacter = 15, .endLine = 1, .endCharacter = 17 },
});
try testFoldingRange(
\\fn main(
Expand All @@ -156,7 +177,7 @@ test "function" {
\\}
, &.{
.{ .startLine = 0, .startCharacter = 8, .endLine = 1, .endCharacter = 12 },
.{ .startLine = 2, .startCharacter = 8, .endLine = 4, .endCharacter = 0 },
.{ .startLine = 2, .startCharacter = 8, .endLine = 3, .endCharacter = 17 },
});
}

Expand Down

0 comments on commit 20ca8f5

Please sign in to comment.