patchloom 0.33.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
//! Rewrite consumer import/use statements after `ast.move` / `ast.extract_to_file`.
//!
//! Detection uses [`super::imports::list_imports`]. This module only rewrites
//! already-found import blocks (Rust `use` first).

use super::Language;
use super::imports::list_imports;

/// One symbol that moved from `old_module` to `new_module`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SymbolMove {
    /// Identifier as it appears in `use path::Name` (not the `as` alias).
    pub name: String,
    /// Module path consumers currently import from (e.g. `crate::old_mod`).
    pub old_module: String,
    /// Module path consumers should import from (e.g. `crate::new_mod`).
    pub new_module: String,
}

/// Rewrite import blocks in `source` for the given symbol moves.
///
/// Returns `None` when no import text changes (no matching consumer, glob
/// left as-is, or language not rewritten).
pub fn rewrite_imports_in_source(
    source: &str,
    lang: Language,
    moves: &[SymbolMove],
) -> Option<String> {
    if moves.is_empty() || !matches!(lang, Language::Rust) {
        return None;
    }
    let imports = list_imports(source, lang);
    if imports.is_empty() {
        return None;
    }

    let lines: Vec<&str> = crate::ops::file::text_lines(source).collect();
    let eol = crate::write::detect_eol(source);
    let mut replacements: Vec<(usize, usize, String)> = Vec::new();
    for import in &imports {
        let Some(rewritten) = rewrite_rust_use_block(&import.text, moves, eol) else {
            continue;
        };
        let start = import.line.saturating_sub(1);
        if start >= lines.len() {
            continue;
        }
        let count = import.text.lines().count().max(1);
        let end = (start + count).min(lines.len());
        replacements.push((start, end, rewritten));
    }
    if replacements.is_empty() {
        return None;
    }
    Some(apply_line_replacements(source, &lines, &replacements))
}

fn apply_line_replacements(
    source: &str,
    lines: &[&str],
    replacements: &[(usize, usize, String)],
) -> String {
    let eol = crate::write::detect_eol(source);
    let mut out = String::new();
    let mut i = 0usize;
    let mut r = 0usize;
    while i < lines.len() {
        if r < replacements.len() && i == replacements[r].0 {
            let (_, end, ref text) = replacements[r];
            out.push_str(text);
            if !text.is_empty() && !text.ends_with('\n') {
                // Keep a trailing newline after the replacement unless this
                // block was the last line of a file that had no final newline.
                if end < lines.len() || source.ends_with('\n') {
                    out.push_str(eol);
                }
            }
            i = end;
            r += 1;
        } else {
            out.push_str(lines[i]);
            if i + 1 < lines.len() || source.ends_with('\n') {
                out.push_str(eol);
            }
            i += 1;
        }
    }
    out
}

/// Rewrite one Rust `use` block (single- or multi-line text from `list_imports`).
fn rewrite_rust_use_block(text: &str, moves: &[SymbolMove], eol: &str) -> Option<String> {
    let compact = flatten_import_text(text);
    let (vis, body) = rust_use_prefix_and_body(&compact)?;
    if body.ends_with("::*") || body.contains("::*") && !body.contains('{') {
        return None;
    }
    if let Some(brace) = body.find('{') {
        let close = body.rfind('}')?;
        if close < brace {
            return None;
        }
        let path = body[..brace].trim().trim_end_matches(':').trim();
        let inner = &body[brace + 1..close];
        if inner.split(',').any(is_glob_item) {
            return None;
        }
        return rewrite_grouped_use(vis, path, inner, moves, eol);
    }
    rewrite_simple_use(vis, body, moves)
}

fn flatten_import_text(text: &str) -> String {
    text.lines()
        .map(str::trim)
        .filter(|l| !l.is_empty())
        .collect::<Vec<_>>()
        .join(" ")
}

fn rust_use_prefix_and_body(text: &str) -> Option<(&str, &str)> {
    let trimmed = text.trim();
    let vis = if trimmed.starts_with("pub(crate) use ") {
        "pub(crate) use "
    } else if trimmed.starts_with("pub(super) use ") {
        "pub(super) use "
    } else if trimmed.starts_with("pub use ") {
        "pub use "
    } else if trimmed.starts_with("use ") {
        "use "
    } else {
        return None;
    };
    let rest = trimmed.get(vis.len()..)?.trim();
    let rest = rest.strip_suffix(';').unwrap_or(rest).trim();
    Some((vis, rest))
}

