refactor: add docstrings / change var names

This commit is contained in:
Aly Thobani 2024-05-10 11:20:21 -07:00
parent 3d457c2f8c
commit 2adf45cdbc
4 changed files with 25 additions and 4 deletions

View file

@ -1,23 +1,26 @@
import { ObsidianActionFn } from "../utils/obsidianVimCommand";
/**
* Follows the link under the cursor, temporarily moving the cursor if necessary for follow-link to
* work (i.e. if the cursor is on a starting square bracket).
*/
export const followLinkUnderCursor: ObsidianActionFn = (vimrcPlugin) => {
// If the cursor is on the starting square bracket(s), we need to move it inside them
const obsidianEditor = vimrcPlugin.getActiveObsidianEditor();
const { line, ch } = obsidianEditor.getCursor();
const firstTwoChars = obsidianEditor.getRange(
{ line, ch },
{ line, ch: ch + 2 }
);
let charOffset = 0;
let numCharsMoved = 0;
for (const char of firstTwoChars) {
if (char === "[") {
obsidianEditor.exec("goRight");
charOffset++;
numCharsMoved++;
}
}
vimrcPlugin.executeObsidianCommand("editor:follow-link");
// Move the cursor back to where it was
for (let i = 0; i < charOffset; i++) {
for (let i = 0; i < numCharsMoved; i++) {
obsidianEditor.exec("goLeft");
}
};

View file

@ -1,6 +1,9 @@
import VimrcPlugin from "../main";
import { ObsidianActionFn } from "../utils/obsidianVimCommand";
/**
* Moves the cursor down `repeat` lines, skipping over folded sections.
*/
export const moveDownSkippingFolds: ObsidianActionFn = (
vimrcPlugin,
cm,
@ -9,6 +12,9 @@ export const moveDownSkippingFolds: ObsidianActionFn = (
moveSkippingFolds(vimrcPlugin, repeat, "down");
};
/**
* Moves the cursor up `repeat` lines, skipping over folded sections.
*/
export const moveUpSkippingFolds: ObsidianActionFn = (
vimrcPlugin,
cm,

View file

@ -3,6 +3,9 @@ import { MotionFn } from "../utils/vimApi";
const HEADING_REGEX = /^#+ /gm;
/**
* Jumps to the repeat-th next heading.
*/
export const jumpToNextHeading: MotionFn = (cm, oldPosition, { repeat }) => {
return jumpToPattern({
cm,
@ -13,6 +16,9 @@ export const jumpToNextHeading: MotionFn = (cm, oldPosition, { repeat }) => {
});
};
/**
* Jumps to the repeat-th previous heading.
*/
export const jumpToPreviousHeading: MotionFn = (
cm,
oldPosition,

View file

@ -6,6 +6,9 @@ const MARKDOWN_LINK_REGEX_STRING = "\\[[^\\]]+?\\]\\([^)]+?\\)";
const LINK_REGEX_STRING = `${WIKILINK_REGEX_STRING}|${MARKDOWN_LINK_REGEX_STRING}`;
const LINK_REGEX = new RegExp(LINK_REGEX_STRING, "g");
/**
* Jumps to the repeat-th next link.
*/
export const jumpToNextLink: MotionFn = (cm, oldPosition, { repeat }) => {
return jumpToPattern({
cm,
@ -16,6 +19,9 @@ export const jumpToNextLink: MotionFn = (cm, oldPosition, { repeat }) => {
});
};
/**
* Jumps to the repeat-th previous link.
*/
export const jumpToPreviousLink: MotionFn = (cm, oldPosition, { repeat }) => {
return jumpToPattern({
cm,