esm7_obsidian-vimrc-support/utils/vimApi.ts
Aly Thobani d2583078ac
Implement and provide default mappings for some Obsidian-specific Vim motions/commands (#222)
* feat: define and expose obsidian-specific vim commands

jumpToNextHeading: g]
jumpToPreviousHeading: g[

* Implement jumpToPreviousLink motion

* Refactoring and implementing jumpToNextLink

* refactor: new jumpToPattern function that can be used for motions

* refactor: renamed file and removed unneeded exports

* fix: return last found index even if fewer than n instances found, instead of undefined

* feat: implement moveUpSkipFold and moveDownSkipFold

* refactor: extract out helper functions for defining obsidian vim actions

* refactor: split vimApi.ts into two files

* refactor: add comment

* refactor: update names, types, etc

* feat: followLinkUnderCursor action

* feat: jumpToLink now jumps to both markdown and wiki links

* refactor: rename fns

* refactor: add docstrings / change var names

* feat: implement looping around

* refactor: cleaner implementation of jumpToPattern

* Change mappings for next/prev heading to [[ and ]]

* Tiny fixes

* docs: update docs now that some more motions are provided by default

---------

Co-authored-by: Erez Shermer <erezshermer@gmail.com>
2024-07-23 20:21:29 +03:00

33 lines
884 B
TypeScript

/**
* Partial representation of the CodeMirror Vim API that we use to define motions, commands, etc.
*
* References:
* https://github.com/replit/codemirror-vim/blob/master/src/vim.js
* https://libvoyant.ucr.edu/resources/codemirror/doc/manual.html
*/
import { Editor as CodeMirrorEditor } from "codemirror";
import { EditorPosition } from "obsidian";
export type MotionFn = (
cm: CodeMirrorEditor,
cursorPosition: EditorPosition, // called `head` in the API
motionArgs: { repeat: number }
) => EditorPosition;
export type ActionFn = (
cm: CodeMirrorEditor,
actionArgs: { repeat: number },
) => void;
export type VimApi = {
defineMotion: (name: string, fn: MotionFn) => void;
defineAction: (name: string, fn: ActionFn) => void;
mapCommand: (
keys: string,
type: string,
name: string,
args: any,
extra: { [x: string]: any }
) => void;
};