Skip to content

Keep terminal editors working after moving them to another editor group - #339488

Merged
Megan Rogge (meganrogge) merged 2 commits into
microsoft:mainfrom
nkramber:fix/183268-terminal-editor-move
Nov 5, 2026
Merged

Megan Rogge (meganrogge) merged 2 commits into
microsoft:mainfrom
nkramber:fix/183268-terminal-editor-move

Conversation

@nkramber

@nkramber nkramber commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

Fixes #183268

Summary

If you move a terminal editor to another editor group (with View: Move Editor into Right Group and the other move commands, by dragging its tab to another group, or with View: Move Editor into New Window), terminal keybindings that send a sequence to the active terminal stop working. Examples are Cmd+Backspace and Cmd+←/→ on macOS, and Ctrl+Backspace on Windows/Linux. The keypress is swallowed until the terminal loses focus and gets it back. The issue title says Mac, but this happens on every platform.

The cause is that TerminalEditorService treats the source half of a move as a real close and stops tracking the terminal. This PR makes it ignore that close.

Root cause

An editor moves across groups in two steps in EditorGroupView.doMoveOrCopyEditorAcrossGroups. The editor first opens in the target group, then closes in the source group with EditorCloseContext.MOVE. The TerminalEditorInput and its terminal instance are the same objects throughout.

TerminalEditorService's onDidCloseEditor handler didn't look at the close context. On a move it:

  1. called _removeInstance, which drops the terminal from instances, deletes its _editorInputs entry and disposes its focus, dispose and capability listeners;
  2. called setActiveInstance(undefined), because the moved terminal was the active one.

Next the target group activates the editor, and onDidActiveEditorChange calls setActiveInstance(instance). The instance is no longer in instances, so the index is -1 and activeInstance stays undefined. As a result:

  • ITerminalService.activeInstance is undefined while a terminal editor is focused, so workbench.action.terminal.sendSequence (which all of these keybindings use) has no target and does nothing.
  • The terminal only comes back later through the "unknown editor" path in onDidVisibleEditorsChange. That path only pushes the instance back into instances. It doesn't re-register its listeners, so onDidFocusInstance, onDidDisposeInstance and onDidChangeInstanceCapability stay silent for that terminal afterwards.

Change

  • terminalEditorService.ts: the onDidCloseEditor handler returns early when e.context === EditorCloseContext.MOVE, because the terminal is still open in the editor area.
    • The terminal stays in instances and keeps its input mapping and listeners.
    • The existing onDidActiveEditorChange handler then re-parents it to the new group (setGroup) and makes it active, as it already does for any activation.
    • I also updated the handler's comment. It previously listed "dragging and dropping to another editor" as a case where the terminal should be removed.
  • New terminalEditorService.test.ts: this is the first test suite for TerminalEditorService. It registers a terminal through the real resolveResource path and drives the service through its IEditorService events:
    • keeps a terminal editor tracked and active when it moves to another group: a MOVE close followed by activation in the target group. It fails without this change ({ instances: [], activeInstance: undefined }).
    • removes a terminal editor when it is closed: a normal close still removes the terminal, which guards the existing behavior.

I outlined this approach on the issue before starting: #183268 (comment)

What's unchanged

scenario how it's handled
Closing a terminal editor (Cmd/Ctrl+W, close button) close context is not MOVE, so the terminal is removed as before
Moving a terminal editor to the terminal panel moveToTerminalView removes it explicitly through detachInstance
Dragging a terminal editor to another VS Code window onDidRequestDetach → detachInstance
An editor that fails to open in the target group it closes with a non-MOVE context, so it's removed as before
Terminal editors created by splitting an editor still picked up by the onDidVisibleEditorsChange path, which this PR doesn't touch

How to test

  1. Run Terminal: Create New Terminal in Editor Area twice.
  2. Type some text in the active terminal (e.g. echo hello world), but don't press Enter.
  3. Run View: Move Editor into Right Group.
  4. Without clicking anywhere, press Cmd+Backspace on macOS (Ctrl+Backspace on Windows/Linux).
    • Expected: the line (macOS) or the previous word (Windows/Linux) is deleted.
    • Before this PR: nothing happens until you focus something else and come back to the terminal.
  5. Repeat step 4 after each of these moves:
    • moving the terminal back with View: Move Editor into Left Group;
    • dragging a terminal editor's tab into another group;
    • running View: Move Editor into New Window.
  6. Regression checks:
    • Cmd/Ctrl+W on a terminal editor still kills the terminal.
    • Terminal: Move Terminal into Panel, or dragging a terminal editor onto the terminal panel, still moves it there without leaving a copy in the editor area.

Automated:

./scripts/test.sh --grep "Workbench - TerminalEditorService"

Validation

  • New suite: the move test fails with the product change reverted and passes with it.
  • Existing terminal tests: all of the terminal browser unit tests pass.
  • Checks: hygiene, eslint, valid-layers-check and typecheck-client pass.
  • Manual testing: done in a dev build on macOS, using the steps above.
    • Cmd+Backspace works after moving right, moving back left and dragging the tab to a new group.
    • After Move Editor into New Window, the terminal stays tracked and renders and runs in the new window.
    • Closing that window still closes the terminal, as before.
    • Moving to the panel and closing with Cmd+W behave as before.

Moving an editor closes it in the source group with EditorCloseContext.MOVE. TerminalEditorService treated that as a real close and dropped the terminal, so the active terminal became undefined and terminal keybindings such as Cmd+Backspace did nothing until the terminal was refocused.

Fixes microsoft#183268
Copilot AI balanced review requested due to automatic review settings October 3, 2026 17:30
@vs-code-engineering

Copy link
Copy Markdown
Contributor

📬 CODENOTIFY

The following users are being notified based on files changed in this PR:

Anthony Kim (@anthonykim1)

Matched files:

  • src/vs/workbench/contrib/terminal/browser/terminalEditorService.ts
  • src/vs/workbench/contrib/terminal/test/browser/terminalEditorService.test.ts

@nkramber

nkramber commented Oct 3, 2026

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

Copilot AI 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.

Copilot review overview

🟢 Approval recommended

The targeted lifecycle fix matches editor move semantics and includes focused regression coverage.

Review effort: Balanced
Findings: None

What changed in this PR

Fixes terminal editor tracking after moves between editor groups.

Changes:

  • Ignores MOVE close events for terminal editors.
  • Adds regression coverage for moves and normal closes.
File Description
terminalEditorService.ts Preserves terminal tracking during editor moves.
terminalEditorService.test.ts Tests move and close behavior.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@meganrogge Megan Rogge (meganrogge) left a comment

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.

Thanks for the fix!

@meganrogge
Megan Rogge (meganrogge) enabled auto-merge (squash) November 3, 2026 19:31
@meganrogge
Megan Rogge (meganrogge) merged commit 7e1ee06 into microsoft:main Nov 5, 2026
59 checks passed
@vs-code-engineering vs-code-engineering Bot added this to the 1.146.0 milestone Nov 5, 2026

Copilot AI 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.

🟢 Approval recommended

No active review issues are reported for the change to preserve terminal-editor tracking during group moves.

No open issues 🎉

Review effort: Max

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

Labels

None yet

Projects

None yet

5 participants