rune/cli/
format.rs

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
use std::fmt;
use std::io::Write;
use std::path::PathBuf;

use similar::{ChangeTag, TextDiff};

use crate::alloc::prelude::*;
use crate::alloc::BTreeSet;
use crate::cli::{AssetKind, CommandBase, Config, Entry, EntryPoint, ExitCode, Io, SharedFlags};
use crate::support::{Context, Result};
use crate::termcolor::{Color, ColorSpec, WriteColor};
use crate::{Diagnostics, Options, Source, Sources};

mod cli {
    use std::path::PathBuf;
    use std::vec::Vec;

    use clap::Parser;

    #[derive(Parser, Debug)]
    #[command(rename_all = "kebab-case")]
    pub(crate) struct Flags {
        /// Exit with a non-zero exit-code even for warnings
        #[arg(long)]
        pub(super) warnings_are_errors: bool,
        /// Perform format checking. If there's any files which needs to be changed
        /// returns a non-successful exitcode.
        #[arg(long)]
        pub(super) check: bool,
        /// Explicit paths to format.
        pub(super) fmt_path: Vec<PathBuf>,
    }
}

pub(super) use cli::Flags;

impl CommandBase for Flags {
    #[inline]
    fn is_workspace(&self, _: AssetKind) -> bool {
        true
    }

    #[inline]
    fn describe(&self) -> &str {
        "Formatting"
    }

    /// Extra paths to run.
    #[inline]
    fn paths(&self) -> &[PathBuf] {
        &self.fmt_path
    }
}

pub(super) fn run<'m, I>(
    io: &mut Io<'_>,
    entry: &mut Entry<'_>,
    c: &Config,
    entrys: I,
    flags: &Flags,
    shared: &SharedFlags,
    options: &Options,
) -> Result<ExitCode>
where
    I: IntoIterator<Item = EntryPoint<'m>>,
{
    let col = Colors::new();

    let mut changed = 0u32;
    let mut failed = 0u32;
    let mut unchanged = 0u32;
    let mut failed_builds = 0u32;

    let context = shared.context(entry, c, None)?;

    let mut paths = BTreeSet::new();

    for e in entrys {
        // NB: We don't have to build argument entries to discover all relevant
        // modules.
        if e.is_argument() {
            paths.try_insert(e.path().try_to_owned()?)?;
            continue;
        }

        let mut diagnostics = if shared.warnings || flags.warnings_are_errors {
            Diagnostics::new()
        } else {
            Diagnostics::without_warnings()
        };

        let mut sources = Sources::new();

        sources.insert(match Source::from_path(e.path()) {
            Ok(source) => source,
            Err(error) => return Err(error).context(e.path().display().try_to_string()?),
        })?;

        let _ = crate::prepare(&mut sources)
            .with_context(&context)
            .with_diagnostics(&mut diagnostics)
            .with_options(options)
            .build();

        diagnostics.emit(&mut io.stdout.lock(), &sources)?;

        if diagnostics.has_error() || flags.warnings_are_errors && diagnostics.has_warning() {
            failed_builds += 1;
        }

        for source in sources.iter() {
            if let Some(path) = source.path() {
                paths.try_insert(path.try_to_owned()?)?;
            }
        }
    }

    for path in paths {
        let mut sources = Sources::new();

        sources.insert(match Source::from_path(&path) {
            Ok(source) => source,
            Err(error) => return Err(error).context(path.display().try_to_string()?),
        })?;

        let mut diagnostics = Diagnostics::new();

        let build = crate::fmt::prepare(&sources)
            .with_options(options)
            .with_diagnostics(&mut diagnostics);

        let result = build.format();

        if !diagnostics.is_empty() {
            diagnostics.emit(io.stdout, &sources)?;
        }

        let Ok(formatted) = result else {
            failed += 1;
            continue;
        };

        for (id, formatted) in formatted {
            let Some(source) = sources.get(id) else {
                continue;
            };

            let same = source.as_str() == formatted;

            if same {
                unchanged += 1;

                if shared.verbose {
                    io.stdout.set_color(&col.green)?;
                    write!(io.stdout, "== ")?;
                    io.stdout.reset()?;
                    writeln!(io.stdout, "{}", source.name())?;
                }

                continue;
            }

            changed += 1;

            if shared.verbose || flags.check {
                io.stdout.set_color(&col.yellow)?;
                write!(io.stdout, "++ ")?;
                io.stdout.reset()?;
                writeln!(io.stdout, "{}", source.name())?;
                diff(io, source.as_str(), &formatted, &col)?;
            }

            if !flags.check {
                if let Some(path) = source.path() {
                    std::fs::write(path, &formatted)?;
                }
            }
        }
    }

    if shared.verbose && unchanged > 0 {
        io.stdout.set_color(&col.green)?;
        write!(io.stdout, "{}", unchanged)?;
        io.stdout.reset()?;
        writeln!(io.stdout, " unchanged")?;
    }

    if shared.verbose && changed > 0 {
        io.stdout.set_color(&col.yellow)?;
        write!(io.stdout, "{}", changed)?;
        io.stdout.reset()?;
        writeln!(io.stdout, " changed")?;
    }

    if shared.verbose || failed > 0 {
        io.stdout.set_color(&col.red)?;
        write!(io.stdout, "{}", failed)?;
        io.stdout.reset()?;
        writeln!(io.stdout, " failed")?;
    }

    if shared.verbose || failed_builds > 0 {
        io.stdout.set_color(&col.red)?;
        write!(io.stdout, "{}", failed_builds)?;
        io.stdout.reset()?;
        writeln!(io.stdout, " failed builds")?;
    }

    if flags.check && changed > 0 {
        io.stdout.set_color(&col.red)?;
        writeln!(
            io.stdout,
            "Failure due to `--check` flag and unformatted files."
        )?;
        io.stdout.reset()?;
        return Ok(ExitCode::Failure);
    }

    if failed > 0 || failed_builds > 0 {
        return Ok(ExitCode::Failure);
    }

    Ok(ExitCode::Success)
}

