Skip to content

Add workspaceEditUndoStops proposal and group typing undo in Markdown editor - #350198

Open
Henning Dieterichs (hediet) wants to merge 7 commits into
mainfrom
hediet/md-editor-undo-granularity
Open

Henning Dieterichs (hediet) wants to merge 7 commits into
mainfrom
hediet/md-editor-undo-granularity

Conversation

@hediet

Copy link
Copy Markdown
Member

Summary

Fixes #328933

In the Markdown custom text editor, every keystroke is applied to the backing TextDocument via a separate vscode.workspace.applyEdit call. Because BulkTextEdits unconditionally creates and closes a new SingleModelEditStackElement for each call, pressing Undo (Ctrl+Z / Cmd+Z) reverted one character at a time instead of word/phrase chunks.

This PR adds a minimal proposed API (workspaceEditUndoStops) to control undo-stop boundaries on single-document WorkspaceEdits and adopts it in MarkdownEditorProvider so contiguous typing and deletions coalesce into word-level undo steps matching VS Code's regular text editor.

Demo

Typing "The quick brown fox jumps over the lazy dog." character-by-character in the Markdown Editor, then pressing Ctrl+Z three times and Ctrl+Y twice (Code OSS from this branch):

undo-granularity-demo.webm
After typing After 1× Ctrl+Z (removes " dog." as one step)
demo-after-typing demo-after-undo-1

What Changed

1. Workbench & Proposed API (workspaceEditUndoStops)

  • Added src/vscode-dts/vscode.proposed.workspaceEditUndoStops.d.ts extending WorkspaceEditMetadata with optional undoStopBefore?: boolean and undoStopAfter?: boolean (both default to true, preserving existing behavior for all callers).
  • Registered workspaceEditUndoStops in src/vs/platform/extensions/common/extensionsApiProposals.ts and gated usage in ExtHostBulkEdits.applyWorkspaceEdit via checkProposedApiEnabled.
  • Threaded undoStopBefore / undoStopAfter through MainThreadBulkEdits -> IBulkEditOptions -> BulkEditService -> BulkTextEdits.
  • In BulkTextEdits.apply (single-document text edits only):
    • When undoStopBefore is true (default), closes any previously open stack element via task.model.pushStackElement() before pushing a new SingleModelEditStackElement.
    • When undoStopBefore is false, reuses the top SingleModelEditStackElement on the document's undo stack if it is still open (canAppend(task.model)).
    • When undoStopAfter is false, leaves the SingleModelEditStackElement open so the next undoStopBefore: false edit can coalesce into it; when true (default), closes it as before.
    • Multi-document or mixed file+text bulk edits always force undo stops and close any open elements on touched models.

2. Markdown Editor Adapter

  • Added extensions/markdown-language-features/src/preview/markdownEditorUndoGrouping.ts (MarkdownEditorUndoGrouper), mirroring VS Code's shouldPushStackElementBetween word/space/edit-kind grouping:
    • Contiguous character insertions coalesce into one undo step; a space after a word starts a new step that coalesces with the following word ("hello" | " world"), and consecutive spaces coalesce then break before the next word.
    • Contiguous backspace and forward-delete sequences each coalesce separately.
    • Cursor jumps (non-contiguous offsets), newlines, multi-edit batches, replacements, large pastes (>32 chars), undo/redo commands, blur, webview reload, panel edits, and external document changes all flush the active group.
  • Updated MarkdownEditorProvider to pass the computed { undoStopBefore, undoStopAfter } metadata to vscode.workspace.applyEdit and enabled the workspaceEditUndoStops proposal in extensions/markdown-language-features/package.json and tsconfig.json.

API Design Note (for review)

  • Why undoStopBefore / undoStopAfter on WorkspaceEditMetadata?
    • Matches the existing TextEditor.edit(cb, { undoStopBefore, undoStopAfter }) API verbatim, so custom text editors get the exact same primitive that TextEditor extensions already use without introducing a new undo-group token lifecycle or custom-editor-only edit method.
    • Defaults to true / true, so existing callers and multi-file edits are unaffected.
    • Tradeoff: The grouping policy (word/space boundaries, deletion direction, paste threshold) lives in the custom editor rather than the workbench, which is intentional because custom editors know their own interaction semantics (e.g. IME/formatting/paste). If we later want a shared helper in @vscode/web-editors, MarkdownEditorUndoGrouper has zero VS Code runtime dependencies (only DocumentTextEdit) and can move there cleanly.

Validation

  • coreTypecheck (dw run .tmp\dev-workspace.ts coreTypecheck): passed
  • Markdown extension typecheck (dw run .tmp\dev-workspace.ts typecheck): passed
  • Hygiene / oxlint (build/hygiene.ts on all 14 touched source/test files): passed (0 errors)
  • Workbench unit tests (scripts/test.bat --run src/vs/workbench/api/test/browser/extHostBulkEdits.test.ts --run src/vs/workbench/api/test/browser/mainThreadEditors.test.ts): 13 passing (including proposal gating, coalescing consecutive edits, undo/redo, and undoStopBefore: true boundary breaking)
  • Markdown extension integration & unit tests (dw run .tmp\dev-workspace.ts markdownExtensionTests): 226 passing (including MarkdownEditorUndoGrouper unit tests and end-to-end Markdown editor bridge typing + undo/redo test)
  • Manual verification in live Code OSS (slot-15, recorded above)

