Apply versioned workspace edits in the VS Code client

applyRename accepts changes or documentChanges with per-document
versions, refusing both at once, resource operations and stale
versions before applying one WorkspaceEdit. The disposable
extension-host acceptance covers the versioned apply and refusals.
This commit is contained in:
faumaray committed 2026-12-06 02:34:13 +03:00
1 parent a075bd55d8
commit 5f41870a75
4 files changed
+76 -4

No files matched your search

+34 -3
View File
@@ -116,8 +116,14 @@ export interface RenameChange {
}
export type RenameChanges = Record<string, RenameChange[]>;
export interface VersionedChange {
textDocument: { uri: string; version?: number | null };
edits: RenameChange[];
}
export interface RenamePlanInput {
changes: RenameChanges;
changes?: RenameChanges;
// LSP documentChanges: versioned edits, refused when stale.
documentChanges?: VersionedChange[];
// Document versions observed when the rename was requested. A mismatch
// means the plan is stale and must not be applied.
versions?: Record<string, number>;
@@ -129,14 +135,39 @@ export interface RenamePlanInput {
// transactionally.
export async function applyRename(input: RenamePlanInput):
Promise<{ applied: boolean; reason?: string }> {
if (input.changes && input.documentChanges) {
return { applied: false, reason: "choose changes or documentChanges" };
}
let changes: RenameChanges;
const versions: Record<string, number> = { ...(input.versions ?? {}) };
if (input.documentChanges) {
changes = {};
for (const change of input.documentChanges) {
if (!change || !change.textDocument ||
typeof change.textDocument.uri !== "string" ||
!Array.isArray(change.edits)) {
return { applied: false, reason: "unsupported document change" };
}
const uri = change.textDocument.uri;
changes[uri] = (changes[uri] ?? []).concat(change.edits);
if (change.textDocument.version !== undefined &&
change.textDocument.version !== null) {
versions[uri] = change.textDocument.version;
}
}
} else if (input.changes) {
changes = input.changes;
} else {
return { applied: false, reason: "no workspace edit changes" };
}
const edit = new vscode.WorkspaceEdit();
const prepared = new Map<string, { version: number; text: string }>();
for (const [uri, edits] of Object.entries(input.changes)) {
for (const [uri, edits] of Object.entries(changes)) {
if (!edits.length) {
return { applied: false, reason: `empty edit list for ${uri}` };
}
const document = await vscode.workspace.openTextDocument(vscode.Uri.parse(uri));
const expected = input.versions?.[uri];
const expected = versions[uri];
if (expected !== undefined && expected !== document.version) {
return { applied: false, reason: `document changed after preparation: ${uri}` };
}
+28
View File
@@ -181,5 +181,33 @@ export async function run(): Promise<void> {
assert.strictEqual((await api.applyRename({ changes: invalid })).applied, false);
assert.strictEqual((await vscode.workspace.openTextDocument(fourthPath)).getText(),
"int value = 4;\n");
// documentChanges: versioned edits apply and stale versions are refused.
const fifthPath = path.join(folder.uri.fsPath, "rename-e.cpp");
const sixthPath = path.join(folder.uri.fsPath, "rename-f.cpp");
fs.writeFileSync(fifthPath, "int value = 5;\n");
fs.writeFileSync(sixthPath, "int value = 6;\n");
const fifth = await vscode.workspace.openTextDocument(fifthPath);
const sixth = await vscode.workspace.openTextDocument(sixthPath);
const range = { start: { line: 0, character: 4 }, end: { line: 0, character: 9 } };
const documentChanges = (versionOf: (doc: vscode.TextDocument) => number) => [
{ textDocument: { uri: fifth.uri.toString(), version: versionOf(fifth) },
edits: [{ range, newText: "count" }] },
{ textDocument: { uri: sixth.uri.toString(), version: versionOf(sixth) },
edits: [{ range, newText: "count" }] },
];
const staleChange = await api.applyRename({ documentChanges: documentChanges(() => 0) });
assert.strictEqual(staleChange.applied, false, JSON.stringify(staleChange));
assert.strictEqual((await vscode.workspace.openTextDocument(fifthPath)).getText(),
"int value = 5;\n");
const versioned = await api.applyRename({ documentChanges: documentChanges(doc => doc.version) });
assert.strictEqual(versioned.applied, true, JSON.stringify(versioned));
assert.strictEqual((await vscode.workspace.openTextDocument(fifthPath)).getText(),
"int count = 5;\n");
assert.strictEqual((await vscode.workspace.openTextDocument(sixthPath)).getText(),
"int count = 6;\n");
const resource = await api.applyRename({
documentChanges: [{ kind: "create", uri: fifth.uri.toString() } as any] });
assert.strictEqual(resource.applied, false, JSON.stringify(resource));
console.log('VSCODE_CLIENT_PASS');
}
+1 -1
View File
@@ -39,7 +39,7 @@ missing part. Updated December 5, 2026 after semantic tokens and inlay hints.
| Overload-set signature help | observed overloads are listed with the compiler-selected one first; visibility and overload resolution are not proven |
| textDocument/formatting | needs a plan |
| callHierarchy / typeHierarchy | prepare, incomingCalls and outgoingCalls are implemented and served; outgoing calls are verified over stdio now that the observer reports full function extents. Type hierarchy is open. |
| Client-side application of server-issued workspace edits | Neovim applies `changes` and versioned `documentChanges`; VS Code applies `changes` |
| Client-side application of server-issued workspace edits | Neovim and VS Code both apply `changes` and versioned `documentChanges` |
| Applying multi-file renames through client buffers | plans can be ready; application is client work |
| richer token types | legend has `function` and `type` (class, base, definition names) plus a `declaration` modifier; more types need further compiler spans |
+13
View File
@@ -61,6 +61,19 @@ start coordinate only, so ranges are zero-width until an end is available.
LSP adapters and index/rename/debugging integration remain open. `vbs test`
passed all 17 native suites; CMake passed the reflection and trace suites.
## December 6 versioned workspace edits in the VS Code client
`applyRename` accepts a full WorkspaceEdit: a `changes` map or versioned
`documentChanges`. Choosing both, a resource operation, or a stale document
version is refused before the single WorkspaceEdit is applied; the existing
version/text recheck stays. The disposable extension-host acceptance passed:
```
VS Code client passed: LSP status, replay session, timeline, source, stale
rejection and transactional rename
```
covering the versioned apply, stale-version refusal and resource-operation
refusal.
## December 6 versioned workspace edits in the Neovim client
`clients/neovim/lua/vls/init.lua` `apply_workspace_edit` now accepts a full