coding-tools 0.2.0

Declarative, agent-friendly CLI tools behind one 'ct' command: search, view, verifiable edits, and framed command tests.
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 Jonathan Shook

//! `ct-outline`'s heuristic declaration detection.
//!
//! A rule pack per language turns a file's text into ordered [`Entry`] rows —
//! kind, name, 1-based `start:end` span, and nesting depth. Detection is
//! line-pattern matching plus a block heuristic per language family: brace
//! matching for Rust (over comment/string-stripped text), indentation for
//! Python, heading levels for Markdown (fenced code blocks ignored). It is a
//! comprehension aid, not a parser: start lines are exact; an end the
//! heuristic cannot derive is `None` (rendered `start:?`), never a guess.

/// One detected declaration.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Entry {
    /// The source language's own keyword for the declaration (`fn`, `class`,
    /// `h2`, …) — there is no cross-language kind abstraction.
    pub kind: String,
    /// The declared name (for `impl`, the trait-for-type text; for headings,
    /// the title).
    pub name: String,
    /// Exact 1-based line the declaration starts on.
    pub start: usize,
    /// Best-effort 1-based inclusive end line; `None` when the block
    /// heuristic could not derive one.
    pub end: Option<usize>,
    /// 1-based nesting depth (`1` = top level).
    pub depth: usize,
}

/// A supported language rule pack, keyed by file extension via
/// [`language_for`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Lang {
    /// `.rs` — brace blocks over comment/string-stripped text.
    Rust,
    /// `.py` — indentation blocks.
    Python,
    /// `.md` — ATX headings as the outline; fenced code blocks ignored.
    Markdown,
}

impl Lang {
    /// The display name used in messages.
    pub fn label(self) -> &'static str {
        match self {
            Lang::Rust => "rust",
            Lang::Python => "python",
            Lang::Markdown => "markdown",
        }
    }
}

/// The rule pack for a file extension, or `None` when the language is not
/// (yet) supported.
///
/// # Examples
///
/// ```
/// use coding_tools::outline::{language_for, Lang};
///
/// assert_eq!(language_for("rs"), Some(Lang::Rust));
/// assert_eq!(language_for("py"), Some(Lang::Python));
/// assert_eq!(language_for("md"), Some(Lang::Markdown));
/// assert_eq!(language_for("zig"), None); // skipped in walks
/// ```
pub fn language_for(ext: &str) -> Option<Lang> {
    match ext.to_ascii_lowercase().as_str() {
        "rs" => Some(Lang::Rust),
        "py" => Some(Lang::Python),
        "md" => Some(Lang::Markdown),
        _ => None,
    }
}

/// Outline `text` with the `lang` rule pack, in source order.
///
/// # Examples
///
/// ```
/// use coding_tools::outline::{outline, Lang};
///
/// let src = "pub struct Point { x: i32 }\n\nimpl Point {\n    fn norm(&self) -> i32 { 0 }\n}\n";
/// let entries = outline(Lang::Rust, src);
/// let rows: Vec<String> = entries
///     .iter()
///     .map(|e| format!("{}:{} {} {} d{}", e.start, e.end.map_or("?".into(), |n| n.to_string()), e.kind, e.name, e.depth))
///     .collect();
/// assert_eq!(rows, [
///     "1:1 struct Point d1",
///     "3:5 impl Point d1",
///     "4:4 fn norm d2",
/// ]);
/// ```
pub fn outline(lang: Lang, text: &str) -> Vec<Entry> {
    match lang {
        Lang::Rust => rust_outline(text),
        Lang::Python => python_outline(text),
        Lang::Markdown => markdown_outline(text),
    }
}

// ----- Rust ---------------------------------------------------------------------

