diff --git a/main.ts b/main.ts index 860aa53..1a06123 100644 --- a/main.ts +++ b/main.ts @@ -267,7 +267,7 @@ export default class VimrcPlugin extends Plugin { var cmEditor = this.getCodeMirror(view); if (cmEditor && !this.codeMirrorVimObject.loadedVimrc) { this.defineBasicCommands(this.codeMirrorVimObject); - this.defineObsidianVimMotions(this.codeMirrorVimObject); + this.defineObsidianVimCommands(this.codeMirrorVimObject); this.defineSendKeys(this.codeMirrorVimObject); this.defineObCommand(this.codeMirrorVimObject); this.defineSurround(this.codeMirrorVimObject); @@ -379,7 +379,7 @@ export default class VimrcPlugin extends Plugin { } - defineObsidianVimMotions(vimObject: VimApi) { + defineObsidianVimCommands(vimObject: VimApi) { defineObsidianVimMotion(vimObject, jumpToNextHeading, 'gh'); defineObsidianVimMotion(vimObject, jumpToPreviousHeading, 'gH'); defineObsidianVimMotion(vimObject, jumpToNextLink, 'gl'); diff --git a/utils/jumpToPattern.ts b/utils/jumpToPattern.ts index f44f98b..e694d57 100644 --- a/utils/jumpToPattern.ts +++ b/utils/jumpToPattern.ts @@ -20,11 +20,9 @@ export function jumpToPattern({ }): EditorPosition { const content = cm.getValue(); const startingIdx = cm.indexFromPos(oldPosition); - const jumpFn = - direction === "next" - ? getNthNextInstanceOfPattern - : getNthPreviousInstanceOfPattern; - const matchIdx = jumpFn({ content, regex, startingIdx, n: repeat }); + const findNthMatchFn = + direction === "next" ? findNthNextRegexMatch : findNthPreviousRegexMatch; + const matchIdx = findNthMatchFn({ content, regex, startingIdx, n: repeat }); if (matchIdx === undefined) { return oldPosition; } @@ -33,10 +31,10 @@ export function jumpToPattern({ } /** - * Returns the index of (up to) the n-th instance of a pattern in a string after a given starting - * index. If the pattern is not found at all, returns undefined. + * Returns the index of (up to) the n-th next instance of a pattern in a string after a given + * starting index. If the pattern is not found at all, returns undefined. */ -function getNthNextInstanceOfPattern({ +function findNthNextRegexMatch({ content, regex, startingIdx, @@ -46,12 +44,15 @@ function getNthNextInstanceOfPattern({ regex: RegExp; startingIdx: number; n: number; -}): number { +}): number | undefined { const globalRegex = makeGlobalRegex(regex); globalRegex.lastIndex = startingIdx + 1; let currMatch, lastMatch; let numMatchesFound = 0; - while (numMatchesFound < n && (currMatch = globalRegex.exec(content)) != null) { + while ( + numMatchesFound < n && + (currMatch = globalRegex.exec(content)) != null + ) { lastMatch = currMatch; numMatchesFound++; } @@ -59,10 +60,10 @@ function getNthNextInstanceOfPattern({ } /** - * Returns the index of (up to) the nth-last instance of a pattern in a string before a given + * Returns the index of (up to) the nth-previous instance of a pattern in a string before a given * starting index. If the pattern is not found at all, returns undefined. */ -function getNthPreviousInstanceOfPattern({ +function findNthPreviousRegexMatch({ content, regex, startingIdx, diff --git a/utils/obsidianVimCommand.ts b/utils/obsidianVimCommand.ts index 0dc77e8..e0490df 100644 --- a/utils/obsidianVimCommand.ts +++ b/utils/obsidianVimCommand.ts @@ -5,7 +5,7 @@ import { Editor as CodeMirrorEditor } from "codemirror"; import { Editor as ObsidianEditor } from "obsidian"; -import { MotionFn, VimApi } from "./vimApi"; +import { ActionFn, MotionFn, VimApi } from "./vimApi"; export type ObsidianActionFn = ( obsidianEditor: ObsidianEditor, @@ -29,11 +29,7 @@ export function defineObsidianVimAction( obsidianActionFn: ObsidianActionFn, mapping: string ) { - const actionFn = ( - cm: CodeMirrorEditor, - actionArgs: { repeat: number }, - vimState: any - ) => { + const actionFn: ActionFn = (cm, actionArgs, vimState) => { const obsidianEditor = getActiveObsidianEditor(); obsidianActionFn(obsidianEditor, cm, actionArgs, vimState); };