patchloom 0.8.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
//! AST-aware symbol grouping: move symbols into named modules within a file.

use super::Language;
use super::symbols::{
    check_no_overlapping_spans, extract_symbol_text, extract_symbols, find_symbol, full_symbol_span,
};

/// Where to place a newly created module.
#[derive(Debug, Clone)]
pub enum GroupPosition {
    /// At the position of the first moved symbol (default).
    FirstSymbol,
    /// At the end of the file.
    End,
    /// After a named symbol.
    After(String),
}

/// Specification for a group operation.
#[derive(Debug)]
pub struct GroupSpec {
    pub module: String,
    pub symbols: Vec<String>,
    pub preamble: Option<String>,
    pub position: GroupPosition,
}

/// Result of a group operation.
#[derive(Debug)]
pub struct GroupResult {
    pub content: String,
    pub symbols_moved: usize,
    pub module_created: bool,
}

/// Group named symbols into a module within the same file.
pub fn group_symbols(
    source: &str,
    spec: &GroupSpec,
    lang: Language,
) -> anyhow::Result<GroupResult> {
    let eol = crate::write::detect_eol(source);
    let symbols = extract_symbols(source, lang);
    let lines: Vec<&str> = source.lines().collect();

    // Check if target module already exists
    let existing_mod = find_symbol(&symbols, &spec.module)
        .filter(|s| s.kind == super::symbols::SymbolKind::Module);
    let module_exists = existing_mod.is_some();

    // Find each symbol to move and collect their text + spans
    let mut to_move: Vec<(usize, usize, String)> = Vec::new(); // (start_0, end_0, text)
    let mut missing: Vec<&str> = Vec::new();

    for name in &spec.symbols {
        if let Some(sym) = find_symbol(&symbols, name) {
            // Check if already inside the target module
            if let Some(parent_mod) = &existing_mod
                && sym.start_line >= parent_mod.start_line
                && sym.end_line <= parent_mod.end_line
            {
                continue; // already inside target module
            }
            let (full_start, full_end) = full_symbol_span(source, sym, lang);
            let text = extract_symbol_text(source, sym, lang).to_string();
            to_move.push((
                full_start.saturating_sub(1),
                full_end.min(lines.len()),
                text,
            ));
        } else {
            missing.push(name);
        }
    }

    if !missing.is_empty() {
        anyhow::bail!("symbol(s) not found: {}", missing.join(", "));
    }

    if to_move.is_empty() {
        return Ok(GroupResult {
            content: source.to_string(),
            symbols_moved: 0,
            module_created: false,
        });
    }

    let symbols_moved = to_move.len();

    // Detect overlapping spans that would corrupt content during removal
    {
        let spans: Vec<(usize, usize)> = to_move.iter().map(|(s, e, _)| (*s, *e)).collect();
        let span_names: Vec<&str> = to_move
            .iter()
            .map(|(s, _, _)| {
                symbols
                    .iter()
                    .find(|sym| {
                        let (fs, _) = full_symbol_span(source, sym, lang);
                        fs.saturating_sub(1) == *s
                    })
                    .map(|sym| sym.name.as_str())
                    .unwrap_or("?")
            })
            .collect();
        check_no_overlapping_spans(&spans, &span_names)?;
    }

    // Sort spans by start position (descending) for safe removal
    to_move.sort_by_key(|b| std::cmp::Reverse(b.0));

    // Build the module content: indent each symbol by 4 spaces
    let mut module_body = String::new();
    if let Some(preamble) = &spec.preamble {
        module_body.push_str(&indent_text(preamble, "    ", eol));
        module_body.push_str(eol);
        module_body.push_str(eol);
    }

    // Collect texts in original order (we sorted descending for removal)
    let mut texts_in_order: Vec<(usize, &str)> = to_move
        .iter()
        .map(|(start, _, text)| (*start, text.as_str()))
        .collect();
    texts_in_order.sort_by_key(|(start, _)| *start);

    for (i, (_, text)) in texts_in_order.iter().enumerate() {
        if i > 0 {
            module_body.push_str(eol);
        }
        let trimmed = text.trim_end_matches(['\r', '\n']);
        module_body.push_str(&indent_text(trimmed, "    ", eol));
        module_body.push_str(eol);
    }

    // Remove symbols from source (in reverse order to preserve offsets)
    let mut modified_lines: Vec<String> = lines.iter().map(|l| l.to_string()).collect();
    for (start_0, end_0, _) in &to_move {
        // Remove the lines, also removing a trailing blank line if present
        let mut remove_end = *end_0;
        if remove_end < modified_lines.len() && modified_lines[remove_end].trim().is_empty() {
            remove_end += 1;
        }
        modified_lines.drain(*start_0..remove_end);
    }

    if module_exists {
        // Find the existing module's closing brace in modified_lines
        // Re-parse to find the module in modified source
        let modified_source = modified_lines.join(eol);
        let mod_symbols = extract_symbols(&modified_source, lang);
        if let Some(mod_sym) = find_symbol(&mod_symbols, &spec.module) {
            let close_line_0 = mod_sym.end_line.saturating_sub(1);
            // Insert before closing brace
            let mut result_lines: Vec<String> = Vec::new();
            let re_lines: Vec<&str> = modified_source.lines().collect();
            for (i, line) in re_lines.iter().enumerate() {
                if i == close_line_0 {
                    // Add a blank line before new content if previous line isn't blank
                    if i > 0 && !re_lines[i - 1].trim().is_empty() {
                        result_lines.push(String::new());
                    }
                    for body_line in module_body.lines() {
                        result_lines.push(body_line.to_string());
                    }
                }
                result_lines.push(line.to_string());
            }
            let mut result = result_lines.join(eol);
            if source.ends_with('\n') && !result.ends_with('\n') {
                result.push_str(eol);
            }
            return Ok(GroupResult {
                content: result,
                symbols_moved,
                module_created: false,
            });
        }
    }

    // Build the module block for a new module
    let module_block = format!("mod {} {{{eol}{}}}{eol}", spec.module, module_body);

    // Insert new module at the specified position
    let insert_idx = match &spec.position {
        GroupPosition::FirstSymbol => {
            // Position of the first moved symbol (in the original line indices)
            let first_start = texts_in_order.first().map(|(s, _)| *s).unwrap_or(0);
            // Map to modified_lines index: count how many lines were removed before this point
            let mut offset = 0usize;
            let mut spans_before: Vec<(usize, usize)> = to_move
                .iter()
                .map(|(s, e, _)| {
                    let mut end = *e;
                    if end < lines.len() && lines[end].trim().is_empty() {
                        end += 1;
                    }
                    (*s, end)
                })
                .collect();
            spans_before.sort_by_key(|(s, _)| *s);
            for (s, e) in &spans_before {
                if *s < first_start {
                    offset += e - s;
                }
            }
            first_start.saturating_sub(offset)
        }
        GroupPosition::End => modified_lines.len(),
        GroupPosition::After(sym_name) => {
            let modified_source = modified_lines.join(eol);
            let mod_symbols = extract_symbols(&modified_source, lang);
            if let Some(sym) = find_symbol(&mod_symbols, sym_name) {
                sym.end_line.min(modified_lines.len())
            } else {
                modified_lines.len()
            }
        }
    };

    let insert_idx = insert_idx.min(modified_lines.len());

    // Insert module block
    let block_lines: Vec<String> = module_block.lines().map(String::from).collect();
    // Add blank line before if needed
    if insert_idx > 0
        && insert_idx <= modified_lines.len()
        && !modified_lines[insert_idx - 1].trim().is_empty()
    {
        modified_lines.insert(insert_idx, String::new());
        let idx = insert_idx + 1;
        for (i, bl) in block_lines.iter().enumerate() {
            modified_lines.insert(idx + i, bl.clone());
        }
    } else {
        for (i, bl) in block_lines.iter().enumerate() {
            modified_lines.insert(insert_idx + i, bl.clone());
        }
    }

    let mut result = modified_lines.join(eol);
    if source.ends_with('\n') && !result.ends_with('\n') {
        result.push_str(eol);
    }
    if !source.ends_with('\n') && result.ends_with('\n') {
        result.truncate(result.len() - eol.len());
    }

    Ok(GroupResult {
        content: result,
        symbols_moved,
        module_created: true,
    })
}