/// Replace comments and string/char-literal contents with spaces, preserving
/// line structure, so brace counting and keyword matching see only code.
/// Heuristic: raw strings, nested block comments, and lifetimes are handled;
/// pathological token sequences may still leak.
fn strip_rust(text: &str) -> String {
    let chars: Vec<char> = text.chars().collect();
    let mut out = String::with_capacity(text.len());
    let mut i = 0;
    let n = chars.len();

    // Emit ch verbatim for newline (keep line structure), else a space.
    let blank = |c: char| if c == '\n' { '\n' } else { ' ' };

    while i < n {
        let c = chars[i];
        match c {
            '/' if i + 1 < n && chars[i + 1] == '/' => {
                while i < n && chars[i] != '\n' {
                    out.push(' ');
                    i += 1;
                }
            }
            '/' if i + 1 < n && chars[i + 1] == '*' => {
                let mut depth = 1;
                out.push(' ');
                out.push(' ');
                i += 2;
                while i < n && depth > 0 {
                    if chars[i] == '/' && i + 1 < n && chars[i + 1] == '*' {
                        depth += 1;
                        out.push(blank(chars[i]));
                        out.push(blank(chars[i + 1]));
                        i += 2;
                    } else if chars[i] == '*' && i + 1 < n && chars[i + 1] == '/' {
                        depth -= 1;
                        out.push(blank(chars[i]));
                        out.push(blank(chars[i + 1]));
                        i += 2;
                    } else {
                        out.push(blank(chars[i]));
                        i += 1;
                    }
                }
            }
            'r' | 'b'
                if {
                    // Raw / byte-string starts: r", r#", br", b".
                    let mut j = i;
                    if chars[j] == 'b' && j + 1 < n && chars[j + 1] == 'r' {
                        j += 1;
                    }
                    if chars[j] == 'r' || chars[j] == 'b' {
                        let mut k = j + 1;
                        if chars[j] == 'r' {
                            while k < n && chars[k] == '#' {
                                k += 1;
                            }
                        }
                        k < n && chars[k] == '"'
                    } else {
                        false
                    }
                } =>
            {
                // Copy the prefix, then blank the quoted body.
                let mut hashes = 0;
                out.push(chars[i]);
                i += 1;
                if i < n && chars[i] == 'r' {
                    out.push('r');
                    i += 1;
                }
                while i < n && chars[i] == '#' {
                    hashes += 1;
                    out.push('#');
                    i += 1;
                }
                out.push('"');
                i += 1; // opening quote
                'raw: while i < n {
                    if chars[i] == '"' {
                        let mut k = i + 1;
                        let mut seen = 0;
                        while k < n && chars[k] == '#' && seen < hashes {
                            k += 1;
                            seen += 1;
                        }
                        if seen == hashes {
                            out.push('"');
                            for _ in 0..hashes {
                                out.push('#');
                            }
                            i = k;
                            break 'raw;
                        }
                    }
                    out.push(blank(chars[i]));
                    i += 1;
                }
            }
            '"' => {
                out.push('"');
                i += 1;
                while i < n {
                    if chars[i] == '\\' && i + 1 < n {
                        out.push(' ');
                        out.push(' ');
                        i += 2;
                        continue;
                    }
                    if chars[i] == '"' {
                        out.push('"');
                        i += 1;
                        break;
                    }
                    out.push(blank(chars[i]));
                    i += 1;
                }
            }
            '\'' => {
                // Char literal vs lifetime: 'x' / '\n' are literals; 'a in a
                // generic position is a lifetime and passes through.
                if i + 2 < n && chars[i + 1] == '\\' {
                    // Escaped char literal: blank the body. (Column alignment
                    // is irrelevant; only line structure matters.)
                    out.push('\'');
                    i += 2;
                    while i < n && chars[i] != '\'' {
                        out.push(' ');
                        i += 1;
                    }
                    if i < n {
                        out.push('\'');
                        i += 1;
                    }
                } else if i + 2 < n && chars[i + 2] == '\'' {
                    out.push('\'');
                    out.push(' ');
                    out.push('\'');
                    i += 3;
                } else {
                    out.push('\'');
                    i += 1;
                }
            }
            _ => {
                out.push(c);
                i += 1;
            }
        }
    }
    out
}

/// The leading identifier of `s` (`[A-Za-z0-9_]+`), if any.
fn ident(s: &str) -> Option<&str> {
    let end = s
        .char_indices()
        .find(|(_, c)| !(c.is_ascii_alphanumeric() || *c == '_'))
        .map(|(i, _)| i)
        .unwrap_or(s.len());
    if end == 0 { None } else { Some(&s[..end]) }
}

