дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-06 18:17:15 +02:00
Usage lists read the line text of each file with one IPC call per file, which for ~1800 files took as long as the references request. fs_read_texts reads them in one call on a few threads (265-377 ms -> 57-59 ms on macOS); open editors and virtual documents keep using their editor text. Refs #6823
139 рядки
5.0 KiB
TypeScript
139 рядки
5.0 KiB
TypeScript
/** IPC helpers shared by the code-insight views. */
|
|
import { openCodeLocation } from "@/editor/api";
|
|
import type { EditorView } from "@codemirror/view";
|
|
import { activeEditor, editorsFor, whenSynced } from "@/editor/api";
|
|
import { isVirtualUri, virtualDocument } from "@/platform/virtualDocs";
|
|
import { executeAction } from "@/platform/actions";
|
|
import { IpcError, call, commands, isCancelled, isTauri, unavailableText } from "@/platform/ipc";
|
|
import type { IdeError } from "@/bindings";
|
|
import { maybeEditorService, notifications } from "@/workbench/contributions";
|
|
|
|
type Result<T> = { status: "ok"; data: T } | { status: "error"; error: IdeError };
|
|
|
|
export async function lspRequest<T>(run: () => Promise<Result<T>>): Promise<T> {
|
|
if (!isTauri()) throw new IpcError({ kind: "unavailable", message: "No language server available" });
|
|
try {
|
|
return await call(run());
|
|
} catch (e) {
|
|
if (e instanceof IpcError) throw e;
|
|
throw new IpcError({ kind: "internal", message: e instanceof Error ? e.message : String(e) });
|
|
}
|
|
}
|
|
|
|
export const isUnavailable = (e: unknown) => e instanceof IpcError && e.kind === "unavailable";
|
|
|
|
/** Small hint at the caret (via editor/lsp), or a notification without an editor; `data`: its `data-*` attributes. */
|
|
export function hint(text: string, tone: "info" | "warning" | "error" = "info", data?: Record<string, string>) {
|
|
if (activeEditor()) void executeAction("editor.showHint", { text, tone, data });
|
|
else
|
|
notifications().notify({
|
|
group: "lsp",
|
|
level: tone === "error" ? "error" : "info",
|
|
title: text,
|
|
key: "code-insight.hint",
|
|
});
|
|
}
|
|
|
|
export function reportError(e: unknown) {
|
|
if (isCancelled(e)) return;
|
|
hint(
|
|
isUnavailable(e) ? unavailableText(e) : e instanceof Error ? e.message : String(e),
|
|
isUnavailable(e) ? "info" : "error",
|
|
);
|
|
}
|
|
|
|
export interface CaretContext {
|
|
doc: number;
|
|
path: string;
|
|
line: number;
|
|
col: number;
|
|
/** Identifier at the caret, if any (for titles). */
|
|
word: string | null;
|
|
view: EditorView | null;
|
|
}
|
|
|
|
export function caretContext(): CaretContext | null {
|
|
const cursor = maybeEditorService()?.activeCursor();
|
|
const ed = activeEditor();
|
|
if (cursor) {
|
|
const view = ed && ed.ctx.doc === cursor.doc ? ed.view : null;
|
|
return { ...cursor, word: view ? wordAt(view, cursor.offset) : null, view };
|
|
}
|
|
if (!ed) return null;
|
|
const head = ed.view.state.selection.main.head;
|
|
const line = ed.view.state.doc.lineAt(head);
|
|
return {
|
|
doc: ed.ctx.doc,
|
|
path: ed.ctx.path,
|
|
line: line.number - 1,
|
|
col: head - line.from,
|
|
word: wordAt(ed.view, head),
|
|
view: ed.view,
|
|
};
|
|
}
|
|
|
|
/** Language servers answer from the backend's copy: the caret's position is only valid there once the editor's edits reached it. */
|
|
export function caretSynced(c: CaretContext): Promise<void> {
|
|
return c.view ? whenSynced(c.view) : Promise.resolve();
|
|
}
|
|
|
|
function wordAt(view: EditorView, offset: number): string | null {
|
|
const w = view.state.wordAt(offset);
|
|
return w ? view.state.sliceDoc(w.from, w.to) : null;
|
|
}
|
|
|
|
/** Screen point below the caret of the active editor. */
|
|
export function caretPoint(): { x: number; y: number } {
|
|
const ed = activeEditor();
|
|
if (ed) {
|
|
const c = ed.view.coordsAtPos(ed.view.state.selection.main.head);
|
|
if (c) return { x: c.left, y: c.bottom };
|
|
const r = ed.view.dom.getBoundingClientRect();
|
|
return { x: r.left + 40, y: r.top + 40 };
|
|
}
|
|
return { x: window.innerWidth / 2 - 200, y: window.innerHeight / 3 };
|
|
}
|
|
|
|
/** File lines from an open editor, else a read-only backend read. */
|
|
export async function readFileLines(path: string): Promise<string[] | null> {
|
|
const open = editorsFor(path)[0];
|
|
if (open) return open.view.state.doc.toJSON();
|
|
if (!isTauri()) return null;
|
|
try {
|
|
if (isVirtualUri(path)) return (await virtualDocument(path)).text.split(/\r\n?|\n/);
|
|
const text = await call(commands.fsReadText(path));
|
|
return text.split(/\r\n?|\n/);
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/** File text for a preview pane (open buffer, else disk). */
|
|
export async function previewText(path: string): Promise<string> {
|
|
const lines = await readFileLines(path);
|
|
if (!lines) throw new Error("Cannot read the file");
|
|
return lines.join("\n");
|
|
}
|
|
|
|
export { readManyFileLines } from "@/editor/lsp/files";
|
|
|
|
/**
|
|
* Runs `fn` with a backend document id for `path`, holding a reference only
|
|
* for the duration of the call (ref-counted, so open editors are unaffected).
|
|
*/
|
|
export async function withDocument<T>(path: string, fn: (doc: number) => Promise<T>): Promise<T> {
|
|
const open = editorsFor(path)[0];
|
|
if (open) return fn(open.ctx.doc);
|
|
// Library types (`jdt://…`) are documents the language server provides, not files on disk.
|
|
const info = await lspRequest(() => (isVirtualUri(path) ? commands.docOpenVirtual(path) : commands.docOpen(path)));
|
|
try {
|
|
return await fn(info.id);
|
|
} finally {
|
|
void commands.docClose(info.id);
|
|
}
|
|
}
|
|
|
|
export function openLocation(path: string, line: number, col: number): Promise<void> {
|
|
return openCodeLocation(path, { line, col });
|
|
}
|