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:
1 parent
a075bd55d8
commit
5f41870a75
4 files changed
+76
-4
No files matched your search
@@ -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}` };
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
Reference in new issue
Block a user