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

Support PowerShell intellisense in the terminal #154662

Open
Tyriar opened this issue Jul 10, 2022 · 8 comments
Open

Support PowerShell intellisense in the terminal #154662

Tyriar opened this issue Jul 10, 2022 · 8 comments
Assignees
Labels
feature-request Request for new features or functionality on-testplan terminal-shell-integration Shell integration, command decorations, etc. terminal-shell-pwsh An issue in the terminal specific to PowerShell
Milestone

Comments

@Tyriar
Copy link
Member

Tyriar commented Jul 10, 2022

No description provided.

@Tyriar Tyriar added feature-request Request for new features or functionality terminal-shell-integration Shell integration, command decorations, etc. terminal-shell-pwsh An issue in the terminal specific to PowerShell labels Jul 10, 2022
@Tyriar Tyriar added this to the July 2022 milestone Jul 10, 2022
@Tyriar Tyriar modified the milestones: July 2022, August 2022 Jul 21, 2022
@Tyriar Tyriar modified the milestones: August 2022, September 2022 Aug 4, 2022
@Tyriar Tyriar modified the milestones: September 2022, On Deck Aug 30, 2022
@meganrogge meganrogge removed their assignment Dec 5, 2022
@Tyriar Tyriar modified the milestones: On Deck, January 2023 Jan 6, 2023
Tyriar added a commit that referenced this issue Jan 13, 2023
@Tyriar Tyriar modified the milestones: January 2023, February 2023 Jan 26, 2023
@meganrogge meganrogge modified the milestones: February 2023, March 2023 Feb 23, 2023
@Tyriar Tyriar modified the milestones: March 2023, On Deck Mar 20, 2023
@Tyriar
Copy link
Member Author

Tyriar commented Dec 5, 2023

@cpendery has started some contributions in this area so we can start testing this again 👏

With "terminal.integrated.shellIntegration.suggestEnabled": true set in the latest build off main (landing in Insiders after the upcoming release):

image

@alessandro-newzoo
Copy link

@Tyriar How exactly does this work? I have found zero documentation available about this, and if I google terminal.integrated.shellIntegration.suggestEnabled pretty much nothing comes up?

I have enabled both terminal.integrated.shellIntegration.suggestEnabled and terminal.integrated.shellIntegration.enabled - shell integration works fine but I don't get any suggestions at all.

@Tyriar
Copy link
Member Author

Tyriar commented Dec 8, 2023

@alessandro-newzoo it will only work on the latest insiders build released today, just setting that should make it work. It's triggered by typing - currently.

Here's roughly how it works:

terminal.integrated.shellIntegration.suggestEnabled sets $VSCODE_SUGGEST to 1:

if (options.shellIntegration.suggestEnabled) {
envMixin['VSCODE_SUGGEST'] = '1';
}

That's read by shell integration and enabled some special pwsh key handlers, including for the - key:

# Conditionally enable suggestions
if ($env:VSCODE_SUGGEST -eq '1') {

Some vscode-side keybindings send custom sequences to pwsh which are understood by the key handlers (this ctrl+space keybinding seems to have broken somehow since I initially added it:

registerSendSequenceKeybinding('\x1b[24~e', { // F12,e -> ctrl+space (Native suggest)
when: ContextKeyExpr.and(TerminalContextKeys.focus, ContextKeyExpr.equals(TerminalContextKeyStrings.ShellType, WindowsShellType.PowerShell), TerminalContextKeys.terminalShellIntegrationEnabled, CONTEXT_ACCESSIBILITY_MODE_ENABLED.negate(), ContextKeyExpr.equals(`config.${TerminalSettingId.ShellIntegrationSuggestEnabled}`, true)),
primary: KeyMod.CtrlCmd | KeyCode.Space,
mac: { primary: KeyMod.WinCtrl | KeyCode.Space }
});

When suggest is triggered, we trigger TabExpansion2 and form a custom vscode escape sequence:

function Send-Completions {
$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
}

This is handled by vscode here:

case VSCodeOscPt.Completions:
this._handleCompletionsSequence(this._terminal, data, command, args);
return true;

The completions are fed into a suggest widget derived from the editor's intellisense widget.

@alessandro-newzoo
Copy link

@alessandro-newzoo it will only work on the latest insiders build released today, just setting that should make it work. It's triggered by typing - currently.

Here's roughly how it works:

terminal.integrated.shellIntegration.suggestEnabled sets $VSCODE_SUGGEST to 1:

if (options.shellIntegration.suggestEnabled) {
envMixin['VSCODE_SUGGEST'] = '1';
}

That's read by shell integration and enabled some special pwsh key handlers, including for the - key:

# Conditionally enable suggestions
if ($env:VSCODE_SUGGEST -eq '1') {

Some vscode-side keybindings send custom sequences to pwsh which are understood by the key handlers (this ctrl+space keybinding seems to have broken somehow since I initially added it:

registerSendSequenceKeybinding('\x1b[24~e', { // F12,e -> ctrl+space (Native suggest)
when: ContextKeyExpr.and(TerminalContextKeys.focus, ContextKeyExpr.equals(TerminalContextKeyStrings.ShellType, WindowsShellType.PowerShell), TerminalContextKeys.terminalShellIntegrationEnabled, CONTEXT_ACCESSIBILITY_MODE_ENABLED.negate(), ContextKeyExpr.equals(`config.${TerminalSettingId.ShellIntegrationSuggestEnabled}`, true)),
primary: KeyMod.CtrlCmd | KeyCode.Space,
mac: { primary: KeyMod.WinCtrl | KeyCode.Space }
});

When suggest is triggered, we trigger TabExpansion2 and form a custom vscode escape sequence:

function Send-Completions {
$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
}

This is handled by vscode here:

case VSCodeOscPt.Completions:
this._handleCompletionsSequence(this._terminal, data, command, args);
return true;

The completions are fed into a suggest widget derived from the editor's intellisense widget.

oh okay I see, thank you so much for the thorough explaination!
Weird that this setting terminal.integrated.shellIntegration.suggestEnabled has been there in Stable for so long then, why is it even there?

@cpendery
Copy link
Member

Curious if after #210289 we could enable is by default in Insiders? I've been using it for the past few weeks and haven't found any other issues. Thoughts? @Tyriar

@Tyriar
Copy link
Member Author

Tyriar commented Apr 15, 2024

@alessandro-newzoo sorry about the slow response, but it's been available as an experimental setting for some time but I was pulled off the project to work on some other things. In retrospect it should have remained as an Insiders-only setting back then, but now thanks to @cpendery's work it's becoming more reliable.

@cpendery I switched it on and it still had some problems where it would brick input where you could not interact with the suggest widget or the terminal and not close it. Not sure if that's been fixed yet but that's why it was switched off. I've got it enabled now and I was seeing that problem so hopefully I'll hit it again and be able to diagnose.

It's also not clear if we will have it enabled by default in the future and it might be too disruptive for users to enable it in Insiders. I want suggest to be available as a discoverable option but not necessarily the default experience. The best next step to get more self-hosting will probably be to enable it in the vscode repo, I'll try get some testing in over the next week and we can maybe create a test plan item to get some new eyes on it for this release.

@Tyriar
Copy link
Member Author

Tyriar commented Apr 15, 2024

Created a test plan item: #210394

@Tyriar
Copy link
Member Author

Tyriar commented Apr 25, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Request for new features or functionality on-testplan terminal-shell-integration Shell integration, command decorations, etc. terminal-shell-pwsh An issue in the terminal specific to PowerShell
Projects
None yet
Development

No branches or pull requests

5 participants