fix(editor): make the Generate chooser compact and scannable (#5069)

Generate now uses the compact, caret-anchored action chooser. Constructors, accessors, overrides and tests get distinct icons at the standard action-list size, and the chooser keeps its width while typing to filter.
This commit is contained in:
l0ng-ai
2026-11-22 04:03:48 +08:00
committed by GitHub
parent 9b8c88e1ee
commit 3f3cdeba0d
5 changed files with 75 additions and 4 deletions
+54
View File
@@ -0,0 +1,54 @@
import { expect, ipcCalls, openFromTree, stubDesktop, test } from "./fixtures";
test("Generate fits its actions and keeps keyboard filtering at a narrow width", async ({ workbench: page }) => {
await page.setViewportSize({ width: 570, height: 600 });
const editor = await openFromTree(page, "build.gradle.kts");
await stubDesktop(page);
await page.evaluate(() => {
window.__ipc.handlers.lsp_code_actions = () =>
[
["Generate Constructor", "refactor"],
["Generate Getter", "refactor"],
["Generate Setter", "refactor"],
["Create a unit test", "source.addTest"],
["Organize Imports", "source.organizeImports"],
].map(([title, kind]) => ({
title,
kind,
isPreferred: false,
disabledReason: null,
edit: null,
command: { command: "fixture.generate", arguments: [] },
data: null,
server: "kotlin",
}));
window.__ipc.handlers.lsp_resolve_command = () => null;
window.__ipc.handlers.lsp_execute_code_action = () => [];
});
await editor.locator(".cm-line", { hasText: "return order.toDto()" }).click();
await page.keyboard.press("Meta+n");
const popup = page.getByRole("dialog", { name: "Generate", exact: true });
const options = popup.getByRole("option");
await expect(options).toHaveText([
"Generate Constructor",
"Generate Getter",
"Generate Setter",
"Create a unit test",
]);
await expect(popup).toBeInViewport({ ratio: 1 });
const width = (await popup.boundingBox())!.width;
expect(width).toBeLessThan(300);
const icons = await options.locator("svg").evaluateAll((elements) => elements.map((el) => el.innerHTML));
expect(new Set(icons).size).toBe(3);
await expect(popup.getByRole("listbox")).toBeFocused();
await page.keyboard.type("sett");
await expect(options).toHaveText(["Generate Setter"]);
expect((await popup.boundingBox())!.width).toBe(width);
await page.keyboard.press("Enter");
await expect(popup).toHaveCount(0);
await expect
.poll(() => ipcCalls(page, "lsp_execute_code_action"))
.toMatchObject([{ action: { title: "Generate Setter" } }]);
});
+5 -2
View File
@@ -1,5 +1,5 @@
/** Actions of the LSP editor layer (ids from keymaps/intellij-macos.ts). */
import { isGenerateAction } from "./generate";
import { generateActionIcon, isGenerateAction } from "./generate";
import type { EditorView } from "@codemirror/view";
import { executeAction, registerActions, type Action } from "@/platform/actions";
import { activeEditor, editorsFor, registerSaveParticipant, whenSynced, type TextEditorHandle } from "@/editor/api";
@@ -29,6 +29,7 @@ import {
startHierarchyRefactoring,
} from "@/editor/refactor";
import { evaluateWhen } from "@/platform/context";
import { tx } from "@/platform/i18n";
/** Editor actions apply to the active text editor unless a tool window or popup owns the keyboard. */
export const EDITOR_WHEN = "textEditorActive && (editorFocus || !toolWindowFocus)";
@@ -396,7 +397,9 @@ export function registerLspActions(): () => void {
runKindAction(v, "", {
predicate: isGenerateAction,
none: "Nothing to generate here",
label: "Generate",
label: tx("Generate"),
compact: true,
actionIcon: generateActionIcon,
}),
{ keyWhen: GENERATE_KEY_WHEN },
),
+9
View File
@@ -7,3 +7,12 @@ const GENERATE_TITLE =
export function isGenerateAction(action: CodeAction): boolean {
return action.kind === "source.addTest" || GENERATE_TITLE.test(action.title);
}
export function generateActionIcon({ kind, title }: CodeAction): string {
if (kind === "source.addTest" || /\btests?\b/i.test(title)) return "jvm-test";
if (/\bconstructors?\b/i.test(title)) return "symbol-constructor";
if (/\b(getters?|setters?|accessors?)\b/i.test(title)) return "field";
if (/^override\b/i.test(title)) return "gutter-overridden";
if (/^implement\b/i.test(title)) return "gutter-implemented";
return "method";
}
+6 -2
View File
@@ -407,11 +407,12 @@ function actionItems(
view: EditorView,
actions: CodeAction[],
resolved?: Map<CodeAction, Promise<CodeAction>>,
actionIcon?: (action: CodeAction) => string,
): ListPopupItem[] {
return listedCodeActions(actions).map((a, i) => ({
id: `lsp:${i}`,
label: a.title,
icon: isQuickFix(a) ? quickFixIcon(view) : "bulb",
icon: actionIcon?.(a) ?? (isQuickFix(a) ? quickFixIcon(view) : "bulb"),
quickFix: isQuickFix(a),
emphasis: a.isPreferred,
disabled: a.disabledReason,
@@ -604,6 +605,8 @@ export async function runKindAction(
whole?: boolean;
none: string;
label: string;
compact?: boolean;
actionIcon?: (action: CodeAction) => string;
/**
* Syntax-based fallback when the server offers no enabled action
* (`empty`) or cannot answer; true when it handled the request.
@@ -638,7 +641,8 @@ export async function runKindAction(
kind: "list",
anchor: caretAnchor(view, view.state.selection.main.head),
title: opts.label,
items: actionItems(view, enabled),
compact: opts.compact,
items: actionItems(view, enabled, undefined, opts.actionIcon),
});
} catch (e) {
reportLspError(view, e, true);
+1
View File
@@ -2,6 +2,7 @@
"$ unchanged lines": "$ unchanged lines",
"Expand unchanged lines": "Expand unchanged lines",
"Matching scope": "Matching scope",
"Generate": "Generate",
"Choose Test for {stem} ({count} found)": "Choose Test for {stem} ({count} found)",
"Choose Test Subject for {stem} ({count} found)": "Choose Test Subject for {stem} ({count} found)",
"Code Style": "Code Style",