aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/tools/fs.rs63
-rw-r--r--src/tools/mod.rs7
2 files changed, 52 insertions, 18 deletions
diff --git a/src/tools/fs.rs b/src/tools/fs.rs
index e543cb6..caa3992 100644
--- a/src/tools/fs.rs
+++ b/src/tools/fs.rs
@@ -268,6 +268,29 @@ fn read_line_limited<R: BufRead>(
268 } 268 }
269} 269}
270 270
271enum Matcher {
272 Literal { needle: String, ignore_case: bool },
273 Regex(regex::Regex),
274}
275
276impl Matcher {
277 fn is_match(&self, line: &str) -> bool {
278 match self {
279 Matcher::Literal {
280 needle,
281 ignore_case,
282 } => {
283 if *ignore_case {
284 line.to_lowercase().contains(needle)
285 } else {
286 line.contains(needle)
287 }
288 }
289 Matcher::Regex(regex) => regex.is_match(line),
290 }
291 }
292}
293
271pub fn grep(args: &Value) -> Result<String, String> { 294pub fn grep(args: &Value) -> Result<String, String> {
272 let pattern = req(args, "pattern")?; 295 let pattern = req(args, "pattern")?;
273 if pattern.is_empty() { 296 if pattern.is_empty() {
@@ -282,10 +305,29 @@ pub fn grep(args: &Value) -> Result<String, String> {
282 .get("ignore_case") 305 .get("ignore_case")
283 .and_then(|v| v.as_bool()) 306 .and_then(|v| v.as_bool())
284 .unwrap_or(false); 307 .unwrap_or(false);
285 let needle = if ignore_case { 308 let fixed = args.get("fixed").and_then(|v| v.as_bool()).unwrap_or(false);
286 pattern.to_lowercase() 309
310 let matcher = if fixed {
311 Matcher::Literal {
312 needle: if ignore_case {
313 pattern.to_lowercase()
314 } else {
315 pattern.clone()
316 },
317 ignore_case,
318 }
287 } else { 319 } else {
288 pattern.clone() 320 Matcher::Regex(
321 regex::RegexBuilder::new(&pattern)
322 .case_insensitive(ignore_case)
323 .build()
324 .map_err(|error| {
325 format!(
326 "invalid regex `{pattern}`: {error}\npass \"fixed\": true to match \
327 the text literally"
328 )
329 })?,
330 )
289 }; 331 };
290 332
291 let root = Path::new(&base); 333 let root = Path::new(&base);
@@ -303,7 +345,7 @@ pub fn grep(args: &Value) -> Result<String, String> {
303 if matches >= MAX_MATCHES { 345 if matches >= MAX_MATCHES {
304 break; 346 break;
305 } 347 }
306 grep_file(&file, &needle, ignore_case, &mut matches, &mut out); 348 grep_file(&file, &matcher, &mut matches, &mut out);
307 } 349 }
308 350
309 if matches == 0 { 351 if matches == 0 {
@@ -368,7 +410,7 @@ pub fn glob(args: &Value) -> Result<String, String> {
368 Ok(out) 410 Ok(out)
369} 411}
370 412
371fn grep_file(path: &Path, needle_lower: &str, ignore_case: bool, count: &mut usize, out: &mut String) { 413fn grep_file(path: &Path, matcher: &Matcher, count: &mut usize, out: &mut String) {
372 let meta = match fs::metadata(path) { 414 let meta = match fs::metadata(path) {
373 Ok(m) => m, 415 Ok(m) => m,
374 Err(_) => return, 416 Err(_) => return,
@@ -384,21 +426,12 @@ fn grep_file(path: &Path, needle_lower: &str, ignore_case: bool, count: &mut usi
384 return; 426 return;
385 } 427 }
386 let text = String::from_utf8_lossy(&bytes); 428 let text = String::from_utf8_lossy(&bytes);
387 let mut needle = needle_lower.to_string();
388 if !ignore_case {
389 needle = needle.to_string();
390 }
391 429
392 for (index, line) in text.lines().enumerate() { 430 for (index, line) in text.lines().enumerate() {
393 if *count >= MAX_MATCHES { 431 if *count >= MAX_MATCHES {
394 return; 432 return;
395 } 433 }
396 let hit = if ignore_case { 434 if matcher.is_match(line) {
397 line.to_lowercase().contains(&needle)
398 } else {
399 line.contains(&needle)
400 };
401 if hit {
402 *count += 1; 435 *count += 1;
403 out.push_str(&format!( 436 out.push_str(&format!(
404 "{}:{}: {}\n", 437 "{}:{}: {}\n",
diff --git a/src/tools/mod.rs b/src/tools/mod.rs
index 503de08..7a1a26d 100644
--- a/src/tools/mod.rs
+++ b/src/tools/mod.rs
@@ -117,13 +117,14 @@ pub fn schemas() -> Vec<Value> {
117 "type": "function", 117 "type": "function",
118 "function": { 118 "function": {
119 "name": "grep", 119 "name": "grep",
120 "description": "Search file contents for a literal string, recursively. Skips .git, target, node_modules and similar.", 120 "description": "Search file contents recursively with a regular expression. Skips .git, target, node_modules and similar. Set fixed=true to match literal text instead.",
121 "parameters": { 121 "parameters": {
122 "type": "object", 122 "type": "object",
123 "properties": { 123 "properties": {
124 "pattern": { "type": "string" }, 124 "pattern": { "type": "string", "description": "regular expression, or literal text when fixed=true" },
125 "path": { "type": "string", "description": "file or directory (default .)" }, 125 "path": { "type": "string", "description": "file or directory (default .)" },
126 "ignore_case": { "type": "boolean" } 126 "ignore_case": { "type": "boolean" },
127 "fixed": { "type": "boolean", "description": "treat pattern as literal text" }
127 }, 128 },
128 "required": ["pattern"] 129 "required": ["pattern"]
129 } 130 }