/// Strip one `pub` / `pub(...)` visibility prefix.
fn strip_vis(s: &str) -> &str {
    let Some(rest) = s.strip_prefix("pub") else {
        return s;
    };
    let rest = rest.trim_start();
    if let Some(after) = rest.strip_prefix('(') {
        if let Some(close) = after.find(')') {
            return after[close + 1..].trim_start();
        }
        return s;
    }
    if rest.len() < s.len() { rest } else { s }
}

/// Detect a Rust declaration on a (comment/string-stripped, trimmed) line.
/// `parent_kind` is the enclosing entry's kind, used to suppress items that
/// are local detail rather than structure (e.g. `const` inside a `fn`).
fn rust_decl(trimmed: &str, parent_kind: Option<&str>) -> Option<(String, String)> {
    let s = strip_vis(trimmed);
    let inside_fn = parent_kind == Some("fn");

    // fn, with any qualifier prefix.
    {
        let mut t = s;
        loop {
            let next = ["default ", "const ", "async ", "unsafe "]
                .iter()
                .find_map(|q| t.strip_prefix(q));
            if let Some(rest) = next {
                t = rest.trim_start();
                continue;
            }
            if let Some(rest) = t.strip_prefix("extern") {
                let rest = rest.trim_start();
                if let Some(r) = rest.strip_prefix('"')
                    && let Some(close) = r.find('"')
                {
                    t = r[close + 1..].trim_start();
                    continue;
                }
            }
            break;
        }
        if let Some(rest) = t.strip_prefix("fn ")
            && let Some(name) = ident(rest.trim_start())
        {
            return Some(("fn".into(), name.into()));
        }
    }

    for (kw, kind) in [
        ("mod ", "mod"),
        ("struct ", "struct"),
        ("enum ", "enum"),
        ("trait ", "trait"),
        ("macro_rules! ", "macro"),
    ] {
        if let Some(rest) = s.strip_prefix(kw)
            && let Some(name) = ident(rest.trim_start())
        {
            return Some((kind.into(), name.into()));
        }
    }
    if let Some(rest) = s.strip_prefix("unsafe ")
        && let Some(rest) = rest.trim_start().strip_prefix("trait ")
        && let Some(name) = ident(rest.trim_start())
    {
        return Some(("trait".into(), name.into()));
    }

    if s == "impl" || s.starts_with("impl ") || s.starts_with("impl<") {
        let mut rest = &s[4..];
        if let Some(r) = rest.strip_prefix('<') {
            // Skip the generic parameter list to the matching '>'.
            let mut depth = 1usize;
            let mut idx = None;
            for (i, c) in r.char_indices() {
                match c {
                    '<' => depth += 1,
                    '>' => {
                        depth -= 1;
                        if depth == 0 {
                            idx = Some(i);
                            break;
                        }
                    }
                    _ => {}
                }
            }
            rest = idx.map(|i| &r[i + 1..]).unwrap_or("");
        }
        let mut name = rest.trim();
        if let Some(cut) = name.find('{') {
            name = name[..cut].trim();
        }
        if let Some(cut) = name.find(" where") {
            name = name[..cut].trim();
        }
        if !name.is_empty() {
            return Some(("impl".into(), name.into()));
        }
    }

    if !inside_fn {
        if let Some(rest) = s.strip_prefix("type ")
            && let Some(name) = ident(rest.trim_start())
        {
            return Some(("type".into(), name.into()));
        }
        if let Some(rest) = s.strip_prefix("const ")
            && let Some(name) = ident(rest.trim_start())
        {
            return Some(("const".into(), name.into()));
        }
        if let Some(rest) = s.strip_prefix("static ") {
            let rest = rest.trim_start();
            let rest = rest.strip_prefix("mut ").unwrap_or(rest).trim_start();
            if let Some(name) = ident(rest) {
                return Some(("static".into(), name.into()));
            }
        }
    }
    None
}

/// From the declaration's line, derive the block end: the line where the
/// first `{` after the declaration is balanced, or `start` itself when a `;`
/// terminates first. `None` when neither resolves within sight (20 lines of
/// lookahead for the opener) or the block never closes.
fn brace_block_end(lines: &[&str], start_idx: usize) -> Option<usize> {
    let mut depth = 0usize;
    let mut opened = false;
    for (j, line) in lines.iter().enumerate().skip(start_idx) {
        for c in line.chars() {
            match c {
                '{' => {
                    depth += 1;
                    opened = true;
                }
                '}' => {
                    depth = depth.saturating_sub(1);
                    if opened && depth == 0 {
                        return Some(j + 1);
                    }
                }
                ';' if !opened => return Some(start_idx + 1),
                _ => {}
            }
        }
        if !opened && j >= start_idx + 20 {
            return None;
        }
    }
    None
}

