Skip to content

Keep existing change annotations when WorkspaceChange wraps a WorkspaceEdit - #1918

Open
Ngo Quoc Viet (NgoQuocViet2001) wants to merge 1 commit into
microsoft:mainfrom
NgoQuocViet2001:fix-change-annotations-wrap
Open

Ngo Quoc Viet (NgoQuocViet2001) wants to merge 1 commit into
microsoft:mainfrom
NgoQuocViet2001:fix-change-annotations-wrap

Conversation

@NgoQuocViet2001

Copy link
Copy Markdown
Contributor

new WorkspaceChange(edit) reuses edit.changeAnnotations, but the internal ChangeAnnotations helper starts with a size of 0 and an id counter of 0, so it ignores the annotations it was given:

const edit: WorkspaceEdit = {
	documentChanges: [{ textDocument: { uri: 'file:///a.txt', version: 1 }, edits: [AnnotatedTextEdit.insert(Position.create(0, 0), 'x', 'a1')] }],
	changeAnnotations: { a1: ChangeAnnotation.create('label', true) }
};
new WorkspaceChange(edit).edit.changeAnnotations; // undefined, while the edit still references 'a1'

const first = new WorkspaceChange();
first.getTextEditChange({ uri: 'file:///b.txt', version: 1 }).insert(Position.create(0, 0), 'y', ChangeAnnotation.create('first')); // gets id '1'
const second = new WorkspaceChange(first.edit);
second.getTextEditChange({ uri: 'file:///c.txt', version: 1 }).insert(Position.create(0, 0), 'z', ChangeAnnotation.create('second')); // throws "Id 1 is already in use."

The first case occurs when nothing new is annotated through the helper after wrapping. The edit getter then sees a size of 0 and drops the map, so the existing edits lose their labels and needsConfirmation. This change counts the annotations passed to the constructor and makes nextId() skip ids that are already taken. A fresh WorkspaceChange without annotations still returns changeAnnotations: undefined.

Ran:

  • npm run compile:types and npm --prefix types test: the two new tests in edits.test.ts fail on main and pass with this change (58 passing)
  • npm --prefix types run lint: clean

Copilot AI balanced review requested due to automatic review settings October 23, 2026 13:20

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The narrow fix addresses both reported failures, includes regression coverage, and has no identified blocking issues.

No open issues 🎉

What changed in this PR

This PR fixes WorkspaceChange wrapping an existing WorkspaceEdit so annotation labels and confirmation requirements are preserved.

Changes:

  • Initialize the annotation count from the supplied map.
  • Skip occupied annotation IDs when generating new ones.
  • Add regression tests for preserving annotations and avoiding ID collisions.
File Description
types/​src/​test/​edits.test.ts Adds regression tests for both reported failures.
types/​src/​main.ts Counts existing annotations and generates unused IDs.

Review effort: Balanced

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.

2 participants