patchloom 0.6.0

Structured file editing library and CLI for AI agents: parser-backed JSON/YAML/TOML edits, AST-aware code operations via tree-sitter, multi-file batching, markdown operations, and MCP server
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
use memchr::memchr_iter;
use memchr::memmem;
use regex::Regex;
use serde::Serialize;
use std::collections::BTreeMap;
use std::sync::Arc;

/// A single search match with location and context.
#[derive(Debug, Serialize)]
pub struct SearchMatch {
    #[serde(serialize_with = "serialize_arc_str")]
    pub path: Arc<str>,
    /// 1-based line number of the match.
    pub line: usize,
    /// 1-based byte offset from the start of the line to the match start.
    pub column: usize,
    pub text: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub context_before: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub context_after: Option<Vec<String>>,
}

fn serialize_arc_str<S: serde::Serializer>(s: &Arc<str>, ser: S) -> Result<S::Ok, S::Error> {
    ser.serialize_str(s)
}

/// Aggregated search results across multiple files.
pub struct SearchResults {
    pub matches: Vec<SearchMatch>,
    pub file_match_counts: BTreeMap<Arc<str>, usize>,
}

impl SearchResults {
    pub fn has_matches(&self) -> bool {
        !self.matches.is_empty()
    }
}

/// Matcher abstraction: either a compiled regex or a memchr literal finder.
pub enum Matcher {
    Regex(Regex),
    Literal(Box<memmem::Finder<'static>>),
}

impl Matcher {
    /// Find the first match in `text`, returning (start, end) byte offsets.
    pub fn find(&self, text: &str) -> Option<(usize, usize)> {
        match self {
            Matcher::Regex(re) => re.find(text).map(|m| (m.start(), m.end())),
            Matcher::Literal(finder) => {
                let start = finder.find(text.as_bytes())?;
                Some((start, start + finder.needle().len()))
            }
        }
    }

    /// Iterate all matches in `text` (for multiline mode).
    pub fn find_iter_positions(&self, text: &str) -> Vec<(usize, usize)> {
        match self {
            Matcher::Regex(re) => re.find_iter(text).map(|m| (m.start(), m.end())).collect(),
            Matcher::Literal(finder) => {
                let bytes = text.as_bytes();
                let mut positions = Vec::new();
                let needle_len = finder.needle().len();
                let mut start = 0;
                while let Some(pos) = finder.find(&bytes[start..]) {
                    positions.push((start + pos, start + pos + needle_len));
                    start += pos + needle_len;
                }
                positions
            }
        }
    }

