@@ -239,113 +239,4 @@ function Set-MappedKeyHandlers {
239239 Set-MappedKeyHandler - Chord Alt+ Spacebar - Sequence ' F12,b'
240240 Set-MappedKeyHandler - Chord Shift+ Enter - Sequence ' F12,c'
241241 Set-MappedKeyHandler - Chord Shift+ End - Sequence ' F12,d'
242-
243- # Enable suggestions if the environment variable is set and Windows PowerShell is not being used
244- # as APIs are not available to support this feature
245- if ($env: VSCODE_SUGGEST -eq ' 1' -and $PSVersionTable.PSVersion -ge " 7.0" ) {
246- Remove-Item Env:VSCODE_SUGGEST
247-
248- # VS Code send completions request (may override Ctrl+Spacebar)
249- Set-PSReadLineKeyHandler - Chord ' F12,e' - ScriptBlock {
250- Send-Completions
251- }
252- }
253- }
254-
255- function Send-Completions {
256- $commandLine = " "
257- $cursorIndex = 0
258- $prefixCursorDelta = 0
259- [Microsoft.PowerShell.PSConsoleReadLine ]::GetBufferState([ref ]$commandLine , [ref ]$cursorIndex )
260- $completionPrefix = $commandLine
261-
262- # Start completions sequence
263- $result = " $ ( [char ]0x1b ) ]633;Completions"
264-
265- # Only provide completions for arguments and defer to TabExpansion2.
266- # `[` is included here as namespace commands are not included in CompleteCommand(''),
267- # additionally for some reason CompleteVariable('[') causes the prompt to clear and reprint
268- # multiple times
269- if ($completionPrefix.Contains (' ' )) {
270-
271- # Adjust the completion prefix and cursor index such that tab expansion will be requested
272- # immediately after the last whitespace. This allows the client to perform fuzzy filtering
273- # such that requesting completions in the middle of a word should show the same completions
274- # as at the start. This only happens when the last word does not include special characters:
275- # - `-`: Completion change when flags are used.
276- # - `/` and `\`: Completions change when navigating directories.
277- # - `$`: Completions change when variables.
278- $lastWhitespaceIndex = $completionPrefix.LastIndexOf (' ' )
279- $lastWord = $completionPrefix.Substring ($lastWhitespaceIndex + 1 )
280- if ($lastWord -match ' ^-' ) {
281- $newCursorIndex = $lastWhitespaceIndex + 2
282- $completionPrefix = $completionPrefix.Substring (0 , $newCursorIndex )
283- $prefixCursorDelta = $cursorIndex - $newCursorIndex
284- $cursorIndex = $newCursorIndex
285- }
286- elseif ($lastWord -notmatch ' [/\\$]' ) {
287- if ($lastWhitespaceIndex -ne -1 -and $lastWhitespaceIndex -lt $cursorIndex ) {
288- $newCursorIndex = $lastWhitespaceIndex + 1
289- $completionPrefix = $completionPrefix.Substring (0 , $newCursorIndex )
290- $prefixCursorDelta = $cursorIndex - $newCursorIndex
291- $cursorIndex = $newCursorIndex
292- }
293- }
294- # If it contains `/` or `\`, get completions from the nearest `/` or `\` such that file
295- # completions are consistent regardless of where it was requested
296- elseif ($lastWord -match ' [/\\]' ) {
297- $lastSlashIndex = $completionPrefix.LastIndexOfAny (@ (' /' , ' \' ))
298- if ($lastSlashIndex -ne -1 -and $lastSlashIndex -lt $cursorIndex ) {
299- $newCursorIndex = $lastSlashIndex + 1
300- $completionPrefix = $completionPrefix.Substring (0 , $newCursorIndex )
301- $prefixCursorDelta = $cursorIndex - $newCursorIndex
302- $cursorIndex = $newCursorIndex
303- }
304- }
305-
306- # Get completions using TabExpansion2
307- $completions = $null
308- $completionMatches = $null
309- try
310- {
311- $completions = TabExpansion2 - inputScript $completionPrefix - cursorColumn $cursorIndex
312- $completionMatches = $completions.CompletionMatches | Where-Object { $_.ResultType -ne [System.Management.Automation.CompletionResultType ]::ProviderContainer -and $_.ResultType -ne [System.Management.Automation.CompletionResultType ]::ProviderItem }
313- }
314- catch
315- {
316- # TabExpansion2 may throw when there are no completions, in this case return an empty
317- # list to prevent falling back to file path completions
318- }
319- if ($null -eq $completions -or $null -eq $completionMatches ) {
320- $result += " ;0;$ ( $completionPrefix.Length ) ;$ ( $completionPrefix.Length ) ;[]"
321- } else {
322- $result += " ;$ ( $completions.ReplacementIndex ) ;$ ( $completions.ReplacementLength + $prefixCursorDelta ) ;$ ( $cursorIndex - $prefixCursorDelta ) ;"
323- $json = [System.Collections.ArrayList ]@ ($completionMatches )
324- $mappedCommands = Compress-Completions ($json )
325- $result += $mappedCommands | ConvertTo-Json - Compress
326- }
327- }
328-
329- # End completions sequence
330- $result += " `a "
331-
332- Write-Host - NoNewLine $result
333- }
334-
335- function Compress-Completions ($completions ) {
336- $completions | ForEach-Object {
337- if ($_.CustomIcon ) {
338- , @ ($_.CompletionText , $_.ResultType , $_.ToolTip , $_.CustomIcon )
339- }
340- elseif ($_.CompletionText -eq $_.ToolTip ) {
341- , @ ($_.CompletionText , $_.ResultType )
342- } else {
343- , @ ($_.CompletionText , $_.ResultType , $_.ToolTip )
344- }
345- }
346- }
347-
348- # Register key handlers if PSReadLine is available
349- if (Get-Module - Name PSReadLine) {
350- Set-MappedKeyHandlers
351242}
0 commit comments