M2.3: find in files backend
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>
This commit is contained in:
parent
a6b2fd1300
commit
172693c8dd
21 changed files with 2231 additions and 5 deletions
170
crates/control/tests/search.rs
Normal file
170
crates/control/tests/search.rs
Normal file
|
|
@ -0,0 +1,170 @@
|
|||
//! 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}"
|
||||
);
|
||||
}
|
||||
|
|
@ -12,6 +12,7 @@ publish.workspace = true
|
|||
layer = 4
|
||||
|
||||
[dependencies]
|
||||
lutra-search.workspace = true
|
||||
lutra-text.workspace = true
|
||||
lutra-vfs.workspace = true
|
||||
serde = { version = "1.0.229", features = ["derive"] }
|
||||
|
|
@ -22,5 +23,6 @@ thiserror = "2.0.21"
|
|||
workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
lutra-testkit.workspace = true
|
||||
proptest.workspace = true
|
||||
tempfile.workspace = true
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ mod file;
|
|||
mod params;
|
||||
mod project;
|
||||
mod recovery;
|
||||
pub mod search;
|
||||
mod settings;
|
||||
pub mod theme;
|
||||
pub mod ui;
|
||||
|
|
@ -39,5 +40,7 @@ pub fn register_builtins(registry: &mut Registry) -> Result<(), EngineError> {
|
|||
registry.register(Box::new(theme::List))?;
|
||||
registry.register(Box::new(theme::SystemPreference))?;
|
||||
registry.register(Box::new(ui::RegisterCommands))?;
|
||||
registry.register(Box::new(search::Find))?;
|
||||
registry.register(Box::new(search::Cancel))?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
304
crates/engine/src/cmd/search.rs
Normal file
304
crates/engine/src/cmd/search.rs
Normal file
|
|
@ -0,0 +1,304 @@
|
|||
//! 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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -130,7 +130,8 @@ impl Engine {
|
|||
|
||||
/// Applies pending changes on disk (reloads clean buffers, reports
|
||||
/// changed directories and conflicts), writes due recovery-journal
|
||||
/// entries (see [`crate::journal`]) and returns every queued event.
|
||||
/// entries (see [`crate::journal`]), moves finished search results into
|
||||
/// events (see [`cmd::search`]) and returns every queued event.
|
||||
/// Hosts call it regularly, e.g. every 100 ms from a background thread.
|
||||
///
|
||||
/// # Errors
|
||||
|
|
@ -141,6 +142,7 @@ impl Engine {
|
|||
guard::run(|| {
|
||||
crate::workspace::pump(state);
|
||||
crate::journal::tick(state, std::time::Instant::now());
|
||||
cmd::search::pump(state);
|
||||
Ok(())
|
||||
})?;
|
||||
Ok(state.take_events())
|
||||
|
|
@ -373,6 +375,8 @@ mod tests {
|
|||
"recovery.discard",
|
||||
"recovery.list",
|
||||
"recovery.restore",
|
||||
"search.cancel",
|
||||
"search.find",
|
||||
"settings.get",
|
||||
"settings.list",
|
||||
"settings.reset",
|
||||
|
|
|
|||
|
|
@ -144,6 +144,49 @@ pub enum Event {
|
|||
/// Project-relative paths with `/`, sorted.
|
||||
paths: Vec<String>,
|
||||
},
|
||||
/// Matches of a running search (`search.find`) in one file. A file is
|
||||
/// reported once per search; files arrive in no particular order.
|
||||
SearchResults {
|
||||
/// The search, as `search.find` returned it.
|
||||
search_id: u64,
|
||||
/// Project-relative path of the file.
|
||||
path: String,
|
||||
/// The matches in line order; never empty.
|
||||
matches: Vec<SearchMatch>,
|
||||
},
|
||||
/// A search ended; no `searchResults` for it follow.
|
||||
SearchDone {
|
||||
/// The search.
|
||||
search_id: u64,
|
||||
/// Files with reported matches.
|
||||
files: usize,
|
||||
/// Matches reported.
|
||||
matches: usize,
|
||||
/// The match cap stopped the search; more matches exist.
|
||||
truncated: bool,
|
||||
/// `search.cancel` (or closing the project) stopped the search.
|
||||
cancelled: bool,
|
||||
/// Wall time of the search in milliseconds.
|
||||
elapsed_ms: u64,
|
||||
},
|
||||
}
|
||||
|
||||
/// One match of [`Event::SearchResults`]. Positions are 0-based; columns
|
||||
/// and lengths count UTF-16 code units.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct SearchMatch {
|
||||
/// Line of the match.
|
||||
pub line: usize,
|
||||
/// Start of the match in the line.
|
||||
pub column: usize,
|
||||
/// Length of the match; never 0.
|
||||
pub length: usize,
|
||||
/// The line without its line break, or a window of at most 300
|
||||
/// characters around the match for longer lines.
|
||||
pub preview: String,
|
||||
/// Start of the match inside `preview`.
|
||||
pub preview_column: usize,
|
||||
}
|
||||
|
||||
/// Severity of an [`Event::Notification`].
|
||||
|
|
@ -224,6 +267,39 @@ mod tests {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn search_shapes() {
|
||||
let e = Event::SearchResults {
|
||||
search_id: 2,
|
||||
path: "src/a.php".into(),
|
||||
matches: vec![super::SearchMatch {
|
||||
line: 0,
|
||||
column: 6,
|
||||
length: 4,
|
||||
preview: "<?php echo".into(),
|
||||
preview_column: 6,
|
||||
}],
|
||||
};
|
||||
assert_eq!(
|
||||
serde_json::to_value(e).unwrap(),
|
||||
json!({"kind": "searchResults", "searchId": 2, "path": "src/a.php", "matches": [
|
||||
{"line": 0, "column": 6, "length": 4, "preview": "<?php echo", "previewColumn": 6}
|
||||
]})
|
||||
);
|
||||
let e = Event::SearchDone {
|
||||
search_id: 2,
|
||||
files: 1,
|
||||
matches: 1,
|
||||
truncated: false,
|
||||
cancelled: false,
|
||||
elapsed_ms: 12,
|
||||
};
|
||||
assert_eq!(
|
||||
serde_json::to_value(e).unwrap(),
|
||||
json!({"kind": "searchDone", "searchId": 2, "files": 1, "matches": 1, "truncated": false, "cancelled": false, "elapsedMs": 12})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ui_command_shape() {
|
||||
let e = Event::UiCommand {
|
||||
|
|
|
|||
|
|
@ -23,6 +23,6 @@ pub use buffers::{BufferId, LineEndingName, OpenBuffer};
|
|||
pub use command::{Command, CommandInfo, CommandSpec, Effect, Outcome};
|
||||
pub use engine::{Engine, EngineConfig};
|
||||
pub use error::{EngineError, ErrorPayload};
|
||||
pub use event::{Event, NotificationLevel};
|
||||
pub use event::{Event, NotificationLevel, SearchMatch};
|
||||
pub use registry::Registry;
|
||||
pub use state::State;
|
||||
|
|
|
|||
|
|
@ -35,6 +35,8 @@ pub struct State {
|
|||
pub engine_commands: BTreeSet<&'static str>,
|
||||
/// UI commands the frontend registered (`ui.registerCommands`), by id.
|
||||
pub ui_commands: BTreeMap<String, UiCommand>,
|
||||
/// Running searches (`search.find`).
|
||||
pub searches: crate::cmd::search::Searches,
|
||||
/// Number of notifications emitted, for their ids.
|
||||
notifications: u64,
|
||||
/// Events queued for the host, oldest first.
|
||||
|
|
|
|||
207
crates/engine/tests/search.rs
Normal file
207
crates/engine/tests/search.rs
Normal file
|
|
@ -0,0 +1,207 @@
|
|||
//! `search.find` and `search.cancel`: results arrive through the pump.
|
||||
|
||||
#![allow(
|
||||
clippy::unwrap_used,
|
||||
clippy::expect_used,
|
||||
clippy::panic,
|
||||
clippy::indexing_slicing
|
||||
)]
|
||||
|
||||
use std::fs;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use lutra_engine::cmd::search::MAX_RESULTS_PER_PUMP;
|
||||
use lutra_engine::{Engine, Event};
|
||||
use serde_json::{Value, json};
|
||||
use tempfile::TempDir;
|
||||
|
||||
/// A temp folder with `files`, opened in a fresh engine.
|
||||
fn open_project(files: &[(&str, &str)]) -> (TempDir, Engine) {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
for (path, text) in files {
|
||||
let abs = dir.path().join(path);
|
||||
fs::create_dir_all(abs.parent().unwrap()).unwrap();
|
||||
fs::write(abs, text).unwrap();
|
||||
}
|
||||
let mut engine = Engine::new().unwrap();
|
||||
engine
|
||||
.execute("project.open", json!({"path": dir.path()}))
|
||||
.unwrap();
|
||||
(dir, engine)
|
||||
}
|
||||
|
||||
fn find(engine: &mut Engine, params: Value) -> u64 {
|
||||
let out = engine.execute("search.find", params).unwrap();
|
||||
assert!(out.events.is_empty(), "results only come from the pump");
|
||||
out.value["searchId"].as_u64().unwrap()
|
||||
}
|
||||
|
||||
/// Pumps until `searchDone` for `id`; returns the results (sorted by path)
|
||||
/// and the done event, and the number of pumps.
|
||||
fn collect(engine: &mut Engine, id: u64) -> (Vec<Event>, Event, usize) {
|
||||
let deadline = Instant::now() + Duration::from_secs(10);
|
||||
let mut results = Vec::new();
|
||||
let mut pumps = 0;
|
||||
while Instant::now() < deadline {
|
||||
pumps += 1;
|
||||
for event in engine.pump_vfs().unwrap() {
|
||||
match &event {
|
||||
Event::SearchResults { search_id, .. } if *search_id == id => results.push(event),
|
||||
Event::SearchDone { search_id, .. } if *search_id == id => {
|
||||
results.sort_by_key(path);
|
||||
return (results, event, pumps);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(10));
|
||||
}
|
||||
panic!("no searchDone within 10 s");
|
||||
}
|
||||
|
||||
fn path(event: &Event) -> String {
|
||||
match event {
|
||||
Event::SearchResults { path, .. } => path.clone(),
|
||||
_ => String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn finds_in_files_and_dirty_buffers() {
|
||||
let (_dir, mut engine) = open_project(&[
|
||||
("src/a.php", "<?php\necho 'needle';\n"),
|
||||
("src/b.php", "<?php\n"),
|
||||
("vendor/x/c.php", "<?php // needle\n"),
|
||||
("node_modules/d.js", "needle\n"),
|
||||
]);
|
||||
let out = engine
|
||||
.execute("file.open", json!({"path": "src/b.php"}))
|
||||
.unwrap();
|
||||
let buffer = out.value["id"].as_u64().unwrap();
|
||||
engine
|
||||
.execute(
|
||||
"buffer.edit",
|
||||
json!({"id": buffer, "baseVersion": 1, "edits": [{"from": 6, "to": 6, "insert": "$needle = 1;\n"}]}),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let id = find(&mut engine, json!({"query": "NEEDLE"}));
|
||||
let (results, done, _) = collect(&mut engine, id);
|
||||
let paths: Vec<String> = results.iter().map(path).collect();
|
||||
assert_eq!(paths, ["src/a.php", "src/b.php", "vendor/x/c.php"]);
|
||||
let Event::SearchResults { matches, .. } = &results[1] else {
|
||||
panic!()
|
||||
};
|
||||
assert_eq!(
|
||||
(matches[0].line, matches[0].column, matches[0].length),
|
||||
(1, 1, 6)
|
||||
);
|
||||
assert_eq!(matches[0].preview, "$needle = 1;");
|
||||
let Event::SearchDone {
|
||||
files,
|
||||
matches,
|
||||
truncated,
|
||||
cancelled,
|
||||
..
|
||||
} = done
|
||||
else {
|
||||
panic!()
|
||||
};
|
||||
assert_eq!((files, matches, truncated, cancelled), (3, 3, false, false));
|
||||
|
||||
let id2 = find(
|
||||
&mut engine,
|
||||
json!({"query": "needle", "caseSensitive": true, "include": ["src/*.php"]}),
|
||||
);
|
||||
assert!(id2 > id);
|
||||
let (results, _, _) = collect(&mut engine, id2);
|
||||
assert_eq!(results.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_queries_fail_with_invalid_params() {
|
||||
let (_dir, mut engine) = open_project(&[("a.php", "x")]);
|
||||
for params in [
|
||||
json!({"query": "(", "regex": true}),
|
||||
json!({"query": ""}),
|
||||
json!({"query": "x", "include": ["["]}),
|
||||
json!({"query": "x", "bogus": 1}),
|
||||
json!({}),
|
||||
] {
|
||||
let err = engine.execute("search.find", params.clone()).unwrap_err();
|
||||
assert_eq!(err.code(), "invalid_params", "{params}");
|
||||
}
|
||||
assert_eq!(engine.state().searches.running(), 0);
|
||||
engine.execute("project.close", json!({})).unwrap();
|
||||
let err = engine
|
||||
.execute("search.find", json!({"query": "x"}))
|
||||
.unwrap_err();
|
||||
assert_eq!(err.code(), "disabled");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cancel_and_project_close_end_searches() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
lutra_testkit::generate_search_tree(dir.path(), 2_000);
|
||||
let mut engine = Engine::new().unwrap();
|
||||
engine
|
||||
.execute("project.open", json!({"path": dir.path()}))
|
||||
.unwrap();
|
||||
|
||||
let id = find(&mut engine, json!({"query": "function"}));
|
||||
let out = engine
|
||||
.execute("search.cancel", json!({"searchId": id}))
|
||||
.unwrap();
|
||||
assert_eq!(out.value["cancelled"], true);
|
||||
let (_, done, _) = collect(&mut engine, id);
|
||||
let Event::SearchDone {
|
||||
cancelled, matches, ..
|
||||
} = done
|
||||
else {
|
||||
panic!()
|
||||
};
|
||||
assert!(
|
||||
cancelled || matches == 10_000,
|
||||
"cancelled unless it had already finished"
|
||||
);
|
||||
let out = engine
|
||||
.execute("search.cancel", json!({"searchId": id}))
|
||||
.unwrap();
|
||||
assert_eq!(out.value["cancelled"], false);
|
||||
|
||||
let id = find(&mut engine, json!({"query": "function"}));
|
||||
engine.execute("project.close", json!({})).unwrap();
|
||||
let (results, done, _) = collect(&mut engine, id);
|
||||
assert!(results.is_empty());
|
||||
assert!(matches!(
|
||||
done,
|
||||
Event::SearchDone {
|
||||
cancelled: true,
|
||||
..
|
||||
}
|
||||
));
|
||||
assert_eq!(engine.state().searches.running(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pumps_spread_many_results() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
for i in 0..(MAX_RESULTS_PER_PUMP + 100) {
|
||||
fs::write(dir.path().join(format!("f{i}.txt")), "hit\n").unwrap();
|
||||
}
|
||||
let mut engine = Engine::new().unwrap();
|
||||
engine
|
||||
.execute("project.open", json!({"path": dir.path()}))
|
||||
.unwrap();
|
||||
let id = find(&mut engine, json!({"query": "hit"}));
|
||||
std::thread::sleep(Duration::from_millis(500));
|
||||
let first = engine.pump_vfs().unwrap();
|
||||
let batch = first
|
||||
.iter()
|
||||
.filter(|e| matches!(e, Event::SearchResults { .. }))
|
||||
.count();
|
||||
assert!(batch <= MAX_RESULTS_PER_PUMP, "{batch}");
|
||||
let (rest, done, _) = collect(&mut engine, id);
|
||||
assert_eq!(batch + rest.len(), MAX_RESULTS_PER_PUMP + 100);
|
||||
assert!(matches!(done, Event::SearchDone { files, .. } if files == MAX_RESULTS_PER_PUMP + 100));
|
||||
}
|
||||
|
|
@ -12,6 +12,17 @@ publish.workspace = true
|
|||
layer = 2
|
||||
|
||||
[dependencies]
|
||||
globset = "0.4.18"
|
||||
grep-matcher = "0.1.7"
|
||||
grep-regex = "0.1.13"
|
||||
grep-searcher = "0.1.14"
|
||||
ignore.workspace = true
|
||||
thiserror = "2.0.21"
|
||||
|
||||
[dev-dependencies]
|
||||
lutra-testkit.workspace = true
|
||||
proptest.workspace = true
|
||||
tempfile.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
|
|
|||
|
|
@ -2,3 +2,234 @@
|
|||
//!
|
||||
//! Streams matches from the project files off the UI thread, honouring the
|
||||
//! ignore rules of the project model.
|
||||
//!
|
||||
//! # Query
|
||||
//! A [`Query`] is compiled once into a [`Search`] ([`Search::new`]): a
|
||||
//! literal or a regular expression (Rust `regex` syntax), case sensitive or
|
||||
//! not, optionally whole words only, plus include and exclude globs. An
|
||||
//! invalid pattern or glob fails with [`SearchError`] before anything runs.
|
||||
//! Patterns never match across lines.
|
||||
//!
|
||||
//! # Which files are searched
|
||||
//! The walk follows the rules of `lutra-vfs`:
|
||||
//! - `.gitignore`, `.ignore`, `.git/info/exclude` and the global git excludes
|
||||
//! apply whether or not the folder is a git repository; ignored files and
|
||||
//! everything below an ignored directory are skipped. Ignore files above
|
||||
//! the project root are not read.
|
||||
//! - `.git` (directory or file) at any depth and every `node_modules`
|
||||
//! directory are skipped.
|
||||
//! - `vendor/` directly under the root is library code and is searched even
|
||||
//! when an ignore file lists it (Composer projects usually do); ignore
|
||||
//! files inside it still apply.
|
||||
//! - Hidden (dot) files are searched, as the project tree shows them.
|
||||
//! Symbolic links are not followed.
|
||||
//! - Include globs keep only files that match one of them, exclude globs
|
||||
//! drop files and whole directories. A glob without `/` matches the file
|
||||
//! or directory name (`*.php`), a glob with `/` the project-relative path
|
||||
//! (`src/**/*.php`).
|
||||
//! - Files larger than [`MAX_FILE_SIZE`], binary files (a NUL byte) and
|
||||
//! files that cannot be read are skipped.
|
||||
//!
|
||||
//! # Results
|
||||
//! Each file with matches is reported once as [`FileMatches`], in no
|
||||
//! particular order (the walk is parallel). Positions are 0-based; columns
|
||||
//! and lengths count UTF-16 code units. Lines end at `\n` (a `\r` before it
|
||||
//! is not part of the line). The preview is the line, or a window of at most
|
||||
//! [`MAX_PREVIEW_CHARS`] characters around the match for longer lines.
|
||||
//! At most [`Query::max_results`] matches are reported in total; the
|
||||
//! [`Summary`] says whether the cap cut the search short.
|
||||
//!
|
||||
//! # Threads and cancellation
|
||||
//! [`run`] blocks the calling thread and searches on a pool of up to
|
||||
//! [`MAX_THREADS`] walker threads; [`spawn`] runs it on a thread of its own
|
||||
//! and delivers [`SearchEvent`]s over a channel. A [`CancelToken`] stops a
|
||||
//! running search between files and between lines.
|
||||
//!
|
||||
//! # Open buffers
|
||||
//! An [`Overlay`] maps project-relative paths to text that replaces the file
|
||||
//! on disk, so unsaved edits are searched instead of the stale file. Only
|
||||
//! files the walk visits are looked up; text for a path that is not on disk
|
||||
//! (or is skipped by the rules) is not searched.
|
||||
|
||||
mod lines;
|
||||
mod query;
|
||||
mod walk;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::mpsc::{Receiver, channel};
|
||||
use std::time::Duration;
|
||||
|
||||
pub use query::Search;
|
||||
pub use walk::run;
|
||||
|
||||
/// Largest file searched, in bytes (8 MiB).
|
||||
pub const MAX_FILE_SIZE: u64 = 8 * 1024 * 1024;
|
||||
|
||||
/// Default cap on the number of matches of one search.
|
||||
pub const DEFAULT_MAX_RESULTS: usize = 10_000;
|
||||
|
||||
/// Highest cap a query may ask for.
|
||||
pub const MAX_RESULTS_LIMIT: usize = 100_000;
|
||||
|
||||
/// Longest preview, in characters (Unicode scalar values).
|
||||
pub const MAX_PREVIEW_CHARS: usize = 300;
|
||||
|
||||
/// Most walker threads of one search.
|
||||
pub const MAX_THREADS: usize = 8;
|
||||
|
||||
/// What to search for and where.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Query {
|
||||
/// The text or regular expression; must not be empty.
|
||||
pub pattern: String,
|
||||
/// Treat `pattern` as a regular expression instead of literal text.
|
||||
pub regex: bool,
|
||||
/// Match letter case exactly.
|
||||
pub case_sensitive: bool,
|
||||
/// Only matches that start and end at word boundaries.
|
||||
pub whole_word: bool,
|
||||
/// Only files matching one of these globs; all files when empty.
|
||||
pub include: Vec<String>,
|
||||
/// Skip files and directories matching one of these globs.
|
||||
pub exclude: Vec<String>,
|
||||
/// Stop after this many matches; clamped to `1..=`[`MAX_RESULTS_LIMIT`].
|
||||
pub max_results: usize,
|
||||
}
|
||||
|
||||
impl Query {
|
||||
/// A literal, case-insensitive query for `pattern` with the default cap.
|
||||
pub fn literal(pattern: impl Into<String>) -> Self {
|
||||
Self {
|
||||
pattern: pattern.into(),
|
||||
regex: false,
|
||||
case_sensitive: false,
|
||||
whole_word: false,
|
||||
include: Vec::new(),
|
||||
exclude: Vec::new(),
|
||||
max_results: DEFAULT_MAX_RESULTS,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Why a query cannot run.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum SearchError {
|
||||
/// The pattern is empty.
|
||||
#[error("the search text is empty")]
|
||||
EmptyPattern,
|
||||
/// The pattern is not a valid regular expression, or matches a line
|
||||
/// break.
|
||||
#[error("invalid pattern: {0}")]
|
||||
InvalidPattern(String),
|
||||
/// An include or exclude glob is invalid.
|
||||
#[error("invalid glob: {0}")]
|
||||
InvalidGlob(String),
|
||||
}
|
||||
|
||||
/// One match.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Match {
|
||||
/// 0-based line.
|
||||
pub line: usize,
|
||||
/// Start in the line, in UTF-16 code units.
|
||||
pub column: usize,
|
||||
/// Length in UTF-16 code units; never 0.
|
||||
pub length: usize,
|
||||
/// The line, or a window of it around the match, without the line break.
|
||||
pub preview: String,
|
||||
/// Start of the match inside `preview`, in UTF-16 code units.
|
||||
pub preview_column: usize,
|
||||
}
|
||||
|
||||
/// The matches in one file, in line order.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct FileMatches {
|
||||
/// Project-relative path with `/` separators.
|
||||
pub path: String,
|
||||
/// The matches; never empty.
|
||||
pub matches: Vec<Match>,
|
||||
}
|
||||
|
||||
/// How a search ended.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
pub struct Summary {
|
||||
/// Files with at least one reported match.
|
||||
pub files: usize,
|
||||
/// Matches reported.
|
||||
pub matches: usize,
|
||||
/// The match cap stopped the search; more matches may exist.
|
||||
pub truncated: bool,
|
||||
/// The search was cancelled before it finished.
|
||||
pub cancelled: bool,
|
||||
/// Wall time from start to end.
|
||||
pub elapsed: Duration,
|
||||
}
|
||||
|
||||
/// What a search started with [`spawn`] delivers, in order: any number of
|
||||
/// files, then exactly one summary.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum SearchEvent {
|
||||
/// A file with matches.
|
||||
File(FileMatches),
|
||||
/// The search ended; nothing follows.
|
||||
Done(Summary),
|
||||
}
|
||||
|
||||
/// Text of open buffers by project-relative path (`/` separators), searched
|
||||
/// instead of the file on disk.
|
||||
pub type Overlay = HashMap<String, String>;
|
||||
|
||||
/// Stops a running search. Clones share the flag.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct CancelToken(Arc<AtomicBool>);
|
||||
|
||||
impl CancelToken {
|
||||
/// A token that is not cancelled.
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// Asks the search to stop; it ends with [`Summary::cancelled`] set.
|
||||
pub fn cancel(&self) {
|
||||
self.0.store(true, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
/// Whether [`CancelToken::cancel`] was called.
|
||||
pub fn is_cancelled(&self) -> bool {
|
||||
self.0.load(Ordering::Relaxed)
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs [`run`] on a new thread and returns the channel of its events. A
|
||||
/// receiver that is dropped does not stop the search; cancel it with
|
||||
/// `cancel`.
|
||||
///
|
||||
/// # Parameters
|
||||
/// - `root`: the project root (absolute).
|
||||
/// - `search`: the compiled query.
|
||||
/// - `overlay`: buffer texts that replace files on disk.
|
||||
/// - `cancel`: stops the search.
|
||||
///
|
||||
/// # Errors
|
||||
/// The OS refused to start the thread.
|
||||
pub fn spawn(
|
||||
root: PathBuf,
|
||||
search: Search,
|
||||
overlay: Overlay,
|
||||
cancel: CancelToken,
|
||||
) -> std::io::Result<Receiver<SearchEvent>> {
|
||||
let (tx, rx) = channel();
|
||||
std::thread::Builder::new()
|
||||
.name("lutra-search".into())
|
||||
.spawn(move || {
|
||||
let files = tx.clone();
|
||||
let summary = run(&root, &search, &overlay, &cancel, &move |file| {
|
||||
let _receiver_gone = files.send(SearchEvent::File(file));
|
||||
});
|
||||
let _receiver_gone = tx.send(SearchEvent::Done(summary));
|
||||
})?;
|
||||
Ok(rx)
|
||||
}
|
||||
|
|
|
|||
195
crates/search/src/lines.rs
Normal file
195
crates/search/src/lines.rs
Normal file
|
|
@ -0,0 +1,195 @@
|
|||
//! Turning a matching line into [`Match`]es: UTF-16 columns and previews.
|
||||
|
||||
use grep_matcher::Matcher;
|
||||
use grep_regex::RegexMatcher;
|
||||
|
||||
use crate::{MAX_PREVIEW_CHARS, Match};
|
||||
|
||||
/// Characters shown before the match when a long line is cut to a window.
|
||||
const PREVIEW_LEAD: usize = 80;
|
||||
|
||||
/// Appends the matches of one line to `out`, at most `limit` in total.
|
||||
///
|
||||
/// # Parameters
|
||||
/// - `matcher`: the compiled pattern.
|
||||
/// - `line`: 0-based line number.
|
||||
/// - `bytes`: the line as read, with or without its line break. Invalid
|
||||
/// UTF-8 is replaced by U+FFFD before matching, so positions refer to the
|
||||
/// text an editor shows.
|
||||
/// - `out`: receives the matches; zero-length matches are skipped.
|
||||
/// - `limit`: stop once `out` holds this many matches.
|
||||
///
|
||||
/// A matcher error (none is expected for an in-memory regex) ends the line
|
||||
/// early; the matches found so far are kept.
|
||||
pub(crate) fn collect(
|
||||
matcher: &RegexMatcher,
|
||||
line: usize,
|
||||
bytes: &[u8],
|
||||
out: &mut Vec<Match>,
|
||||
limit: usize,
|
||||
) {
|
||||
let bytes = trim_line_break(bytes);
|
||||
let text = String::from_utf8_lossy(bytes);
|
||||
let mut spans = Vec::new();
|
||||
let _matcher_error = matcher.find_iter(text.as_bytes(), |m| {
|
||||
if m.start() < m.end() {
|
||||
spans.push((m.start(), m.end()));
|
||||
}
|
||||
out.len().saturating_add(spans.len()) < limit
|
||||
});
|
||||
if spans.is_empty() {
|
||||
return;
|
||||
}
|
||||
let window = Window::new(&text);
|
||||
let mut cursor = Cursor::default();
|
||||
for (start, end) in spans {
|
||||
let Some(matched) = text.get(start..end) else {
|
||||
continue;
|
||||
};
|
||||
if !cursor.advance(&text, start) {
|
||||
continue;
|
||||
}
|
||||
let (preview, preview_column) = window.preview(&text, &cursor, start);
|
||||
out.push(Match {
|
||||
line,
|
||||
column: cursor.utf16,
|
||||
length: utf16_len(matched),
|
||||
preview,
|
||||
preview_column,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// `bytes` without a trailing `\n` or `\r\n`.
|
||||
fn trim_line_break(bytes: &[u8]) -> &[u8] {
|
||||
let bytes = bytes.strip_suffix(b"\n").unwrap_or(bytes);
|
||||
bytes.strip_suffix(b"\r").unwrap_or(bytes)
|
||||
}
|
||||
|
||||
/// Number of UTF-16 code units of `s`.
|
||||
fn utf16_len(s: &str) -> usize {
|
||||
s.chars().map(char::len_utf16).sum()
|
||||
}
|
||||
|
||||
/// A position in the line, counted three ways; only moves forward.
|
||||
#[derive(Default)]
|
||||
struct Cursor {
|
||||
/// Byte offset.
|
||||
byte: usize,
|
||||
/// Characters before `byte`.
|
||||
chars: usize,
|
||||
/// UTF-16 code units before `byte`.
|
||||
utf16: usize,
|
||||
}
|
||||
|
||||
impl Cursor {
|
||||
/// Moves to byte offset `to`; false if `to` lies behind the cursor or
|
||||
/// not on a character boundary.
|
||||
fn advance(&mut self, text: &str, to: usize) -> bool {
|
||||
let Some(between) = text.get(self.byte..to) else {
|
||||
return false;
|
||||
};
|
||||
for c in between.chars() {
|
||||
self.chars = self.chars.saturating_add(1);
|
||||
self.utf16 = self.utf16.saturating_add(c.len_utf16());
|
||||
}
|
||||
self.byte = to;
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
/// How previews of one line are cut.
|
||||
enum Window {
|
||||
/// The line is short enough to be the preview of every match.
|
||||
Whole,
|
||||
/// The line is longer than [`MAX_PREVIEW_CHARS`]: the byte offset of
|
||||
/// every character, plus the text length at the end.
|
||||
Cut(Vec<usize>),
|
||||
}
|
||||
|
||||
impl Window {
|
||||
/// Prepares previews for `text`.
|
||||
fn new(text: &str) -> Self {
|
||||
if text.len() <= MAX_PREVIEW_CHARS || text.chars().count() <= MAX_PREVIEW_CHARS {
|
||||
return Self::Whole;
|
||||
}
|
||||
let mut offsets: Vec<usize> = text.char_indices().map(|(i, _)| i).collect();
|
||||
offsets.push(text.len());
|
||||
Self::Cut(offsets)
|
||||
}
|
||||
|
||||
/// The preview for a match at `cursor` (byte offset `start`), and the
|
||||
/// match's UTF-16 column inside it.
|
||||
fn preview(&self, text: &str, cursor: &Cursor, start: usize) -> (String, usize) {
|
||||
let Self::Cut(offsets) = self else {
|
||||
return (text.to_string(), cursor.utf16);
|
||||
};
|
||||
let total = offsets.len().saturating_sub(1);
|
||||
let first = cursor
|
||||
.chars
|
||||
.saturating_sub(PREVIEW_LEAD)
|
||||
.min(total.saturating_sub(MAX_PREVIEW_CHARS));
|
||||
let last = first.saturating_add(MAX_PREVIEW_CHARS).min(total);
|
||||
let from = offsets.get(first).copied().unwrap_or(0);
|
||||
let to = offsets.get(last).copied().unwrap_or(text.len());
|
||||
let preview = text.get(from..to).unwrap_or_default().to_string();
|
||||
let column = text.get(from..start).map(utf16_len).unwrap_or(0);
|
||||
(preview, column)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use grep_regex::RegexMatcherBuilder;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn matcher(pattern: &str) -> RegexMatcher {
|
||||
RegexMatcherBuilder::new()
|
||||
.fixed_strings(true)
|
||||
.line_terminator(Some(b'\n'))
|
||||
.build(pattern)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn columns_count_utf16_units() {
|
||||
let mut out = Vec::new();
|
||||
collect(&matcher("x"), 4, "ä😀x and x\r\n".as_bytes(), &mut out, 10);
|
||||
assert_eq!(out.len(), 2);
|
||||
assert_eq!((out[0].line, out[0].column, out[0].length), (4, 3, 1));
|
||||
assert_eq!(out[0].preview, "ä😀x and x");
|
||||
assert_eq!(out[0].preview_column, 3);
|
||||
assert_eq!(out[1].column, 9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn long_lines_are_cut_around_the_match() {
|
||||
let line = format!("{}needle{}", "a".repeat(1000), "b".repeat(1000));
|
||||
let mut out = Vec::new();
|
||||
collect(&matcher("needle"), 0, line.as_bytes(), &mut out, 10);
|
||||
let m = &out[0];
|
||||
assert_eq!(m.column, 1000);
|
||||
assert_eq!(m.preview.chars().count(), MAX_PREVIEW_CHARS);
|
||||
assert_eq!(
|
||||
m.preview.get(m.preview_column..m.preview_column + 6),
|
||||
Some("needle")
|
||||
);
|
||||
assert_eq!(m.preview_column, PREVIEW_LEAD);
|
||||
|
||||
let tail = format!("{}needle", "a".repeat(1000));
|
||||
out.clear();
|
||||
collect(&matcher("needle"), 0, tail.as_bytes(), &mut out, 10);
|
||||
assert!(out[0].preview.ends_with("needle"));
|
||||
assert_eq!(out[0].preview.chars().count(), MAX_PREVIEW_CHARS);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_utf8_and_limits() {
|
||||
let mut out = Vec::new();
|
||||
collect(&matcher("ab"), 0, b"\xffab ab ab\n", &mut out, 2);
|
||||
assert_eq!(out.len(), 2);
|
||||
assert_eq!(out[0].column, 1);
|
||||
assert_eq!(out[0].preview, "\u{fffd}ab ab ab");
|
||||
}
|
||||
}
|
||||
139
crates/search/src/query.rs
Normal file
139
crates/search/src/query.rs
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
//! Compiling a [`Query`]: the matcher and the path filters.
|
||||
|
||||
use globset::{GlobBuilder, GlobSet, GlobSetBuilder};
|
||||
use grep_regex::{RegexMatcher, RegexMatcherBuilder};
|
||||
|
||||
use crate::{MAX_RESULTS_LIMIT, Query, SearchError};
|
||||
|
||||
/// Largest compiled regex, in bytes; bigger patterns are rejected instead of
|
||||
/// using unbounded memory.
|
||||
const REGEX_SIZE_LIMIT: usize = 16 * 1024 * 1024;
|
||||
|
||||
/// Most nesting a pattern may use.
|
||||
const REGEX_NEST_LIMIT: u32 = 100;
|
||||
|
||||
/// A compiled query, ready to run.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Search {
|
||||
/// Matches one line at a time.
|
||||
pub(crate) matcher: RegexMatcher,
|
||||
/// Include and exclude globs.
|
||||
pub(crate) filter: PathFilter,
|
||||
/// Cap on reported matches.
|
||||
pub(crate) max_results: usize,
|
||||
}
|
||||
|
||||
impl Search {
|
||||
/// Compiles `query`.
|
||||
///
|
||||
/// # Errors
|
||||
/// [`SearchError::EmptyPattern`], [`SearchError::InvalidPattern`] for a
|
||||
/// bad regular expression (or one that can match a line break),
|
||||
/// [`SearchError::InvalidGlob`] for a bad include or exclude glob.
|
||||
pub fn new(query: &Query) -> Result<Self, SearchError> {
|
||||
if query.pattern.is_empty() {
|
||||
return Err(SearchError::EmptyPattern);
|
||||
}
|
||||
let matcher = RegexMatcherBuilder::new()
|
||||
.fixed_strings(!query.regex)
|
||||
.case_insensitive(!query.case_sensitive)
|
||||
.word(query.whole_word)
|
||||
.line_terminator(Some(b'\n'))
|
||||
.crlf(true)
|
||||
.size_limit(REGEX_SIZE_LIMIT)
|
||||
.dfa_size_limit(REGEX_SIZE_LIMIT)
|
||||
.nest_limit(REGEX_NEST_LIMIT)
|
||||
.build(&query.pattern)
|
||||
.map_err(|e| SearchError::InvalidPattern(e.to_string()))?;
|
||||
Ok(Self {
|
||||
matcher,
|
||||
filter: PathFilter::new(&query.include, &query.exclude)?,
|
||||
max_results: query.max_results.clamp(1, MAX_RESULTS_LIMIT),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Include and exclude globs, split by whether they match a name or a path.
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct PathFilter {
|
||||
/// Include globs; `None` when every file is included.
|
||||
include: Option<Globs>,
|
||||
/// Exclude globs; `None` when nothing is excluded.
|
||||
exclude: Option<Globs>,
|
||||
}
|
||||
|
||||
/// Globs matched against the file name and against the relative path.
|
||||
#[derive(Debug, Clone)]
|
||||
struct Globs {
|
||||
/// Globs without `/`.
|
||||
names: GlobSet,
|
||||
/// Globs with `/`.
|
||||
paths: GlobSet,
|
||||
}
|
||||
|
||||
impl PathFilter {
|
||||
/// Compiles the globs; blank ones are ignored.
|
||||
fn new(include: &[String], exclude: &[String]) -> Result<Self, SearchError> {
|
||||
Ok(Self {
|
||||
include: Globs::new(include)?,
|
||||
exclude: Globs::new(exclude)?,
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether the file at `rel` (project-relative, `/` separators) passes
|
||||
/// the include and exclude globs.
|
||||
pub(crate) fn file(&self, rel: &str) -> bool {
|
||||
let included = self.include.as_ref().is_none_or(|g| g.matches(rel));
|
||||
included && !self.excluded(rel)
|
||||
}
|
||||
|
||||
/// Whether an exclude glob matches the file or directory at `rel`.
|
||||
pub(crate) fn excluded(&self, rel: &str) -> bool {
|
||||
self.exclude.as_ref().is_some_and(|g| g.matches(rel))
|
||||
}
|
||||
}
|
||||
|
||||
impl Globs {
|
||||
/// Compiles `globs`; `None` if there is none. Leading and trailing `/`
|
||||
/// are dropped, so `tests/` excludes every directory named `tests`.
|
||||
fn new(globs: &[String]) -> Result<Option<Self>, SearchError> {
|
||||
let mut names = GlobSetBuilder::new();
|
||||
let mut paths = GlobSetBuilder::new();
|
||||
let mut any = false;
|
||||
for glob in globs.iter().map(|g| g.trim()).filter(|g| !g.is_empty()) {
|
||||
let pattern = glob.trim_matches('/');
|
||||
if pattern.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let compiled = GlobBuilder::new(pattern)
|
||||
.literal_separator(true)
|
||||
.backslash_escape(true)
|
||||
.build()
|
||||
.map_err(|e| SearchError::InvalidGlob(e.to_string()))?;
|
||||
if pattern.contains('/') {
|
||||
paths.add(compiled);
|
||||
} else {
|
||||
names.add(compiled);
|
||||
}
|
||||
any = true;
|
||||
}
|
||||
if !any {
|
||||
return Ok(None);
|
||||
}
|
||||
let build = |b: GlobSetBuilder| {
|
||||
b.build()
|
||||
.map_err(|e| SearchError::InvalidGlob(e.to_string()))
|
||||
};
|
||||
Ok(Some(Self {
|
||||
names: build(names)?,
|
||||
paths: build(paths)?,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Whether `rel`'s last component matches a name glob or `rel` a path
|
||||
/// glob.
|
||||
fn matches(&self, rel: &str) -> bool {
|
||||
let name = rel.rsplit('/').next().unwrap_or(rel);
|
||||
self.names.is_match(name) || self.paths.is_match(rel)
|
||||
}
|
||||
}
|
||||
280
crates/search/src/walk.rs
Normal file
280
crates/search/src/walk.rs
Normal file
|
|
@ -0,0 +1,280 @@
|
|||
//! The parallel walk over the project and the per-file search.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||
use std::time::Instant;
|
||||
|
||||
use grep_matcher::LineTerminator;
|
||||
use grep_regex::RegexMatcher;
|
||||
use grep_searcher::{BinaryDetection, Searcher, SearcherBuilder, Sink, SinkMatch};
|
||||
use ignore::{DirEntry, WalkBuilder, WalkState};
|
||||
|
||||
use crate::query::PathFilter;
|
||||
use crate::{
|
||||
CancelToken, FileMatches, MAX_FILE_SIZE, MAX_THREADS, Match, Overlay, Search, Summary, lines,
|
||||
};
|
||||
|
||||
/// Searches the project at `root` and calls `on_file` for every file with
|
||||
/// matches, from the walker threads, while the search runs. Blocks until
|
||||
/// the walk ends, the cap is reached or `cancel` fires.
|
||||
///
|
||||
/// # Parameters
|
||||
/// - `root`: the project root (absolute).
|
||||
/// - `search`: the compiled query.
|
||||
/// - `overlay`: buffer texts that replace files on disk.
|
||||
/// - `cancel`: stops the search; checked between files and lines.
|
||||
/// - `on_file`: receives each file's matches.
|
||||
///
|
||||
/// # Returns
|
||||
/// The totals. Files that cannot be read are skipped silently: a search
|
||||
/// reports what it can read, like the project tree lists what it can.
|
||||
pub fn run(
|
||||
root: &Path,
|
||||
search: &Search,
|
||||
overlay: &Overlay,
|
||||
cancel: &CancelToken,
|
||||
on_file: &(dyn Fn(FileMatches) + Sync),
|
||||
) -> Summary {
|
||||
let started = Instant::now();
|
||||
let totals = Totals::default();
|
||||
walker(root, &search.filter).build_parallel().run(|| {
|
||||
let mut searcher = SearcherBuilder::new()
|
||||
.line_terminator(LineTerminator::crlf())
|
||||
.line_number(true)
|
||||
.binary_detection(BinaryDetection::quit(0))
|
||||
.build();
|
||||
let totals = &totals;
|
||||
Box::new(move |entry| {
|
||||
if cancel.is_cancelled() || totals.full.load(Ordering::Relaxed) {
|
||||
return WalkState::Quit;
|
||||
}
|
||||
let Ok(entry) = entry else {
|
||||
return WalkState::Continue;
|
||||
};
|
||||
let Some(rel) = file_path(root, &entry, &search.filter) else {
|
||||
return WalkState::Continue;
|
||||
};
|
||||
let room = search
|
||||
.max_results
|
||||
.saturating_sub(totals.matches.load(Ordering::Relaxed));
|
||||
let Some(mut matches) = search_file(
|
||||
&mut searcher,
|
||||
&search.matcher,
|
||||
entry.path(),
|
||||
overlay.get(&rel),
|
||||
room,
|
||||
cancel,
|
||||
) else {
|
||||
return WalkState::Continue;
|
||||
};
|
||||
let granted = totals.reserve(matches.len(), search.max_results);
|
||||
if granted < matches.len() {
|
||||
matches.truncate(granted);
|
||||
totals.full.store(true, Ordering::Relaxed);
|
||||
}
|
||||
if granted > 0 {
|
||||
totals.files.fetch_add(1, Ordering::Relaxed);
|
||||
on_file(FileMatches { path: rel, matches });
|
||||
}
|
||||
if totals.full.load(Ordering::Relaxed) {
|
||||
WalkState::Quit
|
||||
} else {
|
||||
WalkState::Continue
|
||||
}
|
||||
})
|
||||
});
|
||||
let cancelled = cancel.is_cancelled();
|
||||
Summary {
|
||||
files: totals.files.load(Ordering::Relaxed),
|
||||
matches: totals.matches.load(Ordering::Relaxed),
|
||||
truncated: totals.full.load(Ordering::Relaxed) && !cancelled,
|
||||
cancelled,
|
||||
elapsed: started.elapsed(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Counters shared by the walker threads.
|
||||
#[derive(Default)]
|
||||
struct Totals {
|
||||
/// Files reported.
|
||||
files: AtomicUsize,
|
||||
/// Matches reported.
|
||||
matches: AtomicUsize,
|
||||
/// The cap was reached, or a file had more matches than fit.
|
||||
full: AtomicBool,
|
||||
}
|
||||
|
||||
impl Totals {
|
||||
/// Takes up to `wanted` matches from what is left under `cap` and
|
||||
/// returns how many were granted. The caller marks the totals full when
|
||||
/// it got fewer than it wanted: only then is a match known to be left
|
||||
/// out.
|
||||
fn reserve(&self, wanted: usize, cap: usize) -> usize {
|
||||
let mut used = self.matches.load(Ordering::Relaxed);
|
||||
loop {
|
||||
let granted = wanted.min(cap.saturating_sub(used));
|
||||
match self.matches.compare_exchange_weak(
|
||||
used,
|
||||
used.saturating_add(granted),
|
||||
Ordering::Relaxed,
|
||||
Ordering::Relaxed,
|
||||
) {
|
||||
Ok(_) => return granted,
|
||||
Err(now) => used = now,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The walker over `root` with the project's rules (see the crate docs).
|
||||
fn walker(root: &Path, filter: &PathFilter) -> WalkBuilder {
|
||||
let threads = std::thread::available_parallelism()
|
||||
.map(|n| n.get())
|
||||
.unwrap_or(2)
|
||||
.clamp(1, MAX_THREADS);
|
||||
let mut builder = WalkBuilder::new(root);
|
||||
builder
|
||||
.hidden(false)
|
||||
.parents(false)
|
||||
.ignore(true)
|
||||
.git_ignore(true)
|
||||
.git_exclude(true)
|
||||
.git_global(true)
|
||||
.require_git(false)
|
||||
.follow_links(false)
|
||||
.threads(threads);
|
||||
let vendor = root.join("vendor");
|
||||
let vendor_is_dir = std::fs::symlink_metadata(&vendor).is_ok_and(|m| m.is_dir());
|
||||
if vendor_is_dir {
|
||||
builder.add(&vendor);
|
||||
}
|
||||
let root = root.to_path_buf();
|
||||
let filter = filter.clone();
|
||||
builder.filter_entry(move |entry| keep_entry(&root, &vendor, &filter, entry));
|
||||
builder
|
||||
}
|
||||
|
||||
/// Whether the walk enters (directory) or visits (file) `entry`: `.git` and
|
||||
/// `node_modules` are skipped, the root's `vendor` is reached through its
|
||||
/// own walk root instead of the project root's walk, and exclude globs drop
|
||||
/// whole directories.
|
||||
fn keep_entry(root: &Path, vendor: &Path, filter: &PathFilter, entry: &DirEntry) -> bool {
|
||||
let name = entry.file_name();
|
||||
if name == ".git" {
|
||||
return false;
|
||||
}
|
||||
let is_dir = entry.file_type().is_some_and(|t| t.is_dir());
|
||||
if is_dir && name == "node_modules" {
|
||||
return false;
|
||||
}
|
||||
if entry.depth() == 1 && entry.path() == vendor {
|
||||
return false;
|
||||
}
|
||||
if is_dir && let Some(rel) = relative(root, entry.path()) {
|
||||
return !filter.excluded(&rel);
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// The project-relative path of `entry` if it is a regular file that the
|
||||
/// globs keep.
|
||||
fn file_path(root: &Path, entry: &DirEntry, filter: &PathFilter) -> Option<String> {
|
||||
if !entry.file_type().is_some_and(|t| t.is_file()) {
|
||||
return None;
|
||||
}
|
||||
let rel = relative(root, entry.path())?;
|
||||
filter.file(&rel).then_some(rel)
|
||||
}
|
||||
|
||||
/// `path` relative to `root` with `/` separators; `None` outside the root or
|
||||
/// for a name that is not valid Unicode.
|
||||
fn relative(root: &Path, path: &Path) -> Option<String> {
|
||||
let rel: PathBuf = path.strip_prefix(root).ok()?.to_path_buf();
|
||||
let parts: Option<Vec<&str>> = rel.components().map(|c| c.as_os_str().to_str()).collect();
|
||||
Some(parts?.join("/"))
|
||||
}
|
||||
|
||||
/// Searches one file, or the buffer text that replaces it, and returns its
|
||||
/// matches; `None` without matches, for a binary, oversized or unreadable
|
||||
/// file, or when cancelled.
|
||||
///
|
||||
/// # Parameters
|
||||
/// - `room`: most matches to collect; one more is collected when possible,
|
||||
/// so the caller can tell that the cap cut the file short (with no room
|
||||
/// left, one match is enough to know that).
|
||||
fn search_file(
|
||||
searcher: &mut Searcher,
|
||||
matcher: &RegexMatcher,
|
||||
path: &Path,
|
||||
buffer: Option<&String>,
|
||||
room: usize,
|
||||
cancel: &CancelToken,
|
||||
) -> Option<Vec<Match>> {
|
||||
let mut sink = Collect {
|
||||
matcher,
|
||||
matches: Vec::new(),
|
||||
limit: room.saturating_add(1),
|
||||
binary: false,
|
||||
cancel,
|
||||
};
|
||||
let result = match buffer {
|
||||
Some(text) => {
|
||||
if text.len() as u64 > MAX_FILE_SIZE {
|
||||
return None;
|
||||
}
|
||||
searcher.search_slice(matcher, text.as_bytes(), &mut sink)
|
||||
}
|
||||
None => {
|
||||
let size = std::fs::metadata(path).ok()?.len();
|
||||
if size > MAX_FILE_SIZE {
|
||||
return None;
|
||||
}
|
||||
searcher.search_path(matcher, path, &mut sink)
|
||||
}
|
||||
};
|
||||
if result.is_err() || sink.binary || cancel.is_cancelled() || sink.matches.is_empty() {
|
||||
return None;
|
||||
}
|
||||
Some(sink.matches)
|
||||
}
|
||||
|
||||
/// Collects the matches of one file.
|
||||
struct Collect<'a> {
|
||||
/// The pattern, run again on each matching line to find every match.
|
||||
matcher: &'a RegexMatcher,
|
||||
/// Matches so far.
|
||||
matches: Vec<Match>,
|
||||
/// Stop at this many matches.
|
||||
limit: usize,
|
||||
/// The file turned out to be binary.
|
||||
binary: bool,
|
||||
/// Stops the file early.
|
||||
cancel: &'a CancelToken,
|
||||
}
|
||||
|
||||
impl Sink for Collect<'_> {
|
||||
type Error = std::io::Error;
|
||||
|
||||
fn matched(&mut self, _searcher: &Searcher, mat: &SinkMatch<'_>) -> Result<bool, Self::Error> {
|
||||
if self.cancel.is_cancelled() {
|
||||
return Ok(false);
|
||||
}
|
||||
let line = mat
|
||||
.line_number()
|
||||
.and_then(|n| usize::try_from(n.saturating_sub(1)).ok())
|
||||
.unwrap_or(0);
|
||||
lines::collect(
|
||||
self.matcher,
|
||||
line,
|
||||
mat.bytes(),
|
||||
&mut self.matches,
|
||||
self.limit,
|
||||
);
|
||||
Ok(self.matches.len() < self.limit)
|
||||
}
|
||||
|
||||
fn binary_data(&mut self, _searcher: &Searcher, _offset: u64) -> Result<bool, Self::Error> {
|
||||
self.binary = true;
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
345
crates/search/tests/search.rs
Normal file
345
crates/search/tests/search.rs
Normal file
|
|
@ -0,0 +1,345 @@
|
|||
//! Find in files over real directories: matching, rules, caps, cancel.
|
||||
|
||||
#![allow(
|
||||
clippy::unwrap_used,
|
||||
clippy::expect_used,
|
||||
clippy::indexing_slicing,
|
||||
clippy::panic
|
||||
)]
|
||||
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use std::sync::Mutex;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use lutra_search::{
|
||||
CancelToken, FileMatches, MAX_FILE_SIZE, Overlay, Query, Search, SearchError, SearchEvent,
|
||||
Summary, run, spawn,
|
||||
};
|
||||
use proptest::prelude::*;
|
||||
|
||||
/// Runs `query` over `root` and returns the files sorted by path.
|
||||
fn find(root: &Path, query: &Query, overlay: &Overlay) -> (Vec<FileMatches>, Summary) {
|
||||
let search = Search::new(query).unwrap();
|
||||
let found = Mutex::new(Vec::new());
|
||||
let summary = run(root, &search, overlay, &CancelToken::new(), &|f| {
|
||||
found.lock().unwrap().push(f)
|
||||
});
|
||||
let mut files = found.into_inner().unwrap();
|
||||
files.sort_by(|a, b| a.path.cmp(&b.path));
|
||||
(files, summary)
|
||||
}
|
||||
|
||||
fn paths(files: &[FileMatches]) -> Vec<&str> {
|
||||
files.iter().map(|f| f.path.as_str()).collect()
|
||||
}
|
||||
|
||||
fn put(root: &Path, rel: &str, text: &[u8]) {
|
||||
let path = root.join(rel);
|
||||
fs::create_dir_all(path.parent().unwrap()).unwrap();
|
||||
fs::write(path, text).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn literal_case_and_whole_word() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
put(
|
||||
dir.path(),
|
||||
"a.php",
|
||||
b"<?php\n$user = new User();\n$users = [];\nUSER\n",
|
||||
);
|
||||
let mut q = Query::literal("user");
|
||||
let (files, summary) = find(dir.path(), &q, &Overlay::new());
|
||||
assert_eq!(summary.matches, 4);
|
||||
let m = &files[0].matches;
|
||||
assert_eq!((m[0].line, m[0].column, m[0].length), (1, 1, 4));
|
||||
assert_eq!(m[0].preview, "$user = new User();");
|
||||
assert_eq!((m[1].line, m[1].column), (1, 12));
|
||||
|
||||
q.case_sensitive = true;
|
||||
assert_eq!(find(dir.path(), &q, &Overlay::new()).1.matches, 2);
|
||||
|
||||
q.whole_word = true;
|
||||
let (files, _) = find(dir.path(), &q, &Overlay::new());
|
||||
assert_eq!(files[0].matches.len(), 1);
|
||||
assert_eq!(files[0].matches[0].line, 1);
|
||||
|
||||
let q = Query::literal("$users = [");
|
||||
let (files, _) = find(dir.path(), &q, &Overlay::new());
|
||||
assert_eq!(files[0].matches[0].line, 2, "regex characters are literal");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn regex_and_invalid_input() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
put(dir.path(), "a.php", b"foo1 foo22 bar\r\nfoo333\r\n");
|
||||
let mut q = Query::literal(r"foo\d+");
|
||||
q.regex = true;
|
||||
let (files, summary) = find(dir.path(), &q, &Overlay::new());
|
||||
assert_eq!(summary.matches, 3);
|
||||
assert_eq!(files[0].matches[2].preview, "foo333", "no \\r in previews");
|
||||
assert_eq!(files[0].matches[1].length, 5);
|
||||
|
||||
q.pattern = "(".into();
|
||||
assert!(matches!(
|
||||
Search::new(&q),
|
||||
Err(SearchError::InvalidPattern(_))
|
||||
));
|
||||
q.pattern = String::new();
|
||||
assert_eq!(Search::new(&q).unwrap_err(), SearchError::EmptyPattern);
|
||||
let mut q = Query::literal("x");
|
||||
q.include = vec!["[".into()];
|
||||
assert!(matches!(Search::new(&q), Err(SearchError::InvalidGlob(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn include_and_exclude_globs() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
for rel in ["src/a.php", "src/b.js", "tests/c.php", "src/deep/d.php"] {
|
||||
put(dir.path(), rel, b"needle\n");
|
||||
}
|
||||
let mut q = Query::literal("needle");
|
||||
q.include = vec!["*.php".into()];
|
||||
let (files, _) = find(dir.path(), &q, &Overlay::new());
|
||||
assert_eq!(
|
||||
paths(&files),
|
||||
["src/a.php", "src/deep/d.php", "tests/c.php"]
|
||||
);
|
||||
|
||||
q.exclude = vec!["tests/".into()];
|
||||
let (files, _) = find(dir.path(), &q, &Overlay::new());
|
||||
assert_eq!(paths(&files), ["src/a.php", "src/deep/d.php"]);
|
||||
|
||||
q.include = vec!["src/*.php".into()];
|
||||
q.exclude = vec![];
|
||||
let (files, _) = find(dir.path(), &q, &Overlay::new());
|
||||
assert_eq!(
|
||||
paths(&files),
|
||||
["src/a.php"],
|
||||
"a path glob keeps to its level"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn honours_the_project_rules() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let tree = lutra_testkit::generate_search_tree(dir.path(), 400);
|
||||
put(dir.path(), "sub/.gitignore", b"skip.php\n");
|
||||
put(dir.path(), "sub/skip.php", b"lutraNeedle\n");
|
||||
put(dir.path(), "sub/.hidden.php", b"lutraNeedle\n");
|
||||
put(dir.path(), ".git/HEAD", b"lutraNeedle\n");
|
||||
let (files, summary) = find(
|
||||
dir.path(),
|
||||
&Query::literal(lutra_testkit::NEEDLE),
|
||||
&Overlay::new(),
|
||||
);
|
||||
let found = paths(&files);
|
||||
assert!(found.iter().all(|p| !p.starts_with("var/")), "{found:?}");
|
||||
assert!(found.iter().all(|p| !p.starts_with("node_modules/")));
|
||||
assert!(found.iter().all(|p| !p.starts_with(".git/")));
|
||||
assert!(!found.contains(&"bin/data.bin"), "binary skipped");
|
||||
assert!(!found.contains(&"debug.log"), "ignored file skipped");
|
||||
assert!(!found.contains(&"sub/skip.php"), "nested .gitignore");
|
||||
assert!(found.contains(&"sub/.hidden.php"), "dot files are searched");
|
||||
let vendor = found.iter().filter(|p| p.starts_with("vendor/")).count();
|
||||
assert_eq!(vendor, tree.needle_vendor, "vendor is searched");
|
||||
assert!(vendor > 0);
|
||||
assert_eq!(summary.files, tree.needle_files + 1);
|
||||
assert!(!summary.truncated && !summary.cancelled);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn skips_large_and_binary_files() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let mut big = b"needle\n".to_vec();
|
||||
big.resize(usize::try_from(MAX_FILE_SIZE).unwrap() + 1, b'a');
|
||||
put(dir.path(), "big.txt", &big);
|
||||
let mut late_nul = b"needle\n".to_vec();
|
||||
late_nul.extend(b"filler\n".repeat(50_000));
|
||||
late_nul.push(0);
|
||||
put(dir.path(), "late.bin", &late_nul);
|
||||
put(dir.path(), "ok.txt", b"needle\n");
|
||||
let (files, _) = find(dir.path(), &Query::literal("needle"), &Overlay::new());
|
||||
assert_eq!(paths(&files), ["ok.txt"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overlay_replaces_the_disk_text() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
put(dir.path(), "a.php", b"old text\n");
|
||||
put(dir.path(), "b.php", b"needle on disk\n");
|
||||
let mut overlay = Overlay::new();
|
||||
overlay.insert("a.php".into(), "line\n needle in buffer\n".into());
|
||||
overlay.insert("b.php".into(), "gone\n".into());
|
||||
overlay.insert("missing.php".into(), "needle\n".into());
|
||||
let (files, _) = find(dir.path(), &Query::literal("needle"), &overlay);
|
||||
assert_eq!(paths(&files), ["a.php"]);
|
||||
assert_eq!(files[0].matches[0].line, 1);
|
||||
assert_eq!(files[0].matches[0].column, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn caps_the_number_of_matches() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
for i in 0..20 {
|
||||
put(dir.path(), &format!("f{i}.txt"), b"x x x x x\n");
|
||||
}
|
||||
let mut q = Query::literal("x");
|
||||
q.max_results = 7;
|
||||
let (files, summary) = find(dir.path(), &q, &Overlay::new());
|
||||
assert_eq!(summary.matches, 7);
|
||||
assert!(summary.truncated);
|
||||
assert_eq!(files.iter().map(|f| f.matches.len()).sum::<usize>(), 7);
|
||||
assert_eq!(summary.files, files.len());
|
||||
|
||||
q.max_results = 100;
|
||||
let (_, summary) = find(dir.path(), &q, &Overlay::new());
|
||||
assert_eq!(summary.matches, 100);
|
||||
assert!(
|
||||
!summary.truncated,
|
||||
"exactly at the cap with nothing left out"
|
||||
);
|
||||
q.max_results = 99;
|
||||
assert!(find(dir.path(), &q, &Overlay::new()).1.truncated);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spawn_streams_and_cancels() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let tree = lutra_testkit::generate_search_tree(dir.path(), 300);
|
||||
let search = Search::new(&Query::literal(lutra_testkit::NEEDLE)).unwrap();
|
||||
let rx = spawn(
|
||||
dir.path().into(),
|
||||
search.clone(),
|
||||
Overlay::new(),
|
||||
CancelToken::new(),
|
||||
)
|
||||
.unwrap();
|
||||
let events: Vec<SearchEvent> = rx.iter().collect();
|
||||
let files = events
|
||||
.iter()
|
||||
.filter(|e| matches!(e, SearchEvent::File(_)))
|
||||
.count();
|
||||
assert_eq!(files, tree.needle_files);
|
||||
let Some(SearchEvent::Done(summary)) = events.last() else {
|
||||
panic!("no summary last: {events:?}");
|
||||
};
|
||||
assert_eq!(summary.files, files);
|
||||
|
||||
let cancel = CancelToken::new();
|
||||
cancel.cancel();
|
||||
let rx = spawn(dir.path().into(), search, Overlay::new(), cancel).unwrap();
|
||||
let events: Vec<SearchEvent> = rx.iter().collect();
|
||||
assert_eq!(events.len(), 1);
|
||||
let SearchEvent::Done(summary) = &events[0] else {
|
||||
panic!("{events:?}");
|
||||
};
|
||||
assert!(summary.cancelled);
|
||||
assert_eq!(summary.matches, 0);
|
||||
}
|
||||
|
||||
/// Measures the first result and the total on a generated tree of 20,000
|
||||
/// files. Run in release: `cargo test -p lutra-search --release --test
|
||||
/// search -- --ignored --nocapture measure`.
|
||||
#[test]
|
||||
#[ignore = "measurement; run explicitly in release"]
|
||||
fn measure_20k_tree() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let made = Instant::now();
|
||||
let tree = lutra_testkit::generate_search_tree(dir.path(), 20_000);
|
||||
println!("generated {} files in {:?}", tree.files, made.elapsed());
|
||||
for (label, pattern) in [
|
||||
("common literal", "function"),
|
||||
("rare literal", lutra_testkit::NEEDLE),
|
||||
] {
|
||||
for round in 0..3 {
|
||||
let search = Search::new(&Query::literal(pattern)).unwrap();
|
||||
let started = Instant::now();
|
||||
let rx = spawn(
|
||||
dir.path().into(),
|
||||
search,
|
||||
Overlay::new(),
|
||||
CancelToken::new(),
|
||||
)
|
||||
.unwrap();
|
||||
let mut first: Option<Duration> = None;
|
||||
let mut summary = None;
|
||||
for event in rx {
|
||||
match event {
|
||||
SearchEvent::File(_) => {
|
||||
first.get_or_insert_with(|| started.elapsed());
|
||||
}
|
||||
SearchEvent::Done(s) => summary = Some(s),
|
||||
}
|
||||
}
|
||||
let s = summary.unwrap();
|
||||
println!(
|
||||
"{label} round {round}: first {:?}, total {:?}, files {}, matches {}, truncated {}",
|
||||
first.unwrap(),
|
||||
s.elapsed,
|
||||
s.files,
|
||||
s.matches,
|
||||
s.truncated
|
||||
);
|
||||
}
|
||||
}
|
||||
let mut q = Query::literal("function");
|
||||
q.max_results = lutra_search::MAX_RESULTS_LIMIT;
|
||||
let search = Search::new(&q).unwrap();
|
||||
let s = run(
|
||||
dir.path(),
|
||||
&search,
|
||||
&Overlay::new(),
|
||||
&CancelToken::new(),
|
||||
&|_| {},
|
||||
);
|
||||
println!(
|
||||
"common literal, cap 100k: total {:?}, files {}, matches {}, truncated {}",
|
||||
s.elapsed, s.files, s.matches, s.truncated
|
||||
);
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#![proptest_config(ProptestConfig::with_cases(64))]
|
||||
|
||||
/// Random patterns, flags and file contents never panic, and every
|
||||
/// reported match lies inside its preview.
|
||||
#[test]
|
||||
fn random_queries_never_panic(
|
||||
pattern in "\\PC{0,12}",
|
||||
regex in any::<bool>(),
|
||||
case_sensitive in any::<bool>(),
|
||||
whole_word in any::<bool>(),
|
||||
content in proptest::collection::vec(any::<u8>(), 0..400),
|
||||
text in "\\PC{0,200}",
|
||||
) {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
fs::write(dir.path().join("bytes.txt"), &content).unwrap();
|
||||
fs::write(dir.path().join("text.php"), &text).unwrap();
|
||||
let query = Query {
|
||||
pattern,
|
||||
regex,
|
||||
case_sensitive,
|
||||
whole_word,
|
||||
include: vec![],
|
||||
exclude: vec![],
|
||||
max_results: 50,
|
||||
};
|
||||
let Ok(search) = Search::new(&query) else {
|
||||
return Ok(());
|
||||
};
|
||||
let found = Mutex::new(Vec::new());
|
||||
let summary = run(dir.path(), &search, &Overlay::new(), &CancelToken::new(), &|f| {
|
||||
found.lock().unwrap().push(f)
|
||||
});
|
||||
let found = found.into_inner().unwrap();
|
||||
prop_assert!(summary.matches <= 50);
|
||||
for file in found {
|
||||
for m in file.matches {
|
||||
prop_assert!(m.length > 0);
|
||||
let units = m.preview.encode_utf16().count();
|
||||
prop_assert!(m.preview_column + m.length <= units || m.preview_column <= units);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -10,6 +10,8 @@
|
|||
clippy::indexing_slicing
|
||||
)]
|
||||
|
||||
pub mod tree;
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Mutex;
|
||||
|
||||
|
|
@ -17,6 +19,8 @@ use lutra_control::{Backend, EventSink, Subscribers};
|
|||
use lutra_engine::{CommandInfo, Effect, Engine, EngineConfig, EngineError};
|
||||
use serde_json::Value;
|
||||
|
||||
pub use tree::{NEEDLE, SearchTree, generate_search_tree};
|
||||
|
||||
/// Engine directories inside `root` (`root/config`, `root/data`), for
|
||||
/// [`Engine::with_config`] and [`EngineBackend::with_config`] in tests.
|
||||
pub fn engine_config(root: &Path) -> EngineConfig {
|
||||
|
|
|
|||
151
crates/testkit/src/tree.rs
Normal file
151
crates/testkit/src/tree.rs
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
//! Generated project trees for search tests and measurements.
|
||||
//!
|
||||
//! [`generate_search_tree`] writes a deterministic PHP project of any size:
|
||||
//! project sources, templates, scripts and docs, a gitignored `var/`, a
|
||||
//! gitignored Composer `vendor/` (searched as library code), an excluded
|
||||
//! `node_modules/` and a binary file. Every [`NEEDLE_EVERY`]th file of each
|
||||
//! group contains [`NEEDLE`] once; [`SearchTree`] reports how many files of
|
||||
//! each group have it, so tests know the expected result without searching
|
||||
//! twice.
|
||||
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
/// A token that occurs only where the generator puts it.
|
||||
pub const NEEDLE: &str = "lutraNeedle";
|
||||
|
||||
/// Every this many files of a group contains [`NEEDLE`].
|
||||
pub const NEEDLE_EVERY: usize = 50;
|
||||
|
||||
/// What [`generate_search_tree`] wrote.
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
|
||||
pub struct SearchTree {
|
||||
/// Files written in total, including `.gitignore` and the binary file.
|
||||
pub files: usize,
|
||||
/// Searchable files (sources, templates, scripts, docs, vendor) that
|
||||
/// contain [`NEEDLE`].
|
||||
pub needle_files: usize,
|
||||
/// Of those, files under `vendor/`.
|
||||
pub needle_vendor: usize,
|
||||
/// Files containing [`NEEDLE`] that a search must skip (ignored,
|
||||
/// excluded or binary).
|
||||
pub needle_skipped: usize,
|
||||
}
|
||||
|
||||
/// Writes a project of about `files` files below `root`.
|
||||
///
|
||||
/// Shares: 70 % PHP sources in `src/`, 8 % templates, 5 % JavaScript, 5 %
|
||||
/// Markdown, 6 % ignored PHP in `var/cache/`, 6 % PHP in `vendor/`, plus a
|
||||
/// few files in `node_modules/` and `bin/data.bin` (a NUL byte, then the
|
||||
/// needle). Every PHP file contains `function` on several lines, the common
|
||||
/// literal for measurements.
|
||||
///
|
||||
/// # Panics
|
||||
/// If a file cannot be written (test helper).
|
||||
pub fn generate_search_tree(root: &Path, files: usize) -> SearchTree {
|
||||
let mut tree = SearchTree::default();
|
||||
write(root, ".gitignore", "/var/\n/vendor/\n*.log\n", &mut tree);
|
||||
let groups: [(&str, usize, Kind); 6] = [
|
||||
("src", 70, Kind::Php),
|
||||
("templates", 8, Kind::Twig),
|
||||
("assets", 5, Kind::Js),
|
||||
("docs", 5, Kind::Markdown),
|
||||
("var/cache", 6, Kind::Php),
|
||||
("vendor/acme/toolkit/src", 6, Kind::Php),
|
||||
];
|
||||
for (dir, share, kind) in groups {
|
||||
let count = files.saturating_mul(share) / 100;
|
||||
for i in 0..count {
|
||||
let needle = i % NEEDLE_EVERY == 0;
|
||||
let rel = format!("{dir}/Module{:03}/{}", i / 100, kind.file_name(i));
|
||||
write(root, &rel, &kind.content(i, needle), &mut tree);
|
||||
if !needle {
|
||||
continue;
|
||||
}
|
||||
if dir == "var/cache" {
|
||||
tree.needle_skipped += 1;
|
||||
} else {
|
||||
tree.needle_files += 1;
|
||||
if dir.starts_with("vendor") {
|
||||
tree.needle_vendor += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for i in 0..3 {
|
||||
let rel = format!("node_modules/pkg{i}/index.js");
|
||||
write(root, &rel, &Kind::Js.content(i, true), &mut tree);
|
||||
tree.needle_skipped += 1;
|
||||
}
|
||||
write(root, "debug.log", &format!("{NEEDLE}\n"), &mut tree);
|
||||
tree.needle_skipped += 1;
|
||||
let mut binary = b"\x89BIN\x00\x01\x02".to_vec();
|
||||
binary.extend_from_slice(NEEDLE.as_bytes());
|
||||
fs::create_dir_all(root.join("bin")).unwrap();
|
||||
fs::write(root.join("bin/data.bin"), binary).unwrap();
|
||||
tree.files += 1;
|
||||
tree.needle_skipped += 1;
|
||||
tree
|
||||
}
|
||||
|
||||
/// Writes `text` to `root/rel`, creating directories, and counts it.
|
||||
fn write(root: &Path, rel: &str, text: &str, tree: &mut SearchTree) {
|
||||
let path = root.join(rel);
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent).unwrap();
|
||||
}
|
||||
fs::write(path, text).unwrap();
|
||||
tree.files += 1;
|
||||
}
|
||||
|
||||
/// The kind of a generated file.
|
||||
#[derive(Clone, Copy)]
|
||||
enum Kind {
|
||||
/// A PHP class.
|
||||
Php,
|
||||
/// A Twig template.
|
||||
Twig,
|
||||
/// A JavaScript module.
|
||||
Js,
|
||||
/// A Markdown page.
|
||||
Markdown,
|
||||
}
|
||||
|
||||
impl Kind {
|
||||
/// File name of the `i`th file.
|
||||
fn file_name(self, i: usize) -> String {
|
||||
match self {
|
||||
Kind::Php => format!("Service{i}.php"),
|
||||
Kind::Twig => format!("page{i}.html.twig"),
|
||||
Kind::Js => format!("widget{i}.js"),
|
||||
Kind::Markdown => format!("note{i}.md"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Content of the `i`th file, with [`NEEDLE`] once if `needle`.
|
||||
fn content(self, i: usize, needle: bool) -> String {
|
||||
let marker = if needle { NEEDLE } else { "plain" };
|
||||
match self {
|
||||
Kind::Php => {
|
||||
let mut s = format!(
|
||||
"<?php\n\ndeclare(strict_types=1);\n\nnamespace App\\Module{};\n\nuse App\\Contracts\\Handler;\n\n/**\n * Service number {i}.\n */\nfinal class Service{i} implements Handler\n{{\n private array $items = [];\n\n",
|
||||
i / 100
|
||||
);
|
||||
for m in 0..8 {
|
||||
s.push_str(&format!(
|
||||
" public function handle{m}(int $id, string $name): ?string\n {{\n $key = sprintf('%s-%d', $name, $id + {m});\n if (isset($this->items[$key])) {{\n return $this->items[$key];\n }}\n return null;\n }}\n\n"
|
||||
));
|
||||
}
|
||||
s.push_str(&format!(" public const MARKER = '{marker}';\n}}\n"));
|
||||
s
|
||||
}
|
||||
Kind::Twig => format!(
|
||||
"{{% extends 'base.html.twig' %}}\n{{% block body %}}\n <h1>Page {i}</h1>\n <p class=\"{marker}\">{{{{ content }}}}</p>\n{{% endblock %}}\n"
|
||||
),
|
||||
Kind::Js => format!(
|
||||
"export function widget{i}(el) {{\n el.dataset.kind = '{marker}';\n return el;\n}}\n"
|
||||
),
|
||||
Kind::Markdown => format!("# Note {i}\n\nSome text about {marker} and more text.\n"),
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue