mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-19 00:10:00 +00:00
fix(refactor): closing a Java refactoring dialog cancels it; identifiers in any script (#3734)
Closing Change Signature or Extract Interface while it was checking for conflicts left the check running, and its edit was applied (or its Problems Detected asked) after the dialog was gone; closing now cancels the request in flight and nothing is confirmed, previewed or applied afterwards. The identifier check was ASCII-only and rejected valid Java names in other scripts; it now follows Java's identifier rules.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/** IDEA's Change Signature dialog for Java methods (`signature.ts`). */
|
||||
import { useState } from "react";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import type { MethodSignature } from "@/bindings";
|
||||
import { Checkbox, Dialog, IconButton, Input, Select, Spinner, T, Toolbar } from "@/ui";
|
||||
import { useTx } from "@/platform/i18n";
|
||||
@@ -46,7 +46,11 @@ function ChangeSignatureDialogInner({ request }: { request: SignatureRequest })
|
||||
const ready = !busy && !problem && signatureChanged(info, signature);
|
||||
const current = Math.min(selected, rows.length - 1);
|
||||
|
||||
// Closing the dialog cancels a refactoring still being checked: it applies nothing.
|
||||
const running = useRef<AbortController | null>(null);
|
||||
useEffect(() => () => running.current?.abort(), []);
|
||||
const close = () => {
|
||||
running.current?.abort();
|
||||
useSignatureDialog.setState({ request: null });
|
||||
request.view.focus();
|
||||
};
|
||||
@@ -72,9 +76,10 @@ function ChangeSignatureDialogInner({ request }: { request: SignatureRequest })
|
||||
};
|
||||
const submit = async (preview: boolean) => {
|
||||
if (!ready) return;
|
||||
const run = (running.current = new AbortController());
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
const outcome = await changeSignature(request, signature, delegate, preview);
|
||||
const outcome = await changeSignature(request, signature, delegate, preview, run.signal);
|
||||
if (useSignatureDialog.getState().request !== request) return;
|
||||
setBusy(false);
|
||||
if (outcome.status === "done") close();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/** IDEA's Extract Interface dialog for Java classes (`extractInterface.ts`). */
|
||||
import { useState } from "react";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { Checkbox, Dialog, Input, Select, Spinner, T } from "@/ui";
|
||||
import { useTx } from "@/platform/i18n";
|
||||
import {
|
||||
@@ -39,7 +39,11 @@ function ExtractInterfaceDialogInner({ request }: { request: InterfaceRequest })
|
||||
const ready = !busy && !problem && !!target;
|
||||
const allChecked = checked.size === info.members.length;
|
||||
|
||||
// Closing the dialog cancels a refactoring still being checked: it applies nothing.
|
||||
const running = useRef<AbortController | null>(null);
|
||||
useEffect(() => () => running.current?.abort(), []);
|
||||
const close = () => {
|
||||
running.current?.abort();
|
||||
useExtractInterfaceDialog.setState({ request: null });
|
||||
request.view.focus();
|
||||
};
|
||||
@@ -52,10 +56,11 @@ function ExtractInterfaceDialogInner({ request }: { request: InterfaceRequest })
|
||||
});
|
||||
const submit = async (preview: boolean) => {
|
||||
if (!ready) return;
|
||||
const run = (running.current = new AbortController());
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
const members = info.members.map((m) => m.handleIdentifier).filter((id) => checked.has(id));
|
||||
const outcome = await extractInterface(request, trimmed, target, members, preview);
|
||||
const outcome = await extractInterface(request, trimmed, target, members, preview, run.signal);
|
||||
if (useExtractInterfaceDialog.getState().request !== request) return;
|
||||
setBusy(false);
|
||||
if (outcome.status === "done") close();
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
const cancelRequest = vi.fn(async (_id: number) => ({ status: "ok", data: null }));
|
||||
vi.mock("@/platform/ipc", async (original) => {
|
||||
const ipc = await original<typeof import("@/platform/ipc")>();
|
||||
return { ...ipc, commands: { ...ipc.commands, cancelRequest } };
|
||||
});
|
||||
|
||||
const { cancellable } = await import("./signature");
|
||||
|
||||
describe("cancellable refactoring requests", () => {
|
||||
it("cancels the request in flight when the dialog closes", async () => {
|
||||
const run = new AbortController();
|
||||
let sent = -1;
|
||||
let answer!: (v: string) => void;
|
||||
const reply = cancellable(run.signal, (id) => {
|
||||
sent = id;
|
||||
return new Promise<string>((r) => (answer = r));
|
||||
});
|
||||
run.abort();
|
||||
expect(cancelRequest).toHaveBeenCalledWith(sent);
|
||||
answer("late");
|
||||
await expect(reply).resolves.toBe("late");
|
||||
});
|
||||
|
||||
it("leaves a request alone once it answered", async () => {
|
||||
cancelRequest.mockClear();
|
||||
const run = new AbortController();
|
||||
await cancellable(run.signal, async () => "done");
|
||||
run.abort();
|
||||
expect(cancelRequest).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -25,5 +25,9 @@ describe("extract interface", () => {
|
||||
expect(interfaceProblem("Shape", "Shape", 1)).toBe("Interface name should differ from the class name");
|
||||
expect(interfaceProblem("HasArea", "Shape", 0)).toBe("Select the members to form the interface");
|
||||
expect(interfaceProblem("HasArea", "Shape", 2)).toBeNull();
|
||||
// Java identifiers take letters of any script.
|
||||
expect(interfaceProblem("处理器", "Shape", 1)).toBeNull();
|
||||
expect(interfaceProblem("Größe", "Shape", 1)).toBeNull();
|
||||
expect(interfaceProblem("a-b", "Shape", 1)).toBe("Interface name is not a valid identifier");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -17,7 +17,7 @@ import { tx } from "@/platform/i18n";
|
||||
import { commands, isCancelled, nextRequestId, unavailableText } from "@/platform/ipc";
|
||||
import { editorService } from "@/workbench/contributions";
|
||||
import { cannotPerform } from "./catalog";
|
||||
import { applyEdit, confirmProblems } from "./signature";
|
||||
import { JAVA_NAME, applyEdit, cancellable, confirmProblems } from "./signature";
|
||||
|
||||
/** Languages whose server extracts interfaces through the dialog. */
|
||||
export const EXTRACT_INTERFACE_LANGUAGES = new Set(["java"]);
|
||||
@@ -51,8 +51,6 @@ export function memberLabel(m: InterfaceMember): string {
|
||||
return `${m.name}${params}: ${m.typeName}`;
|
||||
}
|
||||
|
||||
const JAVA_NAME = /^[A-Za-z_$][\w$]*$/;
|
||||
|
||||
/** Why the interface can't be extracted yet, or null. */
|
||||
export function interfaceProblem(name: string, className: string, members: number): string | null {
|
||||
if (!name) return "No interface name specified";
|
||||
@@ -127,6 +125,8 @@ export async function extractInterface(
|
||||
pkg: PackageChoice,
|
||||
members: readonly string[],
|
||||
preview: boolean,
|
||||
/** The dialog closed: nothing is confirmed, previewed or applied any more. */
|
||||
signal?: AbortSignal,
|
||||
): Promise<InterfaceOutcome> {
|
||||
const { view } = request;
|
||||
if (view.state.doc !== request.text) return { status: "stay", error: "The file changed; open the dialog again" };
|
||||
@@ -136,18 +136,22 @@ export async function extractInterface(
|
||||
for (;;) {
|
||||
await whenSynced(view);
|
||||
const token: string | null = confirmation;
|
||||
extracted = await lspRequest(() =>
|
||||
commands.lspExtractInterface(nextRequestId(), request.doc, request.pos, [...members], name, pkg.json, token),
|
||||
extracted = await cancellable(signal, (id) =>
|
||||
lspRequest(() =>
|
||||
commands.lspExtractInterface(id, request.doc, request.pos, [...members], name, pkg.json, token),
|
||||
),
|
||||
);
|
||||
if (signal?.aborted) return { status: "stay" };
|
||||
const fatal = extracted.problems.find((p) => p.severity === "fatal");
|
||||
if (fatal || (!extracted.edit && !extracted.confirmation))
|
||||
return { status: "stay", error: fatal?.message ?? "Nothing to change" };
|
||||
if (!extracted.confirmation) break;
|
||||
if (!(await confirmProblems(extracted.problems.map((p) => p.message)))) return { status: "stay" };
|
||||
if (!(await confirmProblems(extracted.problems.map((p) => p.message))) || signal?.aborted)
|
||||
return { status: "stay" };
|
||||
confirmation = extracted.confirmation;
|
||||
}
|
||||
} catch (e) {
|
||||
if (isCancelled(e)) return { status: "stay" };
|
||||
if (isCancelled(e) || signal?.aborted) return { status: "stay" };
|
||||
return { status: "stay", error: errorText(e) };
|
||||
}
|
||||
const edit = extracted.edit!;
|
||||
|
||||
@@ -23,6 +23,10 @@ describe("change signature", () => {
|
||||
it("tells what keeps the signature from being submitted", () => {
|
||||
expect(signatureProblem(info)).toBeNull();
|
||||
expect(signatureProblem({ ...info, methodName: "2x" })).toBe("Method name is not a valid identifier");
|
||||
expect(signatureProblem({ ...info, methodName: "处理" })).toBeNull();
|
||||
expect(
|
||||
signatureProblem({ ...info, parameters: info.parameters.map((p, i) => ({ ...p, name: `数量${i}` })) }),
|
||||
).toBeNull();
|
||||
expect(signatureProblem({ ...info, returnType: " " })).toBe("Return type is not specified");
|
||||
const dup = { ...info, parameters: [info.parameters[0]!, { ...info.parameters[1]!, name: "a" }] };
|
||||
expect(signatureProblem(dup)).toBe("Duplicate parameter name");
|
||||
|
||||
@@ -50,7 +50,8 @@ export function signatureText(sig: {
|
||||
return head + throws;
|
||||
}
|
||||
|
||||
const JAVA_NAME = /^[A-Za-z_$][\w$]*$/;
|
||||
/** A Java identifier: letters of any script, `$` and `_`, then digits and combining marks too. */
|
||||
export const JAVA_NAME = /^[\p{L}\p{Nl}\p{Sc}\p{Pc}][\p{L}\p{Nl}\p{Sc}\p{Pc}\p{Nd}\p{Mn}\p{Mc}]*$/u;
|
||||
|
||||
/** Why the edited signature can't be submitted yet, or null. */
|
||||
export function signatureProblem(sig: MethodSignature): string | null {
|
||||
@@ -137,6 +138,18 @@ export async function applyEdit(view: EditorView, edit: WorkspaceEdit) {
|
||||
}
|
||||
}
|
||||
|
||||
/** 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> {
|
||||
const id = nextRequestId();
|
||||
const abort = () => void commands.cancelRequest(id);
|
||||
signal?.addEventListener("abort", abort);
|
||||
try {
|
||||
return await send(id);
|
||||
} finally {
|
||||
signal?.removeEventListener("abort", abort);
|
||||
}
|
||||
}
|
||||
|
||||
export async function confirmProblems(messages: readonly string[]): Promise<boolean> {
|
||||
const { button } = await ask({
|
||||
title: "Problems Detected",
|
||||
@@ -158,6 +171,8 @@ export async function changeSignature(
|
||||
signature: MethodSignature,
|
||||
delegate: boolean,
|
||||
preview: boolean,
|
||||
/** The dialog closed: nothing is confirmed, previewed or applied any more. */
|
||||
signal?: AbortSignal,
|
||||
): Promise<SignatureOutcome> {
|
||||
const { view } = request;
|
||||
if (view.state.doc !== request.text) return { status: "stay", error: "The file changed; open the dialog again" };
|
||||
@@ -167,18 +182,20 @@ export async function changeSignature(
|
||||
for (;;) {
|
||||
await whenSynced(view);
|
||||
const token: string | null = confirmation;
|
||||
changed = await lspRequest(() =>
|
||||
commands.lspChangeSignature(nextRequestId(), request.doc, request.pos, signature, delegate, token),
|
||||
changed = await cancellable(signal, (id) =>
|
||||
lspRequest(() => commands.lspChangeSignature(id, request.doc, request.pos, signature, delegate, token)),
|
||||
);
|
||||
if (signal?.aborted) return { status: "stay" };
|
||||
const fatal = changed.problems.find((p) => p.severity === "fatal");
|
||||
if (fatal || (!changed.edit && !changed.confirmation))
|
||||
return { status: "stay", error: fatal?.message ?? "Nothing to change" };
|
||||
if (!changed.confirmation) break;
|
||||
if (!(await confirmProblems(changed.problems.map((p) => p.message)))) return { status: "stay" };
|
||||
if (!(await confirmProblems(changed.problems.map((p) => p.message))) || signal?.aborted)
|
||||
return { status: "stay" };
|
||||
confirmation = changed.confirmation;
|
||||
}
|
||||
} catch (e) {
|
||||
if (isCancelled(e)) return { status: "stay" };
|
||||
if (isCancelled(e) || signal?.aborted) return { status: "stay" };
|
||||
return { status: "stay", error: errorText(e) };
|
||||
}
|
||||
const edit = changed.edit!;
|
||||
|
||||
Reference in New Issue
Block a user