mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-25 08:02:56 +00:00
fix(refactor): a dialog closed while editors sync never sends its request (#3752)
cancellable checked the dialog's signal before lspRequest's editor sync; cancellable now hands its sender a wanted check, which lspRequest applies after syncing (and applyEdit does the same).
This commit is contained in:
@@ -40,4 +40,17 @@ describe("cancellable refactoring requests", () => {
|
||||
expect(isCancelled(err)).toBe(true);
|
||||
expect(send).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("tells the sender once the dialog closed after the request was handed over", async () => {
|
||||
const run = new AbortController();
|
||||
let wanted!: () => boolean;
|
||||
const reply = cancellable(run.signal, async (_id, w) => {
|
||||
wanted = w;
|
||||
return "sent";
|
||||
});
|
||||
expect(wanted()).toBe(true);
|
||||
run.abort();
|
||||
expect(wanted()).toBe(false);
|
||||
await reply;
|
||||
});
|
||||
});
|
||||
|
||||
@@ -136,9 +136,10 @@ export async function extractInterface(
|
||||
for (;;) {
|
||||
await whenSynced(view);
|
||||
const token: string | null = confirmation;
|
||||
extracted = await cancellable(signal, (id) =>
|
||||
lspRequest(() =>
|
||||
commands.lspExtractInterface(id, request.doc, request.pos, [...members], name, pkg.json, token),
|
||||
extracted = await cancellable(signal, (id, wanted) =>
|
||||
lspRequest(
|
||||
() => commands.lspExtractInterface(id, request.doc, request.pos, [...members], name, pkg.json, token),
|
||||
wanted,
|
||||
),
|
||||
);
|
||||
if (signal?.aborted) return { status: "stay" };
|
||||
|
||||
@@ -134,21 +134,30 @@ export async function applyEdit(view: EditorView, edit: WorkspaceEdit, signal?:
|
||||
await whenAllSynced();
|
||||
// The dialog closed while the editors were syncing: nothing is applied.
|
||||
if (signal?.aborted) return;
|
||||
await lspRequest(() => commands.applyWorkspaceEdit(edit));
|
||||
await lspRequest(
|
||||
() => commands.applyWorkspaceEdit(edit),
|
||||
() => !signal?.aborted,
|
||||
);
|
||||
} catch (e) {
|
||||
reportLspError(view, e, true);
|
||||
}
|
||||
}
|
||||
|
||||
/** Sends `send(id)` with a fresh request id that `signal` cancels. */
|
||||
export async function cancellable<T>(signal: AbortSignal | undefined, send: (id: number) => Promise<T>): Promise<T> {
|
||||
/**
|
||||
* Sends `send(id, wanted)` with a fresh request id that `signal` cancels. `wanted` goes to
|
||||
* `lspRequest`, which syncs editors first: a dialog closed meanwhile never sends the request.
|
||||
*/
|
||||
export async function cancellable<T>(
|
||||
signal: AbortSignal | undefined,
|
||||
send: (id: number, wanted: () => boolean) => Promise<T>,
|
||||
): Promise<T> {
|
||||
// Closed before the request went out: it never does (a cancel can't reach an unsent request).
|
||||
if (signal?.aborted) throw new IpcError({ kind: "cancelled", message: "Cancelled" });
|
||||
const id = nextRequestId();
|
||||
const abort = () => void commands.cancelRequest(id);
|
||||
signal?.addEventListener("abort", abort);
|
||||
try {
|
||||
return await send(id);
|
||||
return await send(id, () => !signal?.aborted);
|
||||
} finally {
|
||||
signal?.removeEventListener("abort", abort);
|
||||
}
|
||||
@@ -186,8 +195,8 @@ export async function changeSignature(
|
||||
for (;;) {
|
||||
await whenSynced(view);
|
||||
const token: string | null = confirmation;
|
||||
changed = await cancellable(signal, (id) =>
|
||||
lspRequest(() => commands.lspChangeSignature(id, request.doc, request.pos, signature, delegate, token)),
|
||||
changed = await cancellable(signal, (id, wanted) =>
|
||||
lspRequest(() => commands.lspChangeSignature(id, request.doc, request.pos, signature, delegate, token), wanted),
|
||||
);
|
||||
if (signal?.aborted) return { status: "stay" };
|
||||
const fatal = changed.problems.find((p) => p.severity === "fatal");
|
||||
|
||||
Reference in New Issue
Block a user