aboutsummaryrefslogtreecommitdiff
path: root/bin/src/traits.rs
blob: 1807ad0c4e1125e346f0229fc809ea90688661ba (plain)
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
use std::{
    io::{self, Write},
    str,
};

use ariadne::{
    CharSet, Color, Config as CliConfig, Label, LabelAttach, Report as CliReport,
    ReportKind as CliReportKind, Source, Fmt
};
use lib::Report;
use rnix::TextRange;
use vfs::{FileId, ReadOnlyVfs};

#[derive(Debug)]
pub struct LintResult {
    pub file_id: FileId,
    pub reports: Vec<Report>,
}

pub trait WriteDiagnostic {
    fn write(&mut self, report: &LintResult, vfs: &ReadOnlyVfs) -> io::Result<()>;
}

impl<T> WriteDiagnostic for T
where
    T: Write,
{
    fn write(&mut self, lint_result: &LintResult, vfs: &ReadOnlyVfs) -> io::Result<()> {
        let file_id = lint_result.file_id;
        let src = str::from_utf8(vfs.get(file_id)).unwrap();
        let path = vfs.file_path(file_id);
        let range = |at: TextRange| at.start().into()..at.end().into();
        let src_id = path.to_str().unwrap_or("<unknown>");
        for report in lint_result.reports.iter() {
            let offset = report
                .diagnostics
                .iter()
                .map(|d| d.at.start().into())
                .min()
                .unwrap_or(0usize);
            report
                .diagnostics
                .iter()
                .fold(
                    CliReport::build(CliReportKind::Warning, src_id, offset)
                        .with_config(
                            CliConfig::default()
                                .with_cross_gap(true)
                                .with_multiline_arrows(false)
                                .with_label_attach(LabelAttach::Middle)
                                .with_char_set(CharSet::Unicode),
                        )
                        .with_message(report.note)
                        .with_code(report.code),
                    |cli_report, diagnostic| {
                        cli_report.with_label(
                            Label::new((src_id, range(diagnostic.at)))
                                .with_message(&colorize(&diagnostic.message))
                                .with_color(Color::Magenta),
                        )
                    },
                )
                .finish()
                .write((src_id, Source::from(src)), &mut *self)?;
        }
        Ok(())
    }
}

// everything within backticks is colorized, backticks are removed
fn colorize(message: &str) -> String {
    message.split('`')
        .enumerate()
        .map(|(idx, part)| {
            if idx % 2 == 1 {
                part.fg(Color::Cyan).to_string()
            } else {
                part.to_string()
            }
        })
        .collect::<Vec<_>>()
        .join("")
}