@@ -245,6 +245,17 @@ function Send-Completions {
245
245
$cursorIndex = $newCursorIndex
246
246
}
247
247
}
248
+ # If it contains `/` or `\`, get completions from the nearest `/` or `\` such that file
249
+ # completions are consistent regardless of where it was requested
250
+ elseif ($lastWord -match ' [/\\]' ) {
251
+ $lastSlashIndex = $completionPrefix.LastIndexOfAny (@ (' /' , ' \' ))
252
+ if ($lastSlashIndex -ne -1 && $lastSlashIndex -lt $cursorIndex ) {
253
+ $newCursorIndex = $lastSlashIndex + 1
254
+ $completionPrefix = $completionPrefix.Substring (0 , $newCursorIndex )
255
+ $prefixCursorDelta = $cursorIndex - $newCursorIndex
256
+ $cursorIndex = $newCursorIndex
257
+ }
258
+ }
248
259
249
260
# Get completions using TabExpansion2
250
261
$completions = TabExpansion2 - inputScript $completionPrefix - cursorColumn $cursorIndex
@@ -293,6 +304,17 @@ function Send-Completions {
293
304
# If there is no space, get completions using CompletionCompleters as it gives us more
294
305
# control and works on the empty string
295
306
else {
307
+ # If it contains `/` or `\`, get completions from the nearest `/` or `\` such that file
308
+ # completions are consistent regardless of where it was requested
309
+ if ($completionPrefix -match ' [/\\]' ) {
310
+ $lastSlashIndex = $completionPrefix.LastIndexOfAny (@ (' /' , ' \' ))
311
+ if ($lastSlashIndex -ne -1 && $lastSlashIndex -lt $cursorIndex ) {
312
+ $newCursorIndex = $lastSlashIndex + 1
313
+ $completionPrefix = $completionPrefix.Substring (0 , $newCursorIndex )
314
+ $prefixCursorDelta = $cursorIndex - $newCursorIndex
315
+ $cursorIndex = $newCursorIndex
316
+ }
317
+ }
296
318
# Note that CompleteCommand isn't included here as it's expensive
297
319
$completions = $ (
298
320
# Add trailing \ for directories so behavior aligns with TabExpansion2
0 commit comments