patchloom 0.20.0

Structured file editing library and CLI for AI agents: parser-backed JSON/YAML/TOML edits, AST-aware code operations, 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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
//! AST write helpers for the public library API (feature `ast` + `files`/`cli`).
//!
//! Covers #1459 (signature rewrite), #1493 (rename / replace-in-symbol / in-content
//! signature rewrite), and #1495 (batch rename).

use std::path::{Path, PathBuf};

use crate::ast::rewrite::{FunctionSigEdit, rewrite_function_signature};
use crate::ast::{Language, rename, replace as ast_replace};
use crate::containment::PathGuard;
use crate::fallback::{EditError, EditErrorKind};
use crate::plan::Operation;
use crate::write::WritePolicy;

use super::{
    ApplyMode, ContentEditResult, EditResult, PostWriteHooks, ensure_contained, make_diff,
    maybe_post_write, write_if_apply,
};

/// Rewrite a function signature on disk (PathGuard + ApplyMode like other writers).
///
/// Prefer structured fields on [`FunctionSigEdit`]. Optionally pass `new_signature`
/// to replace the entire signature span (any language with tree-sitter support).
/// At least one of the structured fields or `new_signature` must be set.
///
/// Available with `features = ["ast"]` and `files` or `cli` for the write path.
#[cfg(all(feature = "ast", any(feature = "cli", feature = "files")))]
pub fn ast_rewrite_signature(
    path: &Path,
    name: &str,
    edit: &FunctionSigEdit,
    new_signature: Option<&str>,
    mode: ApplyMode,
    guard: Option<&PathGuard>,
) -> anyhow::Result<EditResult> {
    if new_signature.is_none()
        && edit.visibility.is_none()
        && edit.parameters.is_none()
        && edit.return_type.is_none()
    {
        return Err(EditError::new(
            EditErrorKind::InvalidInput,
            "ast_rewrite_signature requires new_signature and/or visibility/parameters/return_type",
        )
        .into());
    }
    let op = Operation::AstRewriteSignature {
        path: path.to_string_lossy().into(),
        old: name.into(),
        new_signature: new_signature.map(str::to_string),
        visibility: edit.visibility.clone(),
        parameters: edit.parameters.clone(),
        return_type: edit.return_type.clone(),
        lang: None,
    };
    let cwd = path.parent().unwrap_or_else(|| Path::new("."));
    super::execute_as_edit_result(op, mode, cwd, guard, "ast.rewrite_signature", None)
}

/// In-memory function signature rewrite (preview/apply parity with on-disk API).
///
/// See #1493. When `new_signature` is set, replaces the full signature span via
/// `rewrite_function_signature_full` semantics; otherwise applies structured
/// [`FunctionSigEdit`] fields.
#[cfg(feature = "ast")]
pub fn ast_rewrite_signature_in_content(
    content: &str,
    function_name: &str,
    edit: &FunctionSigEdit,
    new_signature: Option<&str>,
    lang: Language,
) -> anyhow::Result<ContentEditResult> {
    let new_content = if let Some(full) = new_signature {
        // Full-span replace is currently Rust-oriented (tree-sitter function_item).
        let _ = lang;
        crate::ast::rewrite::replace_function_signature(content, function_name, full).ok_or_else(
            || {
                EditError::new(
                    EditErrorKind::NoMatch,
                    format!("function `{function_name}` not found for signature rewrite"),
                )
            },
        )?
    } else {
        rewrite_function_signature(content, function_name, edit, lang).ok_or_else(|| {
            EditError::new(
                EditErrorKind::NoMatch,
                format!("function `{function_name}` not found for signature rewrite"),
            )
        })?
    };
    let changed = new_content != content;
    let diff = if changed {
        make_diff("<content>", content, &new_content)
    } else {
        String::new()
    };
    Ok(ContentEditResult {
        original: content.to_string(),
        new_content,
        diff,
        changed,
        match_count: if changed { 1 } else { 0 },
        match_mode: None,
        match_score: None,

        matched_text: None,
    })
}

