import { Text } from "@codemirror/text" ; import { Editor , EditorPosition } from "obsidian" ; import { EditorState } from "@codemirror/state" ; import { EditorView } from "@codemirror/view" ; export function posFromIndex ( doc , offset ) { let line = doc . lineAt ( offset ) ; return { line : line . number - 1 , ch : offset - line . from } ; } export function indexFromPos ( doc , pos ) { const ch = pos . ch ; const line = doc . line ( pos . line + 1 ) ; return Math . min ( line . from + Math . max ( 0 , ch ) , line . to ) ; } export function editorToCodeMirrorState ( editor ) { return ( editor as any ) . cm . state ; } export function editorToCodeMirrorView ( editor ) { return ( editor as any ) . cm ; } export function matchWordBackwards ( editor , cursor , charValidator , maxLookBackDistance = 50 ) { let query = "" , separatorChar = null ; let lookBackEnd = Math . max ( 0 , cursor . ch - maxLookBackDistance ) ; for ( let i = cursor . ch - 1 ; i >= lookBackEnd ; i -- ) { const prevChar = editor . getRange ( { ... cursor , ch : i } , { ... cursor , ch : i + 1 } ) ; if ( ! charValidator ( prevChar ) ) { separatorChar = prevChar ; break ; } query = prevChar + query ; } return { query , separatorChar } ; }