77
88namespace Microsoft . Windows . PowerShell . ScriptAnalyzer
99{
10- // TODO add documentation
10+ /// <summary>
11+ /// A class to represent text to which `TextEdit`s can be applied.
12+ /// </summary>
1113 public class EditableText
1214 {
15+ /// <summary>
16+ /// The text that is available for editing.
17+ /// </summary>
1318 public string Text { get ; private set ; }
19+
20+ /// <summary>
21+ /// The lines in the Text.
22+ /// </summary>
1423 public string [ ] Lines { get ; private set ; }
24+
25+ /// <summary>
26+ /// The new line character in the Text.
27+ /// </summary>
1528 public string NewLine { get ; private set ; }
16- public int NumNewLineChars { get { return NewLine . Length ; } }
1729
30+ /// <summary>
31+ /// Construct an EditableText type object.
32+ /// </summary>
33+ /// <param name="text"></param>
1834 public EditableText ( string text )
1935 {
2036 if ( text == null )
2137 {
2238 throw new ArgumentNullException ( nameof ( text ) ) ;
2339 }
40+
2441 this . Text = text ;
2542 Lines = this . Text . GetLines ( ) . ToArray ( ) ;
2643 NewLine = GetNewLineCharacters ( ) ;
2744 }
2845
29- // TODO Use EditorServices implementation as it is very well tested.
30- public EditableText ApplyEdit ( TextEdit textEdit )
31- {
32- ValidateTextEdit ( textEdit ) ;
33- var stringBuilder = new StringBuilder ( Text . Substring (
34- 0 ,
35- GetOffset ( textEdit . StartLineNumber , textEdit . StartColumnNumber ) ) ) ;
36- stringBuilder . Append ( textEdit . Text ) ;
37- stringBuilder . Append ( Text . Substring ( GetOffset ( textEdit . EndLineNumber , textEdit . EndColumnNumber ) ) ) ;
38- return new EditableText ( stringBuilder . ToString ( ) ) ;
39- }
40-
4146 // TODO replace apply edit with an optimized version of this.
47+ /// <summary>
48+ /// Apply edits defined by a TextEdit object to Text.
49+ /// </summary>
50+ /// <param name="textEdit">A TextEdit object that encapsulates the text and the range that need to be replaced.</param>
51+ /// <returns>An editable object which contains the supplied edit.</returns>
4252 public EditableText ApplyEdit1 ( TextEdit textEdit )
4353 {
4454 ValidateTextEdit ( textEdit ) ;
@@ -91,6 +101,7 @@ public EditableText ApplyEdit1(TextEdit textEdit)
91101
92102 return new EditableText ( String . Join ( NewLine , Lines ) ) ;
93103 }
104+
94105 // TODO Add a method that takes multiple edits, checks if they are unique and applies them.
95106
96107 public override string ToString ( )
@@ -120,7 +131,7 @@ private void ValidateTextEditExtent(TextEdit textEdit)
120131 }
121132 }
122133
123- private int GetOffset ( int lineNumber , int columnNumber )
134+ private int GetOffset ( int lineNumber , int columnNumber )
124135 {
125136 if ( lineNumber < 1 )
126137 {
@@ -137,7 +148,7 @@ private int GetOffset(int lineNumber, int columnNumber)
137148 var offset = 0 ;
138149 for ( var k = 0 ; k < zeroBasedLineNumber ; k ++ )
139150 {
140- offset += Lines [ k ] . Length + NumNewLineChars ;
151+ offset += Lines [ k ] . Length + NewLine . Length ;
141152 }
142153
143154 return offset + zeroBasedColumnNumber ;
@@ -153,7 +164,6 @@ private string GetNewLineCharacters()
153164 return Text . Substring ( Lines [ 0 ] . Length , GetNumNewLineCharacters ( ) ) ;
154165 }
155166
156- // TODO No need to do all this. Just look at the first character at the end of first line.
157167 private int GetNumNewLineCharacters ( )
158168 {
159169 if ( Lines . Length == 1 )
@@ -172,11 +182,5 @@ private int GetNumNewLineCharacters()
172182
173183 return numCharDiff / ( Lines . Length - 1 ) ;
174184 }
175-
176- private IScriptExtent GetExtent ( )
177- {
178- return new ScriptExtent ( new ScriptPosition ( null , 1 , 1 , Lines [ 0 ] ) ,
179- new ScriptPosition ( null , Lines . Length , Lines [ Lines . Length - 1 ] . Length + 1 , Lines [ Lines . Length - 1 ] ) ) ;
180- }
181185 }
182186}
0 commit comments