Skip to content

Commit f670863

Browse files
daviwilKayla Davis
authored and
Kayla Davis
committed
Improve ScriptFile loading of trailing newlines
This change fixes a bug in ScriptFile where there would not be a final empty line if the last content line ended with a newline. This was causing some problems when editing the last line of the file in an editor because the line numbers would be out of range.
1 parent b5c5b54 commit f670863

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

src/PowerShellEditorServices/Session/ScriptFile.cs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -227,19 +227,15 @@ public int GetOffsetAtPosition(int lineNumber, int columnNumber)
227227
/// <param name="textReader">A TextReader to use for reading file contents.</param>
228228
private void ReadFile(TextReader textReader)
229229
{
230-
this.FileLines = new List<string>();
231-
232-
// Read the file contents line by line
233-
string fileLine = null;
234-
do
235-
{
236-
fileLine = textReader.ReadLine();
237-
if (fileLine != null)
238-
{
239-
FileLines.Add(fileLine);
240-
}
241-
}
242-
while (fileLine != null);
230+
string fileContents = textReader.ReadToEnd();
231+
232+
// Split the file contents into lines and trim
233+
// any carriage returns from the strings.
234+
this.FileLines =
235+
fileContents
236+
.Split('\n')
237+
.Select(line => line.TrimEnd('\r'))
238+
.ToList();
243239

244240
// Parse the contents to get syntax tree and errors
245241
this.ParseFileContents();

0 commit comments

Comments
 (0)