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

Suggest exit keyword with score penalty #223946

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
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 @@ -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 [<exitcode>]")
)
$mappedCommands = Compress-Completions($commands)
$result = "$([char]0x1b)]633;CompletionsPwshCommands;commands;"
$result += $mappedCommands | ConvertTo-Json -Compress
Expand Down Expand Up @@ -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);"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ function rawCompletionToSimpleCompletionItem(rawCompletion: PwshCompletion): Sim
detail,
isFile: rawCompletion.ResultType === 3,
isDirectory: rawCompletion.ResultType === 4,
isKeyword: rawCompletion.ResultType === 12,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 10 additions & 2 deletions src/vs/workbench/services/suggest/browser/simpleCompletionModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

@anthonykim1 anthonykim1 Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this score referring to the index of suggestion? For example, would low score represent first/last thing that would show up as a suggestion

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anthonykim1 this is fed into the a sort function such that exit in pwsh has special sorting behavior as keywords are generally unlikely to be used in the repl

if (score !== 0) {
return score;
}
}
// Sort by the score
score = b.score[0] - a.score[0];
if (score !== 0) {
return score;
}
Expand Down
Loading