Skip to content

Commit

Permalink
Merge pull request #440 from moonbitlang/moon-fmt-block-style
Browse files Browse the repository at this point in the history
add `moon fmt --block-style`
  • Loading branch information
Young-Flash authored Nov 4, 2024
2 parents 9ed4cf9 + 61c878d commit 62b13a6
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 4 deletions.
5 changes: 5 additions & 0 deletions crates/moon/src/cli/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ pub struct FmtSubcommand {
#[clap(long)]
pub sort_input: bool,

/// Add separator between each segments
#[clap(long)]
pub block_style: bool,

pub args: Vec<String>,
}

Expand Down Expand Up @@ -68,6 +72,7 @@ pub fn run_fmt(cli: &UniversalFlags, cmd: FmtSubcommand) -> anyhow::Result<i32>
run_mode,
fmt_opt: Some(FmtOpt {
check: cmd.check,
block_style: cmd.block_style,
extra_args: cmd.args,
}),
build_graph: cli.build_graph,
Expand Down
18 changes: 14 additions & 4 deletions crates/moon/src/cli/tool/format_and_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,25 @@ pub struct FormatAndDiffSubcommand {
#[clap(long)]
new: PathBuf,

/// Add separator between each segments
#[clap(long)]
block_style: bool,

pub args: Vec<String>,
}

pub fn run_format_and_diff(cmd: FormatAndDiffSubcommand) -> anyhow::Result<i32> {
let mut args = vec![
"-exit-code",
cmd.old.to_str().unwrap(),
"-o",
cmd.new.to_str().unwrap(),
];
if cmd.block_style {
args.push("-block-style")
}
let mut execution = std::process::Command::new("moonfmt")
.arg("-exit-code")
.arg(cmd.old.to_str().unwrap())
.arg("-o")
.arg(cmd.new.to_str().unwrap())
.args(args)
.args(&cmd.args)
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
Expand Down
31 changes: 31 additions & 0 deletions crates/moon/tests/test_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4001,6 +4001,37 @@ fn test_moon_fmt_extra_args() {
);
}

#[test]
fn test_moon_fmt_block_style() {
let dir = TestDir::new("moon_fmt.in");
check(
get_stdout(&dir, ["fmt", "--block-style", "--sort-input", "--dry-run"]),
expect![[r#"
moonfmt ./lib/hello.mbt -w -o ./target/wasm-gc/release/format/lib/hello.mbt -block-style
moonfmt ./lib/hello_wbtest.mbt -w -o ./target/wasm-gc/release/format/lib/hello_wbtest.mbt -block-style
moonfmt ./main/main.mbt -w -o ./target/wasm-gc/release/format/main/main.mbt -block-style
"#]],
);

check(
get_stdout(
&dir,
[
"fmt",
"--block-style",
"--check",
"--sort-input",
"--dry-run",
],
),
expect![[r#"
moon tool format-and-diff --old ./lib/hello.mbt --new ./target/wasm-gc/release/format/lib/hello.mbt --block-style
moon tool format-and-diff --old ./lib/hello_wbtest.mbt --new ./target/wasm-gc/release/format/lib/hello_wbtest.mbt --block-style
moon tool format-and-diff --old ./main/main.mbt --new ./target/wasm-gc/release/format/main/main.mbt --block-style
"#]],
);
}

#[test]
fn test_export_memory_name() {
let dir = TestDir::new("export_memory.in");
Expand Down
8 changes: 8 additions & 0 deletions crates/moonbuild/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ fn gen_inplace_fmt_command(
.arg("-o")
.arg(&item.phony_out)
.args(&moonbuild_opt.fmt_opt.as_ref().unwrap().extra_args)
.arg_with_cond(
moonbuild_opt.fmt_opt.as_ref().unwrap().block_style,
"-block-style",
)
.build();
build.cmdline = Some(command);
build.desc = Some(format!("moonfmt {}", item.input));
Expand Down Expand Up @@ -233,6 +237,10 @@ fn gen_fmt_to_command(
.arg(&item.input)
.arg("--new")
.arg(&item.output)
.arg_with_cond(
moonbuild_opt.fmt_opt.as_ref().unwrap().block_style,
"--block-style",
)
.args(&moonbuild_opt.fmt_opt.as_ref().unwrap().extra_args)
.build();
build.cmdline = Some(command);
Expand Down
1 change: 1 addition & 0 deletions crates/moonutil/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ pub struct TestArtifacts {
#[derive(Debug, Clone, Default)]
pub struct FmtOpt {
pub check: bool,
pub block_style: bool,
pub extra_args: Vec<String>,
}

Expand Down
1 change: 1 addition & 0 deletions docs/manual-zh/src/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ Format source code

* `--check` — Check only and don't change the source code
* `--sort-input` — Sort input files
* `--block-style` — Add separator between each segments



Expand Down
1 change: 1 addition & 0 deletions docs/manual/src/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ Format source code

* `--check` — Check only and don't change the source code
* `--sort-input` — Sort input files
* `--block-style` — Add separator between each segments



Expand Down

0 comments on commit 62b13a6

Please sign in to comment.