Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/features/folding_range.zig
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ const Builder = struct {
.endLine = range.end.line,
.endCharacter = range.end.character,
.kind = folding_range.kind,
// TODO this should be simplified https://codeberg.org/ziglang/zig/issues/30627
.collapsedText = if (folding_range.kind != null and folding_range.kind.? == .imports) "@import(...)" else null,
});
}

Expand Down Expand Up @@ -203,15 +205,15 @@ pub fn generateFoldingRanges(allocator: std.mem.Allocator, tree: *const Ast, enc
end_import = node;
} else if (start_import != null and end_import != null) {
// We found a non-import after a sequence of imports, create folding range
try builder.add(null, tree.firstToken(start_import.?), ast.lastToken(tree, end_import.?), .inclusive, .inclusive);
try builder.add(.imports, tree.firstToken(start_import.?), ast.lastToken(tree, end_import.?), .inclusive, .inclusive);
start_import = null;
end_import = null;
}
}

// Handle the case where imports continue to the end of the file
if (start_import != null and end_import != null and start_import.? != end_import.?) {
try builder.add(null, tree.firstToken(start_import.?), ast.lastToken(tree, end_import.?), .inclusive, .inclusive);
try builder.add(.imports, tree.firstToken(start_import.?), ast.lastToken(tree, end_import.?), .inclusive, .inclusive);
}
}

Expand Down
36 changes: 32 additions & 4 deletions tests/lsp_features/folding_range.zig
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,14 @@ test "imports" {
\\const std = @import("std");
\\const builtin = @import("builtin");
, &.{
.{ .startLine = 0, .startCharacter = 0, .endLine = 1, .endCharacter = 34 },
.{
.startLine = 0,
.startCharacter = 0,
.endLine = 1,
.endCharacter = 34,
.kind = .imports,
.collapsedText = "@import(...)",
},
});
try testFoldingRange(
\\const std = @import("std");
Expand All @@ -435,7 +442,14 @@ test "imports" {
\\
\\pub fn main() void {}
, &.{
.{ .startLine = 0, .startCharacter = 0, .endLine = 3, .endCharacter = 23 },
.{
.startLine = 0,
.startCharacter = 0,
.endLine = 3,
.endCharacter = 23,
.kind = .imports,
.collapsedText = "@import(...)",
},
});
// Single import should not create folding range
try testFoldingRange(
Expand All @@ -453,8 +467,22 @@ test "imports" {
\\const lsp = @import("lsp");
\\const types = @import("types");
, &.{
.{ .startLine = 0, .startCharacter = 0, .endLine = 1, .endCharacter = 34 },
.{ .startLine = 5, .startCharacter = 0, .endLine = 6, .endCharacter = 30 },
.{
.startLine = 0,
.startCharacter = 0,
.endLine = 1,
.endCharacter = 34,
.kind = .imports,
.collapsedText = "@import(...)",
},
.{
.startLine = 5,
.startCharacter = 0,
.endLine = 6,
.endCharacter = 30,
.kind = .imports,
.collapsedText = "@import(...)",
},
});
}

Expand Down