…tops

Fixes #328933

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 24, 2026 16:22

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.

🟡 Changes recommended

Undo grouping still merges edits across navigation, deletion-direction changes, paste operations, and batched word boundaries, causing Undo to remove more text than intended.

4 open issues
What changed in this PR

This PR addresses character-by-character undo in the Markdown custom editor by adding proposed workspace-edit undo controls and using them to group typing and deletions.

Changes:

  • Adds and gates workspaceEditUndoStops, threading its metadata through the bulk-edit pipeline.
  • Introduces Markdown undo grouping and resets it at supported interaction boundaries.
  • Adds proposal-gating, undo/redo, grouping, and bridge tests.
File Description
src/​vscode-dts/​vscode.proposed.workspaceEditUndoStops.d.ts Declares proposed undo-stop metadata.
src/​vs/​workbench/​contrib/​bulkEdit/​browser/​bulkTextEdits.ts Supports appending single-document edits to open undo elements.
src/​vs/​workbench/​contrib/​bulkEdit/​browser/​bulkEditService.ts Passes undo controls while isolating mixed edit groups.
src/​vs/​workbench/​api/​test/​browser/​mainThreadEditors.test.ts Tests coalescing, explicit boundaries, and undo/redo.
src/​vs/​workbench/​api/​test/​browser/​extHostBulkEdits.test.ts Tests proposal gating and metadata forwarding.
src/​vs/​workbench/​api/​common/​extHostBulkEdits.ts Gates and forwards proposed metadata.
src/​vs/​workbench/​api/​common/​extHost.protocol.ts Extends the bulk-edit remote-call contract.
src/​vs/​workbench/​api/​browser/​mainThreadBulkEdits.ts Forwards undo controls to the bulk-edit service.
src/​vs/​platform/​extensions/​common/​extensionsApiProposals.ts Registers the new proposal.
src/​vs/​editor/​browser/​services/​bulkEditService.ts Adds internal undo-stop options.
extensions/​markdown-language-features/​tsconfig.json Includes the proposal declarations.
extensions/​markdown-language-features/​src/​test/​markdownEditorUndoGrouping.test.ts Tests typing, deletion, and reset grouping rules.
extensions/​markdown-language-features/​src/​test/​markdownEditorBridge.test.ts Tests word-level undo/redo through the bridge.
extensions/​markdown-language-features/​src/​preview/​markdownEditorUndoGrouping.ts Implements Markdown undo-grouping decisions.
extensions/​markdown-language-features/​src/​preview/​markdownEditorProvider.ts Applies grouping metadata and resets grouping state.
extensions/​markdown-language-features/​package.json Enables the proposal for the Markdown extension.

Review effort: Balanced

…it/navigation/paste hints

- Split batched typing insertions at word/space undo-stop boundaries so undo granularity does not depend on IPC batching
- Forward explicit undoEditHint notifications (break, isolated, deleteBackward, deleteForward) from the Markdown editor webview for pointer/navigation moves, plain-text pastes of any length, and deletion direction switches
- Add unit tests and burst-typing integration regression test

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

@connor4312 Connor Peet (connor4312) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Two blocking sync regressions in split writes: a concurrent document change can stall the Markdown editor, and a partially applied batch can silently drop typed text. I also found one non-blocking undo-boundary issue under batching.

Written by Connor's agent running GPT-6.1 Sol

Addresses @connor4312 review feedback on #350198:
- Add workspace.applyEditSequence (under workspaceEditUndoStops) backed by BulkTextEditSequence so multi-segment single-document edits validate document version once and apply atomically with per-step undo stops (eliminating partial writes).
- Buffer document change events during applyEdit/applyEditSequence and replay all buffered external changes on rejection (or after our atomic segments), preventing stalls when a competing edit wins the race.
- Track paste (isolated) and navigation (break) hint offsets through TextDocumentSync so composed pending edits preserve paste/break boundaries without isolating adjacent typing.
- Add unit and bridge regression tests for all three scenarios.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Base: eed628b4 Current: 77951a27

No screenshot changes.

When vscode.workspace.applyEdit(external) and applyEditSequence(editor) race
on the same initial document version, applyEditSequence can win first (fewer
microtask hops than BulkTextEdits) and cause externalApplied to return false.
Handle either race outcome in the test so both edits still land and convergence
is verified deterministically across Electron, Browser, and Remote test runs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
auto-merge was automatically disabled November 25, 2026 21:02

Pull request was closed

In remote integration CI runs, background panel initialization (e.g. Chat) can
steal focus after showTextDocument, causing executeCommand('undo') to target the
focused panel input instead of the test document. Re-focus the document right
before each undo/redo command and revert/close the untitled editor in finally.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…-granularity

Resolved conflict in src/vs/platform/extensions/common/extensionsApiProposals.ts
by keeping both workbenchMode and workspaceEditUndoStops in alphabetical order.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…-granularity

Resolved conflicts with textDocumentReadonly in package.json, tsconfig.json,
and markdownEditorProvider.ts (preserving both document.isReadonly checks and
undo grouping / atomic applyEditSequence).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Markdown editor: Undo groups typing one character at a time

6 participants