From 3ea854d395d075baa8fc902c6bbcc46b101307c9 Mon Sep 17 00:00:00 2001 From: Tal Wrii Date: Wed, 2 Apr 2025 18:38:55 +0200 Subject: [PATCH] jumpLine and fuzzySelectPair functions --- src/bufferMotion.ts | 4 ++++ src/fuzzy.ts | 27 +++++++++++++++++---------- src/main.ts | 17 ++++++++++++----- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/bufferMotion.ts b/src/bufferMotion.ts index 33a5dd1..b326c3d 100644 --- a/src/bufferMotion.ts +++ b/src/bufferMotion.ts @@ -1,5 +1,9 @@ import { Editor, EditorPosition } from 'obsidian'; +export function jumpLine(editor: Editor, line: number) { + return editor.setCursor({ ch: 0, line: line }) +} + export function jump(editor: Editor, position: EditorPosition) { return editor.setCursor(position) } diff --git a/src/fuzzy.ts b/src/fuzzy.ts index 02e1b5c..1c252bf 100644 --- a/src/fuzzy.ts +++ b/src/fuzzy.ts @@ -1,20 +1,27 @@ import { FuzzySuggestModal, App } from 'obsidian'; - export function fuzzySelect(app: App, choices: Array, prompt?: string) { + return new Promise((reject, resolve) => { + let selector = new FuzzySelector(app, prompt || "select:", choices.map((x) => [x, x]), [reject, resolve]) + selector.run() + }) +} + + +export function fuzzySelectPair(app: App, choices: Array<[string, any]>, prompt?: string) { return new Promise((reject, resolve) => { let selector = new FuzzySelector(app, prompt || "select:", choices, [reject, resolve]) selector.run() }) } -class FuzzySelector extends FuzzySuggestModal { +class FuzzySelector extends FuzzySuggestModal<[string, any]> { resolve: (_: any) => void reject: (_: any) => void - choices: Array + choices: Array<[string, any]> - constructor(app: App, prompt: string, choices: Array, callbacks: [resolv: (_: any) => void, reject: (_: any) => void]) { + constructor(app: App, prompt: string, choices: Array<[string, any]>, callbacks: [resolv: (_: any) => void, reject: (_: any) => void]) { const [resolve, reject] = callbacks super(app); this.setPlaceholder(prompt); @@ -23,16 +30,16 @@ class FuzzySelector extends FuzzySuggestModal { this.choices = choices } - getItems(): string[] { - return this.choices; + getItems(): Array<[string, any]> { + return this.choices } - getItemText(choice: string): string { - return choice + getItemText(choice: [string, any]): string { + return choice[0] } - onChooseItem(item: string): void { - this.resolve(item) + onChooseItem(item: [string, any]): void { + this.resolve(item[1]) } run() { diff --git a/src/main.ts b/src/main.ts index 1d15c43..7a63c01 100644 --- a/src/main.ts +++ b/src/main.ts @@ -6,7 +6,7 @@ import { import { execFileSync, execSync } from 'child_process' import { parse as shellParse, quote as shellQuote } from 'shell-quote'; import { expandRegionWithRegexp } from './editorUtils' -import { jump, forwardChar, point, pointMax, pointMin, lineNumber, mark, endOfLine, endOfLinePoint, atEndOfBuffer } from './bufferMotion' +import { jump, jumpLine, forwardChar, point, pointMax, pointMin, lineNumber, mark, endOfLine, endOfLinePoint, atEndOfBuffer } from './bufferMotion' import { forwardRegexp, atRegexp } from './regexpMotion' import { bufferString, restOfLine } from './bufferData' @@ -16,8 +16,7 @@ import { Scope } from './scope' import { History } from './history' import { PrivateApp, DataviewPlugin } from "./types" - -import { fuzzySelect } from './fuzzy' +import { fuzzySelect, fuzzySelectPair } from './fuzzy' import { promptString } from './prompt' import { promptCommand } from './promptCommand' import { popup } from './popup' @@ -225,9 +224,11 @@ export default class ReplPlugin extends Plugin { ) this.addToScopeWithDoc( "fuzzySelect", fuzzySelect.bind(null, this.app), - "(options: Array, prompt?: string) Select from options with fuzzy search" + "(options: Array, prompt?: string) Select from options with fuzzy search") + this.addToScopeWithDoc( + "fuzzySelectPair", fuzzySelectPair.bind(null, this.app), + "(options: Array, prompt?: string) Select items from a list using label strings provided") - ) this.addToScopeWithDoc( "openUrl", openUrl, "(url: string) Open this url in a browser " @@ -315,10 +316,16 @@ export default class ReplPlugin extends Plugin { "pointMax", pointMax.bind(null, editor), "Returns the cursor positoin at the beginning of the note" ) + this.addToScopeWithDoc( "jump", jump.bind(null, editor), "Jump to the given point." ) + this.addToScopeWithDoc( + "jumpLine", jumpLine.bind(null, editor), + "Jump to the given line." + ) + this.addToScopeWithDoc( "selection", selection.bind(null, editor), "Returns the text of the selection."