/// Rename all identifier occurrences of `old` to `new` in a source file.
///
/// Tree-sitter based (skips strings/comments). Zero replacements → `NoMatch`.
/// See #1493.
#[cfg(all(feature = "ast", any(feature = "cli", feature = "files")))]
pub fn ast_rename(
    path: &Path,
    old: &str,
    new: &str,
    mode: ApplyMode,
    guard: Option<&PathGuard>,
) -> anyhow::Result<EditResult> {
    if old.is_empty() || new.is_empty() {
        return Err(EditError::new(
            EditErrorKind::InvalidInput,
            "ast_rename old/new must not be empty",
        )
        .into());
    }
    ensure_contained(guard, path)?;
    let path_str = path.to_string_lossy().into_owned();
    let original = crate::files::load_text_strict(path, &path_str).map_err(|e| {
        // Preserve Binary / InvalidEncoding / InvalidInput peels (#1963).
        if crate::exit::is_load_text_strict_fail(&e) {
            return e;
        }
        EditError::new(EditErrorKind::OperationFailed, e.to_string()).into()
    })?;
    let lang = Language::from_path(path);
    let Some(renamed) = rename::rename_in_source(&original, old, new, lang) else {
        return Err(EditError::new(
            EditErrorKind::ParseError,
            format!("failed to parse {} as {:?}", path_str, lang),
        )
        .into());
    };
    if renamed.replacements == 0 {
        return Err(EditError::new(
            EditErrorKind::NoMatch,
            format!("no identifier matches for `{old}` in {path_str}"),
        )
        .into());
    }
    let policy = WritePolicy::default();
    let (applied, backup_session) = write_if_apply(path, &renamed.content, mode, &policy, guard)?;
    let mut result = super::build_edit_result(
        &path_str,
        original,
        renamed.content,
        applied,
        "ast.rename",
        None,
    );
    result.backup_session = backup_session;
    result.match_count = renamed.replacements;
    Ok(result)
}

/// Options for [`ast_replace_in_symbol`] (#1658).
#[derive(Debug, Clone, Default)]
pub struct AstReplaceInSymbolOptions {
    /// Use regex mode for `old` (default `false`).
    pub regex: bool,
}

/// Replace text inside a named symbol body on disk.
///
/// Supports literal and regex modes via [`AstReplaceInSymbolOptions`].
/// - Missing symbol → `NoMatch` (message names the symbol)
/// - Symbol found, zero pattern matches → `NoMatch` (message names the pattern)
///
/// Zero replacements → `NoMatch`. See #1493, #1658.
#[cfg(all(feature = "ast", any(feature = "cli", feature = "files")))]
pub fn ast_replace_in_symbol(
    path: &Path,
    symbol: &str,
    old: &str,
    new: &str,
    opts: &AstReplaceInSymbolOptions,
    mode: ApplyMode,
    guard: Option<&PathGuard>,
) -> anyhow::Result<EditResult> {
    ensure_contained(guard, path)?;
    let path_str = path.to_string_lossy().into_owned();
    let original = crate::files::load_text_strict(path, &path_str).map_err(|e| {
        // Preserve Binary / InvalidEncoding / InvalidInput peels (#1963).
        if crate::exit::is_load_text_strict_fail(&e) {
            return e;
        }
        EditError::new(EditErrorKind::OperationFailed, e.to_string()).into()
    })?;
    let lang = Language::from_path(path);
    let replaced =
        match ast_replace::replace_in_symbol(&original, symbol, old, new, opts.regex, lang) {
            Ok(Some(r)) => r,
            Ok(None) => {
                return Err(EditError::new(
                    EditErrorKind::NoMatch,
                    format!("symbol `{symbol}` not found in {path_str}"),
                )
                .into());
            }
            Err(e) => {
                return Err(EditError::new(EditErrorKind::OperationFailed, e.to_string()).into());
            }
        };
    if replaced.replacements == 0 {
        return Err(EditError::new(
            EditErrorKind::NoMatch,
            format!("no matches for `{old}` in symbol `{symbol}` in {path_str}"),
        )
        .into());
    }
    let policy = WritePolicy::default();
    let (applied, backup_session) = write_if_apply(path, &replaced.content, mode, &policy, guard)?;
    let mut result = super::build_edit_result(
        &path_str,
        original,
        replaced.content,
        applied,
        "ast.replace_in_symbol",
        None,
    );
    result.backup_session = backup_session;
    result.match_count = replaced.replacements;
    result.match_mode = Some(super::MatchMode::Exact);
    Ok(result)
}

/// Options for [`find_files_with_symbol`] (#1664).
#[derive(Debug, Clone, Default)]
pub struct SymbolSearchOptions {
    /// Cap on returned paths (`None` = no limit).
    pub max_files: Option<usize>,
    /// Restrict to these languages (`None` = any language with a known extension).
    pub languages: Option<Vec<Language>>,
    /// Include hidden files/dirs (default false; same as search).
    pub include_hidden: bool,
}

