oxilean-cli 0.1.2

OxiLean command-line interface
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
//! Rendering functions for structured diagnostic display.

use super::types::{
    DiagnosticLabel, DisplayConfig, LabelKind, RichDiagnostic, Severity, TermColor,
};

// ── low-level helpers ────────────────────────────────────────────────────────

/// Wrap `text` in the ANSI escape for `color` (and reset afterwards) when
/// `cfg.use_color` is true; otherwise return `text` unchanged.
pub fn colorize(text: &str, color: TermColor, cfg: &DisplayConfig) -> String {
    if cfg.use_color {
        format!(
            "{}{}{}",
            color.ansi_code(),
            text,
            TermColor::Reset.ansi_code()
        )
    } else {
        text.to_string()
    }
}

/// Apply `TermColor::Bold` to `text` when color is enabled.
fn bold(text: &str, cfg: &DisplayConfig) -> String {
    colorize(text, TermColor::Bold, cfg)
}

/// Format the diagnostic header line, e.g.
/// `error[E0001]: title` or `warning: missing return`.
fn format_header(diag: &RichDiagnostic, cfg: &DisplayConfig) -> String {
    let label = diag.severity.label();
    let code_part = match &diag.code {
        Some(code) => format!("[{}]", code),
        None => String::new(),
    };
    let header_text = format!("{}{}: {}", label, code_part, diag.title);
    colorize(&bold(&header_text, cfg), diag.severity.color(), cfg)
}

/// Convert a byte offset within `source` to a `(line, column)` pair
/// (both 1-based).  Returns `(1, 1)` if the offset is out of range.
fn offset_to_line_col(source: &str, offset: usize) -> (usize, usize) {
    let safe_offset = offset.min(source.len());
    let prefix = &source[..safe_offset];
    let line = prefix.chars().filter(|&c| c == '\n').count() + 1;
    let col = prefix
        .rfind('\n')
        .map_or(safe_offset, |pos| safe_offset - pos - 1)
        + 1;
    (line, col)
}

/// Return the text of line `line_no` (1-based) from `source`.
fn get_line(source: &str, line_no: usize) -> Option<&str> {
    source.lines().nth(line_no.saturating_sub(1))
}

/// Number of decimal digits needed to represent `n`.
fn digits(n: usize) -> usize {
    if n == 0 {
        1
    } else {
        (n as f64).log10() as usize + 1
    }
}

// ── span underline renderer ──────────────────────────────────────────────────

/// Render a single underlined span within a source listing.
///
/// Returns multiple lines:
/// 1. The source line with a line-number gutter.
/// 2. The underline row (`^^^` / `---` / `...`).
/// 3. (Optionally) a label message row.
pub fn render_span_underline(
    source: &str,
    span: (usize, usize),
    label: &str,
    kind: LabelKind,
    cfg: &DisplayConfig,
) -> String {
    let (start, end) = span;
    let (line_no, col_start) = offset_to_line_col(source, start);
    let (_, col_end) = offset_to_line_col(source, end.saturating_sub(1));

    let line_text = get_line(source, line_no).unwrap_or("");
    let gutter_width = digits(line_no) + 1; // e.g. "5 │"

    let bar = cfg.bar_char();
    let gutter = format!("{:>width$} {} ", line_no, bar, width = gutter_width);
    let empty_gutter = format!("{} {} ", " ".repeat(gutter_width), bar);

    // Source line row.
    let line_row = format!("{}{}", colorize(&gutter, TermColor::Blue, cfg), line_text);

    // Underline row.
    let underline_start = col_start.saturating_sub(1);
    let underline_len = (col_end.saturating_sub(col_start) + 1).max(1);
    let uc = kind.underline_char();
    let underline = uc.to_string().repeat(underline_len);
    let padding = " ".repeat(underline_start);
    let underline_colored = colorize(&underline, kind.color(), cfg);
    let underline_row = format!(
        "{}{}{}",
        colorize(&empty_gutter, TermColor::Blue, cfg),
        padding,
        underline_colored
    );

    let mut rows = vec![line_row, underline_row];

    // Message row.
    if !label.is_empty() {
        let arrow = cfg.arrow_char();
        let msg_padding = " ".repeat(underline_start);
        let msg_row = format!(
            "{}{}{} {}",
            colorize(&empty_gutter, TermColor::Blue, cfg),
            msg_padding,
            colorize(arrow, kind.color(), cfg),
            colorize(label, kind.color(), cfg)
        );
        rows.push(msg_row);
    }

    rows.join("\n")
}

