Skip to content

fix(vscode): maintain focus in terminal when closing diff editor - #22331

Closed
gjuggler wants to merge 4 commits into
google-gemini:mainfrom
gjuggler:gjuggler/issue-22193-vscode-focus
Closed

fix(vscode): maintain focus in terminal when closing diff editor#22331
gjuggler wants to merge 4 commits into
google-gemini:mainfrom
gjuggler:gjuggler/issue-22193-vscode-focus

Conversation

@gjuggler

Copy link
Copy Markdown

Summary

Makes a one-line change to diff-manager to set preserveFocus to true when calling vscode.window.tabGroups.close.

See https://code.visualstudio.com/api/references/vscode-api#TabGroups.close for docs.

This PR also adds unit tests and integration tests. I'll leave it up to repo maintainers whether the enforced verification is worth the added complexity.

Other details

Everything aside from the diff-manager change was an attempt to produce robust testing for the fix:

  • A new unit test file for diff-manager (one didn't exist) that ensures the preserveFocus param is called.
  • A refactor of unit test vscode mocks to reduce duplication & ensure consistency.
    • I know this is against the "minimal PRs" guideline... I'm happy to remove if it makes a reviewer uneasy.
  • An integration test that reproduces the scenario and verifies the fix.
  • I verified that this test fails without the one-line fix.
  • I wasn't able to find a working integration test approach that used vite, due to some technical constraints. So unfortunately it involves a homegrown test runner.
  • I made a best-effort attempt to incorporate the integration test in the right place as part of the release verification flow. But I wasn't able to verify this works on my fork, as the GitHub Action I spun up is stuck in a seemingly-permanent queued state (ref)

Related Issues

Fixes #22193

How to Validate

  1. From the packages/vscode-ide-companion directory, run npm run test:integration. The system will download vscode files to the .vscode-test directory, then it will open a new VS Code window while the test is run.

  2. Follow instructions to run the vscode extension locally in dev mode. Ask Gemini to edit a few files. You can now hit "enter" repeatedly to approve edits without clicking every time.

Pre-Merge Checklist

  • [N/A] Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • [N/A] 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

(Since this is a vscode-specific issue and fix, I did not run extensive manual platform validation.)

@gjuggler
gjuggler requested review from a team as code owners March 13, 2026 15:19
@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 in the VS Code extension where the terminal would lose focus after a diff editor was closed. The core change involves a small but critical modification to the diff-manager to explicitly preserve focus on the active element, typically the terminal, when a diff tab is closed. This fix is backed by a robust testing strategy, including new unit tests for the diff management logic and a new integration test suite that simulates the user interaction to confirm the focus behavior. Additionally, the PR includes refactoring of VS Code mock objects to streamline future testing efforts.

Highlights

  • VS Code Focus Preservation: Implemented a fix to ensure that the terminal maintains focus after closing a diff editor in VS Code, by setting preserveFocus: true when calling vscode.window.tabGroups.close.
  • Comprehensive Testing: Introduced new unit tests for DiffManager to verify the preserveFocus behavior and added a dedicated integration test suite for the VS Code extension to validate the fix in an end-to-end scenario.
  • VS Code Mock Refactor: Refactored VS Code mock utilities into a shared file (vscode-mock.ts) to reduce duplication and improve consistency across unit tests.
  • CI Integration for VS Code Tests: Integrated the new VS Code integration tests into the GitHub Actions CI workflow, ensuring automated validation of the fix.
Changelog
  • .github/actions/run-tests/action.yml
    • Added a new step to run VS Code integration tests within the CI workflow, including xvfb-run for Linux environments.
  • .gitignore
    • Added .vscode-test/ to the ignore list to prevent VS Code test artifacts from being tracked.
  • .prettierignore
    • Added **/.vscode-test/ to the ignore list for Prettier formatting.
  • eslint.config.js
    • Added **/.vscode-test/** to the ignore patterns for ESLint.
  • package-lock.json
    • Updated various dependency entries by removing the peer: true flag.
    • Added new development dependencies including @vscode/test-electron, immediate, jszip, lie, log-symbols, ora, pako, process-nextick-args, setimmediate, and stdin-discarder.
  • package.json
    • Added a new npm script test:integration:vscode to run VS Code specific integration tests.
    • Updated the test:integration:all script to include the new test:integration:vscode command.
  • packages/vscode-ide-companion/esbuild.js
    • Modified esbuild configuration to include integration test entry points (run-test.ts, focus.test.ts) for bundling.
    • Added vitest to external dependencies for esbuild.
  • packages/vscode-ide-companion/package.json
    • Added a new npm script test:integration to execute the VS Code integration tests.
    • Added @vscode/test-electron as a development dependency.
  • packages/vscode-ide-companion/scripts/run-integration-tests.js
    • Added a new script to orchestrate the execution of VS Code integration tests using @vscode/test-electron.
  • packages/vscode-ide-companion/src/diff-manager.test.ts
    • Added new unit tests for DiffManager to verify that vscode.window.tabGroups.close is called with preserveFocus=true when closing, accepting, or cancelling a diff.
    • Added a test to ensure internal state is cleaned up after closing a diff.
  • packages/vscode-ide-companion/src/diff-manager.ts
    • Modified the close method to pass true for the preserveFocus argument when closing a diff editor tab.
  • packages/vscode-ide-companion/src/extension.test.ts
    • Refactored the VS Code mock setup to import and extend from a shared vscodeMock utility.
  • packages/vscode-ide-companion/src/extension.ts
    • Registered a new command gemini.diff.open to programmatically open a diff view, primarily for integration testing purposes.
  • packages/vscode-ide-companion/src/ide-server.test.ts
    • Updated the VS Code mock setup to utilize the new shared vscode-mock.ts utility.
  • packages/vscode-ide-companion/src/integration-tests/focus.test.ts
    • Added a new integration test file to verify that terminal focus is correctly maintained after accepting a diff in VS Code.
  • packages/vscode-ide-companion/src/integration-tests/run-test.ts
    • Added a new script responsible for loading and executing individual VS Code integration test modules, including a global vscode mock interception.
  • packages/vscode-ide-companion/src/open-files-manager.test.ts
    • Refactored VS Code mock setup to use the shared vscode-mock.ts utility and updated test cases to use new mock helpers like createMockTextEditor.
  • packages/vscode-ide-companion/src/utils/vscode-mock.ts
    • Added a new utility file containing reusable mock implementations for vscode.Uri, EventEmitter, vscodeMock object, createMockTextDocument, and createMockTextEditor.
  • packages/vscode-ide-companion/tsconfig.json
    • Updated rootDir to . and adjusted include and exclude paths to properly handle new test files and configurations.
  • packages/vscode-ide-companion/vitest.config.ts
    • Added a new Vitest configuration file to explicitly exclude integration test files from unit test runs.
Activity
  • The pull request was created by gjuggler to address a focus management issue in the VS Code extension.
  • The author has provided a detailed summary of the changes, including the core fix and the extensive testing efforts (unit and integration tests).
  • The pre-merge checklist indicates that tests have been added or updated, confirming the author's commitment to test coverage.
  • The author noted challenges with setting up integration tests, specifically around Vite compatibility and GitHub Actions queuing, demonstrating thorough investigation and transparency.
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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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 introduces a new VS Code integration testing framework for the vscode-ide-companion package, including a shared vscode-mock utility for unit tests and a dedicated script (run-integration-tests.js) to execute end-to-end tests using @vscode/test-electron. The changes also include a new integration test (focus.test.ts) to verify that terminal focus is maintained after accepting a diff, which is supported by a modification in diff-manager.ts to close diff tabs with preserveFocus=true. Additionally, a new gemini.diff.open command was added to facilitate opening diffs programmatically for testing purposes. A high-severity review comment highlighted that this test-specific command should be conditionally registered to prevent security risks and unnecessary API surface area in production builds.

Comment thread packages/vscode-ide-companion/src/extension.ts Outdated
@gemini-cli gemini-cli Bot added priority/p1 Important and should be addressed in the near term. area/core Issues related to User Interface, OS Support, Core Functionality labels Mar 13, 2026
@gemini-cli

gemini-cli Bot commented Mar 28, 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!

@gemini-cli gemini-cli Bot closed this Mar 28, 2026
iiitutu added a commit to iiitutu/gemini-cli that referenced this pull request Apr 13, 2026
Adopts fix from google-gemini#22331 - sets preserveFocus: true in tabGroups.close()
to maintain terminal focus after closing diff tabs in VS Code.
@sripasg sripasg added the size/xl An extra large PR label Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Issues related to User Interface, OS Support, Core Functionality priority/p1 Important and should be addressed in the near term. size/xl An extra large PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gemini CLI doesn't maintain keyboard focus when VS Code extension closes a diff

2 participants