-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
1 parent
89cd8c7
commit d258307
Showing
12 changed files
with
382 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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) => { | ||
const obsidianEditor = vimrcPlugin.getActiveObsidianEditor(); | ||
const { line, ch } = obsidianEditor.getCursor(); | ||
const firstTwoChars = obsidianEditor.getRange( | ||
{ line, ch }, | ||
{ line, ch: ch + 2 } | ||
); | ||
let numCharsMoved = 0; | ||
for (const char of firstTwoChars) { | ||
if (char === "[") { | ||
obsidianEditor.exec("goRight"); | ||
numCharsMoved++; | ||
} | ||
} | ||
vimrcPlugin.executeObsidianCommand("editor:follow-link"); | ||
// Move the cursor back to where it was | ||
for (let i = 0; i < numCharsMoved; i++) { | ||
obsidianEditor.exec("goLeft"); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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, | ||
{ repeat } | ||
) => { | ||
moveSkippingFolds(vimrcPlugin, repeat, "down"); | ||
}; | ||
|
||
/** | ||
* Moves the cursor up `repeat` lines, skipping over folded sections. | ||
*/ | ||
export const moveUpSkippingFolds: ObsidianActionFn = ( | ||
vimrcPlugin, | ||
cm, | ||
{ repeat } | ||
) => { | ||
moveSkippingFolds(vimrcPlugin, repeat, "up"); | ||
}; | ||
|
||
function moveSkippingFolds( | ||
vimrcPlugin: VimrcPlugin, | ||
repeat: number, | ||
direction: "up" | "down" | ||
) { | ||
const obsidianEditor = vimrcPlugin.getActiveObsidianEditor(); | ||
let { line: oldLine, ch: oldCh } = obsidianEditor.getCursor(); | ||
const commandName = direction === "up" ? "goUp" : "goDown"; | ||
for (let i = 0; i < repeat; i++) { | ||
obsidianEditor.exec(commandName); | ||
const { line: newLine, ch: newCh } = obsidianEditor.getCursor(); | ||
if (newLine === oldLine && newCh === oldCh) { | ||
// Going in the specified direction doesn't do anything anymore, stop now | ||
return; | ||
} | ||
[oldLine, oldCh] = [newLine, newCh]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { jumpToPattern } from "../utils/jumpToPattern"; | ||
import { MotionFn } from "../utils/vimApi"; | ||
|
||
const HEADING_REGEX = /^#+ /gm; | ||
|
||
/** | ||
* Jumps to the repeat-th next heading. | ||
*/ | ||
export const jumpToNextHeading: MotionFn = (cm, cursorPosition, { repeat }) => { | ||
return jumpToPattern({ | ||
cm, | ||
cursorPosition, | ||
repeat, | ||
regex: HEADING_REGEX, | ||
direction: "next", | ||
}); | ||
}; | ||
|
||
/** | ||
* Jumps to the repeat-th previous heading. | ||
*/ | ||
export const jumpToPreviousHeading: MotionFn = ( | ||
cm, | ||
cursorPosition, | ||
{ repeat } | ||
) => { | ||
return jumpToPattern({ | ||
cm, | ||
cursorPosition, | ||
repeat, | ||
regex: HEADING_REGEX, | ||
direction: "previous", | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { jumpToPattern } from "../utils/jumpToPattern"; | ||
import { MotionFn } from "../utils/vimApi"; | ||
|
||
const WIKILINK_REGEX_STRING = "\\[\\[[^\\]\\]]+?\\]\\]"; | ||
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, cursorPosition, { repeat }) => { | ||
return jumpToPattern({ | ||
cm, | ||
cursorPosition, | ||
repeat, | ||
regex: LINK_REGEX, | ||
direction: "next", | ||
}); | ||
}; | ||
|
||
/** | ||
* Jumps to the repeat-th previous link. | ||
*/ | ||
export const jumpToPreviousLink: MotionFn = (cm, cursorPosition, { repeat }) => { | ||
return jumpToPattern({ | ||
cm, | ||
cursorPosition, | ||
repeat, | ||
regex: LINK_REGEX, | ||
direction: "previous", | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.