Skip to content

Commit d700a7d

Browse files
committed
don't create folding range for small number of imports
1 parent 12a2039 commit d700a7d

2 files changed

Lines changed: 45 additions & 27 deletions

File tree

src/features/folding_range.zig

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,37 +184,46 @@ pub fn generateFoldingRanges(allocator: std.mem.Allocator, tree: *const Ast, enc
184184
// TODO add folding range normal comments
185185

186186
// Folding range for top level imports
187-
if (tree.mode == .zig) {
187+
if (tree.mode == .zig) blk: {
188188
var start_import: ?Ast.Node.Index = null;
189189
var end_import: ?Ast.Node.Index = null;
190+
var import_count: usize = 0;
190191

191192
const root_decls = tree.rootDecls();
192193
for (root_decls) |node| {
193-
const is_import = blk: {
194-
if (tree.nodeTag(node) != .simple_var_decl) break :blk false;
194+
const is_import = is_import: {
195+
if (tree.nodeTag(node) != .simple_var_decl) break :is_import false;
195196
const var_decl = tree.simpleVarDecl(node);
196-
const init_node = var_decl.ast.init_node.unwrap() orelse break :blk false;
197+
const init_node = var_decl.ast.init_node.unwrap() orelse break :is_import false;
197198

198-
break :blk isImportOrAlias(tree, init_node);
199+
break :is_import isImportOrAlias(tree, init_node);
199200
};
200201

201202
if (is_import) {
202203
if (start_import == null) {
203204
start_import = node;
204205
}
205206
end_import = node;
206-
} else if (start_import != null and end_import != null) {
207-
// We found a non-import after a sequence of imports, create folding range
208-
try builder.add(.imports, tree.firstToken(start_import.?), ast.lastToken(tree, end_import.?), .inclusive, .inclusive);
207+
import_count += 1;
208+
continue;
209+
}
210+
defer {
209211
start_import = null;
210212
end_import = null;
213+
import_count = 0;
211214
}
215+
216+
const start = start_import orelse continue;
217+
const end = end_import orelse continue;
218+
if (import_count < 3) continue;
219+
try builder.add(.imports, tree.firstToken(start), ast.lastToken(tree, end) + 1, .inclusive, .inclusive);
212220
}
213221

214222
// Handle the case where imports continue to the end of the file
215-
if (start_import != null and end_import != null and start_import.? != end_import.?) {
216-
try builder.add(.imports, tree.firstToken(start_import.?), ast.lastToken(tree, end_import.?), .inclusive, .inclusive);
217-
}
223+
const start = start_import orelse break :blk;
224+
const end = end_import orelse break :blk;
225+
if (import_count < 3) break :blk;
226+
try builder.add(.imports, tree.firstToken(start), ast.lastToken(tree, end) + 1, .inclusive, .inclusive);
218227
}
219228

220229
for (0..tree.nodes.len) |i| {

tests/lsp_features/folding_range.zig

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -424,62 +424,71 @@ test "imports" {
424424
try testFoldingRange(
425425
\\const std = @import("std");
426426
\\const builtin = @import("builtin");
427+
\\const root = @import("root");
427428
, &.{
428429
.{
429430
.startLine = 0,
430431
.startCharacter = 0,
431-
.endLine = 1,
432-
.endCharacter = 34,
432+
.endLine = 2,
433+
.endCharacter = 29,
433434
.kind = .imports,
434435
.collapsedText = "@import(...)",
435436
},
436437
});
438+
}
439+
440+
test "imports with aliases" {
437441
try testFoldingRange(
438442
\\const std = @import("std");
439443
\\const builtin = @import("builtin");
440-
\\const lsp = @import("lsp");
441-
\\const types = lsp.types;
444+
\\const foo = @import("foo");
445+
\\const bar = foo.bar;
442446
\\
443447
\\pub fn main() void {}
444448
, &.{
445449
.{
446450
.startLine = 0,
447451
.startCharacter = 0,
448452
.endLine = 3,
449-
.endCharacter = 23,
453+
.endCharacter = 20,
450454
.kind = .imports,
451455
.collapsedText = "@import(...)",
452456
},
453457
});
454-
// Single import should not create folding range
458+
}
459+
460+
test "imports exclude small count" {
455461
try testFoldingRange(
456462
\\const std = @import("std");
457-
\\
458-
\\pub fn main() void {}
463+
\\const builtin = @import("builtin");
459464
, &.{});
460-
// Imports with gap in between should create separate folding ranges
465+
}
466+
467+
test "imports with gap" {
461468
try testFoldingRange(
462469
\\const std = @import("std");
463470
\\const builtin = @import("builtin");
471+
\\const root = @import("root");
464472
\\
465473
\\pub const foo = 5;
466474
\\
467-
\\const lsp = @import("lsp");
468-
\\const types = @import("types");
475+
\\const foo = @import("foo");
476+
\\const bar = @import("bar");
477+
\\const baz = @import("baz");
469478
, &.{
470479
.{
471480
.startLine = 0,
472481
.startCharacter = 0,
473-
.endLine = 1,
474-
.endCharacter = 34,
482+
.endLine = 2,
483+
.endCharacter = 29,
475484
.kind = .imports,
476485
.collapsedText = "@import(...)",
477486
},
478487
.{
479-
.startLine = 5,
488+
.startLine = 6,
480489
.startCharacter = 0,
481-
.endLine = 6,
482-
.endCharacter = 30,
490+
.endLine = 8,
491+
.endCharacter = 27,
483492
.kind = .imports,
484493
.collapsedText = "@import(...)",
485494
},

0 commit comments

Comments
 (0)