@@ -90,7 +90,7 @@ public ScriptFileMarker[] SyntaxMarkers
90
90
/// <summary>
91
91
/// Gets the list of strings for each line of the file.
92
92
/// </summary>
93
- internal IList < string > FileLines
93
+ internal List < string > FileLines
94
94
{
95
95
get ;
96
96
private set ;
@@ -197,7 +197,18 @@ public ScriptFile(
197
197
/// </summary>
198
198
/// <param name="text">Input string to be split up into lines.</param>
199
199
/// <returns>The lines in the string.</returns>
200
+ [ Obsolete ( "This method is not designed for public exposure and will be retired in later versions of EditorServices" ) ]
200
201
public static IList < string > GetLines ( string text )
202
+ {
203
+ return GetLinesInternal ( text ) ;
204
+ }
205
+
206
+ /// <summary>
207
+ /// Get the lines in a string.
208
+ /// </summary>
209
+ /// <param name="text">Input string to be split up into lines.</param>
210
+ /// <returns>The lines in the string.</returns>
211
+ internal static List < string > GetLinesInternal ( string text )
201
212
{
202
213
if ( text == null )
203
214
{
@@ -311,38 +322,12 @@ public void ValidatePosition(BufferPosition bufferPosition)
311
322
/// <param name="column">The 1-based column to be validated.</param>
312
323
public void ValidatePosition ( int line , int column )
313
324
{
314
- ValidatePosition ( line , column , isInsertion : false ) ;
315
- }
316
-
317
- /// <summary>
318
- /// Throws ArgumentOutOfRangeException if the given position is outside
319
- /// of the file's buffer extents. If the position is for an insertion (an applied change)
320
- /// the index may be 1 past the end of the file, which is just appended.
321
- /// </summary>
322
- /// <param name="line">The 1-based line to be validated.</param>
323
- /// <param name="column">The 1-based column to be validated.</param>
324
- /// <param name="isInsertion">If true, the position to validate is for an applied change.</param>
325
- public void ValidatePosition ( int line , int column , bool isInsertion )
326
- {
327
- // If new content is being added, VSCode sometimes likes to add it at (FileLines.Count + 1),
328
- // which used to crash EditorServices. Now we append it on to the end of the file.
329
- // See https://github.com/PowerShell/vscode-powershell/issues/1283
330
- int maxLine = isInsertion ? this . FileLines . Count + 1 : this . FileLines . Count ;
325
+ int maxLine = this . FileLines . Count ;
331
326
if ( line < 1 || line > maxLine )
332
327
{
333
328
throw new ArgumentOutOfRangeException ( $ "Position { line } :{ column } is outside of the line range of 1 to { maxLine } .") ;
334
329
}
335
330
336
- // If we are inserting at the end of the file, the column should be 1
337
- if ( isInsertion && line == maxLine )
338
- {
339
- if ( column != 1 )
340
- {
341
- throw new ArgumentOutOfRangeException ( $ "Insertion at the end of a file must occur at column 1") ;
342
- }
343
- return ;
344
- }
345
-
346
331
// The maximum column is either **one past** the length of the string
347
332
// or 1 if the string is empty.
348
333
string lineString = this . FileLines [ line - 1 ] ;
@@ -354,6 +339,19 @@ public void ValidatePosition(int line, int column, bool isInsertion)
354
339
}
355
340
}
356
341
342
+
343
+ /// <summary>
344
+ /// Defunct ValidatePosition method call. The isInsertion parameter is ignored.
345
+ /// </summary>
346
+ /// <param name="line"></param>
347
+ /// <param name="column"></param>
348
+ /// <param name="isInsertion"></param>
349
+ [ Obsolete ( "Use ValidatePosition(int, int) instead" ) ]
350
+ public void ValidatePosition ( int line , int column , bool isInsertion )
351
+ {
352
+ ValidatePosition ( line , column ) ;
353
+ }
354
+
357
355
/// <summary>
358
356
/// Applies the provided FileChange to the file's contents
359
357
/// </summary>
@@ -373,9 +371,6 @@ public void ApplyChange(FileChange fileChange)
373
371
}
374
372
else
375
373
{
376
- this . ValidatePosition ( fileChange . Line , fileChange . Offset , isInsertion : true ) ;
377
- this . ValidatePosition ( fileChange . EndLine , fileChange . EndOffset , isInsertion : true ) ;
378
-
379
374
// VSCode sometimes likes to give the change start line as (FileLines.Count + 1).
380
375
// This used to crash EditorServices, but we now treat it as an append.
381
376
// See https://github.com/PowerShell/vscode-powershell/issues/1283
@@ -387,9 +382,19 @@ public void ApplyChange(FileChange fileChange)
387
382
this . FileLines . Add ( finalLine ) ;
388
383
}
389
384
}
385
+ // Similarly, when lines are deleted from the end of the file,
386
+ // VSCode likes to give the end line as (FileLines.Count + 1).
387
+ else if ( fileChange . EndLine == this . FileLines . Count + 1 && String . Empty . Equals ( fileChange . InsertString ) )
388
+ {
389
+ int lineIndex = fileChange . Line - 1 ;
390
+ this . FileLines . RemoveRange ( lineIndex , this . FileLines . Count - lineIndex ) ;
391
+ }
390
392
// Otherwise, the change needs to go between existing content
391
393
else
392
394
{
395
+ this . ValidatePosition ( fileChange . Line , fileChange . Offset ) ;
396
+ this . ValidatePosition ( fileChange . EndLine , fileChange . EndOffset ) ;
397
+
393
398
// Get the first fragment of the first line
394
399
string firstLineFragment =
395
400
this . FileLines [ fileChange . Line - 1 ]
@@ -576,7 +581,7 @@ private void SetFileContents(string fileContents)
576
581
{
577
582
// Split the file contents into lines and trim
578
583
// any carriage returns from the strings.
579
- this . FileLines = GetLines ( fileContents ) ;
584
+ this . FileLines = GetLinesInternal ( fileContents ) ;
580
585
581
586
// Parse the contents to get syntax tree and errors
582
587
this . ParseFileContents ( ) ;
0 commit comments