patchloom 0.34.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
//! Extract a named symbol **into a separate source file** (`ast.extract_to_file`).
//!
//! Not to be confused with [`crate::ast::symbol_extract`] (tree-sitter visitors
//! that build the in-memory symbol list).

use super::Language;
use super::symbols::{
    SymbolKind, extract_symbol_text, find_symbol, full_symbol_span, try_extract_symbols,
};

/// Result of an extract-to-file operation.
#[derive(Debug)]
pub struct ExtractResult {
    /// The source file content after extraction.
    pub source_content: String,
    /// The target file content (extracted code).
    pub target_content: String,
    /// Number of lines extracted.
    pub extracted_lines: usize,
}

/// Extract a named symbol from source and produce content for a new file.
///
/// When `unwrap` is true and the symbol is a module, the module's body
/// content is extracted (un-indented by one level) without the `mod name { }` wrapper.
pub fn extract_to_file(
    source: &str,
    symbol: &str,
    replacement: Option<&str>,
    unwrap: bool,
    prepend: Option<&str>,
    lang: Language,
) -> anyhow::Result<ExtractResult> {
    // Empty/whitespace prepend inserts junk blanks vs omitting the field.
    // Newline-only remains insert-a-blank-line (same as wrap preamble).
    if let Some(pre) = prepend
        && pre.trim().is_empty()
        && !pre.contains('\n')
        && !pre.contains('\r')
    {
        return Err(anyhow::Error::new(crate::exit::InvalidInputError {
            msg: "ast extract prepend must not be empty".into(),
        }));
    }

    let eol = crate::write::detect_eol(source);
    let symbols = match try_extract_symbols(source, lang) {
        Ok(s) => s,
        Err(crate::ast::ParseFailure::DeadlineExceeded) => {
            return Err(crate::exit::ParseTimeoutError {
                msg: format!("parse deadline exceeded for {lang}"),
            }
            .into());
        }
        Err(crate::ast::ParseFailure::NoGrammar) => {
            return Err(crate::exit::NoMatchError {
                msg: format!("symbol '{symbol}' not found"),
            }
            .into());
        }
    };
    let sym = find_symbol(&symbols, symbol).ok_or_else(|| {
        anyhow::Error::new(crate::exit::NoMatchError {
            msg: format!("symbol '{symbol}' not found"),
        })
    })?;

    let (full_start, full_end) = full_symbol_span(source, sym, lang);
    let lines: Vec<&str> = crate::ops::file::text_lines(source).collect();
    let start_0 = full_start.saturating_sub(1);
    let end_0 = full_end.min(lines.len());

    // Extract the symbol's content for the target file
    let target_body = if unwrap && sym.kind == SymbolKind::Module {
        unwrap_module_body(&lines, sym.start_line.saturating_sub(1), end_0, eol)
    } else {
        let text = extract_symbol_text(source, sym, lang);
        text.to_string()
    };

    let extracted_lines = target_body.lines().count();

    // Build target content
    let mut target_content = String::new();
    if let Some(pre) = prepend {
        target_content.push_str(pre);
        if !pre.ends_with('\n') {
            target_content.push_str(eol);
        }
        target_content.push_str(eol);
    }
    target_content.push_str(&target_body);
    if !target_content.ends_with('\n') {
        target_content.push_str(eol);
    }

    // Build source content with the symbol removed or replaced
    let mut source_lines: Vec<String> = lines.iter().map(|l| l.to_string()).collect();
    let mut remove_end = end_0;
    // Also remove trailing blank line
    if remove_end < source_lines.len() && source_lines[remove_end].trim().is_empty() {
        remove_end += 1;
    }
    source_lines.drain(start_0..remove_end.min(source_lines.len()));

    if let Some(repl) = replacement {
        // Insert replacement text at the position
        let repl_lines: Vec<String> = repl.lines().map(String::from).collect();
        for (i, line) in repl_lines.iter().enumerate() {
            source_lines.insert(start_0 + i, line.clone());
        }
    }

    let mut source_content = source_lines.join(eol);
    if source.ends_with('\n') && !source_content.ends_with('\n') {
        source_content.push_str(eol);
    }

    Ok(ExtractResult {
        source_content,
        target_content,
        extracted_lines,
    })
}

