refactor: update names, types, etc

This commit is contained in:
Aly Thobani 2024-04-27 13:59:22 -07:00
parent 51fc17cf98
commit 57bf405cb0
3 changed files with 17 additions and 20 deletions

View file

@ -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');

View file

@ -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,

View file

@ -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);
};