fn diff(io: &mut Io, source: &str, val: &str, col: &Colors) -> Result<(), anyhow::Error> {
    let diff = TextDiff::from_lines(source, val);

    for (idx, group) in diff.grouped_ops(3).iter().enumerate() {
        if idx > 0 {
            println!("{:-^1$}", "-", 80);
        }

        for op in group {
            for change in diff.iter_inline_changes(op) {
                let (sign, color) = match change.tag() {
                    ChangeTag::Delete => ("-", &col.red),
                    ChangeTag::Insert => ("+", &col.green),
                    ChangeTag::Equal => (" ", &col.dim),
                };

                io.stdout.set_color(color)?;

                write!(io.stdout, "{}", Line(change.old_index()))?;
                write!(io.stdout, "{sign}")?;

                for (_, value) in change.iter_strings_lossy() {
                    write!(io.stdout, "{value}")?;
                }

                io.stdout.reset()?;

                if change.missing_newline() {
                    writeln!(io.stdout)?;
                }
            }
        }
    }

    Ok(())
}

struct Line(Option<usize>);

impl fmt::Display for Line {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.0 {
            None => write!(f, "    "),
            Some(idx) => write!(f, "{:<4}", idx + 1),
        }
    }
}

struct Colors {
    red: ColorSpec,
    green: ColorSpec,
    yellow: ColorSpec,
    dim: ColorSpec,
}

impl Colors {
    fn new() -> Self {
        let mut this = Self {
            red: ColorSpec::new(),
            green: ColorSpec::new(),
            yellow: ColorSpec::new(),
            dim: ColorSpec::new(),
        };

        this.red.set_fg(Some(Color::Red));
        this.green.set_fg(Some(Color::Green));
        this.yellow.set_fg(Some(Color::Yellow));

        this
    }
}