tersify 0.5.0

Universal LLM context compressor — pipe anything, get token-optimized output
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
//! AST-based compression: extract function/method signatures and stub bodies.
//!
//! Produces a skeleton view of source code — all function/method signatures are
//! preserved, all implementations replaced with `{ /* ... */ }`.
//! Ideal when passing large files as context and only the API surface matters.

use super::ast_ts;
use super::util::{brace_counts, collapse_blank_lines, leading_ws};
use crate::detect::Language;

/// Compress by stubbing all function/method bodies.
///
/// Signatures, types, imports, module declarations, and trait definitions are
/// preserved; only the implementation code inside function bodies is removed.
///
/// # Examples
///
/// ```
/// use tersify::{compress, detect};
/// use std::path::Path;
/// let src = "pub fn add(a: i32, b: i32) -> i32 {\n    a + b\n}\n";
/// let ct = detect::detect_for_path(Path::new("add.rs"), src);
/// let out = compress::compress_with(src, &ct, &compress::CompressOptions { ast: true, ..Default::default() }).unwrap();
/// assert!(out.contains("pub fn add(a: i32, b: i32) -> i32"));
/// assert!(!out.contains("a + b"));
/// ```
pub fn stub_bodies(input: &str, lang: &Language) -> String {
    // Prefer the precise tree-sitter path; fall back to the heuristic parser.
    if let Some(result) = ast_ts::try_stub_bodies(input, lang) {
        return result;
    }
    match lang {
        Language::Python => stub_python(input),
        Language::Ruby => stub_ruby(input),
        _ => stub_cstyle(input),
    }
}

// ── C-style languages (Rust, Go, JS, TS, Generic) ───────────────────────────

fn stub_cstyle(input: &str) -> String {
    let lines: Vec<&str> = input.lines().collect();
    let mut out: Vec<String> = Vec::new();
    let mut i = 0;
    let mut depth: i32 = 0;

    while i < lines.len() {
        let line = lines[i];
        let trimmed = line.trim();
        let (opens, closes) = brace_counts(line);

        // Only stub bodies at top level (depth 0) or one level deep (impl/class methods at depth 1)
        if (depth == 0 || depth == 1) && is_fn_start(trimmed) {
            // Collect the full signature — may span multiple lines until we find `{`
            let mut sig_lines: Vec<&str> = vec![line];
            let mut total_opens = opens;
            let mut total_closes = closes;
            let mut j = i + 1;

            while total_opens == total_closes && j < lines.len() {
                sig_lines.push(lines[j]);
                let (o, c) = brace_counts(lines[j]);
                total_opens += o;
                total_closes += c;
                j += 1;
            }

            if total_opens > total_closes {
                // Body found — emit signature stub on one line
                let indent = leading_ws(line);
                let sig = build_sig(&sig_lines);
                out.push(format!("{}{} {{ /* ... */ }}", indent, sig));

                // Skip the body until depth returns to the level before the opening brace
                let target_depth = depth;
                depth += total_opens - total_closes;
                i = j;
                while i < lines.len() && depth > target_depth {
                    let (o, c) = brace_counts(lines[i]);
                    depth += o - c;
                    i += 1;
                }
            } else {
                // No body found (trait method declaration, extern fn, etc.) — keep as-is
                for &l in &sig_lines {
                    out.push(l.to_string());
                }
                depth += total_opens - total_closes;
                i = j;
            }
            continue;
        }

        depth += opens - closes;
        out.push(line.to_string());
        i += 1;
    }

    collapse_blank_lines(&out.join("\n"))
}

/// Detect whether `trimmed` is the start of a function or method declaration.
fn is_fn_start(trimmed: &str) -> bool {
    let s = strip_modifiers(trimmed);

    // Rust / generic `fn`
    if s.starts_with("fn ") {
        return true;
    }
    // Go / Swift
    if s.starts_with("func ") {
        return true;
    }
    // Kotlin
    if s.starts_with("fun ") {
        return true;
    }
    // JavaScript / TypeScript — standalone or exported
    if s.starts_with("function ") || s.starts_with("async function ") {
        return true;
    }
    if s.starts_with("export ") {
        let after = s.strip_prefix("export ").unwrap_or(s);
        let after = after.strip_prefix("default ").unwrap_or(after);
        let after = after.strip_prefix("async ").unwrap_or(after);
        if after.starts_with("function ") {
            return true;
        }
    }
    // Java methods: after visibility/static/final modifiers → `ReturnType name(`
    if is_java_method(s) {
        return true;
    }

    false
}

