mago_cli/commands/
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
use clap::Parser;

use mago_interner::ThreadedInterner;
use mago_service::config::Configuration;
use mago_service::formatter::FormatterService;
use mago_service::source::SourceService;

use crate::utils::bail;

#[derive(Parser, Debug)]
#[command(
    name = "format",
    aliases = ["fmt"],
    about = "Format source files",
    long_about = r#"
Format source files.

This command will format source files according to the rules defined in the configuration file.
"#
)]
pub struct FormatCommand {
    #[arg(long, short = 'w', help = "The width of the printed source code", value_name = "WIDTH")]
    pub print_width: Option<usize>,
    #[arg(long, short = 'd', help = "Run the command without writing any changes to disk")]
    pub dry_run: bool,
}

pub async fn execute(command: FormatCommand, mut configuration: Configuration) -> i32 {
    let interner = ThreadedInterner::new();

    let source_service = SourceService::new(interner.clone(), configuration.source);
    let source_manager = source_service.load().await.unwrap_or_else(bail);

    if let Some(width) = command.print_width {
        configuration.format.print_width = Some(width);
    }

    let service = FormatterService::new(configuration.format, interner.clone(), source_manager.clone());

    let changed = service.run(command.dry_run).await.unwrap_or_else(bail);

    if changed == 0 {
        mago_feedback::info!("All source files are already formatted");

        return 0;
    }

    if command.dry_run {
        mago_feedback::info!("Found {} source files that need formatting", changed);

        1
    } else {
        mago_feedback::info!("Formatted {} source files successfully", changed);

        0
    }
}