Skip to content

Commit d039aa2

Browse files
Copilotbergmeister
andauthored
Preserve exhausted formatter selections
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
1 parent e45a772 commit d039aa2

3 files changed

Lines changed: 18 additions & 18 deletions

File tree

‎Engine/Formatter.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public static string Format<TCmdlet>(
5454
ScriptBlockAst scriptAst = null;
5555
Token[] scriptTokens = null;
5656
bool skipParsing = false;
57+
bool isRangeSpecified = range != null;
5758
foreach (var rule in ruleOrder)
5859
{
5960
if (!settings.RuleArguments.ContainsKey(rule))
@@ -68,6 +69,10 @@ public static string Format<TCmdlet>(
6869
text = ScriptAnalyzer.Instance.Fix(text, range, skipParsing, out Range updatedRange, out bool fixesWereApplied, ref scriptAst, ref scriptTokens, skipVariableAnalysis: true);
6970
skipParsing = !fixesWereApplied;
7071
range = updatedRange;
72+
if (isRangeSpecified && range == null)
73+
{
74+
break;
75+
}
7176
}
7277

7378
return text.ToString();

‎Engine/ScriptAnalyzer.cs‎

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,7 +1594,7 @@ public EditableText Fix(EditableText text, Range range, bool skipParsing, out Ra
15941594
}
15951595

15961596
previousUnusedCorrections = unusedCorrections;
1597-
} while (previousUnusedCorrections > 0);
1597+
} while (previousUnusedCorrections > 0 && (isRangeNull || range != null));
15981598

15991599
updatedRange = range;
16001600
return text;
@@ -1743,19 +1743,15 @@ private static EditableText Fix(
17431743
var endLine = range.End.Line + lineDelta;
17441744
if (correction.Lines.Length == 0 && endLine < range.Start.Line)
17451745
{
1746-
if (text.LineCount == 0)
1747-
{
1748-
range = null;
1749-
}
1750-
else
1751-
{
1752-
endLine = Math.Min(Math.Max(endLine, 1), text.LineCount);
1753-
var endColumn = Math.Min(
1754-
Math.Max(range.End.Column + columnDelta, 1),
1755-
text.Lines[endLine - 1].Length + 1);
1756-
var end = new Position(endLine, endColumn);
1757-
range = new Range(end, end);
1758-
}
1746+
range = null;
1747+
}
1748+
else if (correction.Lines.Length == 0
1749+
&& lineDelta < 0
1750+
&& correction.EndLineNumber == range.End.Line)
1751+
{
1752+
range = new Range(
1753+
range.Start,
1754+
new Position(endLine, text.Lines[endLine - 1].Length + 1));
17591755
}
17601756
else
17611757
{

‎Tests/Engine/InvokeFormatter.tests.ps1‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,10 @@ function foo {
105105

106106
$text.ToString() | Should -Be "first`nlast"
107107
$arguments[3].End.Line | Should -Be 1
108+
$arguments[3].End.Column | Should -Be 6
108109
}
109110

110-
It "Should collapse the range when an edit deletes all selected lines" {
111+
It "Should exhaust the range when an edit deletes all selected lines" {
111112
$text = New-Object -TypeName "Microsoft.Windows.PowerShell.ScriptAnalyzer.EditableText" -ArgumentList "first`nlast"
112113
$correctionType = "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent" -as [type]
113114
$correction = [Activator]::CreateInstance(
@@ -127,10 +128,8 @@ function foo {
127128
[Reflection.BindingFlags]'NonPublic, Static')
128129

129130
$fixMethod.Invoke($null, $arguments) | Out-Null
130-
131131
$text.ToString() | Should -Be "first"
132-
$arguments[3].Start.Line | Should -Be 1
133-
$arguments[3].End.Line | Should -Be 1
132+
$arguments[3] | Should -BeNullOrEmpty
134133
}
135134

136135
It "Should apply all corrections when earlier fixes expand the selected line" {

0 commit comments

Comments
 (0)