/// Heuristic: after stripping modifiers, does this look like a Java method declaration?
///
/// Pattern: two or more words before `(`, where the last word (method name) is a
/// lowercase-starting identifier that isn't a control-flow keyword.
fn is_java_method(s: &str) -> bool {
    if !s.contains('(') {
        return false;
    }
    let before_paren = match s.find('(') {
        Some(pos) => s[..pos].trim_end(),
        None => return false,
    };
    let parts: Vec<&str> = before_paren.split_whitespace().collect();
    if parts.len() < 2 {
        return false; // needs at least ReturnType + methodName
    }
    let name = parts.last().unwrap_or(&"");
    if !name.chars().next().is_some_and(|c| c.is_ascii_lowercase()) {
        return false; // method names start lowercase; constructors handled separately
    }
    ![
        "if", "while", "for", "switch", "catch", "try", "new", "return", "throw", "else",
    ]
    .contains(name)
}

/// Strip language visibility/async/unsafe modifiers to expose the core keyword.
fn strip_modifiers(s: &str) -> &str {
    let mut cur = s;
    loop {
        let prev = cur;
        for prefix in &[
            // Rust
            "pub(crate) ",
            "pub(super) ",
            "pub ",
            "async ",
            "unsafe ",
            "const ",
            "extern ",
            // Java
            "public ",
            "private ",
            "protected ",
            "static ",
            "final ",
            "abstract ",
            "synchronized ",
            "native ",
            "strictfp ",
            // Kotlin
            "override ",
            "open ",
            "inline ",
            "tailrec ",
            "suspend ",
            "operator ",
            "infix ",
            "data ",
            "sealed ",
            // Swift
            "internal ",
            "fileprivate ",
            "mutating ",
        ] {
            if let Some(rest) = cur.strip_prefix(prefix) {
                cur = rest;
                break;
            }
        }
        // Handle `extern "C" fn` — skip the ABI string
        if cur.starts_with('"')
            && let Some(end) = cur.find("\" ")
        {
            cur = &cur[end + 2..];
            continue;
        }
        if cur == prev {
            break;
        }
    }
    cur
}

/// Build a clean single-line signature from possibly multi-line input.
fn build_sig(lines: &[&str]) -> String {
    // Join and normalise whitespace
    let joined = lines.join(" ");
    let normalized: String = joined.split_whitespace().collect::<Vec<_>>().join(" ");
    // Strip everything from the first unquoted `{` onwards
    if let Some(pos) = first_brace_pos(&normalized) {
        normalized[..pos].trim_end().to_string()
    } else {
        normalized.trim().to_string()
    }
}

/// Find the byte position of the first `{` not inside a string.
fn first_brace_pos(s: &str) -> Option<usize> {
    let chars: Vec<char> = s.chars().collect();
    let mut i = 0;
    let mut byte_pos = 0;

    while i < chars.len() {
        let c = chars[i];
        if c == '"' || c == '\'' || c == '`' {
            let q = c;
            let char_len = c.len_utf8();
            byte_pos += char_len;
            i += 1;
            while i < chars.len() && chars[i] != q {
                if chars[i] == '\\' {
                    byte_pos += chars[i].len_utf8();
                    i += 1;
                }
                byte_pos += chars[i].len_utf8();
                i += 1;
            }
            if i < chars.len() {
                byte_pos += chars[i].len_utf8();
                i += 1; // closing quote
            }
            continue;
        }
        if c == '{' {
            return Some(byte_pos);
        }
        byte_pos += c.len_utf8();
        i += 1;
    }
    None
}

// ── Ruby ─────────────────────────────────────────────────────────────────────