/// Extract the body of a module, removing the wrapper and un-indenting.
fn unwrap_module_body(lines: &[&str], sym_start_0: usize, sym_end_0: usize, eol: &str) -> String {
    // Find the opening brace line
    let mut body_start = sym_start_0;
    let mut brace_line_tail: Option<&str> = None;
    for (i, line) in lines.iter().enumerate().take(sym_end_0).skip(sym_start_0) {
        let trimmed = line.trim();
        if trimmed.contains('{') {
            // Check if there is code after the `{` on the same line.
            if let Some(open) = line.find('{') {
                let after = line[open + 1..].trim();
                // Ignore if the rest is just `}` or empty (handled below).
                if !after.is_empty() && !after.starts_with('}') {
                    brace_line_tail = Some(line[open + 1..].trim_end());
                }
            }
            body_start = i + 1;
            break;
        }
    }

    // The closing brace is on the last line
    let body_end = if sym_end_0 > 0 {
        sym_end_0 - 1
    } else {
        sym_end_0
    };

    if body_start >= body_end {
        // Opening and closing braces may be on the same line (e.g.
        // `mod foo { fn bar() {} }`).  Extract text between braces.
        if body_start > 0 {
            let brace_line = lines[body_start - 1];
            if let Some(open) = brace_line.find('{') {
                let after_open = &brace_line[open + 1..];
                if let Some(close) = after_open.rfind('}') {
                    let inner = after_open[..close].trim();
                    if !inner.is_empty() {
                        return inner.to_string();
                    }
                }
            }
        }
        return String::new();
    }

    let body_lines = &lines[body_start..body_end];

    // Find common indentation, counted in characters so that multi-byte
    // whitespace indents stay comparable (#2377).
    let min_indent = body_lines
        .iter()
        .filter(|l| !l.trim().is_empty())
        .map(|l| crate::write::indent_char_count(l))
        .min()
        .unwrap_or(0);

    // Un-indent
    let mut parts: Vec<String> = Vec::new();

    // Prepend any code that was on the same line as the opening `{`.
    if let Some(tail) = brace_line_tail {
        let trimmed = tail.trim();
        // Strip trailing `}` if the closing brace is also on this tail
        // (already handled by the single-line branch above, but be safe).
        parts.push(trimmed.to_string());
    }

    parts.extend(body_lines.iter().map(|line| {
        if line.trim().is_empty() {
            String::new()
        } else {
            // `indent_strip_offset` clamps to the line's own indent, so a line
            // shorter than `min_indent` needs no separate guard.
            line[crate::write::indent_strip_offset(line, min_indent)..].to_string()
        }
    }));

    parts.join(eol)
}

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

    /// #2377: the un-indent step measured the common indent in bytes, so a
    /// body mixing ASCII and multi-byte whitespace sliced mid-character.
    #[test]
    fn extract_body_with_wide_whitespace_indent_does_not_panic() {
        let source = "mod tests {\n  fn a() {}\n\u{3000}fn b() {}\n}\n";
        let result = extract_to_file(source, "tests", None, false, None, Language::Rust).unwrap();
        assert!(result.target_content.contains("fn a() {}"));
        assert!(result.target_content.contains("fn b() {}"));
    }

    #[test]
    fn extract_body_shorter_than_common_indent_is_kept() {
        // The blank-ish line must not be sliced past its own length.
        let source = "mod tests {\n    fn a() {}\n\n    fn b() {}\n}\n";
        let result = extract_to_file(source, "tests", None, false, None, Language::Rust).unwrap();
        assert!(result.target_content.contains("fn a() {}"));
        assert!(result.target_content.contains("fn b() {}"));
    }

    #[test]
    fn extract_basic_function() {
        let source = "fn foo() {\n    42\n}\n\nfn bar() {}\n";
        let result = extract_to_file(source, "foo", None, false, None, Language::Rust).unwrap();
        assert!(!result.source_content.contains("fn foo"));
        assert!(result.source_content.contains("fn bar"));
        assert!(result.target_content.contains("fn foo()"));
        assert!(
            result.target_content.contains("    42"),
            "target should contain the function body: {}",
            result.target_content
        );
    }

    #[test]
    fn extract_with_replacement() {
        let source = "mod tests {\n    fn test_a() {}\n}\n\nfn other() {}\n";
        let replacement = "#[path = \"tests.rs\"]\nmod tests;";
        let result = extract_to_file(
            source,
            "tests",
            Some(replacement),
            true,
            None,
            Language::Rust,
        )
        .unwrap();
        assert!(result.source_content.contains("#[path = \"tests.rs\"]"));
        assert!(result.source_content.contains("mod tests;"));
        assert!(!result.source_content.contains("fn test_a"));
    }

    #[test]
    fn extract_unwrap_module() {
        let source = "mod tests {\n    fn test_a() {\n        assert!(true);\n    }\n}\n";
        let result = extract_to_file(source, "tests", None, true, None, Language::Rust).unwrap();
        // Content should be un-indented
        assert!(result.target_content.contains("fn test_a()"));
        assert!(result.target_content.contains("    assert!(true);"));
        // Should NOT contain the mod wrapper
        assert!(!result.target_content.contains("mod tests"));
    }

    #[test]
    fn extract_with_prepend() {
        let source = "fn foo() {}\n\nfn bar() {}\n";
        let result = extract_to_file(
            source,
            "foo",
            None,
            false,
            Some("use super::*;"),
            Language::Rust,
        )
        .unwrap();
        assert!(result.target_content.starts_with("use super::*;"));
        assert!(result.target_content.contains("fn foo()"));
    }

    #[test]
    fn extract_no_unwrap() {
        let source = "mod tests {\n    fn test_a() {}\n}\n";
        let result = extract_to_file(source, "tests", None, false, None, Language::Rust).unwrap();
        // Full module text should be preserved
        assert!(result.target_content.contains("mod tests {"));
    }

    #[test]
    fn extract_symbol_not_found() {
        let source = "fn foo() {}\n";
        let result = extract_to_file(source, "nonexistent", None, false, None, Language::Rust);
        assert!(result.is_err(), "expected error, got Ok: {result:?}");
    }

    #[test]
    fn extract_preserves_attributes() {
        let source = "/// Doc comment.\n#[cfg(test)]\nmod tests {\n    fn t() {}\n}\n";
        let result = extract_to_file(source, "tests", None, false, None, Language::Rust).unwrap();
        assert!(result.target_content.contains("/// Doc comment."));
        assert!(result.target_content.contains("#[cfg(test)]"));
    }

    #[test]
    fn unwrap_module_with_trailing_comment_on_brace_line() {
        // Opening brace line has a trailing comment: `mod tests { // test module`
        // The brace detection must still find it
        let source =
            "mod tests { // test module\n    fn test_a() {\n        assert!(true);\n    }\n}\n";
        let result = extract_to_file(source, "tests", None, true, None, Language::Rust).unwrap();
        // The mod declaration line should NOT leak into the unwrapped body
        assert!(
            !result.target_content.contains("mod tests"),
            "mod declaration should not appear in unwrapped body: {}",
            result.target_content
        );
        assert!(result.target_content.contains("fn test_a()"));
    }

    #[test]
    fn extract_preserves_crlf_line_endings() {
        let source = "fn foo() {\r\n    42\r\n}\r\n\r\nfn bar() {}\r\n";
        let result = extract_to_file(source, "foo", None, false, None, Language::Rust).unwrap();
        // Source content should preserve CRLF
        let bytes = result.source_content.as_bytes();
        for (i, &b) in bytes.iter().enumerate() {
            if b == b'\n' {
                assert!(
                    i > 0 && bytes[i - 1] == b'\r',
                    "bare LF in source at byte {i}: {:?}",
                    result.source_content
                );
            }
        }
        // Target content should preserve CRLF
        let target_bytes = result.target_content.as_bytes();
        for (i, &b) in target_bytes.iter().enumerate() {
            if b == b'\n' {
                assert!(
                    i > 0 && target_bytes[i - 1] == b'\r',
                    "bare LF in target at byte {i}: {:?}",
                    result.target_content
                );
            }
        }
    }

    #[test]
    fn unwrap_single_line_module() {
        // Regression: single-line modules returned empty body because
        // body_start (line after `{`) >= body_end (line before `}`).
        let source = "mod foo { fn bar() {} }\n";
        let result = extract_to_file(source, "foo", None, true, None, Language::Rust).unwrap();
        assert!(
            result.target_content.contains("fn bar()"),
            "single-line module body should be extracted, got: {:?}",
            result.target_content
        );
    }

    #[test]
    fn unwrap_module_preserves_brace_line_content() {
        // Code after the opening `{` on the same line should be preserved.
        let source = "mod foo { use bar::*;\n    fn baz() {}\n}\n";
        let result = extract_to_file(source, "foo", None, true, None, Language::Rust).unwrap();
        assert!(
            result.target_content.contains("use bar::*;"),
            "content on the brace line should be preserved, got: {:?}",
            result.target_content
        );
        assert!(
            result.target_content.contains("fn baz()"),
            "body content should also be present, got: {:?}",
            result.target_content
        );
    }

    #[test]
    fn extract_empty_prepend_is_invalid_input() {
        let source = "fn foo() { let x = 1; }\n";
        for pre in ["", "   "] {
            let err = extract_to_file(source, "foo", None, false, Some(pre), Language::Rust)
                .expect_err("empty/whitespace prepend must be invalid_input");
            assert!(
                crate::exit::is_invalid_input(&err),
                "prepend {pre:?} must classify as invalid_input: {err}"
            );
            assert!(
                err.to_string().contains("must not be empty"),
                "message must say must not be empty: {err}"
            );
        }
    }

    #[test]
    fn extract_newline_prepend_is_ok() {
        let source = "fn foo() { let x = 1; }\n";
        let result = extract_to_file(source, "foo", None, false, Some("\n"), Language::Rust)
            .expect("newline-only prepend is insert-a-blank-line");
        assert!(
            result.target_content.starts_with('\n'),
            "newline prepend should insert a blank line: {:?}",
            result.target_content
        );
        assert!(result.target_content.contains("fn foo()"));
    }
}