diff --git a/crates/ide-host/src/ops/fs.rs b/crates/ide-host/src/ops/fs.rs index b522a09f4..2ed4a6242 100644 --- a/crates/ide-host/src/ops/fs.rs +++ b/crates/ide-host/src/ops/fs.rs @@ -439,6 +439,29 @@ pub async fn fs_read_text(host: &Host, window: &str, path: PathBuf) -> IdeResult .await } +/// Reads many text files in one call, as `fs_read_text` does each (usage lists show a line from +/// each of hundreds of files): `None` for one that can't be read. +#[command(trust = always)] +pub async fn fs_read_texts(host: &Host, window: &str, paths: Vec) -> IdeResult>> { + let project = host.project(window).ok(); + host.blocking(move || { + let read = |path: &PathBuf| match &project { + Some(p) => p.text_of(path).ok(), + None => ide_vfs::load_text(path).ok().map(|t| t.text), + }; + let workers = std::thread::available_parallelism().map_or(4, |n| n.get().min(8)); + let chunk = paths.len().div_ceil(workers).max(1); + Ok(std::thread::scope(|scope| { + let parts: Vec<_> = paths + .chunks(chunk) + .map(|part| (part.len(), scope.spawn(move || part.iter().map(read).collect::>()))) + .collect(); + parts.into_iter().flat_map(|(len, h)| h.join().unwrap_or_else(|_| vec![None; len])).collect() + })) + }) + .await +} + /// The file `window` may hand to the OS's default application: `path` itself, resolved, when it is a /// file inside the window's project (its folder or a content root). The window must be trusted and /// open locally; a remote window's files live on its own machine and are not launched here. @@ -550,6 +573,7 @@ ide_command::domain!( fs_entry_kind, fs_read_dir, fs_read_text, + fs_read_texts, fs_rename, fs_scratches_dir, fs_set_writable, diff --git a/src-tauri/tests/commands.snap b/src-tauri/tests/commands.snap index 2dbe09d18..fcd3e0189 100644 --- a/src-tauri/tests/commands.snap +++ b/src-tauri/tests/commands.snap @@ -361,6 +361,7 @@ fs_read_dir always project_host bin fs_read_image always project_host bindings async fs_read_range always project_host bindings async fs_read_text always project_host bindings async +fs_read_texts always project_host bindings async fs_rename always project_host bindings async fs_resolve_reference always project_host bindings async fs_scratches_dir always project_host bindings sync diff --git a/src/bindings.ts b/src/bindings.ts index a8561cb4c..b5c60f8d8 100644 --- a/src/bindings.ts +++ b/src/bindings.ts @@ -919,6 +919,11 @@ export const commands = { * definition). Prefers the unsaved buffer when the file is open. */ fsReadText: (path: string) => typedError(__TAURI_INVOKE("fs_read_text", { path })), + /** + * Reads many text files in one call, as `fs_read_text` does each (usage lists show a line from + * each of hundreds of files): `None` for one that can't be read. + */ + fsReadTexts: (paths: string[]) => typedError<(string | null)[], IdeError>(__TAURI_INVOKE("fs_read_texts", { paths })), fsRename: (from: string, to: string) => typedError(__TAURI_INVOKE("fs_rename", { from, to })), /** The existing file or directory a path written in `from` names (`href`, `url()`, a string literal). */ fsResolveReference: (from: IpcPath, value: string) => typedError(__TAURI_INVOKE("fs_resolve_reference", { from, value })), diff --git a/src/editor/lsp/files.ts b/src/editor/lsp/files.ts index b5e6729af..dd10619a3 100644 --- a/src/editor/lsp/files.ts +++ b/src/editor/lsp/files.ts @@ -24,13 +24,36 @@ export async function readFileLines(path: string): Promise { } } -/** Reads several files with bounded concurrency; missing files map to `null`. */ +/** + * Lines of several files; missing ones map to `null`. Files on disk come in one backend call: + * a usage list can name thousands, and one call per file is slow where IPC is (WebView2). + */ export async function readManyFileLines( paths: readonly string[], concurrency = 4, ): Promise> { const out = new Map(); - const queue = [...new Set(paths)]; + const queue: string[] = []; + const onDisk: string[] = []; + for (const p of new Set(paths)) { + if (isTauri() && !editorsFor(p)[0] && !isVirtualUri(p)) onDisk.push(p); + else queue.push(p); + } + if (onDisk.length > 1) { + try { + const texts = await call(commands.fsReadTexts(onDisk)); + onDisk.forEach((p, i) => { + const text = texts[i]; + // Opened meanwhile: its editor has the newer text. + out.set( + p, + editorsFor(p)[0] ? editorsFor(p)[0]!.view.state.doc.toJSON() : text == null ? null : text.split(/\r\n?|\n/), + ); + }); + } catch { + queue.push(...onDisk); + } + } else queue.push(...onDisk); const worker = async () => { for (let p = queue.shift(); p !== undefined; p = queue.shift()) { // oxlint-disable-next-line no-await-in-loop -- workers drain a shared queue; concurrency is bounded by the worker count diff --git a/src/features/code-insight/ipc.ts b/src/features/code-insight/ipc.ts index b3b9c1ae3..b76059cfa 100644 --- a/src/features/code-insight/ipc.ts +++ b/src/features/code-insight/ipc.ts @@ -115,19 +115,7 @@ export async function previewText(path: string): Promise { return lines.join("\n"); } -export async function readManyFileLines( - paths: readonly string[], - concurrency = 4, -): Promise> { - const out = new Map(); - const queue = [...new Set(paths)]; - const worker = async () => { - // oxlint-disable-next-line no-await-in-loop -- bounded worker pool: queue.shift() limits fan-out to `concurrency` - for (let p = queue.shift(); p !== undefined; p = queue.shift()) out.set(p, await readFileLines(p)); - }; - await Promise.all(Array.from({ length: Math.min(concurrency, queue.length) }, worker)); - return out; -} +export { readManyFileLines } from "@/editor/lsp/files"; /** * Runs `fn` with a backend document id for `path`, holding a reference only