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>
170 lines
5.4 KiB
Rust
170 lines
5.4 KiB
Rust
//! Find in files over a real TCP control connection: `search.find` answers
|
|
//! at once, `searchResults` and `searchDone` arrive as event lines from the
|
|
//! pump, `search.cancel` ends a search.
|
|
|
|
#![allow(
|
|
clippy::unwrap_used,
|
|
clippy::expect_used,
|
|
clippy::indexing_slicing,
|
|
clippy::panic
|
|
)]
|
|
|
|
use std::io::{BufRead, BufReader, Write};
|
|
use std::net::TcpStream;
|
|
use std::sync::Arc;
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
use std::time::Duration;
|
|
|
|
use lutra_testkit::EngineBackend;
|
|
use serde_json::{Value, json};
|
|
|
|
/// A client that keeps responses and event lines apart.
|
|
struct Client {
|
|
reader: BufReader<TcpStream>,
|
|
writer: TcpStream,
|
|
events: Vec<Value>,
|
|
}
|
|
|
|
impl Client {
|
|
fn connect(addr: std::net::SocketAddr) -> Self {
|
|
let stream = TcpStream::connect(addr).unwrap();
|
|
stream
|
|
.set_read_timeout(Some(Duration::from_secs(10)))
|
|
.unwrap();
|
|
Self {
|
|
writer: stream.try_clone().unwrap(),
|
|
reader: BufReader::new(stream),
|
|
events: Vec::new(),
|
|
}
|
|
}
|
|
|
|
fn line(&mut self) -> Value {
|
|
let mut line = String::new();
|
|
self.reader.read_line(&mut line).expect("line within 10 s");
|
|
serde_json::from_str(&line).unwrap()
|
|
}
|
|
|
|
fn call(&mut self, request: Value) -> Value {
|
|
self.writer
|
|
.write_all(format!("{request}\n").as_bytes())
|
|
.unwrap();
|
|
loop {
|
|
let line = self.line();
|
|
if line.get("event").is_some() {
|
|
self.events.push(line["event"].clone());
|
|
} else {
|
|
return line;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Reads event lines until `searchDone` for `id`; returns the
|
|
/// `searchResults` of that search and the done event.
|
|
fn until_done(&mut self, id: u64) -> (Vec<Value>, Value) {
|
|
let mut results = Vec::new();
|
|
let mut pending = std::mem::take(&mut self.events).into_iter();
|
|
loop {
|
|
let event = match pending.next() {
|
|
Some(e) => e,
|
|
None => {
|
|
let line = self.line();
|
|
let Some(event) = line.get("event") else {
|
|
continue;
|
|
};
|
|
event.clone()
|
|
}
|
|
};
|
|
if event["searchId"] != id {
|
|
continue;
|
|
}
|
|
match event["kind"].as_str() {
|
|
Some("searchResults") => results.push(event),
|
|
Some("searchDone") => return (results, event),
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn execute(command: &str, params: Value) -> Value {
|
|
json!({"id": command, "method": "engine.execute", "params": {"command": command, "params": params}})
|
|
}
|
|
|
|
/// Pumps the backend every 20 ms, like the desktop's pump thread, until
|
|
/// dropped.
|
|
struct Pump(Arc<AtomicBool>);
|
|
|
|
impl Pump {
|
|
fn start(backend: Arc<EngineBackend>) -> Self {
|
|
let stop = Arc::new(AtomicBool::new(false));
|
|
let flag = stop.clone();
|
|
std::thread::spawn(move || {
|
|
while !flag.load(Ordering::Relaxed) {
|
|
backend.pump();
|
|
std::thread::sleep(Duration::from_millis(20));
|
|
}
|
|
});
|
|
Self(stop)
|
|
}
|
|
}
|
|
|
|
impl Drop for Pump {
|
|
fn drop(&mut self) {
|
|
self.0.store(true, Ordering::Relaxed);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn find_streams_results_then_done_and_cancels() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let tree = lutra_testkit::generate_search_tree(dir.path(), 1_000);
|
|
let backend = Arc::new(EngineBackend::new());
|
|
let server = lutra_control::start(0, backend.clone()).unwrap();
|
|
let mut client = Client::connect(server.local_addr());
|
|
client.call(json!({"id": 1, "method": "events.subscribe"}));
|
|
let r = client.call(execute("project.open", json!({"path": dir.path()})));
|
|
assert!(r["result"]["root"].is_string(), "{r}");
|
|
let _pump = Pump::start(backend.clone());
|
|
|
|
let r = client.call(execute(
|
|
"search.find",
|
|
json!({"query": lutra_testkit::NEEDLE, "caseSensitive": true}),
|
|
));
|
|
let id = r["result"]["searchId"].as_u64().expect("searchId");
|
|
let (results, done) = client.until_done(id);
|
|
assert_eq!(results.len(), tree.needle_files);
|
|
for result in &results {
|
|
let m = &result["matches"][0];
|
|
assert!(m["line"].is_u64() && m["column"].is_u64(), "{result}");
|
|
assert!(
|
|
m["preview"]
|
|
.as_str()
|
|
.unwrap()
|
|
.contains(lutra_testkit::NEEDLE)
|
|
);
|
|
assert!(m["previewColumn"].is_u64());
|
|
let path = result["path"].as_str().unwrap();
|
|
assert!(!path.starts_with("var/") && !path.contains('\\'), "{path}");
|
|
}
|
|
assert_eq!(done["files"], tree.needle_files);
|
|
assert_eq!(done["cancelled"], false);
|
|
assert_eq!(done["truncated"], false);
|
|
assert!(done["elapsedMs"].is_u64());
|
|
|
|
let r = client.call(execute("search.find", json!({"query": "(", "regex": true})));
|
|
assert_eq!(r["error"]["code"], "invalid_params", "{r}");
|
|
|
|
let r = client.call(execute(
|
|
"search.find",
|
|
json!({"query": "function", "maxResults": 100000}),
|
|
));
|
|
let id = r["result"]["searchId"].as_u64().unwrap();
|
|
let r = client.call(execute("search.cancel", json!({"searchId": id})));
|
|
assert!(r["result"]["cancelled"].is_boolean(), "{r}");
|
|
let (_, done) = client.until_done(id);
|
|
assert_eq!(done["kind"], "searchDone");
|
|
assert!(
|
|
done["cancelled"] == true || r["result"]["cancelled"] == false,
|
|
"{done}"
|
|
);
|
|
}
|