fn rewrite_simple_use(vis: &str, body: &str, moves: &[SymbolMove]) -> Option<String> {
    for mv in moves {
        let old = normalize_module(&mv.old_module);
        let Some(tail) = module_tail(body, &old) else {
            continue;
        };
        if tail.is_empty() || tail == "*" || tail.contains("::") {
            continue;
        }
        let (name, _) = split_name_alias(tail);
        if name != mv.name {
            continue;
        }
        let new = normalize_module(&mv.new_module);
        return Some(format!("{vis}{new}::{tail};"));
    }
    None
}

fn rewrite_grouped_use(
    vis: &str,
    path: &str,
    inner: &str,
    moves: &[SymbolMove],
    eol: &str,
) -> Option<String> {
    let path = normalize_module(path);
    let items: Vec<&str> = inner
        .split(',')
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .collect();
    if items.is_empty() {
        return None;
    }

    let mut remaining: Vec<String> = Vec::new();
    let mut moved: Vec<(String, String)> = Vec::new();
    for item in &items {
        match match_grouped_item(&path, item, moves) {
            Some((dest, dest_item)) => moved.push((dest, dest_item)),
            None => remaining.push((*item).to_string()),
        }
    }
    if moved.is_empty() {
        return None;
    }

    let mut parts: Vec<String> = Vec::new();
    if !remaining.is_empty() {
        let refs: Vec<&str> = remaining.iter().map(String::as_str).collect();
        parts.push(format_use_items(vis, &path, &refs));
    }
    // Group moved items by destination module, preserving encounter order.
    let mut dest_items: Vec<(String, Vec<String>)> = Vec::new();
    for (dest, dest_item) in moved {
        if let Some((_, bucket)) = dest_items.iter_mut().find(|(d, _)| *d == dest) {
            bucket.push(dest_item);
        } else {
            dest_items.push((dest, vec![dest_item]));
        }
    }
    for (dest, names) in dest_items {
        let refs: Vec<&str> = names.iter().map(String::as_str).collect();
        parts.push(format_use_items(vis, &dest, &refs));
    }
    Some(parts.join(eol))
}

/// Match a grouped item against moves.
///
/// rustfmt crate-root form `use crate::{old_mod::helper}` treats
/// `{group}::{item_prefix}` as the module and the last path segment as
/// the symbol.
fn match_grouped_item(
    group_path: &str,
    item: &str,
    moves: &[SymbolMove],
) -> Option<(String, String)> {
    let (name, alias) = split_name_alias(item);
    if name.contains('{') {
        return None;
    }
    let (item_mod, symbol) = match name.rfind("::") {
        Some(i) => (&name[..i], &name[i + 2..]),
        None => ("", name),
    };
    if symbol.is_empty() || symbol == "*" {
        return None;
    }
    let full_mod = if item_mod.is_empty() {
        group_path.to_string()
    } else if group_path.is_empty() {
        normalize_module(item_mod)
    } else {
        format!("{}::{}", group_path, normalize_module(item_mod))
    };
    for mv in moves {
        if mv.name == symbol && normalize_module(&mv.old_module) == full_mod {
            let dest_item = match alias {
                Some(a) => format!("{symbol} as {a}"),
                None => symbol.to_string(),
            };
            return Some((normalize_module(&mv.new_module), dest_item));
        }
    }
    None
}

fn format_use_items(vis: &str, module: &str, items: &[&str]) -> String {
    if items.len() == 1 {
        let item = items[0];
        let (name, alias) = split_name_alias(item);
        if name == "self" {
            return match alias {
                Some(a) => format!("{vis}{module} as {a};"),
                None => format!("{vis}{module};"),
            };
        }
        format!("{vis}{module}::{item};")
    } else {
        format!("{vis}{module}::{{{}}};", items.join(", "))
    }
}

fn normalize_module(module: &str) -> String {
    module.trim().trim_end_matches(':').trim().to_string()
}

fn module_tail<'a>(path: &'a str, module: &str) -> Option<&'a str> {
    if path == module {
        return Some("");
    }
    path.strip_prefix(module)
        .and_then(|rest| rest.strip_prefix("::"))
}

fn split_name_alias(item: &str) -> (&str, Option<&str>) {
    let item = strip_line_comment(item);
    let mut parts = item.split_whitespace();
    let name = parts.next().unwrap_or("");
    let alias = match (parts.next(), parts.next()) {
        (Some(kw), Some(alias)) if kw.eq_ignore_ascii_case("as") => Some(alias),
        _ => None,
    };
    (name, alias)
}

fn strip_line_comment(s: &str) -> &str {
    match s.find("//") {
        Some(i) => s[..i].trim(),
        None => s.trim(),
    }
}

