mod summary;
pub use summary::{FileSummary, FindFilesPage, find_files};
use crate::{
ByteRange, content_hash,
find_hits::{FileHits, Matcher},
find_position::{self, Cursor, to_match},
scope, structural,
};
use ast_grep_language::SupportLang;
use ignore::{WalkBuilder, overrides::OverrideBuilder};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::{
collections::{BTreeMap, HashSet},
ops::Range,
path::{Path, PathBuf},
};
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
pub struct FindQuery {
pub paths: Vec<PathBuf>,
#[serde(default)]
pub globs: Vec<String>,
pub literal: Option<String>,
pub regex: Option<String>,
#[serde(default)]
pub pattern: Option<String>,
#[serde(default)]
pub language: Option<String>,
#[serde(default)]
pub offset: usize,
pub limit: usize,
#[serde(default)]
pub within: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct Match {
pub path: PathBuf,
pub line: usize,
pub column: usize,
pub range: ByteRange,
pub text: String,
pub captures: Vec<Option<String>>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub vars: BTreeMap<String, String>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub var_ranges: BTreeMap<String, ByteRange>,
pub file_hash: String,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct FindPage {
pub matches: Vec<Match>,
pub total: usize,
pub files: usize,
pub next: Option<usize>,
}
#[derive(Debug, Clone)]
pub struct Hit {
pub range: Range<usize>,
pub captures: Vec<Option<String>>,
pub vars: BTreeMap<String, String>,
pub var_ranges: BTreeMap<String, Range<usize>>,
}
pub fn find(query: &FindQuery) -> Result<FindPage, String> {
let matcher = Matcher::new(query)?;
let paths = files(query)?;
validate_within(query, &paths)?;
let mut page = FindPage::default();
for path in paths {
let Some(text) = read_text(&path)? else {
continue;
};
let window = (
query.offset.saturating_sub(page.total),
query.limit - page.matches.len(),
);
let Some(hits) = find_in_file(&matcher, query, &path, &text, window)? else {
continue;
};
collect_file(&mut page, hits, (&path, &text));
}
let shown = query.offset + page.matches.len();
page.next = (shown < page.total).then_some(shown);
Ok(page)
}
fn read_text(path: &Path) -> Result<Option<String>, String> {
match std::fs::read_to_string(path) {
Ok(text) => Ok(Some(text)),
Err(error) if error.kind() == std::io::ErrorKind::InvalidData => Ok(None),
Err(error) => Err(format!("reading {}: {error}", path.display())),
}
}
fn find_in_file(
matcher: &Matcher,
query: &FindQuery,
path: &Path,
text: &str,
window: (usize, usize),
) -> Result<Option<FileHits>, String> {
if query.within.is_empty() {
return matcher.hits(path, text, None, window).map(Some);
}
let Some(language) = structural::enabled_language(path) else {
return Ok(None);
};
let spans = scope::spans(language, text, &query.within);
matcher.hits(path, text, Some(&spans), window).map(Some)
}
fn validate_within(query: &FindQuery, paths: &[PathBuf]) -> Result<(), String> {
if query.within.is_empty() {
return Ok(());
}
let languages: HashSet<SupportLang> = paths
.iter()
.filter_map(|path| structural::enabled_language(path))
.collect();
scope::validate(&query.within, &languages)
}
fn collect_file(page: &mut FindPage, hits: FileHits, (path, text): (&Path, &str)) {
if hits.total == 0 {
return;
}
page.files += 1;
page.total += hits.total;
if hits.hits.is_empty() {
return;
}
let hash = content_hash(text);
let mut cursor = Cursor::default();
for hit in hits.hits {
let position = find_position::position(&mut cursor, text, hit.range.start);
page.matches
.push(to_match(path, text, hit, &hash, position));
}
}
fn files(query: &FindQuery) -> Result<Vec<PathBuf>, String> {
let mut found = Vec::new();
for root in &query.paths {
if root.is_file() {
found.push(root.clone());
continue;
}
collect_directory(root, &query.globs, &mut found)?;
}
found.sort();
found.dedup();
Ok(found)
}
fn collect_directory(
root: &Path,
globs: &[String],
found: &mut Vec<PathBuf>,
) -> Result<(), String> {
let mut walk = WalkBuilder::new(root);
walk.overrides(overrides(root, globs)?);
for entry in walk.build() {
let entry = entry.map_err(|error| format!("walking: {error}"))?;
if entry.file_type().is_some_and(|kind| kind.is_file()) {
found.push(entry.into_path());
}
}
Ok(())
}
fn overrides(root: &Path, globs: &[String]) -> Result<ignore::overrides::Override, String> {
let mut builder = OverrideBuilder::new(root);
for glob in globs {
builder.add(glob).map_err(|error| error.to_string())?;
}
builder.build().map_err(|error| error.to_string())
}