(uri: string, line: number)
| 33 | } |
| 34 | |
| 35 | async function fetchLineText(uri: string, line: number): Promise<string> { |
| 36 | try { |
| 37 | interface EditorManagerLike { |
| 38 | getFile?: (uri: string, type: string) => EditorFileLike | null; |
| 39 | } |
| 40 | |
| 41 | interface EditorFileLike { |
| 42 | session?: { |
| 43 | doc?: { |
| 44 | line?: (n: number) => { text?: string } | null; |
| 45 | toString?: () => string; |
| 46 | }; |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | const em = (globalThis as Record<string, unknown>).editorManager as |
| 51 | | EditorManagerLike |
| 52 | | undefined; |
| 53 | |
| 54 | const openFile = em?.getFile?.(uri, "uri"); |
| 55 | if (openFile?.session?.doc) { |
| 56 | const doc = openFile.session.doc; |
| 57 | if (typeof doc.line === "function") { |
| 58 | const lineObj = doc.line(line + 1); |
| 59 | if (lineObj && typeof lineObj.text === "string") { |
| 60 | return lineObj.text; |
| 61 | } |
| 62 | } |
| 63 | if (typeof doc.toString === "function") { |
| 64 | const content = doc.toString(); |
| 65 | const lines = content.split("\n"); |
| 66 | if (lines[line] !== undefined) { |
| 67 | return lines[line]; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | const fs = fsOperation(uri); |
| 73 | if (fs && (await fs.exists())) { |
| 74 | const encoding = |
| 75 | (settings as { value?: { defaultFileEncoding?: string } })?.value |
| 76 | ?.defaultFileEncoding || "utf-8"; |
| 77 | const content = await fs.readFile(encoding); |
| 78 | if (typeof content === "string") { |
| 79 | const lines = content.split("\n"); |
| 80 | if (lines[line] !== undefined) { |
| 81 | return lines[line]; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } catch (error) { |
| 86 | console.warn(`Failed to fetch line text for ${uri}:${line}`, error); |
| 87 | } |
| 88 | return ""; |
| 89 | } |
| 90 | |
| 91 | function getWordAtCursor(view: EditorView): string { |
| 92 | const { state } = view; |
no test coverage detected