fn indent_text(text: &str, indent: &str, eol: &str) -> String {
    text.lines()
        .map(|line| {
            if line.trim().is_empty() {
                String::new()
            } else {
                format!("{indent}{line}")
            }
        })
        .collect::<Vec<_>>()
        .join(eol)
}

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

    #[test]
    fn group_basic() {
        let source = "fn foo() {}\n\nfn bar() {}\n\nfn baz() {}\n";
        let spec = GroupSpec {
            module: "helpers".into(),
            symbols: vec!["foo".into(), "bar".into()],
            preamble: None,
            position: GroupPosition::FirstSymbol,
        };
        let result = group_symbols(source, &spec, Language::Rust).unwrap();
        assert!(result.content.contains("mod helpers {"));
        assert!(result.content.contains("    fn foo()"));
        assert!(result.content.contains("    fn bar()"));
        assert!(result.content.contains("fn baz()")); // not grouped
        assert_eq!(result.symbols_moved, 2);
        assert!(result.module_created);
    }

    #[test]
    fn group_with_preamble() {
        let source = "fn test_a() {}\n\nfn test_b() {}\n";
        let spec = GroupSpec {
            module: "tests".into(),
            symbols: vec!["test_a".into(), "test_b".into()],
            preamble: Some("use super::*;".into()),
            position: GroupPosition::FirstSymbol,
        };
        let result = group_symbols(source, &spec, Language::Rust).unwrap();
        assert!(result.content.contains("use super::*;"));
        assert!(result.content.contains("mod tests {"));
    }

    #[test]
    fn group_symbol_not_found() {
        let source = "fn foo() {}\n";
        let spec = GroupSpec {
            module: "m".into(),
            symbols: vec!["nonexistent".into()],
            preamble: None,
            position: GroupPosition::FirstSymbol,
        };
        let result = group_symbols(source, &spec, Language::Rust);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not found"));
    }

    #[test]
    fn group_preserves_attributes() {
        let source = "#[test]\nfn test_a() {}\n\nfn helper() {}\n";
        let spec = GroupSpec {
            module: "tests".into(),
            symbols: vec!["test_a".into()],
            preamble: None,
            position: GroupPosition::FirstSymbol,
        };
        let result = group_symbols(source, &spec, Language::Rust).unwrap();
        assert!(result.content.contains("#[test]"));
        assert!(result.content.contains("fn test_a()"));
    }

    #[test]
    fn group_position_end() {
        let source = "fn alpha() {}\n\nfn beta() {}\n";
        let spec = GroupSpec {
            module: "m".into(),
            symbols: vec!["alpha".into()],
            preamble: None,
            position: GroupPosition::End,
        };
        let result = group_symbols(source, &spec, Language::Rust).unwrap();
        // Module should be after beta
        let beta_pos = result.content.find("fn beta").unwrap();
        let mod_pos = result.content.find("mod m {").unwrap();
        assert!(mod_pos > beta_pos);
    }

    #[test]
    fn group_append_to_existing_module() {
        let source = "mod helpers {\n    fn existing() {}\n}\n\nfn new_helper() {}\n";
        let spec = GroupSpec {
            module: "helpers".into(),
            symbols: vec!["new_helper".into()],
            preamble: None,
            position: GroupPosition::FirstSymbol,
        };
        let result = group_symbols(source, &spec, Language::Rust).unwrap();
        assert!(result.content.contains("fn existing()"));
        assert!(result.content.contains("fn new_helper()"));
        assert!(!result.module_created);
        // Should only have one "mod helpers"
        assert_eq!(result.content.matches("mod helpers").count(), 1);
    }

    #[test]
    fn group_no_op_already_inside() {
        let source = "mod m {\n    fn foo() {}\n}\n";
        let spec = GroupSpec {
            module: "m".into(),
            symbols: vec!["foo".into()],
            preamble: None,
            position: GroupPosition::FirstSymbol,
        };
        let result = group_symbols(source, &spec, Language::Rust).unwrap();
        assert_eq!(result.symbols_moved, 0);
    }

    #[test]
    fn group_position_after_symbol() {
        let source = "fn anchor() {}\n\nfn target() {}\n\nfn trailing() {}\n";
        let spec = GroupSpec {
            module: "m".into(),
            symbols: vec!["target".into()],
            preamble: None,
            position: GroupPosition::After("anchor".into()),
        };
        let result = group_symbols(source, &spec, Language::Rust).unwrap();
        let anchor_pos = result.content.find("fn anchor").unwrap();
        let mod_pos = result.content.find("mod m {").unwrap();
        let trailing_pos = result.content.find("fn trailing").unwrap();
        assert!(anchor_pos < mod_pos);
        assert!(mod_pos < trailing_pos);
        assert!(result.module_created);
        assert_eq!(result.symbols_moved, 1);
    }

    #[test]
    fn group_preserves_crlf_line_endings() {
        let source = "fn foo() {}\r\n\r\nfn bar() {}\r\n\r\nfn baz() {}\r\n";
        let spec = GroupSpec {
            module: "helpers".into(),
            symbols: vec!["foo".into(), "bar".into()],
            preamble: None,
            position: GroupPosition::FirstSymbol,
        };
        let result = group_symbols(source, &spec, Language::Rust).unwrap();
        let bytes = result.content.as_bytes();
        for (i, &b) in bytes.iter().enumerate() {
            if b == b'\n' {
                assert!(
                    i > 0 && bytes[i - 1] == b'\r',
                    "bare LF at byte {i} in: {:?}",
                    result.content
                );
            }
        }
        // Verify no stray \r\r sequences (the bug trim_end_matches('\n') caused)
        assert!(
            !result.content.contains("\r\r"),
            "stray \\r\\r corruption in: {:?}",
            result.content
        );
    }

    #[test]
    fn group_non_overlapping_adjacent_symbols() {
        // Adjacent symbols with proper separation should group fine.
        let source = "/// Doc A\nfn alpha() {}\n\n/// Doc B\nfn beta() {}\n\nfn gamma() {}\n";
        let spec = GroupSpec {
            module: "m".into(),
            symbols: vec!["alpha".into(), "beta".into()],
            preamble: None,
            position: GroupPosition::FirstSymbol,
        };
        let result = group_symbols(source, &spec, Language::Rust);
        let r = result.expect("group_symbols should succeed");
        assert_eq!(r.symbols_moved, 2);
        assert!(r.content.contains("mod m {"));
    }

    #[test]
    fn group_creates_new_module_when_name_matches_function() {
        // If a function "helpers" exists, group should create a new `mod helpers`
        // rather than inserting content inside the function body.
        let source = "fn helpers() {}\n\nfn target() {}\n";
        let spec = GroupSpec {
            module: "helpers".into(),
            symbols: vec!["target".into()],
            preamble: None,
            position: GroupPosition::End,
        };
        let result = group_symbols(source, &spec, Language::Rust).unwrap();
        assert!(result.module_created);
        assert!(result.content.contains("mod helpers {"));
        assert!(result.content.contains("fn helpers()")); // function preserved
        assert_eq!(result.symbols_moved, 1);
    }
}