From 0f227ee76acda65498b7e6aa820c767eb8423473 Mon Sep 17 00:00:00 2001 From: David Arnold Date: Sat, 29 Jan 2022 20:21:22 -0500 Subject: [PATCH 1/4] chore: add treefmt - to format them all - this is a unified & modern approch to keeping all files according to fmt-spec - has excellent editor integration --- flake.lock | 39 ++++++++++++++++++++++++++++++++++++++- flake.nix | 7 +++++++ treefmt.toml | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 treefmt.toml diff --git a/flake.lock b/flake.lock index a16c929..9f292f6 100644 --- a/flake.lock +++ b/flake.lock @@ -21,6 +21,21 @@ "type": "github" } }, + "flake-utils": { + "locked": { + "lastModified": 1610051610, + "narHash": "sha256-U9rPz/usA1/Aohhk7Cmc2gBrEEKRzcW4nwPWMPwja4Y=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "3982c9903e93927c2164caa727cd3f6a0e6d14cc", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, "flakeCompat": { "flake": false, "locked": { @@ -73,7 +88,8 @@ "fenix": "fenix", "flakeCompat": "flakeCompat", "flakeUtils": "flakeUtils", - "nixpkgs": "nixpkgs" + "nixpkgs": "nixpkgs", + "treefmt": "treefmt" } }, "rust-analyzer-src": { @@ -92,6 +108,27 @@ "repo": "rust-analyzer", "type": "github" } + }, + "treefmt": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1640364438, + "narHash": "sha256-sV1Oa9JVNmpAwZ4cnVs7ovHrKL8EPg3J08jPX/a48LI=", + "owner": "numtide", + "repo": "treefmt", + "rev": "26d7f5f15e5d22d4413ba7c5ae0447fdc0b5cc76", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt", + "type": "github" + } } }, "root": "root", diff --git a/flake.nix b/flake.nix index c9ddd38..c311fd9 100644 --- a/flake.nix +++ b/flake.nix @@ -5,6 +5,8 @@ flakeCompat.url = github:edolstra/flake-compat; flakeCompat.flake = false; flakeUtils.url = "github:numtide/flake-utils"; + treefmt.url = "github:numtide/treefmt"; + treefmt.inputs.nixpkgs.follows = "nixpkgs"; nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; }; outputs = @@ -15,6 +17,7 @@ let nixpkgs = import inputs.nixpkgs { inherit system; }; cargoToml = builtins.fromTOML ( builtins.readFile ./Cargo.toml ); + treefmt = inputs.treefmt.defaultPackage.${ system }; fenix = inputs.fenix.packages.${ system }; fenixPlatform = nixpkgs.makeRustPlatform { inherit ( fenix.latest ) cargo rustc; }; in @@ -53,7 +56,11 @@ fenix.latest.rust-src fenix.latest.rustc fenix.latest.rustfmt + treefmt + nixpkgs.shfmt + nixpkgs.nodePackages.prettier ]; + shellHook = "cargo build"; }; } ); diff --git a/treefmt.toml b/treefmt.toml new file mode 100644 index 0000000..944d008 --- /dev/null +++ b/treefmt.toml @@ -0,0 +1,32 @@ +# One CLI to format the code tree - https://github.com/numtide/treefmt + +[formatter.nix] +command = "./target/debug/alejandra" +includes = ["*.nix"] + +[formatter.prettier] +command = "prettier" +options = ["--write"] +includes = [ + "*.css", + "*.html", + "*.js", + "*.json", + "*.jsx", + "*.md", + "*.mdx", + "*.scss", + "*.ts", + "*.yaml", +] + +[formatter.shell] +command = "shfmt" +options = [ + "-i", + "2", # indent 2 + "-s", # simplify the code + "-w", # write back to the file +] +includes = ["*.sh"] + From baa158d51c983cd54afeef81508f24be62f287d6 Mon Sep 17 00:00:00 2001 From: David Arnold Date: Sat, 29 Jan 2022 20:32:36 -0500 Subject: [PATCH 2/4] style: treefmt all the things --- README.md | 18 ++++++++---------- treefmt.toml | 6 ++++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index de9b3dd..8f8c3a2 100644 --- a/README.md +++ b/README.md @@ -40,12 +40,12 @@ [^benchmark-specs] | Cores | Seconds | - |:-----:|:--------: - | 1 | 40 | - | 2 | 21 | - | 4 | 15 | - | 8 | 11 | - | 16 | 10 | + | :---: | :-----: | + | 1 | 40 | + | 2 | 21 | + | 4 | 15 | + | 8 | 11 | + | 16 | 10 | - ✔️ **Powerful** @@ -84,6 +84,7 @@ Let's get Alejandra on our systems: - Nix stable: Pick one depending on your platform: + ```bash $ nix-env -ivA aarch64-darwin -f https://github.com/kamadorueda/alejandra/tarball/main $ nix-env -ivA aarch64-linux -f https://github.com/kamadorueda/alejandra/tarball/main @@ -118,7 +119,6 @@ Let's get Alejandra on our systems: ## Footnotes [^benchmark-specs]: - Running on a [machine](https://github.com/kamadorueda/machine) with: - CPU: 16 x Intel(R) Core(TM) i7-10700K @@ -126,9 +126,7 @@ Let's get Alejandra on our systems: - BogoMips: 7599.80 - Cache Size: 16384 KB -[^semantic-changes]: - - The methodology to claim this is: +[^semantic-changes]: The methodology to claim this is: 1. Checkout [Nixpkgs](https://github.com/nixos/nixpkgs) and run: diff --git a/treefmt.toml b/treefmt.toml index 944d008..cd3f5ed 100644 --- a/treefmt.toml +++ b/treefmt.toml @@ -23,8 +23,10 @@ includes = [ [formatter.shell] command = "shfmt" options = [ - "-i", - "2", # indent 2 + "-bn", # binary ops like && and | may start a line + "-ci", # switch cases will be indented + "-sr", # redirect operators will be followed by a space + "-i", "2", # indent 2 "-s", # simplify the code "-w", # write back to the file ] From 825377e6683fb4ec9c3828d37fb94480bd12ede2 Mon Sep 17 00:00:00 2001 From: David Arnold Date: Sat, 29 Jan 2022 20:38:46 -0500 Subject: [PATCH 3/4] style: treefmt w/ rustfmt, as well --- treefmt.toml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/treefmt.toml b/treefmt.toml index cd3f5ed..8b52479 100644 --- a/treefmt.toml +++ b/treefmt.toml @@ -20,6 +20,15 @@ includes = [ "*.yaml", ] +[formatter.rust] +command = "rustfmt" +options = [ + "--edition", "2021", + "--config", + "enum_discrim_align_threshold=999,format_code_in_doc_comments=true,format_generated_files=true,format_macro_bodies=true,format_macro_matchers=true,format_strings=true,hex_literal_case=Lower,imports_granularity=Crate,imports_layout=HorizontalVertical,max_width=80,normalize_comments=true,normalize_doc_attributes=true,reorder_impl_items=true,struct_field_align_threshold=999,unstable_features=true,use_small_heuristics=Max,version=Two", +] +includes = ["*.rs"] + [formatter.shell] command = "shfmt" options = [ From 7179111c29670f2e224a53d463c0f25e0ee8e0cd Mon Sep 17 00:00:00 2001 From: David Arnold Date: Sat, 29 Jan 2022 21:32:04 -0500 Subject: [PATCH 4/4] chore: cleaner rustfmt args --- treefmt.toml | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/treefmt.toml b/treefmt.toml index 8b52479..816146c 100644 --- a/treefmt.toml +++ b/treefmt.toml @@ -25,7 +25,23 @@ command = "rustfmt" options = [ "--edition", "2021", "--config", - "enum_discrim_align_threshold=999,format_code_in_doc_comments=true,format_generated_files=true,format_macro_bodies=true,format_macro_matchers=true,format_strings=true,hex_literal_case=Lower,imports_granularity=Crate,imports_layout=HorizontalVertical,max_width=80,normalize_comments=true,normalize_doc_attributes=true,reorder_impl_items=true,struct_field_align_threshold=999,unstable_features=true,use_small_heuristics=Max,version=Two", + """enum_discrim_align_threshold=999,\ + format_code_in_doc_comments=true,\ + format_generated_files=true,\ + format_macro_bodies=true,\ + format_macro_matchers=true,\ + format_strings=true,\ + hex_literal_case=Lower,\ + imports_granularity=Crate,\ + imports_layout=HorizontalVertical,\ + max_width=80,\ + normalize_comments=true,\ + normalize_doc_attributes=true,\ + reorder_impl_items=true,\ + struct_field_align_threshold=999,\ + unstable_features=true,\ + use_small_heuristics=Max,\ + version=Two""", ] includes = ["*.rs"]