// ── full diagnostic renderer ─────────────────────────────────────────────────

/// Render a `RichDiagnostic` as a multi-line string in rustc style.
///
/// Example output (plain text):
/// ```text
/// error[E0001]: undeclared variable `x`
///  --> src/main.oxilean:3:5
///   |
/// 3 | let y := x + 1
///   |          ^ undeclared
///   |
///   = note: make sure the variable is declared before use
///   = help: add `let x := 0` before this expression
/// ```
pub fn render_diagnostic(diag: &RichDiagnostic, cfg: &DisplayConfig) -> String {
    let mut out = Vec::new();

    // ── header ──────────────────────────────────────────────────────────────
    out.push(format_header(diag, cfg));

    // ── source view with labels ──────────────────────────────────────────────
    if let Some(ref source) = diag.source {
        if !diag.labels.is_empty() {
            // Find the line range we need to display.
            let (min_line, max_line) = label_line_range(source, &diag.labels);

            let gutter_width = digits(max_line) + 1;
            let bar = cfg.bar_char();
            let corner = cfg.corner_char();
            let hbar = cfg.hbar_char();

            // " --> file:line:col" (use first primary label for location).
            if let Some(primary) = diag.labels.iter().find(|l| l.kind == LabelKind::Primary) {
                let (line_no, col) = offset_to_line_col(source, primary.span.0);
                let arrow_line = format!(
                    " {} src:{}:{}",
                    colorize("-->", TermColor::Blue, cfg),
                    line_no,
                    col
                );
                out.push(arrow_line);
            }

            // Top border line.
            let border_top = format!(
                "{}{} {}",
                " ".repeat(gutter_width + 2),
                colorize(corner, TermColor::Blue, cfg),
                colorize(hbar, TermColor::Blue, cfg),
            );
            out.push(border_top);

            // Render each relevant source line.
            for line_no in min_line..=max_line {
                let line_text = get_line(source, line_no).unwrap_or("");
                let gutter = format!("{:>width$} {} ", line_no, bar, width = gutter_width);
                let source_row =
                    format!("{}{}", colorize(&gutter, TermColor::Blue, cfg), line_text);
                out.push(source_row);

                // Render any labels that fall on this line.
                let labels_on_line: Vec<&DiagnosticLabel> = diag
                    .labels
                    .iter()
                    .filter(|l| {
                        let (ln, _) = offset_to_line_col(source, l.span.0);
                        ln == line_no
                    })
                    .collect();

                for lbl in labels_on_line {
                    let rendered =
                        render_span_underline(source, lbl.span, &lbl.message, lbl.kind, cfg);
                    // The first line of `rendered` is the source line we already printed;
                    // skip it and only emit the underline + message lines.
                    let extra: Vec<&str> = rendered.lines().skip(1).collect();
                    for row in extra {
                        out.push(row.to_string());
                    }
                }
            }

            // Bottom gutter separator.
            let empty_gutter = format!(
                "{} {} ",
                " ".repeat(gutter_width),
                colorize(bar, TermColor::Blue, cfg)
            );
            out.push(empty_gutter);
        }
    }

    // ── notes ────────────────────────────────────────────────────────────────
    for note in &diag.notes {
        let prefix = colorize("= note:", TermColor::Cyan, cfg);
        out.push(format!("  {} {}", prefix, note));
    }

    // ── help ─────────────────────────────────────────────────────────────────
    for help in &diag.help {
        let prefix = colorize("= help:", TermColor::Green, cfg);
        out.push(format!("  {} {}", prefix, help));
    }

    out.join("\n")
}

