File tree 3 files changed +51
-5
lines changed
PowerShellEditorServices/Workspace
PowerShellEditorServices.Protocol/Server
test/PowerShellEditorServices.Test/Session
3 files changed +51
-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,40 @@ 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
+ var list = new List < string > ( ) ;
208
+ int cursor = 0 ;
209
+
210
+ while ( cursor < text . Length )
211
+ {
212
+ int nextBreak = text . IndexOf ( '\n ' , cursor ) ;
213
+
214
+ // Last line
215
+ if ( nextBreak == - 1 )
216
+ {
217
+ list . Add ( text . Substring ( cursor ) ) ;
218
+ break ;
219
+ }
220
+
221
+ if ( nextBreak > cursor && text [ nextBreak - 1 ] == '\r ' )
222
+ {
223
+ list . Add ( text . Substring ( cursor , nextBreak - cursor - 1 ) ) ;
224
+ }
225
+ else
226
+ {
227
+ list . Add ( text . Substring ( cursor , nextBreak - cursor ) ) ;
228
+ }
229
+
230
+ cursor = nextBreak + 1 ;
231
+ }
232
+
233
+ return list ;
208
234
}
209
235
210
236
/// <summary>
@@ -517,7 +543,7 @@ private void SetFileContents(string fileContents)
517
543
{
518
544
// Split the file contents into lines and trim
519
545
// any carriage returns from the strings.
520
- this . FileLines = GetLines ( fileContents ) . ToList ( ) ;
546
+ this . FileLines = GetLines ( fileContents ) ;
521
547
522
548
// Parse the contents to get syntax tree and errors
523
549
this . ParseFileContents ( ) ;
Original file line number Diff line number Diff line change @@ -213,6 +213,8 @@ public class ScriptFileGetLinesTests
213
213
new string [ ] { "\r \n " } ,
214
214
StringSplitOptions . None ) ;
215
215
216
+ private const string TestStringUnix = "Line One\n Line Two\n Line Three\n Line Four\n Line Five" ;
217
+
216
218
public ScriptFileGetLinesTests ( )
217
219
{
218
220
this . scriptFile =
@@ -296,6 +298,19 @@ public void CanGetRangeAtLineBoundaries()
296
298
297
299
Assert . Equal ( expectedLines , lines ) ;
298
300
}
301
+
302
+ [ Fact ]
303
+ public void CanSplitLines ( )
304
+ {
305
+ Assert . Equal ( TestStringLines , scriptFile . FileLines ) ;
306
+ }
307
+
308
+ [ Fact ]
309
+ public void CanGetSameLinesWithUnixLineBreaks ( )
310
+ {
311
+ var unixFile = ScriptFileChangeTests . CreateScriptFile ( TestStringUnix ) ;
312
+ Assert . Equal ( scriptFile . FileLines , unixFile . FileLines ) ;
313
+ }
299
314
}
300
315
301
316
public class ScriptFilePositionTests
You can’t perform that action at this time.
0 commit comments