MCPcopy Index your code
hub / github.com/esm7/obsidian-vimrc-support / jumpToPattern

Function jumpToPattern

utils/jumpToPattern.ts:22–47  ·  view source on GitHub ↗
({
  cm,
  cursorPosition,
  repeat,
  regex,
  filterMatch = () => true,
  direction,
}: {
  cm: CodeMirrorEditor;
  cursorPosition: EditorPosition;
  repeat: number;
  regex: RegExp;
  filterMatch?: (match: RegExpExecArray) => boolean;
  direction: "next" | "previous";
})

Source from the content-addressed store, hash-verified

20 * @param direction The direction to jump in.
21 */
22export function jumpToPattern({
23 cm,
24 cursorPosition,
25 repeat,
26 regex,
27 filterMatch = () => true,
28 direction,
29}: {
30 cm: CodeMirrorEditor;
31 cursorPosition: EditorPosition;
32 repeat: number;
33 regex: RegExp;
34 filterMatch?: (match: RegExpExecArray) => boolean;
35 direction: "next" | "previous";
36}): EditorPosition {
37 const content = cm.getValue();
38 const cursorIdx = cm.indexFromPos(cursorPosition);
39 const orderedMatches = getOrderedMatches({ content, regex, cursorIdx, filterMatch, direction });
40 const effectiveRepeat = (repeat % orderedMatches.length) || orderedMatches.length;
41 const matchIdx = orderedMatches[effectiveRepeat - 1]?.index;
42 if (matchIdx === undefined) {
43 return cursorPosition;
44 }
45 const newCursorPosition = cm.posFromIndex(matchIdx);
46 return newCursorPosition;
47}
48
49/**
50 * Returns an ordered array of all matches of a regex in a string in the given direction from the

Callers 3

jumpToNextLinkFunction · 0.90
jumpToPreviousLinkFunction · 0.90
jumpToHeadingFunction · 0.90

Calls 1

getOrderedMatchesFunction · 0.85

Tested by

no test coverage detected