diff --git a/packages/app-core/src/lib/cm-vim-default-keymap.test.ts b/packages/app-core/src/lib/cm-vim-default-keymap.test.ts index 7ee0ad22..0f9de08e 100644 --- a/packages/app-core/src/lib/cm-vim-default-keymap.test.ts +++ b/packages/app-core/src/lib/cm-vim-default-keymap.test.ts @@ -2,7 +2,8 @@ import { afterEach, describe, expect, it } from 'vitest' import { EditorSelection, EditorState } from '@codemirror/state' import { EditorView, keymap, type KeyBinding } from '@codemirror/view' -import { vim } from '@replit/codemirror-vim' +import { getCM, vim } from '@replit/codemirror-vim' +import { indentLess, indentMore } from '@codemirror/commands' import { markdown, markdownLanguage } from '@codemirror/lang-markdown' import { SearchQuery, @@ -289,6 +290,84 @@ describe('Ctrl+A reaches Vim where Mod is Ctrl (#993)', () => { }) }) +// #1059: on Linux/Windows defaultKeymap's Mod-[ (indentLess) is Ctrl+[, Vim's +// Escape. indentLess reports the key handled even on a no-op dedent, so +// the keymap ate Ctrl+[ before the Vim plugin saw it and insert mode stayed on. +describe('Ctrl+[ reaches Vim where Mod is Ctrl (#1059)', () => { + const views: EditorView[] = [] + afterEach(() => { + views.splice(0).forEach((v) => v.destroy()) + }) + + const mount = (doc: string): EditorView => { + const view = new EditorView({ + state: EditorState.create({ + doc, + // Keymap first: the real editors run CodeMirror's keymap before Vim's, + // which is what let indentLess win. + extensions: [keymap.of([...vimAwareDefaultKeymap(true, false)]), vim()] + }), + parent: document.body + }) + views.push(view) + view.focus() + return view + } + + const press = (view: EditorView, key: string, keyCode: number, ctrlKey = false): void => { + view.contentDOM.dispatchEvent( + new KeyboardEvent('keydown', { key, keyCode, ctrlKey, bubbles: true, cancelable: true }) + ) + } + + const insertMode = (view: EditorView): boolean => + (getCM(view) as { state?: { vim?: { insertMode?: boolean } } } | null)?.state?.vim + ?.insertMode === true + + const runFor = (key: string, vimMode = true, mac = false): NonNullable => { + const binding = vimAwareDefaultKeymap(vimMode, mac).find((b) => b.key === key) + if (!binding?.run) throw new Error(`no run for ${key}`) + return binding.run + } + + it('ends insert mode on the dispatched Ctrl+[ chord', () => { + const view = mount('hello') + press(view, 'i', 73) + expect(insertMode(view)).toBe(true) + press(view, '[', 91, true) + expect(insertMode(view)).toBe(false) + }) + + it('defers Mod-[ in every Vim mode and leaves Cmd-[ on macOS', () => { + const view = mount('hello') + press(view, 'i', 73) + expect(runFor('Mod-[')(view)).toBe(false) + press(view, 'v', 86) + expect(runFor('Mod-[')(view)).toBe(false) + + const mac = new EditorView({ + state: EditorState.create({ + doc: ' hello', + extensions: [keymap.of([...vimAwareDefaultKeymap(true, true)]), vim()] + }), + parent: document.body + }) + views.push(mac) + const macIndent = vimAwareDefaultKeymap(true, true).find((b) => b.key === 'Mod-[') + expect(macIndent?.run).toBe(indentLess) + expect(macIndent?.run?.(mac)).toBe(true) + expect(mac.state.doc.toString()).toBe('hello') + }) + + it('keeps Ctrl+[ dedenting with Vim off and leaves Mod-] indentMore alone', () => { + const off = vimAwareDefaultKeymap(false, false).find((b) => b.key === 'Mod-[') + expect(off?.run).toBe(indentLess) + const on = vimAwareDefaultKeymap(true, false).find((b) => b.key === 'Mod-]') + expect(on?.run).toBe(indentMore) + expect(on?.preventDefault).not.toBe(false) + }) +}) + // #510: on Linux/Windows the search panel's Mod-f is Ctrl+F, which Vim wants // for page-forward; the keymap runs ahead of the Vim plugin, so the panel won. describe('vimAwareSearchKeymap', () => { diff --git a/packages/app-core/src/lib/cm-vim-default-keymap.ts b/packages/app-core/src/lib/cm-vim-default-keymap.ts index 6455c84a..64b68c61 100644 --- a/packages/app-core/src/lib/cm-vim-default-keymap.ts +++ b/packages/app-core/src/lib/cm-vim-default-keymap.ts @@ -190,12 +190,51 @@ const vimModeKeymapModIsCtrl: readonly KeyBinding[] = deferKeysToVim( new Set(['Mod-a']) ) +/** + * Ctrl+[ belongs to Vim: `` is Escape. + * + * `defaultKeymap` binds `Mod-[` to `indentLess`, which on Linux and Windows + * is Ctrl+[ — the key Vim users press to leave insert mode. `indentLess` + * reports the key handled even when nothing was dedented, and (like every + * binding here) it runs ahead of the Vim plugin, so Ctrl+[ never reached + * Vim's `` → `` mapping and insert mode never ended. + * + * Unlike the motion keys the collision is in *insert* mode, so the binding + * defers to the Vim plugin in every Vim mode: insert mode ends on it, and in + * normal/visual modes Vim's no-op Escape consumes it. With Vim off, Ctrl+[ + * dedents as before. macOS keeps `Cmd-[` → `indentLess` untouched. + * + * `Mod-]` stays with `indentMore`: Vim does not map Ctrl+] here, so + * deferring it would remove the indent shortcut without gaining a motion. + */ +function deferCtrlBracketToVim(bindings: readonly KeyBinding[]): KeyBinding[] { + return bindings.map((binding) => { + if (binding.key !== 'Mod-[' || !binding.run) return binding + const native = binding.run + return { + ...binding, + // Same constraint as above: preventDefault would consume the key even + // when the command defers. + preventDefault: false, + run: (view: EditorView): boolean => { + const vim = (getCM(view) as { state?: { vim?: unknown } } | null)?.state?.vim + return vim ? false : native(view) + } + } + }) +} + +const vimModeKeymapModIsCtrlWithBrackets: readonly KeyBinding[] = deferCtrlBracketToVim( + vimModeKeymapModIsCtrl +) + /** * CodeMirror's `defaultKeymap`, made Vim-aware: in Vim mode the macOS * emacs-style control chords are stripped so Vim's ``/``/``/… * bindings work, the arrow keys defer to Vim in normal/visual mode so they * move/extend like `hjkl`, Escape defers to Vim in insert/visual mode so it - * leaves the mode, and off macOS Ctrl+A defers to Vim outside insert mode; + * leaves the mode, off macOS Ctrl+A defers to Vim outside insert mode, and + * Ctrl+[ keeps Vim's `` Escape in every mode; * with Vim off the full keymap is used. In every case select all leaves the * note's title out on its first press (#1006). */ @@ -204,7 +243,7 @@ export function vimAwareDefaultKeymap( mac: boolean = isMacPlatform() ): readonly KeyBinding[] { if (!vimMode) return noteDefaultKeymap - return mac ? vimModeKeymap : vimModeKeymapModIsCtrl + return mac ? vimModeKeymap : vimModeKeymapModIsCtrlWithBrackets } /**