fn is_glob_item(part: &str) -> bool {
    let name = split_name_alias(part).0;
    name == "*"
}

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

    fn rust_move(name: &str) -> SymbolMove {
        SymbolMove {
            name: name.into(),
            old_module: "crate::old_mod".into(),
            new_module: "crate::new_mod".into(),
        }
    }

    #[test]
    fn rewrite_simple_use_line() {
        let source = "use crate::old_mod::helper;\n\nfn main() {}\n";
        let out = rewrite_imports_in_source(source, Language::Rust, &[rust_move("helper")])
            .expect("should rewrite simple use");
        assert_eq!(out, "use crate::new_mod::helper;\n\nfn main() {}\n");
    }

    #[test]
    fn rewrite_grouped_partial_move() {
        let source = "use crate::old_mod::{alpha, beta, gamma};\n\nfn main() {}\n";
        let moves = [rust_move("alpha"), rust_move("gamma")];
        let out = rewrite_imports_in_source(source, Language::Rust, &moves)
            .expect("should rewrite grouped use");
        assert_eq!(
            out,
            "use crate::old_mod::beta;\nuse crate::new_mod::{alpha, gamma};\n\nfn main() {}\n"
        );
    }

    #[test]
    fn rewrite_preserves_visibility_prefix() {
        let source = "pub use crate::old_mod::helper;\npub(crate) use crate::old_mod::helper;\n";
        let out = rewrite_imports_in_source(source, Language::Rust, &[rust_move("helper")])
            .expect("should rewrite vis use");
        assert_eq!(
            out,
            "pub use crate::new_mod::helper;\npub(crate) use crate::new_mod::helper;\n"
        );
    }

    #[test]
    fn rewrite_leaves_indented_use_unchanged() {
        let source = "fn main() {\n    use crate::old_mod::helper;\n}\n";
        let out = rewrite_imports_in_source(source, Language::Rust, &[rust_move("helper")]);
        assert_eq!(out, None);
    }

    #[test]
    fn rewrite_no_matching_consumer_is_unchanged() {
        let source = "use crate::other::foo;\n\nfn main() {}\n";
        let out = rewrite_imports_in_source(source, Language::Rust, &[rust_move("helper")]);
        assert_eq!(out, None);
    }

    #[test]
    fn rewrite_keeps_name_as_alias() {
        let source = "use crate::old_mod::Name as Alias;\n\nfn main() {}\n";
        let out = rewrite_imports_in_source(source, Language::Rust, &[rust_move("Name")])
            .expect("should rewrite aliased use");
        assert_eq!(out, "use crate::new_mod::Name as Alias;\n\nfn main() {}\n");
    }

    #[test]
    fn rewrite_leaves_glob_use_unchanged() {
        let source = "use crate::old_mod::*;\n\nfn main() {}\n";
        let out = rewrite_imports_in_source(source, Language::Rust, &[rust_move("helper")]);
        assert_eq!(out, None);
    }

    #[test]
    fn rewrite_grouped_self_leftover_is_bare_module_use() {
        let source = "use crate::old_mod::{self, helper};\n\nfn main() {}\n";
        let out = rewrite_imports_in_source(source, Language::Rust, &[rust_move("helper")])
            .expect("should rewrite grouped self leftover");
        assert_eq!(
            out,
            "use crate::old_mod;\nuse crate::new_mod::helper;\n\nfn main() {}\n"
        );
    }

    #[test]
    fn rewrite_grouped_split_preserves_crlf() {
        let source = "use crate::old_mod::{alpha, beta, gamma};\r\n\r\nfn main() {}\r\n";
        let moves = [rust_move("alpha"), rust_move("gamma")];
        let out = rewrite_imports_in_source(source, Language::Rust, &moves)
            .expect("should rewrite grouped use");
        assert_eq!(
            out,
            "use crate::old_mod::beta;\r\nuse crate::new_mod::{alpha, gamma};\r\n\r\nfn main() {}\r\n"
        );
        assert!(!out.contains('\n') || out.contains("\r\n"));
        assert!(
            !out.replace("\r\n", "").contains('\n'),
            "mixed endings: {out:?}"
        );
    }

    #[test]
    fn rewrite_rustfmt_crate_root_grouped_path_item() {
        let source = "use crate::{old_mod::helper, other::Thing};\n\nfn main() {}\n";
        let out = rewrite_imports_in_source(source, Language::Rust, &[rust_move("helper")])
            .expect("should rewrite rustfmt crate-root item");
        assert_eq!(
            out,
            "use crate::other::Thing;\nuse crate::new_mod::helper;\n\nfn main() {}\n"
        );
    }
}