/// Paths under `root` that likely contain identifier `name` (#1664).
///
/// Uses the same ignore / binary skip rules as library file collection, then
/// a word-boundary heuristic on source files with a known language extension.
/// Compose with [`ast_rename_batch`]:
///
/// ```rust,no_run
/// # #[cfg(all(feature = "ast", any(feature = "cli", feature = "files")))]
/// # {
/// use patchloom::api::{
///     ApplyMode, AstRenameBatchOptions, SymbolSearchOptions, ast_rename_batch,
///     find_files_with_symbol,
/// };
/// use std::path::Path;
///
/// let root = Path::new(".");
/// let paths = find_files_with_symbol(root, "OldType", &SymbolSearchOptions::default())?;
/// let path_refs: Vec<&Path> = paths.iter().map(Path::new).collect();
/// let _ = ast_rename_batch(
///     &path_refs,
///     "OldType",
///     "NewType",
///     &AstRenameBatchOptions {
///         mode: ApplyMode::Preview,
///         ..Default::default()
///     },
///     None,
/// )?;
/// # }
/// # Ok::<(), anyhow::Error>(())
/// ```
#[cfg(all(feature = "ast", any(feature = "cli", feature = "files")))]
pub fn find_files_with_symbol(
    root: &Path,
    name: &str,
    opts: &SymbolSearchOptions,
) -> anyhow::Result<Vec<PathBuf>> {
    if name.is_empty() {
        return Err(EditError::new(
            EditErrorKind::InvalidInput,
            "find_files_with_symbol name must not be empty",
        )
        .into());
    }
    // max_files: 0 means empty result (not "one file" via `len >= 0` after push).
    if opts.max_files == Some(0) {
        return Ok(Vec::new());
    }
    let all = crate::files::collect_file_paths(root, opts.include_hidden)?;
    let mut out = Vec::new();
    for path in all {
        let lang = Language::from_path(&path);
        if lang == Language::Unknown {
            continue;
        }
        if let Some(ref allowed) = opts.languages
            && !allowed.contains(&lang)
        {
            continue;
        }
        let Ok(bytes) = std::fs::read(&path) else {
            continue;
        };
        if crate::files::is_binary(&bytes) {
            continue;
        }
        let Ok(content) = String::from_utf8(bytes) else {
            continue;
        };
        if content_has_identifier(&content, name) {
            out.push(path);
            if opts.max_files.is_some_and(|m| out.len() >= m) {
                break;
            }
        }
    }
    Ok(out)
}

/// Options for [`ast_rename_project`] (#1689).
#[derive(Debug, Clone, Default)]
#[cfg(all(feature = "ast", any(feature = "cli", feature = "files")))]
pub struct AstRenameProjectOptions {
    pub search: SymbolSearchOptions,
    pub rename: AstRenameBatchOptions,
}

/// Result of [`ast_rename_project`]: discovery set plus per-file rename outcomes.
#[derive(Debug)]
#[cfg(all(feature = "ast", any(feature = "cli", feature = "files")))]
pub struct AstRenameProjectReport {
    /// Paths discovery considered (even when rename is a no-op).
    pub paths_considered: Vec<PathBuf>,
    /// Per-file rename results (same order as batch processing).
    pub results: Vec<AstRenameFileResult>,
}

/// Discover files containing `old_name` under `root` then batch-rename (#1689).
///
/// Preferred one-shot for agent hosts: “rename OldType project-wide” without a
/// separate path list from the model.
#[cfg(all(feature = "ast", any(feature = "cli", feature = "files")))]
pub fn ast_rename_project(
    root: &Path,
    old_name: &str,
    new_name: &str,
    opts: &AstRenameProjectOptions,
    guard: Option<&PathGuard>,
) -> anyhow::Result<AstRenameProjectReport> {
    let paths = find_files_with_symbol(root, old_name, &opts.search)?;
    if paths.is_empty() {
        return Err(EditError::new(
            EditErrorKind::NoMatch,
            format!(
                "no files under {} contain identifier {old_name:?}",
                root.display()
            ),
        )
        .into());
    }
    let path_refs: Vec<&Path> = paths.iter().map(PathBuf::as_path).collect();
    let results = ast_rename_batch(&path_refs, old_name, new_name, &opts.rename, guard)?;
    Ok(AstRenameProjectReport {
        paths_considered: paths,
        results,
    })
}

/// Word-boundary check for an identifier (ASCII-aware; good enough for discovery).
#[cfg(all(feature = "ast", any(feature = "cli", feature = "files")))]
fn content_has_identifier(content: &str, name: &str) -> bool {
    if name.is_empty() || !content.contains(name) {
        return false;
    }
    let bytes = content.as_bytes();
    let name_bytes = name.as_bytes();
    let mut start = 0usize;
    while let Some(rel) = content[start..].find(name) {
        let i = start + rel;
        let before_ok = i == 0 || !is_ident_byte(bytes[i - 1]);
        let after = i + name_bytes.len();
        let after_ok = after >= bytes.len() || !is_ident_byte(bytes[after]);
        if before_ok && after_ok {
            return true;
        }
        start = i + 1;
        if start >= content.len() {
            break;
        }
    }
    false
}