fn rust_outline(text: &str) -> Vec<Entry> {
    let stripped = strip_rust(text);
    let lines: Vec<&str> = stripped.lines().collect();
    let mut entries: Vec<Entry> = Vec::new();
    // Stack of (kind, end_line) for open enclosing entries.
    let mut stack: Vec<(String, Option<usize>)> = Vec::new();

    for (i, raw) in lines.iter().enumerate() {
        let line_no = i + 1;
        while let Some((_, Some(end))) = stack.last() {
            if *end < line_no {
                stack.pop();
            } else {
                break;
            }
        }
        let trimmed = raw.trim_start();
        if trimmed.is_empty() || trimmed.starts_with('#') {
            continue; // attributes / blanks
        }
        let parent_kind = stack.last().map(|(k, _)| k.as_str());
        if let Some((kind, name)) = rust_decl(trimmed, parent_kind) {
            let end = brace_block_end(&lines, i);
            entries.push(Entry {
                kind: kind.clone(),
                name,
                start: line_no,
                end,
                depth: stack.len() + 1,
            });
            // Only block-owners enclose later lines.
            if end.is_some_and(|e| e > line_no) {
                stack.push((kind, end));
            }
        }
    }
    entries
}

// ----- Python -------------------------------------------------------------------

/// Leading indentation in columns (tab = 4).
fn indent_cols(line: &str) -> usize {
    line.chars()
        .take_while(|c| *c == ' ' || *c == '\t')
        .map(|c| if c == '\t' { 4 } else { 1 })
        .sum()
}

fn python_outline(text: &str) -> Vec<Entry> {
    let lines: Vec<&str> = text.lines().collect();
    let mut entries: Vec<Entry> = Vec::new();
    let mut stack: Vec<(usize, usize)> = Vec::new(); // (indent, entry index)
    let mut in_triple: Option<&str> = None;

    for (i, line) in lines.iter().enumerate() {
        let content = line.trim();
        // Rough triple-quoted string tracking so docstring text cannot
        // masquerade as declarations.
        if let Some(q) = in_triple {
            if content.contains(q) {
                in_triple = None;
            }
            continue;
        }
        for q in ["\"\"\"", "'''"] {
            if content.starts_with(q) && !content[q.len()..].contains(q) {
                in_triple = Some(q);
            }
        }
        if in_triple.is_some() || content.is_empty() || content.starts_with('#') {
            continue;
        }

        let decl = content
            .strip_prefix("async def ")
            .or_else(|| content.strip_prefix("def "))
            .map(|rest| ("def", rest))
            .or_else(|| content.strip_prefix("class ").map(|rest| ("class", rest)));
        let Some((kind, rest)) = decl else { continue };
        let Some(name) = ident(rest.trim_start()) else {
            continue;
        };

        let indent = indent_cols(line);
        while let Some((top_indent, _)) = stack.last() {
            if *top_indent >= indent {
                stack.pop();
            } else {
                break;
            }
        }

        // End: the last content line indented deeper than the declaration.
        let mut end = i + 1;
        for (j, later) in lines.iter().enumerate().skip(i + 1) {
            let t = later.trim();
            if t.is_empty() {
                continue;
            }
            if indent_cols(later) <= indent {
                break;
            }
            end = j + 1;
        }

        entries.push(Entry {
            kind: kind.into(),
            name: name.into(),
            start: i + 1,
            end: Some(end),
            depth: stack.len() + 1,
        });
        stack.push((indent, entries.len() - 1));
    }
    entries
}

// ----- Markdown -----------------------------------------------------------------

