Skip to content

Commit 662d961

Browse files
author
Kapil Borle
authored
Merge pull request #785 from PowerShell/kapilmb/update-formatter
Make minor improvements to fixing mechanism
2 parents bec2128 + 4c15216 commit 662d961

File tree

4 files changed

+54
-8
lines changed

4 files changed

+54
-8
lines changed

Engine/EditableText.cs

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Collections.ObjectModel;
45
using System.Globalization;
56
using System.Linq;
67
using System.Management.Automation.Language;
@@ -16,6 +17,11 @@ public class EditableText
1617
{
1718
private TextLines lines { get; set; }
1819

20+
/// <summary>
21+
/// Return the number of lines in the text.
22+
/// </summary>
23+
public int LineCount => lines.Count;
24+
1925
/// <summary>
2026
/// The text that is available for editing.
2127
/// </summary>
@@ -24,7 +30,7 @@ public class EditableText
2430
/// <summary>
2531
/// The lines in the Text.
2632
/// </summary>
27-
public string[] Lines { get { return lines.ToArray(); } }
33+
public ReadOnlyCollection<string> Lines => lines.ReadOnly();
2834

2935
/// <summary>
3036
/// The new line character in the Text.
@@ -118,8 +124,8 @@ public bool IsValidRange(Range range)
118124
throw new ArgumentNullException(nameof(range));
119125
}
120126

121-
return range.Start.Line <= Lines.Length
122-
&& range.End.Line <= Lines.Length
127+
return range.Start.Line <= Lines.Count
128+
&& range.End.Line <= Lines.Count
123129
&& range.Start.Column <= Lines[range.Start.Line - 1].Length
124130
&& range.End.Column <= Lines[range.End.Line - 1].Length + 1;
125131
}

Engine/ScriptAnalyzer.cs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1557,7 +1557,6 @@ public EditableText Fix(EditableText text, Range range, out Range updatedRange)
15571557
throw new ArgumentNullException(nameof(text));
15581558
}
15591559

1560-
// todo validate range
15611560
var isRangeNull = range == null;
15621561
if (!isRangeNull && !text.IsValidRange(range))
15631562
{
@@ -1569,7 +1568,7 @@ public EditableText Fix(EditableText text, Range range, out Range updatedRange)
15691568
}
15701569

15711570
range = isRangeNull ? null : SnapToEdges(text, range);
1572-
var previousLineCount = text.Lines.Length;
1571+
var previousLineCount = text.LineCount;
15731572
var previousUnusedCorrections = 0;
15741573
do
15751574
{
@@ -1598,9 +1597,7 @@ public EditableText Fix(EditableText text, Range range, out Range updatedRange)
15981597
}
15991598

16001599
previousUnusedCorrections = unusedCorrections;
1601-
1602-
// todo add a TextLines.NumLines property because accessing TextLines.Lines is expensive
1603-
var lineCount = text.Lines.Length;
1600+
var lineCount = text.LineCount;
16041601
if (!isRangeNull && lineCount != previousLineCount)
16051602
{
16061603
range = new Range(

Engine/TextLines.cs

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Collections.ObjectModel;
45
using System.Globalization;
56
using System.Linq;
67

@@ -80,6 +81,15 @@ public string this[int index]
8081
}
8182
}
8283

84+
/// <summary>
85+
/// Return a readonly collection of the current object.
86+
/// </summary>
87+
/// <returns>A readonly collection of the current object.</returns>
88+
public ReadOnlyCollection<string> ReadOnly()
89+
{
90+
return new ReadOnlyCollection<string>(this);
91+
}
92+
8393
/// <summary>
8494
/// Adds the given string to the end of the list.
8595
/// </summary>

Tests/Engine/EditableText.tests.ps1

+33
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,38 @@ function foo {
104104
$result = $editableText.ApplyEdit($edit)
105105
$result.ToString() | Should Be $expected
106106
}
107+
108+
It "Should return a read-only collection of lines in the text" {
109+
$def = @'
110+
function foo {
111+
param(
112+
[bool] $param1
113+
)
114+
}
115+
'@
116+
$text = New-Object `
117+
-TypeName "Microsoft.Windows.PowerShell.ScriptAnalyzer.EditableText" `
118+
-ArgumentList @($def)
119+
120+
{$text.Lines.Add("abc")} | Should Throw
121+
}
122+
123+
It "Should return the correct number of lines in the text" {
124+
$def = @'
125+
function foo
126+
{
127+
get-childitem
128+
$x=1+2
129+
$hashtable = @{
130+
property1 = "value"
131+
anotherProperty = "another value"
132+
}
133+
}
134+
'@
135+
$text = New-Object `
136+
-TypeName "Microsoft.Windows.PowerShell.ScriptAnalyzer.EditableText" `
137+
-ArgumentList @($def)
138+
$text.LineCount | Should Be 9
139+
}
107140
}
108141
}

0 commit comments

Comments
 (0)