Skip to content

Support PowerShell intellisense in the terminal #154662

Closed

Description

@Tyriar
No description provided.

Activity

  1. added
    feature-requestRequest for new features or functionality
    terminal-shell-integrationShell integration infrastructure, command decorations, etc.
    terminal-shell-pwshAn issue in the terminal specific to PowerShell, including shell integration
    on Jul 10, 2022
  2. 6 remaining items

  3. added this to the On Deck milestone on Mar 20, 2023
  4. Tyriar commented on Dec 5, 2023

    @Tyriar
    ContributorAuthor

    cpendery (@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

  5. x-xyzxyz commented on Dec 8, 2023

    @x-xyzxyz

    Daniel Imms (@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.

  6. Tyriar commented on Dec 8, 2023

    @Tyriar
    ContributorAuthor

    @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.

  7. x-xyzxyz commented on Dec 11, 2023

    @x-xyzxyz

    @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?

  8. cpendery commented on Apr 13, 2024

    @cpendery
    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? Daniel Imms (@Tyriar)

  9. Tyriar commented on Apr 15, 2024

    @Tyriar
    ContributorAuthor

    @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 (@cpendery)'s work it's becoming more reliable.

    cpendery (@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.

  10. Tyriar commented on Apr 15, 2024

    @Tyriar
    ContributorAuthor

    Created a test plan item: #210394

  11. Tyriar commented on Apr 25, 2024

    @Tyriar
    ContributorAuthor
  12. Tyriar commented on Dec 10, 2024

    @Tyriar
    ContributorAuthor

    Closing this as we will be moving away from the pwsh native approach and instead going for the terminal-suggest built-in extension to provide completions. See #226562, https://github.com/microsoft/vscode/issues?q=is%3Aopen+is%3Aissue+label%3Aterminal-suggest

  13. locked and limited conversation to collaborators on Jan 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

*duplicateIssue identified as a duplicate of another issue(s)feature-requestRequest for new features or functionalityon-testplanterminal-shell-integrationShell integration infrastructure, command decorations, etc.terminal-shell-pwshAn issue in the terminal specific to PowerShell, including shell integration

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions