Skip to content

fix(vscode-ide-companion): restore terminal focus after closing diff tabs - #23215

Closed
iiitutu wants to merge 8 commits into
google-gemini:mainfrom
iiitutu:fix-vscode-terminal-focus
Closed

fix(vscode-ide-companion): restore terminal focus after closing diff tabs#23215
iiitutu wants to merge 8 commits into
google-gemini:mainfrom
iiitutu:fix-vscode-terminal-focus

Conversation

@iiitutu

@iiitutu iiitutu commented Mar 20, 2026

Copy link
Copy Markdown

Summary

Fixes a UX bug where the VS Code integrated terminal loses focus after approving a file change in the Gemini CLI.

When the CLI calls openDiff, the VS Code companion extension opens a diff tab in the background (preserveFocus: true). Upon accepting the change, the CLI sends a closeDiff request. Closing this background diff tab causes VS Code to default its focus to the active editor area, stealing focus away from the integrated terminal where the user is actively working.

This PR adds an explicit workbench.action.terminal.focus command after closing the diff tab to ensure focus is restored to the terminal.

Details

In packages/vscode-ide-companion/src/diff-manager.ts, the closeDiffEditor method closes the diff tab using vscode.window.tabGroups.close(tab). This change appends an API call to re-focus the terminal immediately afterward.

Related Issues

Fixes #22193, Fixes #8635, Fixes #10956

How to Validate

  1. Build the companion extension: npm run build -w packages/vscode-ide-companion
  2. Start the Gemini CLI in VS Code's integrated terminal.
  3. Prompt Gemini to make a code edit that requires user approval.
  4. When prompted with [Proceed] in the terminal, press Enter.
  5. Focus should remain squarely in the terminal, and the next Enter keypress should correctly act on the CLI instead of inserting a newline in an editor tab.

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • MacOS
      • npm run
      • npx
      • Docker
      • Podman
      • Seatbelt
    • Windows
      • npm run
      • npx
      • Docker
    • Linux
      • npm run
      • npx
      • Docker

@iiitutu
iiitutu requested a review from a team as a code owner March 20, 2026 06:29
@google-cla

google-cla Bot commented Mar 20, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a user experience issue where the VS Code integrated terminal would lose focus after the Gemini CLI closed a diff tab. The change ensures that the terminal remains the active window, allowing users to continue interacting with the CLI without interruption and improving workflow efficiency.

Highlights

  • UX Bug Fix: Restored focus to the integrated terminal in VS Code after closing a diff tab, preventing focus from inadvertently shifting to the editor area.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request addresses a user experience issue by restoring focus to the terminal after a diff tab is closed. The approach is sound, but the new command execution could lead to an unhandled promise rejection if it fails. I've added a suggestion to wrap it in a try...catch block to make it more robust.

Comment thread packages/vscode-ide-companion/src/diff-manager.ts Outdated
@gemini-cli gemini-cli Bot added the status/need-issue Pull requests that need to have an associated issue. label Mar 20, 2026
@gemini-cli gemini-cli Bot added priority/p2 Important but can be addressed in a future release. and removed status/need-issue Pull requests that need to have an associated issue. labels Mar 20, 2026
try {
await vscode.commands.executeCommand(
'workbench.action.terminal.focus',
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This does what I think you want to do but it will always focus the terminal which may not be what the user actually wants. I don't know if it will be annoying for the user or not. This also will focus the terminal window including creating a new terminal session if the user has no active terminal sessions.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think the more correct & minimal fix is this one-liner that sets the 'maintainFocus' parameter to true when calling vscode.window.tabGroups.close:

Screenshot 2026-03-28 at 08 04 12

I looked into this pretty extensively, and put up #22331 a couple weeks back. (In hindsight, that PR probably has too much testing for such a small fix.)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@kschaab mind weighing in here?

Let's get the best fix in, it potentially may be Greg's suggestion. Worth taking a look

@gemini-cli

gemini-cli Bot commented Apr 4, 2026

Copy link
Copy Markdown
Contributor

Hi there! Thank you for your interest in contributing to Gemini CLI.

To ensure we maintain high code quality and focus on our prioritized roadmap, we have updated our contribution policy (see Discussion #17383).

We only guarantee review and consideration of pull requests for issues that are explicitly labeled as 'help wanted'. All other community pull requests are subject to closure after 14 days if they do not align with our current focus areas. For this reason, we strongly recommend that contributors only submit pull requests against issues explicitly labeled as 'help-wanted'.

This pull request is being closed as it has been open for 14 days without a 'help wanted' designation. We encourage you to find and contribute to existing 'help wanted' issues in our backlog! Thank you for your understanding and for being part of our community!

@cocosheng-g

Copy link
Copy Markdown
Contributor

@gjuggler, apologies for the bot closing this PR! We have reopened it. Please sync your branch to the latest main and we will have someone review it shortly.

@iiitutu

iiitutu commented Apr 13, 2026

Copy link
Copy Markdown
Author

Yes, I think #22331 is a better fix.

@gjuggler

Copy link
Copy Markdown

Yes, I think #22331 is a better fix.

@iiitutu would you be willing to adopt that change in this PR? That's likely the faster route versus fighting the PR bots any further, haha.

If the single-line fix will pass review, that's probably the ideal PR.

(The unit tests in #22331 didn't actually do much, and the integration tests were messy and would likely add more maintenance burden than they're worth).

Adopts fix from google-gemini#22331 - sets preserveFocus: true in tabGroups.close()
to maintain terminal focus after closing diff tabs in VS Code.
@iiitutu

iiitutu commented Apr 13, 2026

Copy link
Copy Markdown
Author

Adopts the fix from #22331 - uses preserveFocus: true in tabGroups.close() to maintain terminal focus after closing diff tabs.

@gemini-cli

gemini-cli Bot commented May 2, 2026

Copy link
Copy Markdown
Contributor

Hi there! Thank you for your contribution. To keep our backlog manageable, we are closing pull requests that haven't seen maintainer activity for 30 days. If you're still working on this, please let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority/p2 Important but can be addressed in a future release. size/xs An extra small PR type/bug

Projects

None yet

6 participants