Skip to content

Commit 7466f6e

Browse files
DanTupCommit Queue
authored andcommitted
[analysis_server] Fix preservation of strings when indenting code
When code indentation is replaced, strings should not be updated. This loop had inverted logic which meant that after processing a string before the current line, we would then exit early and not look at the subsequent string ranges (which we might be a match for). Fixes #60951 Change-Id: I1a94ca05e968bb7f2193544779b2634b784f8aee Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/435580 Commit-Queue: Keerti Parthasarathy <[email protected]> Reviewed-by: Keerti Parthasarathy <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 712d776 commit 7466f6e

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

pkg/analysis_server_plugin/lib/edit/correction_utils.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,11 @@ final class CorrectionUtils {
299299
for (var lineRange in lineRanges) {
300300
if (lineOffset > lineRange.offset && lineOffset < lineRange.end) {
301301
inString = true;
302+
break;
302303
}
303-
if (lineOffset > lineRange.end) {
304+
// We can skip the rest if this line ends before the end of this range
305+
// because subsequent ranges are after it.
306+
if (lineOffset < lineRange.end) {
304307
break;
305308
}
306309
}

pkg/analysis_server_plugin/test/edit/correction_utils_test.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,41 @@ void f(bool? b4, bool? b5) {
138138
);
139139
}
140140

141+
Future<void> test_replaceSourceIndent_multipleStrings() async {
142+
// Test multiple strings because the issue
143+
// https://github.com/dart-lang/sdk/issues/60951 was related to only the
144+
// first being processed correctly.
145+
await assertReplacedIndentation(
146+
includeLeading: true,
147+
indentOld: '',
148+
indentNew: ' ',
149+
r'''
150+
var i = 0;
151+
var x = '';
152+
var y = """
153+
0 indent
154+
1 indent
155+
2 indent
156+
1 indent
157+
0 indent
158+
""";
159+
var j = 1;
160+
''',
161+
r'''
162+
var i = 0;
163+
var x = '';
164+
var y = """
165+
0 indent
166+
1 indent
167+
2 indent
168+
1 indent
169+
0 indent
170+
""";
171+
var j = 1;
172+
''',
173+
);
174+
}
175+
141176
Future<void> test_replaceSourceIndent_noLeading_empty_crlf() async {
142177
await assertReplacedIndentation(
143178
indentOld: '',

0 commit comments

Comments
 (0)