# Send tab completion to terminal function Send-Completions { # Get current command line $commandLine = "" # TODO: Take cursor into account $cursor = 0 # TODO: Since fuzzy matching exists, should completions be provided only for character after the # last space and then filter on the client side? That would let you trigger ctrl+space # anywhere on a word and have full completions available [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$commandLine, [ref]$cursor) # Get and send completions $completionPrefix = $commandLine if ($completionPrefix.Length -eq 0) { # TODO: Send bell instead? $result = "`e]633;Completions`a" Write-Host -NoNewLine $result return } $completions = TabExpansion2 -inputScript $completionPrefix -cursorColumn $completionPrefix.Length if ($null -eq $completions.CompletionMatches) { $result = "`e]633;Completions`a" } else { $result = "`e]633;Completions;$($completions.ReplacementIndex);$($completions.ReplacementLength);" $serialized = $completions.CompletionMatches | ConvertTo-Json -Compress $result += $serialized $result += "`a" } Write-Host -NoNewLine $result } function Send-Completions2 { $commandLine = "" $cursorIndex = 0 # TODO: Since fuzzy matching exists, should completions be provided only for character after the # last space and then filter on the client side? That would let you trigger ctrl+space # anywhere on a word and have full completions available [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$commandLine, [ref]$cursorIndex) $completionPrefix = $commandLine # Get completions $result = "`e]633;Completions" if ($completionPrefix.Length -gt 0) { # Get and send completions $completions = TabExpansion2 -inputScript $completionPrefix -cursorColumn $cursorIndex if ($null -ne $completions.CompletionMatches) { $result += ";$($completions.ReplacementIndex);$($completions.ReplacementLength);$($cursorIndex);" $result += $completions.CompletionMatches | ConvertTo-Json -Compress } } $result += "`a" Write-Host -NoNewLine $result } function Set-MappedKeyHandlers { # # Conditionally enable suggestions # if ($env:VSCODE_SUGGEST -eq '1') { # Remove-Item Env:VSCODE_SUGGEST # VS Code send completions request (may override Ctrl+Spacebar) Set-PSReadLineKeyHandler -Chord 'F12,b' -ScriptBlock { Send-Completions2 } # MIKE TODO! This is awesome but doesn't work now, because we backspace and # retype the command, re-triggering the '-' to get sent. Dumb. # # Suggest trigger characters # Set-PSReadLineKeyHandler -Chord "-" -ScriptBlock { # [Microsoft.PowerShell.PSConsoleReadLine]::Insert("-") # Send-Completions2 # } # } } # Register key handlers if PSReadLine is available if (Get-Module -Name PSReadLine) { Set-MappedKeyHandlers }