@@ -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 | {
0 commit comments