    /// Count matches in `text`, optionally stopping after the first match.
    pub fn count_matches(&self, text: &str, stop_after_first: bool) -> usize {
        match self {
            Matcher::Regex(re) => {
                if stop_after_first {
                    usize::from(re.find(text).is_some())
                } else {
                    re.find_iter(text).count()
                }
            }
            Matcher::Literal(finder) => {
                let bytes = text.as_bytes();
                if stop_after_first {
                    return usize::from(finder.find(bytes).is_some());
                }
                let needle_len = finder.needle().len();
                let mut count = 0;
                let mut start = 0;
                while let Some(pos) = finder.find(&bytes[start..]) {
                    count += 1;
                    start += pos + needle_len;
                }
                count
            }
        }
    }
}

/// Build the right matcher for the given search parameters.
pub fn build_matcher(
    pattern: &str,
    literal: bool,
    case_insensitive: bool,
    multiline: bool,
) -> anyhow::Result<Matcher> {
    // Use memchr for literal, case-sensitive, non-multiline searches.
    if literal && !case_insensitive && !multiline {
        return Ok(Matcher::Literal(Box::new(
            memmem::Finder::new(pattern.as_bytes()).into_owned(),
        )));
    }

    let escaped = if literal {
        regex::escape(pattern)
    } else {
        pattern.to_string()
    };
    let re = if multiline || case_insensitive {
        regex::RegexBuilder::new(&escaped)
            .dot_matches_new_line(multiline)
            .case_insensitive(case_insensitive)
            .build()?
    } else {
        Regex::new(&escaped)?
    };
    Ok(Matcher::Regex(re))
}

/// Per-file search result collected from parallel threads.
pub struct FileResult {
    pub path_str: Arc<str>,
    pub matches: Vec<SearchMatch>,
    pub count: usize,
}

/// Parameters for searching a single file (avoids too-many-arguments).
pub struct SearchFileParams {
    pub multiline: bool,
    pub invert_match: bool,
    pub count_only: bool,
    pub files_with_matches: bool,
    pub assert_count: Option<usize>,
    pub before_context: Option<usize>,
    pub after_context: Option<usize>,
    pub context: Option<usize>,
    pub quiet: bool,
}

/// Compute a 1-based (line, column) pair from a byte offset into content.
///
/// `newline_offsets` is a list of byte positions where `\n` appears.
/// `start` is the byte offset of the match. Returns a 1-based line number
/// and a 1-based byte column within that line.
pub fn line_and_column_for_offset(newline_offsets: &[usize], start: usize) -> (usize, usize) {
    let line_index = newline_offsets.partition_point(|&offset| offset < start);
    let line_start = if line_index == 0 {
        0
    } else {
        newline_offsets[line_index - 1] + 1
    };
    (line_index + 1, start - line_start + 1)
}

/// Search a single file and return matches/counts.
pub fn search_one_file(
    path: &std::path::Path,
    matcher: &Matcher,
    params: &SearchFileParams,
    cwd: &std::path::Path,
) -> Option<FileResult> {
    #[cfg(feature = "cli")]
    let content = crate::files::read_text_file_logged(path, "search", params.quiet)?;
    #[cfg(not(feature = "cli"))]
    let content = crate::files::read_text_file(path)?;

    #[cfg(any(feature = "cli", feature = "files"))]
    let display = crate::files::relative_display(path, cwd);
    #[cfg(not(any(feature = "cli", feature = "files")))]
    let display = path.strip_prefix(cwd).unwrap_or(path);
    let path_str: Arc<str> = Arc::from(display.to_string_lossy().as_ref());
    let mut file_matches: Vec<SearchMatch> = Vec::new();
    let mut count = 0usize;

    if params.multiline {
        if params.count_only {
            count = matcher.count_matches(
                &content,
                params.files_with_matches && params.assert_count.is_none(),
            );
        } else {
            let newline_offsets: Vec<usize> = memchr_iter(b'\n', content.as_bytes()).collect();
            for (start, end) in matcher.find_iter_positions(&content) {
                count += 1;
                let (line, column) = line_and_column_for_offset(&newline_offsets, start);
                file_matches.push(SearchMatch {
                    path: path_str.clone(),
                    line,
                    column,
                    text: content[start..end].to_string(),
                    context_before: None,
                    context_after: None,
                });
            }
        }
    } else if params.count_only {
        for line in content.lines() {
            let found = matcher.find(line);
            let is_match = if params.invert_match {
                found.is_none()
            } else {
                found.is_some()
            };
            if is_match {
                count += 1;
                if params.files_with_matches && params.assert_count.is_none() {
                    break;
                }
            }
        }
    } else {
        let ctx_before = params.before_context.or(params.context).unwrap_or(0);
        let ctx_after = params.after_context.or(params.context).unwrap_or(0);
        let has_ctx = ctx_before > 0 || ctx_after > 0;

        if has_ctx {
            let lines: Vec<&str> = content.lines().collect();
            for (i, line) in lines.iter().copied().enumerate() {
                let found = matcher.find(line);
                let is_match = if params.invert_match {
                    found.is_none()
                } else {
                    found.is_some()
                };
                if !is_match {
                    continue;
                }
                count += 1;
                let column = found.map_or(1, |(s, _)| s + 1);
                let start = i.saturating_sub(ctx_before);
                let end = (i + 1 + ctx_after).min(lines.len());
                file_matches.push(SearchMatch {
                    path: path_str.clone(),
                    line: i + 1,
                    column,
                    text: line.to_string(),
                    context_before: Some(lines[start..i].iter().map(|s| s.to_string()).collect()),
                    context_after: Some(lines[i + 1..end].iter().map(|s| s.to_string()).collect()),
                });
            }
        } else {
            for (i, line) in content.lines().enumerate() {
                let found = matcher.find(line);
                let is_match = if params.invert_match {
                    found.is_none()
                } else {
                    found.is_some()
                };
                if !is_match {
                    continue;
                }
                count += 1;
                file_matches.push(SearchMatch {
                    path: path_str.clone(),
                    line: i + 1,
                    column: found.map_or(1, |(s, _)| s + 1),
                    text: line.to_string(),
                    context_before: None,
                    context_after: None,
                });
            }
        }
    }

    if count > 0 {
        Some(FileResult {
            path_str,
            matches: file_matches,
            count,
        })
    } else {
        None
    }
}

/// Merge per-file results into a single [`SearchResults`].
///
/// Sorts detailed matches by (path, line) and applies `max_results` cap.
/// This is the aggregation step after parallel file processing.
pub fn merge_file_results(
    file_results: Vec<FileResult>,
    count_only: bool,
    max_results: usize,
) -> SearchResults {
    let total_matches: usize = file_results.iter().map(|fr| fr.matches.len()).sum();
    let mut all_matches: Vec<SearchMatch> = Vec::with_capacity(total_matches);
    let mut file_match_counts: BTreeMap<Arc<str>, usize> = BTreeMap::new();
    for fr in file_results {
        file_match_counts.insert(fr.path_str, fr.count);
        all_matches.extend(fr.matches);
    }

    if !count_only {
        all_matches.sort_unstable_by(|a, b| a.path.cmp(&b.path).then_with(|| a.line.cmp(&b.line)));
    }

    if max_results > 0 && !count_only {
        all_matches.truncate(max_results);
    }

    SearchResults {
        matches: all_matches,
        file_match_counts,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn build_matcher_literal_case_sensitive() {
        let m = build_matcher("hello", true, false, false).unwrap();
        assert!(matches!(m, Matcher::Literal(_)));
        assert!(m.find("say hello world").is_some());
        assert!(m.find("say Hello world").is_none());
    }

    #[test]
    fn build_matcher_literal_case_insensitive_uses_regex() {
        let m = build_matcher("hello", true, true, false).unwrap();
        assert!(matches!(m, Matcher::Regex(_)));
        assert!(m.find("say Hello world").is_some());
    }

    #[test]
    fn build_matcher_regex_pattern() {
        let m = build_matcher(r"hel+o", false, false, false).unwrap();
        assert!(m.find("hello").is_some());
        assert!(m.find("helllo").is_some());
        assert!(m.find("heo").is_none());
    }

    #[test]
    fn line_and_column_for_offset_basic() {
        let offsets = vec![5, 10];
        assert_eq!(line_and_column_for_offset(&offsets, 0), (1, 1));
        assert_eq!(line_and_column_for_offset(&offsets, 5), (1, 6));
        assert_eq!(line_and_column_for_offset(&offsets, 6), (2, 1));
        assert_eq!(line_and_column_for_offset(&offsets, 11), (3, 1));
    }

    #[test]
    fn merge_file_results_sorts_by_path_then_line() {
        let fr1 = FileResult {
            path_str: Arc::from("b.txt"),
            matches: vec![SearchMatch {
                path: Arc::from("b.txt"),
                line: 1,
                column: 1,
                text: "b1".into(),
                context_before: None,
                context_after: None,
            }],
            count: 1,
        };
        let fr2 = FileResult {
            path_str: Arc::from("a.txt"),
            matches: vec![SearchMatch {
                path: Arc::from("a.txt"),
                line: 1,
                column: 1,
                text: "a1".into(),
                context_before: None,
                context_after: None,
            }],
            count: 1,
        };
        let results = merge_file_results(vec![fr1, fr2], false, 0);
        assert_eq!(results.matches[0].text, "a1");
        assert_eq!(results.matches[1].text, "b1");
    }

    #[test]
    fn merge_file_results_applies_max_results() {
        let fr = FileResult {
            path_str: Arc::from("a.txt"),
            matches: vec![
                SearchMatch {
                    path: Arc::from("a.txt"),
                    line: 1,
                    column: 1,
                    text: "l1".into(),
                    context_before: None,
                    context_after: None,
                },
                SearchMatch {
                    path: Arc::from("a.txt"),
                    line: 2,
                    column: 1,
                    text: "l2".into(),
                    context_before: None,
                    context_after: None,
                },
            ],
            count: 2,
        };
        let results = merge_file_results(vec![fr], false, 1);
        assert_eq!(results.matches.len(), 1);
        // file_match_counts still reflects the full count
        assert_eq!(*results.file_match_counts.get("a.txt").unwrap(), 2);
    }

    #[test]
    fn search_one_file_finds_literal_matches() {
        let dir = tempfile::TempDir::new().unwrap();
        let file = dir.path().join("test.txt");
        std::fs::write(&file, "Hello world\nGoodbye world\nHello again\n").unwrap();
        let matcher = build_matcher("Hello", true, false, false).unwrap();
        let params = SearchFileParams {
            multiline: false,
            invert_match: false,
            count_only: false,
            files_with_matches: false,
            assert_count: None,
            before_context: None,
            after_context: None,
            context: None,
            quiet: true,
        };
        let result = search_one_file(&file, &matcher, &params, dir.path()).unwrap();
        assert_eq!(result.count, 2);
        assert_eq!(result.matches.len(), 2);
        assert_eq!(result.matches[0].line, 1);
        assert_eq!(result.matches[1].line, 3);
    }

    #[test]
    fn search_one_file_count_only_skips_matches() {
        let dir = tempfile::TempDir::new().unwrap();
        let file = dir.path().join("test.txt");
        std::fs::write(&file, "Hello\nHello\n").unwrap();
        let matcher = build_matcher("Hello", true, false, false).unwrap();
        let params = SearchFileParams {
            multiline: false,
            invert_match: false,
            count_only: true,
            files_with_matches: false,
            assert_count: None,
            before_context: None,
            after_context: None,
            context: None,
            quiet: true,
        };
        let result = search_one_file(&file, &matcher, &params, dir.path()).unwrap();
        assert_eq!(result.count, 2);
        assert!(
            result.matches.is_empty(),
            "count_only should skip match objects"
        );
    }
}