#[cfg(all(feature = "ast", any(feature = "cli", feature = "files")))]
fn is_ident_byte(b: u8) -> bool {
    b.is_ascii_alphanumeric() || b == b'_'
}

/// Options for [`ast_rename_batch`] (#1495).
#[derive(Debug, Clone)]
pub struct AstRenameBatchOptions {
    pub mode: ApplyMode,
    /// When true (default), a per-file `NoMatch` is recorded and remaining
    /// paths still run (agent-friendly best-effort). When false, the batch
    /// stops after the first `NoMatch` (that error is still in the results).
    pub continue_on_no_match: bool,
    /// Stop after the first hard error (IO, parse, guard). Default false.
    /// Independent of [`Self::continue_on_no_match`] (which only covers
    /// `NoMatch`).
    pub fail_fast: bool,
    /// Optional post-Apply format/lint hooks run per successful write (#1690).
    pub post_write: Option<PostWriteHooks>,
    /// Working directory for [`Self::post_write`] shell commands.
    pub post_write_cwd: Option<std::path::PathBuf>,
}

impl Default for AstRenameBatchOptions {
    fn default() -> Self {
        Self {
            mode: ApplyMode::Preview,
            continue_on_no_match: true,
            fail_fast: false,
            post_write: None,
            post_write_cwd: None,
        }
    }
}

/// Per-file outcome from [`ast_rename_batch`].
#[derive(Debug)]
pub struct AstRenameFileResult {
    pub path: PathBuf,
    pub result: Result<EditResult, EditError>,
}

/// Rename identifiers across many files with same-file serialization.
///
/// - Duplicate paths are processed **once** (first occurrence wins order).
/// - Never parallel-writes the same path; return order follows first-seen paths.
/// - Outer `Err` only for empty `old`/`new`. Per-file failures are in each
///   [`AstRenameFileResult::result`].
///
/// See #1495.
#[cfg(all(feature = "ast", any(feature = "cli", feature = "files")))]
pub fn ast_rename_batch(
    paths: &[&Path],
    old: &str,
    new: &str,
    opts: &AstRenameBatchOptions,
    guard: Option<&PathGuard>,
) -> anyhow::Result<Vec<AstRenameFileResult>> {
    if old.is_empty() || new.is_empty() {
        return Err(EditError::new(
            EditErrorKind::InvalidInput,
            "ast_rename_batch old/new must not be empty",
        )
        .into());
    }
    // Dedupe while preserving first-seen order.
    let mut seen = std::collections::HashSet::new();
    let mut unique_paths: Vec<&Path> = Vec::new();
    for p in paths {
        let key = p.to_path_buf();
        if seen.insert(key) {
            unique_paths.push(*p);
        }
    }

    let mut out = Vec::with_capacity(unique_paths.len());
    for path in unique_paths {
        match ast_rename(path, old, new, opts.mode, guard) {
            Ok(r) => {
                // Post-Apply format/lint hooks when configured (#1690).
                if let Err(e) = maybe_post_write(
                    r.applied,
                    path,
                    opts.post_write.as_ref(),
                    opts.post_write_cwd.as_deref(),
                    r.backup_session.as_deref(),
                ) {
                    let edit_err = e.downcast::<EditError>().unwrap_or_else(|e| {
                        EditError::new(EditErrorKind::FormatFailed, e.to_string())
                    });
                    out.push(AstRenameFileResult {
                        path: path.to_path_buf(),
                        result: Err(edit_err),
                    });
                    if opts.fail_fast {
                        break;
                    }
                    continue;
                }
                out.push(AstRenameFileResult {
                    path: path.to_path_buf(),
                    result: Ok(r),
                });
            }
            Err(e) => {
                let edit_err = e.downcast::<EditError>().unwrap_or_else(|e| {
                    EditError::new(EditErrorKind::OperationFailed, e.to_string())
                });
                let is_no_match = edit_err.kind == EditErrorKind::NoMatch;
                out.push(AstRenameFileResult {
                    path: path.to_path_buf(),
                    result: Err(edit_err),
                });
                // NoMatch: stop when continue_on_no_match is false.
                if is_no_match && !opts.continue_on_no_match {
                    break;
                }
                // Hard errors: stop when fail_fast is true.
                if !is_no_match && opts.fail_fast {
                    break;
                }
            }
        }
    }
    Ok(out)
}