From ea02b83db32fd422002dbb37285b42d1c039a82f Mon Sep 17 00:00:00 2001 From: Daniel Imms <2193314+Tyriar@users.noreply.github.com> Date: Fri, 26 Jul 2024 10:52:07 -0700 Subject: [PATCH] Suggest exit keyword with score penalty Fixes #223945 --- .../terminal/browser/media/shellIntegration.ps1 | 8 ++++++-- .../suggest/browser/terminalSuggestAddon.ts | 1 + .../services/suggest/browser/simpleCompletionItem.ts | 4 ++++ .../suggest/browser/simpleCompletionModel.ts | 12 ++++++++++-- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/vs/workbench/contrib/terminal/browser/media/shellIntegration.ps1 b/src/vs/workbench/contrib/terminal/browser/media/shellIntegration.ps1 index 3bf39da3c3f35..3443cd0d93c4d 100644 --- a/src/vs/workbench/contrib/terminal/browser/media/shellIntegration.ps1 +++ b/src/vs/workbench/contrib/terminal/browser/media/shellIntegration.ps1 @@ -189,7 +189,11 @@ function Set-MappedKeyHandlers { # VS Code send global completions request Set-PSReadLineKeyHandler -Chord 'F12,f' -ScriptBlock { # Get commands, convert to string array to reduce the payload size and send as JSON - $commands = [System.Management.Automation.CompletionCompleters]::CompleteCommand('') + $commands = @( + [System.Management.Automation.CompletionCompleters]::CompleteCommand('') + # Keywords aren't included in CompletionCommand + [System.Management.Automation.CompletionResult]::new('exit', 'exit', [System.Management.Automation.CompletionResultType]::Keyword, "exit []") + ) $mappedCommands = Compress-Completions($commands) $result = "$([char]0x1b)]633;CompletionsPwshCommands;commands;" $result += $mappedCommands | ConvertTo-Json -Compress @@ -256,7 +260,7 @@ function Send-Completions { $_ } } - ([System.Management.Automation.CompletionCompleters]::CompleteVariable($completionPrefix)); + ([System.Management.Automation.CompletionCompleters]::CompleteVariable($completionPrefix)) ) if ($null -ne $completions) { $result += ";$($completions.ReplacementIndex);$($completions.ReplacementLength);$($cursorIndex);" diff --git a/src/vs/workbench/contrib/terminalContrib/suggest/browser/terminalSuggestAddon.ts b/src/vs/workbench/contrib/terminalContrib/suggest/browser/terminalSuggestAddon.ts index 0dbb2b44cd9f1..d3da55fc2fae2 100644 --- a/src/vs/workbench/contrib/terminalContrib/suggest/browser/terminalSuggestAddon.ts +++ b/src/vs/workbench/contrib/terminalContrib/suggest/browser/terminalSuggestAddon.ts @@ -726,6 +726,7 @@ function rawCompletionToSimpleCompletionItem(rawCompletion: PwshCompletion): Sim detail, isFile: rawCompletion.ResultType === 3, isDirectory: rawCompletion.ResultType === 4, + isKeyword: rawCompletion.ResultType === 12, }); } diff --git a/src/vs/workbench/services/suggest/browser/simpleCompletionItem.ts b/src/vs/workbench/services/suggest/browser/simpleCompletionItem.ts index 959d5f42fec88..df8843389f603 100644 --- a/src/vs/workbench/services/suggest/browser/simpleCompletionItem.ts +++ b/src/vs/workbench/services/suggest/browser/simpleCompletionItem.ts @@ -29,6 +29,10 @@ export interface ISimpleCompletion { * Whether the completion is a directory. */ isDirectory?: boolean; + /** + * Whether the completion is a keyword. + */ + isKeyword?: boolean; } export class SimpleCompletionItem { diff --git a/src/vs/workbench/services/suggest/browser/simpleCompletionModel.ts b/src/vs/workbench/services/suggest/browser/simpleCompletionModel.ts index 12297a1f4ad37..cc2a50bf2da33 100644 --- a/src/vs/workbench/services/suggest/browser/simpleCompletionModel.ts +++ b/src/vs/workbench/services/suggest/browser/simpleCompletionModel.ts @@ -182,8 +182,16 @@ export class SimpleCompletionModel { } this._filteredItems = target.sort((a, b) => { - // Sort first by the score - let score = b.score[0] - a.score[0]; + // Keywords should always appear at the bottom when they are not an exact match + let score = 0; + if (a.completion.isKeyword && a.labelLow !== wordLow || b.completion.isKeyword && b.labelLow !== wordLow) { + score = (a.completion.isKeyword ? 1 : 0) - (b.completion.isKeyword ? 1 : 0); + if (score !== 0) { + return score; + } + } + // Sort by the score + score = b.score[0] - a.score[0]; if (score !== 0) { return score; }