/// Return the (min_line, max_line) range (1-based, inclusive) that the
/// labels span.
fn label_line_range(source: &str, labels: &[DiagnosticLabel]) -> (usize, usize) {
    let mut min_line = usize::MAX;
    let mut max_line = 1;
    for lbl in labels {
        let (ln_start, _) = offset_to_line_col(source, lbl.span.0);
        let (ln_end, _) = offset_to_line_col(source, lbl.span.1.saturating_sub(1));
        min_line = min_line.min(ln_start);
        max_line = max_line.max(ln_end);
    }
    if min_line == usize::MAX {
        (1, 1)
    } else {
        (min_line, max_line)
    }
}

// ── simple one-liner renderer ─────────────────────────────────────────────────

/// Render a simple one-line diagnostic without source context.
///
/// Output: `severity: message`
pub fn render_simple(severity: Severity, msg: &str, cfg: &DisplayConfig) -> String {
    let label = colorize(severity.label(), severity.color(), cfg);
    let message = colorize(msg, TermColor::White, cfg);
    format!("{}: {}", bold(&label, cfg), message)
}

// ── keyword highlighter ───────────────────────────────────────────────────────

/// Return `source` with all occurrences of each keyword in `keywords` wrapped
/// in bold + cyan coloring (when `cfg.use_color` is true).
pub fn highlight_keyword(source: &str, keywords: &[&str], cfg: &DisplayConfig) -> String {
    if !cfg.use_color || keywords.is_empty() {
        return source.to_string();
    }

    let mut result = source.to_string();
    for &kw in keywords {
        if kw.is_empty() {
            continue;
        }
        let highlighted = format!(
            "{}{}{}{}",
            TermColor::Bold.ansi_code(),
            TermColor::Cyan.ansi_code(),
            kw,
            TermColor::Reset.ansi_code()
        );
        result = result.replace(kw, &highlighted);
    }
    result
}

// ── tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::error_display::types::{
        DiagnosticLabel, DisplayConfig, LabelKind, RichDiagnostic, Severity, TermColor,
    };

    fn plain() -> DisplayConfig {
        DisplayConfig::plain()
    }

    fn colored() -> DisplayConfig {
        DisplayConfig::colored()
    }

    // ── colorize ─────────────────────────────────────────────────────────────

    #[test]
    fn test_colorize_plain_no_escape() {
        let result = colorize("hello", TermColor::Red, &plain());
        assert_eq!(result, "hello");
    }

    #[test]
    fn test_colorize_colored_has_escape() {
        let result = colorize("hello", TermColor::Red, &colored());
        assert!(result.contains("\x1b[31m"));
        assert!(result.contains("hello"));
        assert!(result.contains("\x1b[0m"));
    }

    #[test]
    fn test_colorize_bold() {
        let result = colorize("bold text", TermColor::Bold, &colored());
        assert!(result.contains("\x1b[1m"));
    }

    #[test]
    fn test_colorize_empty_string() {
        let result = colorize("", TermColor::Red, &colored());
        // Should contain ANSI codes but no visible text.
        assert!(!result.contains("hello"));
    }

    // ── render_simple ────────────────────────────────────────────────────────

    #[test]
    fn test_render_simple_error_plain() {
        let out = render_simple(Severity::Error, "undefined variable", &plain());
        assert!(out.contains("error"));
        assert!(out.contains("undefined variable"));
    }

    #[test]
    fn test_render_simple_warning() {
        let out = render_simple(Severity::Warning, "unused import", &plain());
        assert!(out.contains("warning"));
        assert!(out.contains("unused import"));
    }

    #[test]
    fn test_render_simple_info() {
        let out = render_simple(Severity::Info, "elaboration started", &plain());
        assert!(out.contains("info"));
    }

    #[test]
    fn test_render_simple_hint() {
        let out = render_simple(Severity::Hint, "try using rfl", &plain());
        assert!(out.contains("hint"));
        assert!(out.contains("try using rfl"));
    }

    #[test]
    fn test_render_simple_colored_has_ansi() {
        let out = render_simple(Severity::Error, "oops", &colored());
        assert!(out.contains("\x1b["));
    }

    // ── render_diagnostic header ─────────────────────────────────────────────

    #[test]
    fn test_render_diagnostic_header_no_code() {
        let diag = RichDiagnostic::new(Severity::Error, "title only");
        let out = render_diagnostic(&diag, &plain());
        assert!(out.contains("error: title only"));
    }

    #[test]
    fn test_render_diagnostic_header_with_code() {
        let diag = RichDiagnostic::new(Severity::Error, "bad type").with_code("E0042");
        let out = render_diagnostic(&diag, &plain());
        assert!(out.contains("error[E0042]: bad type"));
    }

    #[test]
    fn test_render_diagnostic_warning_header() {
        let diag = RichDiagnostic::new(Severity::Warning, "unused");
        let out = render_diagnostic(&diag, &plain());
        assert!(out.contains("warning: unused"));
    }

    // ── render_diagnostic notes / help ───────────────────────────────────────

    #[test]
    fn test_render_diagnostic_note() {
        let diag = RichDiagnostic::new(Severity::Error, "err").add_note("check your imports");
        let out = render_diagnostic(&diag, &plain());
        assert!(out.contains("note:"));
        assert!(out.contains("check your imports"));
    }

    #[test]
    fn test_render_diagnostic_help() {
        let diag =
            RichDiagnostic::new(Severity::Error, "err").add_help("add `open Nat` at the top");
        let out = render_diagnostic(&diag, &plain());
        assert!(out.contains("help:"));
        assert!(out.contains("add `open Nat`"));
    }

    #[test]
    fn test_render_diagnostic_multiple_notes() {
        let diag = RichDiagnostic::new(Severity::Warning, "w")
            .add_note("note one")
            .add_note("note two");
        let out = render_diagnostic(&diag, &plain());
        assert!(out.contains("note one"));
        assert!(out.contains("note two"));
    }

    // ── render_diagnostic with source ────────────────────────────────────────

    #[test]
    fn test_render_diagnostic_source_line_shown() {
        let source = "let x := 1\nlet y := x + z\nlet w := 0";
        let diag = RichDiagnostic::new(Severity::Error, "undefined z")
            .with_source(source)
            .add_label(DiagnosticLabel::primary((23, 24), "not found"));
        let out = render_diagnostic(&diag, &plain());
        assert!(out.contains("let y := x + z"));
    }

    #[test]
    fn test_render_diagnostic_underline_present() {
        let source = "let x := 1\nfoo bar baz\n";
        let diag = RichDiagnostic::new(Severity::Error, "err")
            .with_source(source)
            .add_label(DiagnosticLabel::primary((11, 14), "here"));
        let out = render_diagnostic(&diag, &plain());
        // Primary underlines use '^'.
        assert!(out.contains('^'));
    }

    #[test]
    fn test_render_diagnostic_secondary_underline() {
        let source = "alpha beta gamma";
        let diag = RichDiagnostic::new(Severity::Warning, "w")
            .with_source(source)
            .add_label(DiagnosticLabel::secondary((6, 10), "secondary"));
        let out = render_diagnostic(&diag, &plain());
        assert!(out.contains('-'));
    }

    #[test]
    fn test_render_diagnostic_note_underline() {
        let source = "some text here";
        let diag = RichDiagnostic::new(Severity::Info, "info")
            .with_source(source)
            .add_label(DiagnosticLabel::note((5, 9), "a note"));
        let out = render_diagnostic(&diag, &plain());
        assert!(out.contains('.'));
    }

    // ── render_span_underline ────────────────────────────────────────────────

    #[test]
    fn test_render_span_underline_produces_carets() {
        let source = "hello world";
        let out = render_span_underline(source, (6, 11), "world", LabelKind::Primary, &plain());
        assert!(out.contains('^'));
        assert!(out.contains("world"));
    }

    #[test]
    fn test_render_span_underline_message_row() {
        let source = "abcdef";
        let out = render_span_underline(source, (0, 3), "abc here", LabelKind::Primary, &plain());
        assert!(out.contains("abc here"));
    }

    #[test]
    fn test_render_span_underline_no_message() {
        let source = "abcdef";
        let out = render_span_underline(source, (0, 3), "", LabelKind::Primary, &plain());
        // Should still have caret row but no extra message row.
        let line_count = out.lines().count();
        assert_eq!(line_count, 2); // source + underline only
    }

    #[test]
    fn test_render_span_underline_multiline_source() {
        let source = "line one\nline two\nline three";
        let out =
            render_span_underline(source, (9, 17), "line two", LabelKind::Secondary, &plain());
        assert!(out.contains('-'));
        assert!(out.contains("line two"));
    }

    // ── highlight_keyword ─────────────────────────────────────────────────────

    #[test]
    fn test_highlight_keyword_no_color() {
        let result = highlight_keyword("let x := 0", &["let"], &plain());
        // No color → unchanged.
        assert_eq!(result, "let x := 0");
    }

    #[test]
    fn test_highlight_keyword_colored() {
        let result = highlight_keyword("let x := 0", &["let"], &colored());
        assert!(result.contains("\x1b["));
        assert!(result.contains("let"));
    }

    #[test]
    fn test_highlight_keyword_multiple() {
        let result = highlight_keyword("fun x => x", &["fun", "=>"], &colored());
        assert!(result.contains("fun"));
        assert!(result.contains("=>"));
    }

    #[test]
    fn test_highlight_keyword_empty_keywords() {
        let src = "unchanged";
        let result = highlight_keyword(src, &[], &colored());
        assert_eq!(result, src);
    }

    #[test]
    fn test_highlight_keyword_empty_source() {
        let result = highlight_keyword("", &["let"], &colored());
        assert_eq!(result, "");
    }

    // ── offset_to_line_col ───────────────────────────────────────────────────

    #[test]
    fn test_offset_to_line_col_first_line() {
        let source = "hello world";
        let (ln, col) = offset_to_line_col(source, 0);
        assert_eq!((ln, col), (1, 1));
    }

    #[test]
    fn test_offset_to_line_col_second_line() {
        let source = "line1\nline2";
        let (ln, col) = offset_to_line_col(source, 6);
        assert_eq!((ln, col), (2, 1));
    }

    #[test]
    fn test_offset_to_line_col_end_of_source() {
        let source = "abc";
        let (ln, col) = offset_to_line_col(source, 3);
        assert_eq!((ln, col), (1, 4));
    }

    // ── severity ordering ─────────────────────────────────────────────────────

    #[test]
    fn test_severity_ordering() {
        assert!(Severity::Error > Severity::Warning);
        assert!(Severity::Warning > Severity::Info);
        assert!(Severity::Info > Severity::Hint);
    }

    // ── DisplayConfig ─────────────────────────────────────────────────────────

    #[test]
    fn test_display_config_plain_bar() {
        let cfg = DisplayConfig::plain();
        assert_eq!(cfg.bar_char(), "|");
    }

    #[test]
    fn test_display_config_unicode_bar() {
        let cfg = DisplayConfig::colored();
        assert_eq!(cfg.bar_char(), "");
    }
}