fn stub_ruby(input: &str) -> String {
    let lines: Vec<&str> = input.lines().collect();
    let mut out: Vec<String> = Vec::new();
    let mut i = 0;

    while i < lines.len() {
        let line = lines[i];
        let trimmed = line.trim();

        if trimmed.starts_with("def ") {
            let indent = leading_ws(line);

            // Collect multi-line signature until parentheses are balanced
            let mut sig_lines: Vec<&str> = vec![line];
            let mut j = i + 1;
            while j < lines.len() && ruby_sig_continues(&sig_lines) {
                sig_lines.push(lines[j]);
                j += 1;
            }

            // Emit compact signature + stub
            let sig_joined = sig_lines.join(" ");
            let sig_clean: String = sig_joined.split_whitespace().collect::<Vec<_>>().join(" ");
            out.push(format!("{}{}", indent, sig_clean.trim_start()));
            out.push(format!("{}  # ...", indent));

            // Skip body until the matching `end` (track keyword depth)
            let mut depth = 1i32;
            i = j;
            while i < lines.len() {
                let inner = lines[i].trim();
                if ruby_opens_block(inner) {
                    depth += 1;
                }
                if ruby_closes_block(inner) {
                    depth -= 1;
                    if depth == 0 {
                        out.push(lines[i].to_string());
                        i += 1;
                        break;
                    }
                }
                i += 1;
            }
            continue;
        }

        out.push(line.to_string());
        i += 1;
    }

    collapse_blank_lines(&out.join("\n"))
}

/// Returns true if the last signature line leaves parentheses unbalanced.
fn ruby_sig_continues(sig_lines: &[&str]) -> bool {
    let joined = sig_lines.join(" ");
    let opens: i32 = joined.chars().filter(|&c| c == '(').count() as i32;
    let closes: i32 = joined.chars().filter(|&c| c == ')').count() as i32;
    opens > closes
}

/// Keywords that open a Ruby block requiring a matching `end`.
fn ruby_opens_block(trimmed: &str) -> bool {
    for kw in &[
        "def ", "class ", "module ", "if ", "unless ", "while ", "until ", "for ", "case ",
    ] {
        if trimmed.starts_with(kw) {
            return true;
        }
    }
    matches!(trimmed, "begin" | "do") || trimmed.starts_with("begin ")
}

/// A line that closes a Ruby block.
fn ruby_closes_block(trimmed: &str) -> bool {
    trimmed == "end"
        || trimmed.starts_with("end ")
        || trimmed.starts_with("end#")
        || trimmed.starts_with("end;")
}

// ── Python ───────────────────────────────────────────────────────────────────

fn stub_python(input: &str) -> String {
    let lines: Vec<&str> = input.lines().collect();
    let mut out: Vec<String> = Vec::new();
    let mut i = 0;

    while i < lines.len() {
        let line = lines[i];
        let trimmed = line.trim();

        if trimmed.starts_with("def ") || trimmed.starts_with("async def ") {
            let def_indent = leading_ws(line).len();

            // Collect multi-line signature until we see the `:` that ends the header
            let mut sig_lines: Vec<&str> = vec![line];
            let mut j = i + 1;

            // A `def` header ends at the `:` that isn't inside parens or brackets
            while !sig_ends(&sig_lines) && j < lines.len() {
                sig_lines.push(lines[j]);
                j += 1;
            }

            // Emit the signature as-is (joining multi-line into one)
            let sig_raw = sig_lines.join(" ");
            let sig_clean: String = sig_raw.split_whitespace().collect::<Vec<_>>().join(" ");
            let sig_final = if sig_clean.ends_with(':') {
                sig_clean
            } else {
                format!("{}:", sig_clean)
            };
            let indent = leading_ws(line);
            out.push(format!("{}{}", indent, sig_final.trim_start()));
            out.push(format!("{}    ...", indent));

            // Skip the body — all lines more indented than def_indent
            i = j;
            while i < lines.len() {
                let next = lines[i];
                if next.trim().is_empty() {
                    i += 1;
                    continue;
                }
                if leading_ws(next).len() > def_indent {
                    i += 1;
                } else {
                    break;
                }
            }
            continue;
        }

        out.push(line.to_string());
        i += 1;
    }

    out.join("\n")
}

