Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request completions for args at beginning of word #224420

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -215,21 +215,40 @@ function Set-MappedKeyHandlers {
function Send-Completions {
$commandLine = ""
$cursorIndex = 0
$prefixCursorDelta = 0
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$commandLine, [ref]$cursorIndex)
$completionPrefix = $commandLine

# Start completions sequence
$result = "$([char]0x1b)]633;Completions"

# If there is a space in the input, defer to TabExpansion2 as it's more complicated to
# determine what type of completions to use
# determine what type of completions to use.
# `[` is included here as namespace commands are not included in CompleteCommand(''),
# additionally for some reason CompleteVariable('[') causes the prompt to clear and reprint
# multiple times
if ($completionPrefix.Contains(' ') -or $completionPrefix.Contains('[') -or $PSVersionTable.PSVersion -lt "6.0") {

# Adjust the completion prefix and cursor index such that tab expansion will be requested
# immediately after the last whitespace. This allows the client to perform fuzzy filtering
# such that requesting completions in the middle of a word should show the same completions
# as at the start. This only happens when the last word does not include `/` and `\` as
# often it will significantly change completions like when navigating directories.
$lastWhitespaceIndex = $completionPrefix.LastIndexOf(' ')
$lastWord = $completionPrefix.Substring($lastWhitespaceIndex + 1)
if ($lastWord.Contains('/') -eq $false -and $lastWord.Contains('\\') -eq $false) {
if ($lastWhitespaceIndex -ne -1 -and $lastWhitespaceIndex -lt $cursorIndex) {
$newCursorIndex = $lastWhitespaceIndex + 1
$completionPrefix = $completionPrefix.Substring(0, $newCursorIndex)
$prefixCursorDelta = $cursorIndex - $newCursorIndex
$cursorIndex = $newCursorIndex
}
}

# Get completions using TabExpansion2
$completions = TabExpansion2 -inputScript $completionPrefix -cursorColumn $cursorIndex
if ($null -ne $completions.CompletionMatches) {
$result += ";$($completions.ReplacementIndex);$($completions.ReplacementLength);$($cursorIndex);"
$result += ";$($completions.ReplacementIndex);$($completions.ReplacementLength + $prefixCursorDelta);$($cursorIndex - $prefixCursorDelta);"
$json = [System.Collections.ArrayList]@($completions.CompletionMatches)
# Add `.` and `..` to the completions list for results that only contain files and dirs
if ($completions.CompletionMatches.Count -gt 0 -and $completions.CompletionMatches.Where({ $_.ResultType -eq 3 -or $_.ResultType -eq 4 })) {
Expand Down Expand Up @@ -269,7 +288,7 @@ function Send-Completions {
([System.Management.Automation.CompletionCompleters]::CompleteVariable(''))
)
if ($null -ne $completions) {
$result += ";$($completions.ReplacementIndex);$($completions.ReplacementLength);$($cursorIndex);"
$result += ";$($completions.ReplacementIndex);$($completions.ReplacementLength + $prefixCursorDelta);$($cursorIndex - $prefixCursorDelta);"
$mappedCommands = Compress-Completions($completions)
$result += $mappedCommands | ConvertTo-Json -Compress
} else {
Expand Down
Loading