Skip to content

Commit e021fce

Browse files
Copilotbergmeister
andauthored
Fix formatter range tracking through iterative edits
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
1 parent 9d5d3b7 commit e021fce

2 files changed

Lines changed: 62 additions & 13 deletions

File tree

‎Engine/ScriptAnalyzer.cs‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,7 +1556,6 @@ public EditableText Fix(EditableText text, Range range, bool skipParsing, out Ra
15561556
}
15571557

15581558
range = isRangeNull ? null : SnapToEdges(text, range);
1559-
var previousLineCount = text.LineCount;
15601559
var previousUnusedCorrections = 0;
15611560
do
15621561
{
@@ -1578,7 +1577,7 @@ public EditableText Fix(EditableText text, Range range, bool skipParsing, out Ra
15781577

15791578
this.outputWriter.WriteVerbose($"Found {corrections.Count} violations.");
15801579
int unusedCorrections;
1581-
Fix(text, corrections, out unusedCorrections);
1580+
Fix(text, corrections, out unusedCorrections, ref range);
15821581
var numberOfFixedViolatons = corrections.Count - unusedCorrections;
15831582
fixesWereApplied = numberOfFixedViolatons > 0;
15841583
this.outputWriter.WriteVerbose($"Fixed {numberOfFixedViolatons} violations.");
@@ -1595,16 +1594,6 @@ public EditableText Fix(EditableText text, Range range, bool skipParsing, out Ra
15951594
}
15961595

15971596
previousUnusedCorrections = unusedCorrections;
1598-
var lineCount = text.LineCount;
1599-
if (!isRangeNull && lineCount != previousLineCount)
1600-
{
1601-
range = new Range(
1602-
range.Start,
1603-
range.End.Shift(lineCount - previousLineCount, 0));
1604-
range = SnapToEdges(text, range);
1605-
}
1606-
1607-
previousLineCount = lineCount;
16081597
} while (previousUnusedCorrections > 0);
16091598

16101599
updatedRange = range;
@@ -1728,14 +1717,29 @@ private static IEnumerable<CorrectionExtent> GetCorrectionExtentsForFix(
17281717
private static EditableText Fix(
17291718
EditableText text,
17301719
List<CorrectionExtent> correctionExtents,
1731-
out int unusedCorrections)
1720+
out int unusedCorrections,
1721+
ref Range range)
17321722
{
17331723
var correctionsToUse = GetCorrectionExtentsForFix(correctionExtents);
17341724
var count = 0;
17351725
foreach (var correction in correctionsToUse)
17361726
{
17371727
count++;
17381728
text.ApplyEdit(correction);
1729+
if (range != null)
1730+
{
1731+
var lineDelta = correction.Lines.Length - 1
1732+
- (correction.EndLineNumber - correction.StartLineNumber);
1733+
var columnDelta = 0;
1734+
if (correction.EndLineNumber == range.End.Line)
1735+
{
1736+
var replacementEndColumn = correction.Lines.Last().TrimEnd('\r').Length
1737+
+ (correction.Lines.Length == 1 ? correction.StartColumnNumber : 1);
1738+
columnDelta = replacementEndColumn - correction.EndColumnNumber;
1739+
}
1740+
1741+
range = new Range(range.Start, range.End.Shift(lineDelta, columnDelta));
1742+
}
17391743
}
17401744

17411745
unusedCorrections = correctionExtents.Count - count;

‎Tests/Engine/InvokeFormatter.tests.ps1‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,51 @@ function foo {
8181

8282
Invoke-Formatter -ScriptDefinition $def -Range @(3, 1, 4, 1) | Should -Be $expected
8383
}
84+
85+
It "Should apply all corrections when earlier fixes expand the selected line" {
86+
$def = "if(`$a){'x'}`nif(`$b){'y'}"
87+
$expected = "if(`$a){'x'}`nif (`$b) { 'y' }"
88+
89+
Invoke-Formatter -ScriptDefinition $def -Range @(2, 1, 2, 12) | Should -Be $expected
90+
}
91+
92+
It "Should track the selected lines when fixes <Name>" -TestCases @(
93+
@{
94+
Name = 'expand the last line'
95+
Selected = "if(`$b){'y'}"
96+
Expected = "if (`$b) { 'y' }"
97+
}
98+
@{
99+
Name = 'shorten the last line'
100+
Selected = "if (`$b) { 'y' }"
101+
Expected = "if (`$b) { 'y' }"
102+
}
103+
@{
104+
Name = 'expand multiple lines'
105+
Selected = "if(`$b){'y'}`nif(`$c){'z'}"
106+
Expected = "if (`$b) { 'y' }`nif (`$c) { 'z' }"
107+
}
108+
@{
109+
Name = 'remove a line'
110+
Selected = "if(`$b)`n{`n'y'`n}"
111+
Expected = "if (`$b) {`n 'y'`n}"
112+
}
113+
@{
114+
Name = 'add a line'
115+
Selected = "if(`$b) {`n'y'`n}"
116+
Expected = "if (`$b)`n{`n 'y'`n}"
117+
Settings = 'CodeFormattingAllman'
118+
}
119+
) {
120+
param($Selected, $Expected, $Settings = 'CodeFormatting')
121+
122+
$def = "if(`$a){'x'}`n$Selected`nif(`$d){'w'}"
123+
$lines = $Selected -split "`n"
124+
$range = @(2, 1, (1 + $lines.Count), (1 + $lines[-1].Length))
125+
126+
Invoke-Formatter -ScriptDefinition $def -Range $range -Settings $Settings |
127+
Should -Be "if(`$a){'x'}`n$Expected`nif(`$d){'w'}"
128+
}
84129
}
85130

86131
Context "When no settings are given" {

0 commit comments

Comments
 (0)