fn markdown_outline(text: &str) -> Vec<Entry> {
    let lines: Vec<&str> = text.lines().collect();
    let mut headings: Vec<(usize, usize, String)> = Vec::new(); // (level, line, title)
    let mut in_fence = false;

    for (i, line) in lines.iter().enumerate() {
        let t = line.trim_start();
        if t.starts_with("```") || t.starts_with("~~~") {
            in_fence = !in_fence;
            continue;
        }
        if in_fence {
            continue;
        }
        let level = t.chars().take_while(|c| *c == '#').count();
        if (1..=6).contains(&level)
            && let Some(rest) = t.get(level..)
            && rest.starts_with(' ')
        {
            let title = rest.trim().trim_end_matches('#').trim().to_string();
            headings.push((level, i + 1, title));
        }
    }

    let total = lines.len();
    headings
        .iter()
        .enumerate()
        .map(|(idx, (level, start, title))| {
            // The section runs to the line before the next heading of the
            // same or a shallower level.
            let end = headings[idx + 1..]
                .iter()
                .find(|(l, ..)| l <= level)
                .map(|(_, s, _)| s - 1)
                .unwrap_or(total);
            Entry {
                kind: format!("h{level}"),
                name: title.clone(),
                start: *start,
                end: Some(end.max(*start)),
                depth: *level,
            }
        })
        .collect()
}

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

    fn row(e: &Entry) -> String {
        format!(
            "{}:{} {} {} d{}",
            e.start,
            e.end.map_or("?".to_string(), |n| n.to_string()),
            e.kind,
            e.name,
            e.depth
        )
    }

    #[test]
    fn rust_detects_kinds_spans_and_nesting() {
        let src = r#"
pub mod inner;

/// Doc with a brace { that must not count.
pub struct Point {
    x: i32,
}

impl Display for Point {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        let brace = "{"; // string brace must not count
        write!(f, "{}", self.x)
    }
}

pub(crate) async fn fetch() {}

macro_rules! declare {
    () => {};
}
"#;
        let rows: Vec<String> = rust_outline(src).iter().map(row).collect();
        assert_eq!(
            rows,
            [
                "2:2 mod inner d1",
                "5:7 struct Point d1",
                "9:14 impl Display for Point d1",
                "10:13 fn fmt d2",
                "16:16 fn fetch d1",
                "18:20 macro declare d1",
            ]
        );
    }

    #[test]
    fn rust_suppresses_locals_inside_fn_but_keeps_assoc_items() {
        let src = "impl Foo {\n    const MAX: u8 = 1;\n    fn go() {\n        const LOCAL: u8 = 2;\n    }\n}\n";
        let rows: Vec<String> = rust_outline(src).iter().map(row).collect();
        assert_eq!(
            rows,
            ["1:6 impl Foo d1", "2:2 const MAX d2", "3:5 fn go d2"]
        );
    }

    #[test]
    fn rust_unclosed_block_yields_unknown_end() {
        let src = "fn broken() {\n    let x = 1;\n";
        let e = &rust_outline(src)[0];
        assert_eq!((e.start, e.end), (1, None));
    }

    #[test]
    fn python_indentation_nesting_and_spans() {
        let src = "class A:\n    def m(self):\n        pass\n\n    async def n(self):\n        pass\n\ndef top():\n    pass\n";
        let rows: Vec<String> = python_outline(src).iter().map(row).collect();
        assert_eq!(
            rows,
            [
                "1:6 class A d1",
                "2:3 def m d2",
                "5:6 def n d2",
                "8:9 def top d1",
            ]
        );
    }

    #[test]
    fn python_docstring_text_is_not_a_declaration() {
        let src = "def real():\n    \"\"\"\n    def fake():\n    \"\"\"\n    pass\n";
        let rows: Vec<String> = python_outline(src).iter().map(row).collect();
        assert_eq!(rows, ["1:5 def real d1"]);
    }

    #[test]
    fn markdown_headings_with_fenced_code_ignored() {
        let src = "# Title\n\nintro\n\n```sh\n# not a heading\n```\n\n## Section A\n\nbody\n\n## Section B\n";
        let rows: Vec<String> = markdown_outline(src).iter().map(row).collect();
        assert_eq!(
            rows,
            ["1:13 h1 Title d1", "9:12 h2 Section A d2", "13:13 h2 Section B d2"]
        );
    }

    #[test]
    fn language_keying_is_extension_based() {
        assert_eq!(language_for("RS"), Some(Lang::Rust));
        assert_eq!(language_for("txt"), None);
    }
}