From ceb6b5f797b02b700fb3a4e08a46e4615d573446 Mon Sep 17 00:00:00 2001 From: Danny Tuppeny Date: Thu, 16 Oct 2025 09:57:04 -0700 Subject: [PATCH] [analysis_server] Fix folding ranges when nested ranges end on the same line When the client only supports folding by-line (VS Code), we truncate ranges that intersect but allow nested ranges as long as they don't. This code was performing truncation if the outer range ended on the same line as the inner range, but this is perfectly fine and didn't need to truncate. Fixes https://github.com/Dart-Code/Dart-Code/issues/5750 Change-Id: I05e2307574481a7a53ee01ef76bd8a26bfde09c0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/454920 Commit-Queue: Keerti Parthasarathy Reviewed-by: Brian Wilkerson Reviewed-by: Keerti Parthasarathy --- .../lib/src/lsp/handlers/handler_folding.dart | 4 ++-- .../test/lsp/folding_test.dart | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/pkg/analysis_server/lib/src/lsp/handlers/handler_folding.dart b/pkg/analysis_server/lib/src/lsp/handlers/handler_folding.dart index 94e4b761de6..65f254c4da3 100644 --- a/pkg/analysis_server/lib/src/lsp/handlers/handler_folding.dart +++ b/pkg/analysis_server/lib/src/lsp/handlers/handler_folding.dart @@ -106,8 +106,8 @@ class FoldingHandler for (var i = 0; i < foldingRanges.length - 1; i++) { var range = foldingRanges[i]; var next = foldingRanges[i + 1]; - // If this item runs into the next but does not completely enclose it... - if (range.endLine >= next.startLine && range.endLine <= next.endLine) { + // If this item runs into the next but does not enclose it... + if (range.endLine >= next.startLine && range.endLine < next.endLine) { // Truncate it to end on the line before. var newEndLine = next.startLine - 1; diff --git a/pkg/analysis_server/test/lsp/folding_test.dart b/pkg/analysis_server/test/lsp/folding_test.dart index a2442e5dae0..bd6cf18895b 100644 --- a/pkg/analysis_server/test/lsp/folding_test.dart +++ b/pkg/analysis_server/test/lsp/folding_test.dart @@ -482,6 +482,30 @@ void f(int a) { }, requireAll: false); } + /// https://github.com/Dart-Code/Dart-Code/issues/5750 + Future test_switchStatement_nested_sameEndLine_lineFoldingOnly() async { + lineFoldingOnly = true; + + var content = ''' +Future f(int i) async { + switch (i) {/*[0*/ + case 1:/*[1*/ + print('something'); + f(/*[2*/ + 1, + );/*2]*//*1]*/ + }/*0]*/ +} +'''; + + await computeRanges(content); + expectRanges({ + 0: noFoldingKind, + 1: noFoldingKind, + 2: noFoldingKind, + }, requireAll: false); + } + /// Even without lineFolding, try/catch/finally folding regions end /// on the last statement of each block (matching if/else) to avoid long /// lines when folded.