fix(vim): let af / if work on the right-hand side of a noremap (#7382)

A noremap runs its keys against codemirror-vim's built-in keymap only, and the function text objects were added after every mapclear as user mappings, so nnoremap X daf deleted nothing (same cause as <C-g>u in #7364). af / if are now part of the built-in keymap through the existing vim-core patch, with a placeholder motion the host overrides, and are no longer re-mapped after mapclear.
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-12-02 12:58:43 +08:00
1 джерело a372385788
коміт 54ce048ec9
5 files changed
+46 -26

No files matched your search

+13
Переглянути файл
@@ -103,4 +103,17 @@ describe("af / if in Vim", () => {
keys(cm, "a");
expect((cm as unknown as { state: { vim: { insertMode: boolean } } }).state.vim.insertMode).toBe(true);
});
it("works on the right-hand side of a noremap", () => {
// noremap runs its keys against the built-in keymap only, so af / if must be part of it.
applyRcCommands(parseVimrc("nnoremap X daf\nnnoremap Y vif").commands);
let cm = open(JAVA, JAVA.indexOf("return 1"));
keys(cm, "X");
expect(view!.state.doc.toString()).not.toContain("one()");
cm = open(JAVA, JAVA.indexOf("return 1"));
keys(cm, "Y");
const sel = view!.state.selection.main;
expect(view!.state.sliceDoc(sel.from, sel.to)).toContain("return 1;");
applyRcCommands([]);
});
});
+2 -4
Переглянути файл
@@ -25,7 +25,7 @@ import {
import { useVimMode, type VimModeState } from "./mode";
import { resolveActionId, type RcCommand, type SetOp } from "./rc";
import { useVimSettings } from "./settings";
import { mapFunctionTextObjects } from "./textObjects";
import { defineFunctionTextObjects } from "./textObjects";
import { resetLineNumberOptions } from "./lineNumbers";
export { setLineNumberOptionsActive } from "./lineNumbers";
@@ -523,7 +523,7 @@ export function defineExCommands() {
Vim.defineEx("wq", "wq", saveThenClose);
Vim.defineEx("xit", "x", saveThenClose);
Vim.defineEx("action", "action", (_cm, params) => void runIdeAction(params.args?.[0]));
mapFunctionTextObjects();
defineFunctionTextObjects();
}
/**
@@ -608,8 +608,6 @@ export function applyRcCommands(commands: readonly RcCommand[]): string[] {
Vim.mapclear();
resetLineNumberOptions();
shadowLeaderBuiltins(commands);
// `mapclear` drops them with the user mappings; before those, which may remap them.
mapFunctionTextObjects();
const problems: string[] = [];
for (const c of commands) {
try {
+5 -17
Переглянути файл
@@ -110,8 +110,11 @@ interface VimState {
let defined = false;
/** Defines the motion behind `af` / `if` (once). */
function defineFunctionTextObjects() {
/**
* Defines the motion behind `af` / `if` (once). The keys are part of the built-in keymap
* (patches/@replit__codemirror-vim-core), so a `noremap` right-hand side reaches them too.
*/
export function defineFunctionTextObjects() {
if (defined) return;
defined = true;
Vim.defineMotion("openideaFunction", ((
@@ -148,18 +151,3 @@ function defineFunctionTextObjects() {
return [cm.posFromIndex(r.from), cm.posFromIndex(vim.visualMode ? r.to - 1 : r.to)];
}) as never);
}
/** Maps `af` / `if` in Operator-pending and Visual mode; again after every `mapclear`. */
export function mapFunctionTextObjects() {
defineFunctionTextObjects();
for (const context of ["operatorPending", "visual"]) {
Vim._mapCommand({ keys: "af", type: "motion", motion: "openideaFunction", context } as never);
Vim._mapCommand({
keys: "if",
type: "motion",
motion: "openideaFunction",
motionArgs: { textObjectInner: true },
context,
} as never);
}
}