rsread 0.1.5

Fast multi-language code reader: structure tree, syntax highlighting, duplicate detection and Git change markers for Python, Rust, Go and JavaScript
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
//! Terminal rendering: structure tree, code blocks with Git markers, warning panels.

use crate::analyzer::{Analysis, Duplicates, Item, Kind};
use crate::git::{Change, GitInfo, ItemGit};
use crate::highlight::strip_visible_prefix;
use crate::lang::Lang;
use colored::{Color, Colorize};
use std::collections::HashSet;
use std::io::Write;
use unicode_width::UnicodeWidthStr;

/// Print a line to stdout; exit quietly on a closed pipe (`rsread x.py | head`).
pub fn emit(s: impl AsRef<str>) {
    let mut out = std::io::stdout().lock();
    if writeln!(out, "{}", s.as_ref()).is_err() {
        std::process::exit(0);
    }
}

pub struct Renderer {
    pub emoji: bool,
    pub width: usize,
}

#[derive(Clone, Copy)]
pub struct View {
    pub line_numbers: bool,
    pub strip: bool,
}

enum Entry<'a> {
    Item(&'a Item),
    Group(Vec<&'a Item>),
}

fn digits(n: usize) -> usize {
    n.max(1).to_string().len()
}

fn truncate_width(s: &str, max: usize) -> String {
    if s.width() <= max {
        return s.to_string();
    }
    let mut out = String::new();
    let mut w = 0;
    for c in s.chars() {
        let cw = unicode_width::UnicodeWidthChar::width(c).unwrap_or(0);
        if w + cw + 1 > max {
            break;
        }
        out.push(c);
        w += cw;
    }
    out.push('…');
    out
}

impl Renderer {
    fn ico(&self, emoji: &'static str, ascii: &'static str) -> &'static str {
        if self.emoji {
            emoji
        } else {
            ascii
        }
    }

    pub fn icon_info(&self) -> &'static str {
        self.ico("ℹ️ ", "[i]")
    }

    pub fn icon_warn(&self) -> &'static str {
        self.ico("⚠️ ", "[!]")
    }

    pub fn icon_ok(&self) -> &'static str {
        self.ico("✅", "[ok]")
    }

    pub fn icon_copy(&self) -> &'static str {
        self.ico("📋", "[copy]")
    }

    pub fn icon_dir(&self) -> &'static str {
        self.ico("📁", "[dir]")
    }

    pub fn icon_file(&self) -> &'static str {
        self.ico("📄", "[file]")
    }

    pub fn icon_save(&self) -> &'static str {
        self.ico("💾", "[save]")
    }

    fn kind_icon(&self, it: &Item) -> &'static str {
        match (it.kind, it.is_async) {
            (Kind::Class, _) => self.ico("🏛️", "C"),
            (Kind::Struct, _) => self.ico("🏗️", "S"),
            (Kind::Enum, _) => self.ico("🔢", "E"),
            (Kind::Trait, _) | (Kind::Interface, _) => self.ico("🧩", "T"),
            (Kind::Impl, _) => self.ico("🔩", "I"),
            (Kind::Module, _) => self.ico("📦", "M"),
            (Kind::Type, _) => self.ico("🏷️", "t"),
            (Kind::Macro, _) => self.ico("✨", "!"),
            (Kind::Function, true) | (Kind::Method, true) => self.ico("⚡", "~"),
            (Kind::Function, false) => self.ico("🔧", "f"),
            (Kind::Method, false) => self.ico("⚙️", "m"),
        }
    }

    fn name_styled(&self, it: &Item) -> String {
        let n = it.name.as_str();
        match it.kind {
            Kind::Class
            | Kind::Struct
            | Kind::Enum
            | Kind::Trait
            | Kind::Interface
            | Kind::Type => n.bold().cyan().to_string(),
            Kind::Impl => n.bold().blue().to_string(),
            Kind::Module | Kind::Macro => n.bold().magenta().to_string(),
            Kind::Function => n.bold().yellow().to_string(),
            Kind::Method => n.bold().green().to_string(),
        }
    }

    fn badge_parts(g: &ItemGit) -> Vec<(String, Color)> {
        let mut v = Vec::new();
        if g.added > 0 {
            v.push((format!("+{}", g.added), Color::Green));
        }
        if g.modified > 0 {
            v.push((format!("~{}", g.modified), Color::Yellow));
        }
        if g.deleted > 0 {
            v.push((format!("-{}", g.deleted), Color::Red));
        }
        v
    }

    pub fn git_badge(g: &ItemGit) -> String {
        Self::badge_parts(g)
            .into_iter()
            .map(|(t, c)| t.color(c).bold().to_string())
            .collect::<Vec<_>>()
            .join(" ")
    }

    fn git_badge_plain_width(g: &ItemGit) -> usize {
        let parts = Self::badge_parts(g);
        parts.iter().map(|(t, _)| t.len()).sum::<usize>() + parts.len().saturating_sub(1)
    }

    pub fn git_summary(&self, g: &GitInfo) {
        if !g.has_changes() {
            return;
        }
        emit(format!(
            "{}{}",
            self.ico("📝 ", ""),
            "Git Changes Summary".bold()
        ));
        if g.added > 0 {
            emit(format!("{} {} lines added", "+".green().bold(), g.added));
        }
        if g.deleted > 0 {
            emit(format!("{} {} lines deleted", "-".red().bold(), g.deleted));
        }
        if g.modified > 0 {
            emit(format!(
                "{} {} lines modified",
                "~".yellow().bold(),
                g.modified
            ));
        }
        emit("");
    }

    pub fn panel(&self, color: Color, lines: &[String]) {
        for l in lines {
            emit(format!("{} {}", "┃".color(color).bold(), l));
        }
    }

    // ---- structure ---------------------------------------------------------

    #[allow(clippy::too_many_arguments)]
    pub fn structure(&self, a: &Analysis, label: &str, git: Option<&GitInfo>, dups: &Duplicates) {
        if let Some(g) = git {
            self.git_summary(g);
        }
        let changed = git.is_some_and(|g| g.has_changes());
        let mut head = format!(
            "{}{} │ {}",
            self.ico("🌳 ", ""),
            format!("{} Code Analysis", a.lang.name()).bold(),
            label.bold().cyan()
        );
        if changed {
            head.push_str(&format!(
                " {}",
                format!("{}(Git changes detected)", self.ico("📝 ", "")).yellow()
            ));
        }
        emit(head);

        if !a.error_lines.is_empty() {
            let lines = a
                .error_lines
                .iter()
                .map(|l| l.to_string())
                .collect::<Vec<_>>()
                .join(", ");
            emit(format!(
                "{} {}",
                self.icon_warn(),
                format!("Syntax errors near line {lines}: structure may be incomplete").yellow()
            ));
        }

        let flagged: HashSet<usize> = dups
            .redefinitions
            .iter()
            .flat_map(|g| g.ids.iter().copied())
            .collect();

        let top = a.children(None);
        let funcs: Vec<&Item> = top
            .iter()
            .copied()
            .filter(|i| i.kind == Kind::Function)
            .collect();
        let others: Vec<&Item> = top
            .iter()
            .copied()
            .filter(|i| i.kind != Kind::Function)
            .collect();
        let mut entries: Vec<Entry> = others.into_iter().map(Entry::Item).collect();
        if !funcs.is_empty() {
            if entries.is_empty() {
                entries.extend(funcs.into_iter().map(Entry::Item));
            } else {
                entries.push(Entry::Group(funcs));
            }
        }
        if entries.is_empty() {
            emit(format!(
                "{} {}",
                self.icon_info(),
                "No classes, functions or methods found".dimmed()
            ));
        }
        let n = entries.len();
        for (i, e) in entries.iter().enumerate() {
            let last = i + 1 == n;
            match e {
                Entry::Item(it) => self.tree_item(a, it, "", last, git, &flagged),
                Entry::Group(items) => {
                    let conn = if last { "└── " } else { "├── " };
                    emit(format!(
                        "{}{}{}",
                        conn.bright_black(),
                        self.ico("📄 ", ""),
                        "Standalone Functions".bold().white()
                    ));
                    let prefix = if last { "    " } else { "│   " };
                    for (j, it) in items.iter().enumerate() {
                        self.tree_item(a, it, prefix, j + 1 == items.len(), git, &flagged);
                    }
                }
            }
        }

        self.stats_line(a);
        if !dups.redefinitions.is_empty() || !dups.identical.is_empty() {
            emit("");
            self.duplicates(a, dups, false);
        }
    }

    fn tree_item(
        &self,
        a: &Analysis,
        it: &Item,
        prefix: &str,
        last: bool,
        git: Option<&GitInfo>,
        flagged: &HashSet<usize>,
    ) {
        let conn = if last { "└── " } else { "├── " };
        emit(format!(
            "{}{}{}",
            prefix.bright_black(),
            conn.bright_black(),
            self.item_line(a, it, prefix.width(), git, flagged)
        ));
        let kids = a.children(Some(it.id));
        let child_prefix = format!("{}{}", prefix, if last { "    " } else { "│   " });
        let n = kids.len();
        for (i, k) in kids.iter().enumerate() {
            self.tree_item(a, k, &child_prefix, i + 1 == n, git, flagged);
        }
    }

    fn item_line(
        &self,
        a: &Analysis,
        it: &Item,
        prefix_w: usize,
        git: Option<&GitInfo>,
        flagged: &HashSet<usize>,
    ) -> String {
        let icon = self.kind_icon(it);
        let range = format!(":{}-{}", it.start, it.end);
        let g = git
            .map(|g| g.range_stats(it.start, it.end))
            .unwrap_or_default();
        let dup = flagged.contains(&it.id);

        let mut used = prefix_w + 4 + icon.width() + 1 + it.name.width() + 1 + range.width();
        if !g.is_empty() {
            used += 1 + Self::git_badge_plain_width(&g);
        }
        if dup {
            used += 3;
        }
        let budget = self.width.saturating_sub(used + 2);
        let tail = if !it.tail.is_empty() && budget >= 6 {
            truncate_width(&it.tail, budget)
        } else {
            String::new()
        };
        used += tail.width();

        let mut s = format!("{} {}", icon, self.name_styled(it));
        if !tail.is_empty() {
            s.push_str(&tail.bright_black().to_string());
        }
        s.push(' ');
        s.push_str(&range.bright_black().to_string());
        if !g.is_empty() {
            s.push(' ');
            s.push_str(&Self::git_badge(&g));
        }
        if dup {
            s.push_str(&format!(" {}", self.icon_warn().trim().red()));
        }
        if a.lang == Lang::Python && !it.decorators.is_empty() {
            let d = it
                .decorators
                .iter()
                .take(2)
                .cloned()
                .collect::<Vec<_>>()
                .join(" ");
            if self.width > used + d.width() + 4 {
                used += d.width() + 1;
                s.push_str(&format!(" {}", d.magenta().dimmed()));
            }
        }
        if let Some(sum) = &it.summary {
            let room = self.width.saturating_sub(used + 4);
            if room >= 12 {
                s.push_str(&format!(
                    " {}",
                    format!("— {}", truncate_width(sum, room))
                        .bright_black()
                        .italic()
                ));
            }
        }
        s
    }

    fn stats_line(&self, a: &Analysis) {
        let mut parts = vec![format!("{} lines ({} blank)", a.total_lines, a.blank_lines)];
        for k in Kind::ALL {
            let n = a.items.iter().filter(|i| i.kind == k).count();
            if n > 0 {
                parts.push(format!(
                    "{} {}",
                    n,
                    if n == 1 { singular(k) } else { k.plural() }
                ));
            }
        }
        emit(format!(
            "\n{}{}",
            self.ico("📊 ", ""),
            parts.join(" │ ").bright_black()
        ));
    }

    // ---- duplicates --------------------------------------------------------

    pub fn duplicates(&self, a: &Analysis, d: &Duplicates, show_shared: bool) {
        let mut shown = false;
        if !d.redefinitions.is_empty() {
            shown = true;
            let mut lines = vec![format!(
                "{}{}",
                self.ico("⚠️  ", "[!] "),
                "DUPLICATE METHODS/FUNCTIONS DETECTED".red().bold()
            )];
            for g in &d.redefinitions {
                lines.push(String::new());
                lines.push(format!(
                    "• {} defined {} times {}",
                    g.name.bold(),
                    g.ids.len(),
                    "(the last definition silently wins)".bright_black()
                ));
                self.dup_lines(a, &g.ids, &mut lines);
            }
            self.panel(Color::Red, &lines);
        }
        if !d.identical.is_empty() {
            if shown {
                emit("");
            }
            shown = true;
            let mut lines = vec![format!(
                "{}{}",
                self.ico("👯 ", "[=] "),
                "IDENTICAL BODIES (copy-paste candidates)".yellow().bold()
            )];
            for g in &d.identical {
                lines.push(String::new());
                lines.push(format!("• {}", g.name.bold()));
                self.dup_lines(a, &g.ids, &mut lines);
            }
            self.panel(Color::Yellow, &lines);
        }
        if show_shared && !d.shared_names.is_empty() {
            if shown {
                emit("");
            }
            shown = true;
            let mut lines = vec![format!(
                "{}{}",
                self.ico("🔁 ", "[~] "),
                "SAME NAME IN MULTIPLE SCOPES".blue().bold()
            )];
            for g in &d.shared_names {
                lines.push(String::new());
                lines.push(format!("• {} found {} times:", g.name.bold(), g.ids.len()));
                self.dup_lines(a, &g.ids, &mut lines);
            }
            self.panel(Color::Blue, &lines);
        }
        if !shown {
            emit(format!(
                "{} {}",
                self.icon_ok(),
                "No duplicate methods/functions found".green()
            ));
        }
    }

    fn dup_lines(&self, a: &Analysis, ids: &[usize], out: &mut Vec<String>) {
        let n = ids.len();
        for (i, id) in ids.iter().enumerate() {
            let it = &a.items[*id];
            let mut ctx = a.context(it);
            if it.variant {
                ctx.push_str(", conditional/variant");
            }
            let conn = if i + 1 == n { "└─" } else { "├─" };
            out.push(format!(
                "  {} Line {} {}",
                conn,
                it.def_line,
                format!("({ctx})").bright_black()
            ));
        }
    }

    // ---- code --------------------------------------------------------------

    /// Print lines `start..=end` (1-based). `hl` holds one display string per line.
    pub fn code_block(
        &self,
        hl: &[String],
        plain: &[&str],
        start: usize,
        end: usize,
        git: Option<&GitInfo>,
        view: View,
    ) {
        let end = end.min(hl.len());
        if start == 0 || start > end {
            return;
        }
        let marker_col = git.is_some_and(|g| g.has_changes());
        let w = digits(end);
        let indent = if view.strip {
            plain[start - 1..end]
                .iter()
                .filter(|l| !l.trim().is_empty())
                .map(|l| l.chars().take_while(|c| *c == ' ' || *c == '\t').count())
                .min()
                .unwrap_or(0)
        } else {
            0
        };
        for ln in start..=end {
            let change = git.and_then(|g| g.changes.get(&ln)).copied();
            let (mark, color) = match change {
                Some(Change::Added) => ("+", Some(Color::Green)),
                Some(Change::Modified) => ("~", Some(Color::Yellow)),
                Some(Change::DeletedBelow) => ("-", Some(Color::Red)),
                None => (" ", None),
            };
            let mut s = String::new();
            if !view.strip {
                s.push_str("  ");
            }
            if view.line_numbers {
                let num = format!("{ln:>w$}");
                match color {
                    Some(c) => s.push_str(&num.color(c).bold().to_string()),
                    None => s.push_str(&num.bright_black().to_string()),
                }
                s.push_str(&" │ ".bright_black().to_string());
            }
            if marker_col {
                match color {
                    Some(c) => s.push_str(&mark.color(c).bold().to_string()),
                    None => s.push(' '),
                }
                s.push(' ');
            }
            let code = &hl[ln - 1];
            if indent > 0 {
                s.push_str(&strip_visible_prefix(code, indent));
            } else {
                s.push_str(code);
            }
            emit(s);
        }
    }

    pub fn item_header(&self, a: &Analysis, it: &Item, git: Option<&GitInfo>) -> String {
        let label = match (it.kind, it.parent) {
            (Kind::Method, Some(p)) => format!("{} Method", a.items[p].kind.label()),
            (k, _) => k.label().to_string(),
        };
        let mut s = format!(
            "{}{} '{}' │ Lines {}-{}",
            self.kind_icon(it).to_string() + " ",
            label.bold(),
            it.qual.bold().cyan(),
            it.start,
            it.end
        );
        if let Some(g) = git {
            let st = g.range_stats(it.start, it.end);
            if !st.is_empty() {
                s.push_str(&format!(" │ Git: {}", Self::git_badge(&st)));
            }
        }
        s
    }
}

fn singular(k: Kind) -> &'static str {
    match k {
        Kind::Class => "class",
        Kind::Struct => "struct",
        Kind::Enum => "enum",
        Kind::Trait => "trait",
        Kind::Interface => "interface",
        Kind::Impl => "impl block",
        Kind::Module => "module",
        Kind::Type => "type",
        Kind::Macro => "macro",
        Kind::Function => "function",
        Kind::Method => "method",
    }
}