diff --git a/docs/en/guide/vim.mdx b/docs/en/guide/vim.mdx index 9b076a5153..f8472207a5 100644 --- a/docs/en/guide/vim.mdx +++ b/docs/en/guide/vim.mdx @@ -13,7 +13,7 @@ Use any of these: - The **Vim** widget in the status bar: click it and check **Enabled**. - **Settings › Editor › Vim** › **Enable Vim emulation**. -The setting applies to every editor in every window right away and is remembered across restarts. While Vim mode is on, the status bar widget shows the current mode: `NORMAL`, `INSERT`, `VISUAL`, `V-LINE`, `V-BLOCK` or `REPLACE`. +The setting applies to every editor in every window right away and is remembered across restarts. It also applies to the commit message and to GitHub pull request comment and review fields; there, Escape first leaves Insert or Visual mode, and an Escape in Normal mode does what it does without Vim (leave the field, cancel the comment). While Vim mode is on, the status bar widget shows the current mode: `NORMAL`, `INSERT`, `VISUAL`, `V-LINE`, `V-BLOCK` or `REPLACE`. ## What works diff --git a/docs/zh/guide/vim.mdx b/docs/zh/guide/vim.mdx index 5a1d918c85..092f904d34 100644 --- a/docs/zh/guide/vim.mdx +++ b/docs/zh/guide/vim.mdx @@ -13,7 +13,7 @@ OpenIDEA 内置了 Vim 模拟,参照的是 IntelliJ IDEA 的 IdeaVim 插件。 - 状态栏的 **Vim** 微件:单击后勾选 **已启用**。 - **设置 › 编辑器 › Vim** › **启用 Vim 模拟**。 -设置会立即应用到所有窗口的所有编辑器,重启后保持不变。Vim 模式开启时,状态栏微件显示当前模式:`NORMAL`、`INSERT`、`VISUAL`、`V-LINE`、`V-BLOCK` 或 `REPLACE`。 +设置会立即应用到所有窗口的所有编辑器,重启后保持不变。提交消息和 GitHub 拉取请求的评论、评审输入框也使用 Vim 模式:在这些输入框里 Escape 先退出插入或可视模式,在普通模式下再按 Escape 则和未开 Vim 时一样(离开输入框或取消评论)。Vim 模式开启时,状态栏微件显示当前模式:`NORMAL`、`INSERT`、`VISUAL`、`V-LINE`、`V-BLOCK` 或 `REPLACE`。 ## 支持哪些功能 diff --git a/e2e/submitReviewLayout.spec.ts b/e2e/submitReviewLayout.spec.ts index ab7c6a013d..4d381ce9ee 100644 --- a/e2e/submitReviewLayout.spec.ts +++ b/e2e/submitReviewLayout.spec.ts @@ -1,5 +1,5 @@ import type { Page } from "@playwright/test"; -import { expect, ipcCalls, stubDesktop, test } from "./fixtures"; +import { expect, fieldText, ipcCalls, stubDesktop, test } from "./fixtures"; async function seedReview(page: Page) { await stubDesktop(page); @@ -75,11 +75,11 @@ test("centers the unanchored composer, follows the editor font and reflows its r const comment = (await popup.getByRole("button", { name: "Comment", exact: true }).boundingBox())!; expect(comment.y).toBeGreaterThan(approve.y); expect(comment.x + comment.width).toBeLessThan(300); - await expect(editor).toHaveValue("Please double-check the rounding step."); + await expect.poll(() => fieldText(editor)).toBe("Please double-check the rounding step."); await editor.press("Escape"); await expect(popup).toHaveCount(0); await openReview(page); - await expect(editor).toHaveValue("Please double-check the rounding step."); + await expect.poll(() => fieldText(editor)).toBe("Please double-check the rounding step."); }); test("keeps anchored review controls and the comment visible after a long submission error", async ({ @@ -109,7 +109,7 @@ test("keeps anchored review controls and the comment visible after a long submis const error = popup.getByRole("alert"); await expect(error).toContainText("Review request failed."); await expect(button).toBeEnabled(); - await expect(editor).toHaveValue("Check the batch limit."); + await expect.poll(() => fieldText(editor)).toBe("Check the batch limit."); const box = (await popup.boundingBox())!; const buttonBox = (await button.boundingBox())!; expect(box.y + box.height).toBeLessThan(350); diff --git a/src/editor/TextField.test.tsx b/src/editor/TextField.test.tsx index 573ed48664..719f13438c 100644 --- a/src/editor/TextField.test.tsx +++ b/src/editor/TextField.test.tsx @@ -158,4 +158,66 @@ describe("TextField", () => { disposeVim(); } }); + + it("takes no edits while read-only, and reports caret moves that don't change the text", () => { + const onSelectionChange = vi.fn(); + function Holder({ readOnly }: { readOnly: boolean }) { + const [text, setText] = useState("hello"); + return ( + + ); + } + const { rerender } = render(); + const view = viewOf("Field"); + act(() => view.dispatch({ selection: { anchor: 2 } })); + expect(onSelectionChange).toHaveBeenCalledWith(2, view); + rerender(); + expect(view.state.readOnly).toBe(true); + expect(contentOf("Field").getAttribute("contenteditable")).toBe("false"); + rerender(); + expect(view.state.readOnly).toBe(false); + }); + + it("with Vim mode on, gives a key to onKeyDownFirst before Vim and to onKeyDown after it", async () => { + useVimSettings.setState({ enabled: true, handlers: {} }, true); + const disposeVim = activateVim(); + const first = vi.fn((e: KeyboardEvent) => e.key === "ArrowDown"); + const after = vi.fn((_e: KeyboardEvent) => false); + try { + function Holder() { + const [text, setText] = useState("one\ntwo"); + return ( + + ); + } + render(); + const view = await untilVim("Field"); + act(() => view.focus()); + act(() => view.dispatch({ selection: { anchor: 0 } })); + fireEvent.keyDown(view.contentDOM, { key: "ArrowDown" }); + expect(first).toHaveBeenCalledTimes(1); + expect(view.state.selection.main.head).toBe(0); + fireEvent.keyDown(view.contentDOM, { key: "i" }); + expect(view.dom.getAttribute("data-vim-mode")).toBe("insert"); + fireEvent.keyDown(view.contentDOM, { key: "Escape" }); + expect(view.dom.getAttribute("data-vim-mode")).toBe("normal"); + expect(after.mock.calls.some(([e]) => e.key === "Escape" && !e.defaultPrevented)).toBe(false); + } finally { + disposeVim(); + } + }); }); diff --git a/src/editor/TextField.tsx b/src/editor/TextField.tsx index 0a702e2d12..b1fe926414 100644 --- a/src/editor/TextField.tsx +++ b/src/editor/TextField.tsx @@ -6,7 +6,7 @@ * The commit message field is the first user (#7024). */ import { forwardRef, useImperativeHandle, useLayoutEffect, useRef, type CSSProperties, type RefObject } from "react"; -import { Annotation, Compartment, EditorState } from "@codemirror/state"; +import { Annotation, Compartment, EditorState, Prec } from "@codemirror/state"; import { EditorView, keymap, placeholder as cmPlaceholder } from "@codemirror/view"; import { defaultKeymap, history } from "@codemirror/commands"; import { embeddedEditorExtensions } from "./api"; @@ -32,6 +32,9 @@ const FIELD_THEME = EditorView.theme({ }, }); +const readOnlyExtension = (readOnly: boolean) => + readOnly ? [EditorState.readOnly.of(true), EditorView.editable.of(false)] : []; + export interface TextFieldHandle { focus(): void; /** The live editor view, null before mount and after unmount. */ @@ -57,6 +60,12 @@ export interface TextFieldProps { onBlur?: () => void; /** Runs after Vim has had the key; return true when the key was handled. */ onKeyDown?: (event: KeyboardEvent, view: EditorView) => boolean | void; + /** Runs before Vim and the editor's keys (a completion list open at the field); true when handled. */ + onKeyDownFirst?: (event: KeyboardEvent, view: EditorView) => boolean | void; + /** The caret moved without the text changing. */ + onSelectionChange?: (head: number, view: EditorView) => void; + /** No editing (a request in flight); the text stays selectable. */ + readOnly?: boolean; className?: string; style?: CSSProperties; /** Receives the host element, to anchor popups at the field. */ @@ -77,6 +86,9 @@ export const TextField = forwardRef(function Te invalid, onBlur, onKeyDown, + onKeyDownFirst, + onSelectionChange, + readOnly = false, className, style, hostRef, @@ -86,11 +98,16 @@ export const TextField = forwardRef(function Te const host = useRef(null); const view = useRef(null); const wrapCompartment = useRef(new Compartment()); + const readOnlyCompartment = useRef(new Compartment()); // The handlers change every render; the view is built once and reads them through refs. const onChangeRef = useRef(onChange); onChangeRef.current = onChange; const onKeyDownRef = useRef(onKeyDown); onKeyDownRef.current = onKeyDown; + const onKeyDownFirstRef = useRef(onKeyDownFirst); + onKeyDownFirstRef.current = onKeyDownFirst; + const onSelectionChangeRef = useRef(onSelectionChange); + onSelectionChangeRef.current = onSelectionChange; const onBlurRef = useRef(onBlur); onBlurRef.current = onBlur; const valueRef = useRef(value); @@ -107,10 +124,19 @@ export const TextField = forwardRef(function Te history(), keymap.of(defaultKeymap), wrapCompartment.current.of(wrap ? EditorView.lineWrapping : []), + readOnlyCompartment.current.of(readOnlyExtension(readOnly)), + Prec.highest( + EditorView.domEventHandlers({ + keydown: (event, current) => onKeyDownFirstRef.current?.(event, current) === true, + }), + ), ...(placeholder ? [cmPlaceholder(placeholder)] : []), embeddedEditorExtensions(path), EditorView.updateListener.of((update) => { - if (!update.docChanged) return; + if (!update.docChanged) { + if (update.selectionSet) onSelectionChangeRef.current?.(update.state.selection.main.head, update.view); + return; + } if (update.transactions.some((tr) => tr.annotation(fieldEcho))) return; onChangeRef.current?.(update.state.doc.toString(), update.view); }), @@ -154,6 +180,10 @@ export const TextField = forwardRef(function Te view.current?.dispatch({ effects: wrapCompartment.current.reconfigure(wrap ? EditorView.lineWrapping : []) }); }, [wrap]); + useLayoutEffect(() => { + view.current?.dispatch({ effects: readOnlyCompartment.current.reconfigure(readOnlyExtension(readOnly)) }); + }, [readOnly]); + useLayoutEffect(() => { const editor = view.current; if (!editor || editor.state.doc.toString() === value) return; diff --git a/src/features/github/details/Details.module.css b/src/features/github/details/Details.module.css index daab5a1d6c..9fcf8504bd 100644 --- a/src/features/github/details/Details.module.css +++ b/src/features/github/details/Details.module.css @@ -469,22 +469,26 @@ align-items: center; gap: var(--space-1); } +/* An editor (`src/editor/TextField.tsx`) filling the popup's free height. */ .submitEditor { flex: 1; min-height: 40px; - padding: var(--space-1) var(--space-2); + overflow: hidden; border: 1px solid var(--border); border-radius: var(--radius-control); background: var(--bg-input); color: var(--fg); - font: inherit; line-height: var(--lh-code); - resize: none; - outline: none; } -.submitEditor:focus { +.submitEditor:focus-within { border-color: var(--focus-ring); } +.submitEditor :global(.cm-content) { + padding: var(--space-1) var(--space-2); +} +.submitEditor :global(.cm-editor.cm-focused) { + outline: none; +} .submitError { flex: 0 1 auto; min-height: 0; diff --git a/src/features/github/details/SubmitReviewPopup.tsx b/src/features/github/details/SubmitReviewPopup.tsx index 789116462a..1c3fc8ecc9 100644 --- a/src/features/github/details/SubmitReviewPopup.tsx +++ b/src/features/github/details/SubmitReviewPopup.tsx @@ -6,6 +6,7 @@ * it, or centred in the window. 500×200, resizable. */ import { useEffect, useState } from "react"; +import { TextField } from "@/editor/TextField"; import { Button, IconButton, Popup, Spinner, T } from "@/ui"; import { resolveMonoFont } from "@/platform/fonts"; import { useTx, tx } from "@/platform/i18n"; @@ -129,20 +130,23 @@ function SubmitReview(props: { number: number; draftKey: string; anchor: Element -