diff --git a/e2e/generatePopupLayout.spec.ts b/e2e/generatePopupLayout.spec.ts new file mode 100644 index 000000000..c8233ca8f --- /dev/null +++ b/e2e/generatePopupLayout.spec.ts @@ -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" } }]); +}); diff --git a/src/editor/lsp/actions.ts b/src/editor/lsp/actions.ts index 145023a37..7644849a2 100644 --- a/src/editor/lsp/actions.ts +++ b/src/editor/lsp/actions.ts @@ -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 }, ), diff --git a/src/editor/lsp/generate.ts b/src/editor/lsp/generate.ts index d170115c5..3ca4078b0 100644 --- a/src/editor/lsp/generate.ts +++ b/src/editor/lsp/generate.ts @@ -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"; +} diff --git a/src/editor/lsp/intentions.ts b/src/editor/lsp/intentions.ts index 5d8b9a8bb..bc1313c50 100644 --- a/src/editor/lsp/intentions.ts +++ b/src/editor/lsp/intentions.ts @@ -407,11 +407,12 @@ function actionItems( view: EditorView, actions: CodeAction[], resolved?: Map>, + 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); diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index b13ef514c..27b39256c 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -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",