crates/search: parallel ignore walk + grep-searcher with the project's
ignore rules (vendor searched as library, node_modules and .git skipped),
literal or regex, case, whole word, include/exclude globs, binary and
> 8 MiB skips, match cap, cancel token, buffer overlay.
Engine: search.find returns {searchId} at once and runs off the lock;
pump_vfs drains results into searchResults (one per file, at most 500
per pump) and searchDone; search.cancel. Testkit: generated search tree.
Tests: library unit and property tests, engine tests, TCP control test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
304 lines
10 KiB
Rust
304 lines
10 KiB
Rust
//! Find in files: `search.find` and `search.cancel`.
|
|
//!
|
|
//! `search.find` compiles the query, takes a snapshot of the dirty buffers
|
|
//! and starts the search on its own threads ([`lutra_search::spawn`]), then
|
|
//! returns `{searchId}` at once; the engine lock is never held while files
|
|
//! are read. Results wait in a channel per search until
|
|
//! [`crate::Engine::pump_vfs`] calls [`pump`], which turns them into
|
|
//! [`Event::SearchResults`] (one per file) and a final
|
|
//! [`Event::SearchDone`]. Hosts already publish the pump's events, so
|
|
//! results reach the window and control-channel subscribers in the
|
|
//! engine's event order, at most one batch per pump (every 100 ms on the
|
|
//! desktop) and at most [`MAX_RESULTS_PER_PUMP`] files per batch, so a
|
|
//! subscriber's queue never overflows. Searches whose project is no longer
|
|
//! open are cancelled by the next pump and end with `cancelled: true`.
|
|
|
|
use std::collections::BTreeMap;
|
|
use std::path::{Component, PathBuf};
|
|
use std::sync::mpsc::{Receiver, TryRecvError};
|
|
use std::time::Instant;
|
|
|
|
use lutra_search::{CancelToken, Overlay, Query, Search, SearchEvent};
|
|
use serde::Deserialize;
|
|
use serde_json::{Value, json};
|
|
|
|
use crate::cmd::params::parse;
|
|
use crate::{Command, CommandSpec, EngineError, Event, Outcome, SearchMatch, State};
|
|
|
|
/// Most `searchResults` events one pump emits, over all searches; the rest
|
|
/// wait for the next pump.
|
|
pub const MAX_RESULTS_PER_PUMP: usize = 500;
|
|
|
|
/// Most include or exclude globs one query may carry.
|
|
pub const MAX_GLOBS: usize = 256;
|
|
|
|
/// The running searches.
|
|
#[derive(Debug, Default)]
|
|
pub struct Searches {
|
|
/// The id of the last search started; ids are never reused.
|
|
last_id: u64,
|
|
/// Searches whose `searchDone` has not been emitted yet, by id.
|
|
running: BTreeMap<u64, Running>,
|
|
}
|
|
|
|
impl Searches {
|
|
/// Number of searches whose `searchDone` is still to come.
|
|
pub fn running(&self) -> usize {
|
|
self.running.len()
|
|
}
|
|
}
|
|
|
|
/// One search in flight.
|
|
#[derive(Debug)]
|
|
struct Running {
|
|
/// The project root the search walks.
|
|
root: PathBuf,
|
|
/// Stops it.
|
|
cancel: CancelToken,
|
|
/// Its results and summary.
|
|
events: Receiver<SearchEvent>,
|
|
/// When it started, for a summary the search thread never sent.
|
|
started: Instant,
|
|
/// Files emitted so far.
|
|
files: usize,
|
|
/// Matches emitted so far.
|
|
matches: usize,
|
|
}
|
|
|
|
/// Params of `search.find`.
|
|
#[derive(Deserialize)]
|
|
#[serde(deny_unknown_fields, rename_all = "camelCase")]
|
|
struct FindParams {
|
|
/// The text or regular expression.
|
|
query: String,
|
|
/// `query` is a regular expression.
|
|
#[serde(default)]
|
|
regex: bool,
|
|
/// Match letter case exactly.
|
|
#[serde(default)]
|
|
case_sensitive: bool,
|
|
/// Whole words only.
|
|
#[serde(default)]
|
|
whole_word: bool,
|
|
/// Only files matching one of these globs.
|
|
#[serde(default)]
|
|
include: Vec<String>,
|
|
/// Skip files and directories matching one of these globs.
|
|
#[serde(default)]
|
|
exclude: Vec<String>,
|
|
/// Cap on matches.
|
|
max_results: Option<usize>,
|
|
}
|
|
|
|
/// `search.find`: starts a search in the project.
|
|
pub struct Find;
|
|
|
|
impl Command for Find {
|
|
fn spec(&self) -> &CommandSpec {
|
|
&CommandSpec {
|
|
id: "search.find",
|
|
label: "Find in Files",
|
|
menu_path: None,
|
|
default_shortcut: None,
|
|
params: "{ query, regex?, caseSensitive?, wholeWord?, include?: [glob], exclude?: [glob], maxResults? (default 10000, at most 100000) }; returns { searchId } at once, then events searchResults { searchId, path, matches: [{ line, column, length, preview, previewColumn }] } per file and searchDone { searchId, files, matches, truncated, cancelled, elapsedMs }; invalid_params for an empty query, a bad regex or glob",
|
|
}
|
|
}
|
|
|
|
fn enabled(&self, state: &State) -> bool {
|
|
state.has_project()
|
|
}
|
|
|
|
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
|
|
let id = self.spec().id;
|
|
let p: FindParams = parse(id, params)?;
|
|
if p.include.len() > MAX_GLOBS || p.exclude.len() > MAX_GLOBS {
|
|
return Err(EngineError::invalid_params(
|
|
id,
|
|
format!("at most {MAX_GLOBS} include and {MAX_GLOBS} exclude globs"),
|
|
));
|
|
}
|
|
let query = Query {
|
|
pattern: p.query,
|
|
regex: p.regex,
|
|
case_sensitive: p.case_sensitive,
|
|
whole_word: p.whole_word,
|
|
include: p.include,
|
|
exclude: p.exclude,
|
|
max_results: p.max_results.unwrap_or(lutra_search::DEFAULT_MAX_RESULTS),
|
|
};
|
|
let search =
|
|
Search::new(&query).map_err(|e| EngineError::invalid_params(id, e.to_string()))?;
|
|
let root = state
|
|
.project
|
|
.as_ref()
|
|
.ok_or_else(|| EngineError::Disabled(id.to_string()))?
|
|
.project
|
|
.root()
|
|
.to_path_buf();
|
|
let cancel = CancelToken::new();
|
|
let events = lutra_search::spawn(root.clone(), search, overlay(state), cancel.clone())
|
|
.map_err(|e| EngineError::Failed(format!("cannot start the search: {e}")))?;
|
|
let searches = &mut state.searches;
|
|
searches.last_id = searches.last_id.saturating_add(1);
|
|
let search_id = searches.last_id;
|
|
searches.running.insert(
|
|
search_id,
|
|
Running {
|
|
root,
|
|
cancel,
|
|
events,
|
|
started: Instant::now(),
|
|
files: 0,
|
|
matches: 0,
|
|
},
|
|
);
|
|
Ok(Outcome::value(json!({ "searchId": search_id })))
|
|
}
|
|
}
|
|
|
|
/// Params of `search.cancel`.
|
|
#[derive(Deserialize)]
|
|
#[serde(deny_unknown_fields, rename_all = "camelCase")]
|
|
struct CancelParams {
|
|
/// The search to stop.
|
|
search_id: u64,
|
|
}
|
|
|
|
/// `search.cancel`: stops a running search.
|
|
pub struct Cancel;
|
|
|
|
impl Command for Cancel {
|
|
fn spec(&self) -> &CommandSpec {
|
|
&CommandSpec {
|
|
id: "search.cancel",
|
|
label: "Cancel Search",
|
|
menu_path: None,
|
|
default_shortcut: None,
|
|
params: "{ searchId }; returns { cancelled }: false if the search already ended or never existed; a cancelled search still ends with searchDone (cancelled: true)",
|
|
}
|
|
}
|
|
|
|
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
|
|
let p: CancelParams = parse(self.spec().id, params)?;
|
|
let running = state.searches.running.get(&p.search_id);
|
|
if let Some(running) = running {
|
|
running.cancel.cancel();
|
|
}
|
|
Ok(Outcome::value(json!({ "cancelled": running.is_some() })))
|
|
}
|
|
}
|
|
|
|
/// The text of every dirty buffer by project-relative path, searched
|
|
/// instead of the file on disk. A path that is not plain Unicode cannot
|
|
/// match a walked file and is left out.
|
|
fn overlay(state: &State) -> Overlay {
|
|
state
|
|
.buffers
|
|
.dirty_ids()
|
|
.into_iter()
|
|
.filter_map(|id| state.buffers.get(id))
|
|
.filter_map(|b| Some((slash_path(&b.path)?, b.buffer.text())))
|
|
.collect()
|
|
}
|
|
|
|
/// A project-relative path with `/` separators.
|
|
fn slash_path(path: &std::path::Path) -> Option<String> {
|
|
let parts: Option<Vec<&str>> = path
|
|
.components()
|
|
.map(|c| match c {
|
|
Component::Normal(name) => name.to_str(),
|
|
_ => None,
|
|
})
|
|
.collect();
|
|
Some(parts?.join("/"))
|
|
}
|
|
|
|
/// Moves finished results of running searches into events: at most
|
|
/// [`MAX_RESULTS_PER_PUMP`] `searchResults`, and `searchDone` for every
|
|
/// search that ended. Searches of a project that is no longer open are
|
|
/// cancelled; their remaining results are dropped. A search thread that
|
|
/// vanished without a summary ends with `cancelled: true` and the counts
|
|
/// emitted so far.
|
|
pub fn pump(state: &mut State) {
|
|
let root = state
|
|
.project
|
|
.as_ref()
|
|
.map(|w| w.project.root().to_path_buf());
|
|
let mut budget = MAX_RESULTS_PER_PUMP;
|
|
let mut events = Vec::new();
|
|
let mut finished = Vec::new();
|
|
for (&search_id, running) in state.searches.running.iter_mut() {
|
|
let stale = root.as_ref() != Some(&running.root);
|
|
if stale {
|
|
running.cancel.cancel();
|
|
}
|
|
loop {
|
|
if budget == 0 && !stale {
|
|
break;
|
|
}
|
|
match running.events.try_recv() {
|
|
Ok(SearchEvent::File(file)) => {
|
|
if stale {
|
|
continue;
|
|
}
|
|
budget = budget.saturating_sub(1);
|
|
running.files = running.files.saturating_add(1);
|
|
running.matches = running.matches.saturating_add(file.matches.len());
|
|
events.push(Event::SearchResults {
|
|
search_id,
|
|
path: file.path,
|
|
matches: file.matches.into_iter().map(SearchMatch::from).collect(),
|
|
});
|
|
}
|
|
Ok(SearchEvent::Done(summary)) => {
|
|
events.push(Event::SearchDone {
|
|
search_id,
|
|
files: summary.files,
|
|
matches: summary.matches,
|
|
truncated: summary.truncated,
|
|
cancelled: summary.cancelled || stale,
|
|
elapsed_ms: millis(summary.elapsed),
|
|
});
|
|
finished.push(search_id);
|
|
break;
|
|
}
|
|
Err(TryRecvError::Empty) => break,
|
|
Err(TryRecvError::Disconnected) => {
|
|
events.push(Event::SearchDone {
|
|
search_id,
|
|
files: running.files,
|
|
matches: running.matches,
|
|
truncated: false,
|
|
cancelled: true,
|
|
elapsed_ms: millis(running.started.elapsed()),
|
|
});
|
|
finished.push(search_id);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for search_id in finished {
|
|
state.searches.running.remove(&search_id);
|
|
}
|
|
for event in events {
|
|
state.emit(event);
|
|
}
|
|
}
|
|
|
|
/// Whole milliseconds of `elapsed`, saturating.
|
|
fn millis(elapsed: std::time::Duration) -> u64 {
|
|
u64::try_from(elapsed.as_millis()).unwrap_or(u64::MAX)
|
|
}
|
|
|
|
impl From<lutra_search::Match> for SearchMatch {
|
|
fn from(m: lutra_search::Match) -> Self {
|
|
Self {
|
|
line: m.line,
|
|
column: m.column,
|
|
length: m.length,
|
|
preview: m.preview,
|
|
preview_column: m.preview_column,
|
|
}
|
|
}
|
|
}
|