Skip to content

Commit

Permalink
fix: some improvements to mise fmt (#3615)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx authored Dec 16, 2024
1 parent 4d4f9ff commit 9edf8ef
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 4 deletions.
4 changes: 3 additions & 1 deletion docs/cli/fmt.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

Formats mise.toml

Sorts keys and cleans up whitespace in mise.toml

## Flags

### `-a --all`
Expand All @@ -14,5 +16,5 @@ Format all files from the current directory
Examples:

```
mise format
mise fmt
```
43 changes: 43 additions & 0 deletions e2e/config/test_config_fmt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash

exit 0 # this isn't working right
cat <<EOF >mise.toml
env_path = "."
env_file = ".env"
min_version = "0"
[tasks.a]
[tools]
[tasks.c]
[task_config]
[tasks.b]
[_]
[alias]
[env]
[hooks]
[plugins]
[redactions]
[[watch_files]]
patterns = [ "src/**/*.rs" ]
run = "test"
[vars]
[settings]
EOF

assert "mise fmt"
assert 'cat mise.toml' 'min_version = "0"
env_file = ".env"
env_path = "."
[_]
[env]
[vars]
[hooks]
[[watch_files]]
[tools]
[tasks.a]
[tasks.b]
[tasks.c]
[task_config]
[redactions]
[alias]
[plugins]
[settings]'
5 changes: 4 additions & 1 deletion mise.usage.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -451,9 +451,12 @@ The "--" separates runtimes from the commands to pass along to the subprocess."#
arg "[COMMAND]..." help="Command string to execute (same as --command)" var=true
}
cmd "fmt" help="Formats mise.toml" {
long_help r"Formats mise.toml

Sorts keys and cleans up whitespace in mise.toml"
after_long_help r"Examples:

$ mise format
$ mise fmt
"
flag "-a --all" help="Format all files from the current directory"
}
Expand Down
35 changes: 33 additions & 2 deletions src/cli/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use crate::config::ALL_TOML_CONFIG_FILES;
use crate::Result;
use crate::{config, dirs, file};
use eyre::bail;
use taplo::formatter::Options;

/// Formats mise.toml
///
/// Sorts keys and cleans up whitespace in mise.toml
#[derive(Debug, clap::Args)]
#[clap(verbatim_doc_comment, after_long_help = AFTER_LONG_HELP)]
pub struct Fmt {
Expand All @@ -24,10 +27,14 @@ impl Fmt {
bail!("No config file found in current directory");
}
for p in configs {
if !p.ends_with("toml") {
if !p
.file_name()
.is_some_and(|f| f.to_string_lossy().ends_with("toml"))
{
continue;
}
let toml = file::read_to_string(&p)?;
let toml = sort(toml)?;
let toml = taplo::formatter::format(
&toml,
Options {
Expand Down Expand Up @@ -59,9 +66,33 @@ impl Fmt {
}
}

fn sort(toml: String) -> Result<String> {
let mut doc: toml_edit::DocumentMut = toml.parse()?;
let order = |k: String| match k.as_str() {
"min_version" => 0,
"env_file" => 1,
"env_path" => 2,
"_" => 3,
"env" => 4,
"vars" => 5,
"hooks" => 6,
"watch_files" => 7,
"tools" => 8,
"tasks" => 10,
"task_config" => 11,
"redactions" => 12,
"alias" => 13,
"plugins" => 14,
"settings" => 15,
_ => 9,
};
doc.sort_values_by(|a, _, b, _| order(a.to_string()).cmp(&order(b.to_string())));
Ok(doc.to_string())
}

static AFTER_LONG_HELP: &str = color_print::cstr!(
r#"<bold><underline>Examples:</underline></bold>
$ <bold>mise format</bold>
$ <bold>mise fmt</bold>
"#
);

0 comments on commit 9edf8ef

Please sign in to comment.