jumpLine and fuzzySelectPair functions

This commit is contained in:
Tal Wrii 2025-04-02 18:38:55 +02:00
parent 21691f596b
commit 3ea854d395
3 changed files with 33 additions and 15 deletions

View file

@ -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)
}

View file

@ -1,20 +1,27 @@
import { FuzzySuggestModal, App } from 'obsidian';
export function fuzzySelect(app: App, choices: Array<string>, 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<string> {
class FuzzySelector extends FuzzySuggestModal<[string, any]> {
resolve: (_: any) => void
reject: (_: any) => void
choices: Array<string>
choices: Array<[string, any]>
constructor(app: App, prompt: string, choices: Array<string>, 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<string> {
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() {

View file

@ -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<string>, prompt?: string) Select from options with fuzzy search"
"(options: Array<string>, prompt?: string) Select from options with fuzzy search")
this.addToScopeWithDoc(
"fuzzySelectPair", fuzzySelectPair.bind(null, this.app),
"(options: Array<string, any>, 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."