Skip to content

Commit

Permalink
Request path completions from nearest / or \
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyriar committed Aug 3, 2024
1 parent 220cb47 commit c68db99
Showing 1 changed file with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,17 @@ function Send-Completions {
$cursorIndex = $newCursorIndex
}
}
# If it contains `/` or `\`, get completions from the nearest `/` or `\` such that file
# completions are consistent regardless of where it was requested
elseif ($lastWord -match '[/\\]') {
$lastSlashIndex = $completionPrefix.LastIndexOfAny(@('/', '\'))
if ($lastSlashIndex -ne -1 && $lastSlashIndex -lt $cursorIndex) {
$newCursorIndex = $lastSlashIndex + 1
$completionPrefix = $completionPrefix.Substring(0, $newCursorIndex)
$prefixCursorDelta = $cursorIndex - $newCursorIndex
$cursorIndex = $newCursorIndex
}
}

# Get completions using TabExpansion2
$completions = TabExpansion2 -inputScript $completionPrefix -cursorColumn $cursorIndex
Expand Down Expand Up @@ -293,6 +304,17 @@ function Send-Completions {
# If there is no space, get completions using CompletionCompleters as it gives us more
# control and works on the empty string
else {
# If it contains `/` or `\`, get completions from the nearest `/` or `\` such that file
# completions are consistent regardless of where it was requested
if ($completionPrefix -match '[/\\]') {
$lastSlashIndex = $completionPrefix.LastIndexOfAny(@('/', '\'))
if ($lastSlashIndex -ne -1 && $lastSlashIndex -lt $cursorIndex) {
$newCursorIndex = $lastSlashIndex + 1
$completionPrefix = $completionPrefix.Substring(0, $newCursorIndex)
$prefixCursorDelta = $cursorIndex - $newCursorIndex
$cursorIndex = $newCursorIndex
}
}
# Note that CompleteCommand isn't included here as it's expensive
$completions = $(
# Add trailing \ for directories so behavior aligns with TabExpansion2
Expand Down

0 comments on commit c68db99

Please sign in to comment.