MCPcopy Create free account
hub / github.com/Acode-Foundation/Acode / fetchLineText

Function fetchLineText

src/cm/lsp/references.ts:35–89  ·  view source on GitHub ↗
(uri: string, line: number)

Source from the content-addressed store, hash-verified

33}
34
35async 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
91function getWordAtCursor(view: EditorView): string {
92 const { state } = view;

Callers 1

fetchReferencesFunction · 0.85

Calls 5

fsOperationFunction · 0.85
getFileMethod · 0.65
existsMethod · 0.65
toStringMethod · 0.45
readFileMethod · 0.45

Tested by

no test coverage detected