/// Rough check: has the `def` header line ended (found a `:` outside parens)?
fn sig_ends(sig_lines: &[&str]) -> bool {
    let joined = sig_lines.join(" ");
    let mut depth = 0i32;
    for c in joined.chars() {
        match c {
            '(' | '[' => depth += 1,
            ')' | ']' => depth -= 1,
            ':' if depth == 0 => return true,
            _ => {}
        }
    }
    false
}

// ── Tests ────────────────────────────────────────────────────────────────────

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

    #[test]
    fn stubs_rust_function_body() {
        let src = "pub fn add(a: i32, b: i32) -> i32 {\n    a + b\n}\n";
        let out = stub_bodies(src, &Language::Rust);
        assert!(out.contains("pub fn add(a: i32, b: i32) -> i32"));
        assert!(!out.contains("a + b"));
        assert!(out.contains("/* ... */"));
    }

    #[test]
    fn keeps_struct_and_impl_header() {
        let src = "struct Foo {\n    x: i32,\n}\n\nimpl Foo {\n    fn bar(&self) -> i32 {\n        self.x\n    }\n}\n";
        let out = stub_bodies(src, &Language::Rust);
        assert!(out.contains("struct Foo {"));
        assert!(out.contains("x: i32,"));
        assert!(out.contains("impl Foo {"));
        assert!(out.contains("fn bar(&self) -> i32"));
        assert!(!out.contains("self.x"));
    }

    #[test]
    fn stubs_go_function() {
        let src = "func Add(a, b int) int {\n\treturn a + b\n}\n";
        let out = stub_bodies(src, &Language::Go);
        assert!(out.contains("func Add(a, b int) int"));
        assert!(!out.contains("return a + b"));
    }

    #[test]
    fn stubs_python_function() {
        let src = "def process(data):\n    result = transform(data)\n    return result\n\ndef helper():\n    pass\n";
        let out = stub_python(src);
        assert!(out.contains("def process(data):"));
        assert!(!out.contains("result = transform"));
        assert!(out.contains("def helper():"));
    }

    #[test]
    fn keeps_trait_method_declaration() {
        // Trait methods with semicolons (no body) should be kept
        let src = "trait Validator {\n    fn validate(&self, input: &str) -> bool;\n}\n";
        let out = stub_bodies(src, &Language::Rust);
        assert!(out.contains("fn validate(&self, input: &str) -> bool;"));
    }

    #[test]
    fn stubs_ruby_method_body() {
        let src = "def greet(name)\n  puts \"Hello, #{name}\"\n  42\nend\n";
        let out = stub_bodies(src, &Language::Ruby);
        assert!(out.contains("def greet(name)"));
        assert!(!out.contains("puts"));
        assert!(out.contains("# ..."));
        assert!(out.contains("end"));
    }

    #[test]
    fn stubs_ruby_nested_blocks_correctly() {
        let src = "def process(items)\n  items.each do |i|\n    puts i\n  end\nend\n";
        let out = stub_bodies(src, &Language::Ruby);
        assert!(out.contains("def process(items)"));
        assert!(!out.contains("each"));
        assert!(!out.contains("puts"));
        assert!(out.contains("end"));
    }

    #[test]
    fn stubs_kotlin_function() {
        let src = "fun add(a: Int, b: Int): Int {\n    return a + b\n}\n";
        let out = stub_bodies(src, &Language::Kotlin);
        assert!(out.contains("fun add(a: Int, b: Int): Int"));
        assert!(!out.contains("return a + b"));
        assert!(out.contains("/* ... */"));
    }

    #[test]
    fn stubs_swift_function() {
        let src = "func greet(name: String) -> String {\n    return \"Hello \" + name\n}\n";
        let out = stub_bodies(src, &Language::Swift);
        assert!(out.contains("func greet(name: String) -> String"));
        assert!(!out.contains("Hello"));
        assert!(out.contains("/* ... */"));
    }

    #[test]
    fn stubs_java_method() {
        let src = "public class Foo {\n    public int add(int a, int b) {\n        return a + b;\n    }\n}\n";
        let out = stub_bodies(src, &Language::Java);
        assert!(out.contains("public class Foo {"));
        assert!(out.contains("public int add(int a, int b)"));
        assert!(!out.contains("return a + b"));
        assert!(out.contains("/* ... */"));
    }
}