File tree 3 files changed +58
-5
lines changed
PowerShellEditorServices/Workspace
PowerShellEditorServices.Protocol/Server
test/PowerShellEditorServices.Test/Session
3 files changed +58
-5
lines changed Original file line number Diff line number Diff line change @@ -1096,6 +1096,7 @@ protected async Task HandleCommentHelpRequest(
1096
1096
triggerLine1b ,
1097
1097
out helpLocation ) ;
1098
1098
var result = new CommentHelpRequestResult ( ) ;
1099
+ IList < string > lines = null ;
1099
1100
if ( functionDefinitionAst != null )
1100
1101
{
1101
1102
var funcExtent = functionDefinitionAst . Extent ;
@@ -1104,7 +1105,7 @@ protected async Task HandleCommentHelpRequest(
1104
1105
{
1105
1106
// check if the previous character is `<` because it invalidates
1106
1107
// the param block the follows it.
1107
- var lines = ScriptFile . GetLines ( funcText ) . ToArray ( ) ;
1108
+ lines = ScriptFile . GetLines ( funcText ) ;
1108
1109
var relativeTriggerLine0b = triggerLine1b - funcExtent . StartLineNumber ;
1109
1110
if ( relativeTriggerLine0b > 0 && lines [ relativeTriggerLine0b ] . IndexOf ( "<" ) > - 1 )
1110
1111
{
@@ -1122,8 +1123,12 @@ protected async Task HandleCommentHelpRequest(
1122
1123
requestParams . BlockComment ,
1123
1124
true ,
1124
1125
helpLocation ) ) ;
1126
+
1125
1127
var help = analysisResults ? . FirstOrDefault ( ) ? . Correction ? . Edits [ 0 ] . Text ;
1126
- result . Content = help == null ? null : ScriptFile . GetLines ( help ) . ToArray ( ) ;
1128
+ result . Content = help != null
1129
+ ? ( lines ?? ScriptFile . GetLines ( funcText ) ) . ToArray ( )
1130
+ : null ;
1131
+
1127
1132
if ( helpLocation != null &&
1128
1133
! helpLocation . Equals ( "before" , StringComparison . OrdinalIgnoreCase ) )
1129
1134
{
Original file line number Diff line number Diff line change @@ -197,14 +197,33 @@ 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
- public static IEnumerable < string > GetLines ( string text )
200
+ public static IList < string > GetLines ( string text )
201
201
{
202
202
if ( text == null )
203
203
{
204
204
throw new ArgumentNullException ( nameof ( text ) ) ;
205
205
}
206
206
207
- return text . Split ( '\n ' ) . Select ( line => line . TrimEnd ( '\r ' ) ) ;
207
+ // ReadLine returns null immediately for empty string, so special case it.
208
+ if ( text . Length == 0 )
209
+ {
210
+ return new List < string > { string . Empty } ;
211
+ }
212
+
213
+ using ( var reader = new StringReader ( text ) )
214
+ {
215
+ // 50 is a rough guess for typical average line length, this saves some list
216
+ // resizes in the common case and does not hurt meaningfully if we're wrong.
217
+ var list = new List < string > ( text . Length / 50 ) ;
218
+ string line ;
219
+
220
+ while ( ( line = reader . ReadLine ( ) ) != null )
221
+ {
222
+ list . Add ( line ) ;
223
+ }
224
+
225
+ return list ;
226
+ }
208
227
}
209
228
210
229
/// <summary>
@@ -517,7 +536,7 @@ private void SetFileContents(string fileContents)
517
536
{
518
537
// Split the file contents into lines and trim
519
538
// any carriage returns from the strings.
520
- this . FileLines = GetLines ( fileContents ) . ToList ( ) ;
539
+ this . FileLines = GetLines ( fileContents ) ;
521
540
522
541
// Parse the contents to get syntax tree and errors
523
542
this . ParseFileContents ( ) ;
Original file line number Diff line number Diff line change @@ -296,6 +296,35 @@ public void CanGetRangeAtLineBoundaries()
296
296
297
297
Assert . Equal ( expectedLines , lines ) ;
298
298
}
299
+
300
+ [ Fact ]
301
+ public void CanSplitLines ( )
302
+ {
303
+ Assert . Equal ( TestStringLines , scriptFile . FileLines ) ;
304
+ }
305
+
306
+ [ Fact ]
307
+ public void CanGetSameLinesWithUnixLineBreaks ( )
308
+ {
309
+ var unixFile = ScriptFileChangeTests . CreateScriptFile ( TestString . Replace ( "\r \n " , "\n " ) ) ;
310
+ Assert . Equal ( scriptFile . FileLines , unixFile . FileLines ) ;
311
+ }
312
+
313
+ [ Fact ]
314
+ public void CanGetLineForEmptyString ( )
315
+ {
316
+ var emptyFile = ScriptFileChangeTests . CreateScriptFile ( string . Empty ) ;
317
+ Assert . Equal ( 1 , emptyFile . FileLines . Count ) ;
318
+ Assert . Equal ( string . Empty , emptyFile . FileLines [ 0 ] ) ;
319
+ }
320
+
321
+ [ Fact ]
322
+ public void CanGetLineForSpace ( )
323
+ {
324
+ var spaceFile = ScriptFileChangeTests . CreateScriptFile ( " " ) ;
325
+ Assert . Equal ( 1 , spaceFile . FileLines . Count ) ;
326
+ Assert . Equal ( " " , spaceFile . FileLines [ 0 ] ) ;
327
+ }
299
328
}
300
329
301
330
public class ScriptFilePositionTests
You can’t perform that action at this time.
0 commit comments