Skip to content

Commit 4ae9a90

Browse files
author
Kapil Borle
committed
Add range formatting capability
1 parent 4ae32a3 commit 4ae9a90

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

Engine/Commands/InvokeFormatterCommand.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ public class InvokeFormatterCommand : PSCmdlet, IOutputWriter
3737
[ValidateNotNull]
3838
public object Settings { get; set; }
3939

40+
// [Parameter(Mandatory = false)]
41+
// public Range range { get; set; }
42+
43+
[Parameter(Mandatory = false)]
44+
public int StartLineNumber { get; set; } = -1;
45+
[Parameter(Mandatory = false)]
46+
public int StartColumnNumber { get; private set; } = -1;
47+
[Parameter(Mandatory = false)]
48+
public int EndLineNumber { get; private set; } = -1;
49+
[Parameter(Mandatory = false)]
50+
public int EndColumnNumber { get; private set; } = -1;
51+
4052
#if DEBUG
4153
/// <summary>
4254
/// Attaches to an instance of a .Net debugger
@@ -47,6 +59,7 @@ public SwitchParameter AttachAndDebug
4759
get { return attachAndDebug; }
4860
set { attachAndDebug = value; }
4961
}
62+
5063
private bool attachAndDebug = false;
5164
#endif
5265

@@ -85,7 +98,12 @@ protected override void BeginProcessing()
8598

8699
protected override void ProcessRecord()
87100
{
88-
var text = Formatter.Format(ScriptDefinition, inputSettings, this);
101+
// todo add range parameter
102+
// todo add tests to check range formatting
103+
var range = StartLineNumber == -1 ?
104+
null :
105+
new Range(StartLineNumber, StartColumnNumber, EndLineNumber, EndColumnNumber);
106+
var text = Formatter.Format(ScriptDefinition, inputSettings, range, this);
89107
this.WriteObject(text);
90108
}
91109

Engine/Formatter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class Formatter
1414
public static string Format<TCmdlet>(
1515
string scriptDefinition,
1616
Settings settings,
17+
Range range,
1718
TCmdlet cmdlet) where TCmdlet : PSCmdlet, IOutputWriter
1819
{
1920
Helper.Instance = new Helper(cmdlet.SessionState.InvokeCommand, cmdlet);
@@ -40,7 +41,7 @@ public static string Format<TCmdlet>(
4041
var currentSettings = GetCurrentSettings(settings, rule);
4142
ScriptAnalyzer.Instance.UpdateSettings(currentSettings);
4243
ScriptAnalyzer.Instance.Initialize(cmdlet, null, null, null, null, true, false);
43-
text = ScriptAnalyzer.Instance.Fix(text);
44+
text = ScriptAnalyzer.Instance.Fix(text, range);
4445
}
4546

4647
return text.ToString();

Engine/ScriptAnalyzer.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,21 +1539,25 @@ public string Fix(string scriptDefinition)
15391539
throw new ArgumentNullException(nameof(scriptDefinition));
15401540
}
15411541

1542-
return Fix(new EditableText(scriptDefinition)).ToString();
1542+
return Fix(new EditableText(scriptDefinition), null).ToString();
15431543
}
15441544

15451545
/// <summary>
15461546
/// Fix the violations in the given script text.
15471547
/// </summary>
15481548
/// <param name="text">An object of type `EditableText` that encapsulates the script text to be fixed.</param>
15491549
/// <returns>The same instance of `EditableText` that was passed to the method, but the instance encapsulates the fixed script text. This helps in chaining the Fix method.</returns>
1550-
public EditableText Fix(EditableText text)
1550+
public EditableText Fix(EditableText text, Range range)
15511551
{
15521552
if (text == null)
15531553
{
15541554
throw new ArgumentNullException(nameof(text));
15551555
}
15561556

1557+
// todo validate range
1558+
var isRangeNull = range == null;
1559+
range = isRangeNull ? null : SnapToEdges(text, range);
1560+
var previousLineCount = text.Lines.Length;
15571561
var previousUnusedCorrections = 0;
15581562
do
15591563
{
@@ -1562,6 +1566,7 @@ public EditableText Fix(EditableText text)
15621566
.Select(r => r.SuggestedCorrections)
15631567
.Where(sc => sc != null && sc.Any())
15641568
.Select(sc => sc.First())
1569+
.Where(sc => isRangeNull || (sc.Start >= range.Start && sc.End <= range.End))
15651570
.ToList();
15661571

15671572
int unusedCorrections;
@@ -1579,11 +1584,34 @@ public EditableText Fix(EditableText text)
15791584
}
15801585

15811586
previousUnusedCorrections = unusedCorrections;
1587+
1588+
// todo add a TextLines.NumLines property because accessing TextLines.Lines is expensive
1589+
var lineCount = text.Lines.Length;
1590+
if (!isRangeNull && lineCount != previousLineCount)
1591+
{
1592+
range = new Range(
1593+
range.Start,
1594+
range.End.Shift(lineCount - previousLineCount, 0));
1595+
range = SnapToEdges(text, range);
1596+
}
1597+
1598+
previousLineCount = lineCount;
15821599
} while (previousUnusedCorrections > 0);
15831600

15841601
return text;
15851602
}
15861603

1604+
private static Range SnapToEdges(EditableText text, Range range)
1605+
{
1606+
// todo add TextLines.Validate(range) and TextLines.Validate(position)
1607+
// todo TextLines.Lines should return IList instead of array because TextLines.Lines is expensive
1608+
return new Range(
1609+
range.Start.Line,
1610+
Math.Min(range.Start.Column, 1),
1611+
range.End.Line,
1612+
Math.Max(range.End.Column, text.Lines[range.End.Line - 1].Length));
1613+
}
1614+
15871615
private static IEnumerable<CorrectionExtent> GetCorrectionExtentsForFix(
15881616
IEnumerable<CorrectionExtent> correctionExtents)
15891617
{

0 commit comments

Comments
 (0)