@@ -23,6 +23,7 @@ public partial class PSConsoleReadLine
23
23
private int _tabCommandCount ;
24
24
private CommandCompletion _tabCompletions ;
25
25
private Runspace _runspace ;
26
+ private string _directorySeparator ;
26
27
27
28
private static readonly Dictionary < CompletionResultType , PSKeyInfo [ ] > KeysEndingCompletion =
28
29
new Dictionary < CompletionResultType , PSKeyInfo [ ] >
@@ -40,7 +41,7 @@ public partial class PSConsoleReadLine
40
41
private static readonly char [ ] EolChars = { '\r ' , '\n ' } ;
41
42
42
43
// String helper for directory paths
43
- private static readonly string DirectorySeparatorString = System . IO . Path . DirectorySeparatorChar . ToString ( ) ;
44
+ private static readonly string DefaultDirectorySeparator = System . IO . Path . DirectorySeparatorChar . ToString ( ) ;
44
45
45
46
// Stub helper method so completion can be mocked
46
47
[ ExcludeFromCodeCoverage ]
@@ -281,10 +282,26 @@ private CommandCompletion GetCompletions()
281
282
System . Management . Automation . PowerShell ps ;
282
283
if ( ! _mockableMethods . RunspaceIsRemote ( _runspace ) )
283
284
{
285
+ _directorySeparator ??= DefaultDirectorySeparator ;
284
286
ps = System . Management . Automation . PowerShell . Create ( RunspaceMode . CurrentRunspace ) ;
285
287
}
286
288
else
287
289
{
290
+ if ( _directorySeparator is null )
291
+ {
292
+ // Use the default separator by default.
293
+ _directorySeparator = DefaultDirectorySeparator ;
294
+ PSPrimitiveDictionary dict = _runspace . GetApplicationPrivateData ( ) ;
295
+
296
+ if ( dict [ "PSVersionTable" ] is PSPrimitiveDictionary versionTable )
297
+ {
298
+ // If the 'Platform' key is available and its value is not 'Win*', then the server side is macOS or Linux.
299
+ // In that case, we use the forward slash '/' as the directory separator.
300
+ // Otherwise, the server side is Windows and we use the backward slash '\' instead.
301
+ _directorySeparator = versionTable [ "Platform" ] is string platform && ! platform . StartsWith ( "Win" , StringComparison . Ordinal ) ? "/" : @"\" ;
302
+ }
303
+ }
304
+
288
305
ps = System . Management . Automation . PowerShell . Create ( ) ;
289
306
ps . Runspace = _runspace ;
290
307
}
@@ -375,25 +392,25 @@ private void DoReplacementForCompletion(CompletionResult completionResult, Comma
375
392
completions . ReplacementLength = replacementText . Length ;
376
393
}
377
394
378
- private static string GetReplacementTextForDirectory ( string replacementText , ref int cursorAdjustment )
395
+ private string GetReplacementTextForDirectory ( string replacementText , ref int cursorAdjustment )
379
396
{
380
- if ( ! replacementText . EndsWith ( DirectorySeparatorString , StringComparison . Ordinal ) )
397
+ if ( ! replacementText . EndsWith ( _directorySeparator , StringComparison . Ordinal ) )
381
398
{
382
- if ( replacementText . EndsWith ( String . Format ( "{0}\' " , DirectorySeparatorString ) , StringComparison . Ordinal ) ||
383
- replacementText . EndsWith ( String . Format ( "{0}\" " , DirectorySeparatorString ) , StringComparison . Ordinal ) )
399
+ if ( replacementText . EndsWith ( string . Format ( "{0}\' " , _directorySeparator ) , StringComparison . Ordinal ) ||
400
+ replacementText . EndsWith ( string . Format ( "{0}\" " , _directorySeparator ) , StringComparison . Ordinal ) )
384
401
{
385
402
cursorAdjustment = - 1 ;
386
403
}
387
404
else if ( replacementText . EndsWith ( "'" , StringComparison . Ordinal ) ||
388
405
replacementText . EndsWith ( "\" " , StringComparison . Ordinal ) )
389
406
{
390
407
var len = replacementText . Length ;
391
- replacementText = replacementText . Substring ( 0 , len - 1 ) + System . IO . Path . DirectorySeparatorChar + replacementText [ len - 1 ] ;
408
+ replacementText = replacementText . Substring ( 0 , len - 1 ) + _directorySeparator + replacementText [ len - 1 ] ;
392
409
cursorAdjustment = - 1 ;
393
410
}
394
411
else
395
412
{
396
- replacementText = replacementText + System . IO . Path . DirectorySeparatorChar ;
413
+ replacementText += _directorySeparator ;
397
414
}
398
415
}
399
416
return replacementText ;
0 commit comments