From 47936c83fe20182ce65c5ac9c3fd4815b936c0fc Mon Sep 17 00:00:00 2001 From: Kevin Amado Date: Wed, 9 Feb 2022 22:05:23 -0500 Subject: [PATCH] feat: keep funcs implementations on same line --- flake.nix | 99 ++++++++++++++------------- src/rules/lambda.rs | 60 ++++++++++++---- tests/cases/key_value/out | 12 ++-- tests/cases/lambda/in | 4 ++ tests/cases/lambda/out | 38 +++++++---- tests/cases/pat_bind/out | 12 ++-- tests/cases/pattern/out | 140 +++++++++++++++++++------------------- 7 files changed, 207 insertions(+), 158 deletions(-) diff --git a/flake.nix b/flake.nix index 9bbde25..f987291 100644 --- a/flake.nix +++ b/flake.nix @@ -4,61 +4,68 @@ fenix.url = "github:nix-community/fenix"; fenix.inputs.nixpkgs.follows = "nixpkgs"; fenix.inputs.rust-analyzer-src.follows = "rustAnalyzer"; + flakeCompat.url = github:edolstra/flake-compat; flakeCompat.flake = false; + flakeUtils.url = "github:numtide/flake-utils"; + nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; + rustAnalyzer.url = "github:rust-analyzer/rust-analyzer"; rustAnalyzer.flake = false; + treefmt.url = "github:numtide/treefmt"; treefmt.inputs.flake-utils.follows = "flakeUtils"; treefmt.inputs.nixpkgs.follows = "nixpkgs"; }; - outputs = inputs: inputs.flakeUtils.lib.eachSystem [ "x86_64-darwin" "x86_64-linux" ] ( - system: 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 - { - checks.defaultPackage = inputs.self.defaultPackage.${system}; - defaultApp = { - type = "app"; - program = "${inputs.self.defaultPackage.${system}}/bin/alejandra"; - }; - defaultPackage = fenixPlatform.buildRustPackage { - pname = cargoToml.package.name; - version = - let - commit = inputs.self.shortRev or "dirty"; - date = inputs.self.lastModifiedDate or inputs.self.lastModified or "19700101"; - in - "${builtins.substring 0 8 date}_${commit}"; - src = inputs.self.sourceInfo; - cargoLock.lockFile = ./Cargo.lock; - meta = { - description = cargoToml.package.description; - homepage = "https://github.com/kamadorueda/alejandra"; - license = nixpkgs.lib.licenses.unlicense; - maintainers = [ nixpkgs.lib.maintainers.kamadorueda ]; + outputs = inputs: + inputs.flakeUtils.lib.eachSystem [ "x86_64-darwin" "x86_64-linux" ] ( + system: 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 + { + checks.defaultPackage = inputs.self.defaultPackage.${system}; + defaultApp = { + type = "app"; + program = "${inputs.self.defaultPackage.${system}}/bin/alejandra"; }; - }; - devShell = nixpkgs.mkShell { - name = "Alejandra"; - packages = [ - fenix.rust-analyzer - fenix.latest.toolchain - nixpkgs.cargo-tarpaulin - nixpkgs.jq - nixpkgs.nodejs - nixpkgs.nodePackages.prettier - nixpkgs.nodePackages.prettier-plugin-toml - nixpkgs.shfmt - treefmt - ]; - }; - } - ); + defaultPackage = fenixPlatform.buildRustPackage { + pname = cargoToml.package.name; + version = + let + commit = inputs.self.shortRev or "dirty"; + date = + inputs.self.lastModifiedDate or inputs.self.lastModified or "19700101"; + in + "${builtins.substring 0 8 date}_${commit}"; + src = inputs.self.sourceInfo; + cargoLock.lockFile = ./Cargo.lock; + meta = { + description = cargoToml.package.description; + homepage = "https://github.com/kamadorueda/alejandra"; + license = nixpkgs.lib.licenses.unlicense; + maintainers = [ nixpkgs.lib.maintainers.kamadorueda ]; + }; + }; + devShell = nixpkgs.mkShell { + name = "Alejandra"; + packages = [ + fenix.rust-analyzer + fenix.latest.toolchain + nixpkgs.cargo-tarpaulin + nixpkgs.jq + nixpkgs.nodejs + nixpkgs.nodePackages.prettier + nixpkgs.nodePackages.prettier-plugin-toml + nixpkgs.shfmt + treefmt + ]; + }; + } + ); } diff --git a/src/rules/lambda.rs b/src/rules/lambda.rs index 4aed374..b050cb7 100644 --- a/src/rules/lambda.rs +++ b/src/rules/lambda.rs @@ -47,14 +47,7 @@ pub fn rule( // : let child = children.get_next().unwrap(); - match layout { - crate::config::Layout::Tall => { - steps.push_back(crate::builder::Step::FormatWider(child.element)); - } - crate::config::Layout::Wide => { - steps.push_back(crate::builder::Step::Format(child.element)); - } - } + steps.push_back(crate::builder::Step::Format(child.element)); // /**/ children.drain_comments_and_newlines(|element| match element { @@ -71,19 +64,56 @@ pub fn rule( let child = children.get_next().unwrap(); match layout { crate::config::Layout::Tall => { - if is_pattern_type { - steps.push_back(crate::builder::Step::NewLine); - steps.push_back(crate::builder::Step::Pad); - } else if let rnix::SyntaxKind::TOKEN_COMMENT - | rnix::SyntaxKind::TOKEN_WHITESPACE = - child_prev.element.kind() + if is_pattern_type + || matches!( + child_prev.element.kind(), + rnix::SyntaxKind::TOKEN_COMMENT + | rnix::SyntaxKind::TOKEN_WHITESPACE + ) + || (matches!( + child.element.kind(), + rnix::SyntaxKind::NODE_LAMBDA + ) && matches!( + child + .element + .clone() + .into_node() + .unwrap() + .children() + .next() + .unwrap() + .kind(), + rnix::SyntaxKind::NODE_PATTERN + )) + || !matches!( + child.element.kind(), + rnix::SyntaxKind::NODE_ATTR_SET + | rnix::SyntaxKind::NODE_PAREN + | rnix::SyntaxKind::NODE_LAMBDA + | rnix::SyntaxKind::NODE_LET_IN + | rnix::SyntaxKind::NODE_LIST + | rnix::SyntaxKind::NODE_LITERAL + | rnix::SyntaxKind::NODE_STRING + ) { + if build_ctx.pos_new.column > 1 { + steps.push_back(crate::builder::Step::Indent); + } + steps.push_back(crate::builder::Step::NewLine); steps.push_back(crate::builder::Step::Pad); + steps.push_back(crate::builder::Step::FormatWider( + child.element, + )); + if build_ctx.pos_new.column > 1 { + steps.push_back(crate::builder::Step::Dedent); + } } else { steps.push_back(crate::builder::Step::Whitespace); + steps.push_back(crate::builder::Step::FormatWider( + child.element, + )); } - steps.push_back(crate::builder::Step::FormatWider(child.element)); } crate::config::Layout::Wide => { steps.push_back(crate::builder::Step::Whitespace); diff --git a/tests/cases/key_value/out b/tests/cases/key_value/out index e844a22..868a13c 100644 --- a/tests/cases/key_value/out +++ b/tests/cases/key_value/out @@ -85,21 +85,21 @@ rec /* b */ - { b = 1; }; + { b = 1; }; m = a: /* b */ - { - b = 1; - c = 2; - }; + { + b = 1; + c = 2; + }; n = pkgs: { }; o = { pkgs , ... }: - { }; + { }; a /* diff --git a/tests/cases/lambda/in b/tests/cases/lambda/in index bff1206..79d78f6 100644 --- a/tests/cases/lambda/in +++ b/tests/cases/lambda/in @@ -12,4 +12,8 @@ ( aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ) ({ pkgs ? import ./.. { }, locationsXml }: null) + (a: b: c: + { }: + a: b: c: + a) ] diff --git a/tests/cases/lambda/out b/tests/cases/lambda/out index a8309a9..992b95e 100644 --- a/tests/cases/lambda/out +++ b/tests/cases/lambda/out @@ -4,22 +4,23 @@ /* c */ - d + d ) ( { }: - b: - /* - c - */ - d + b: + /* + c + */ + d ) ( - a: { }: - /* - c - */ - d + a: + { }: + /* + c + */ + d ) (a: d) ( @@ -27,14 +28,15 @@ /* c */ - d + d ) ( a /* b */ - : d + : + d ) ( a @@ -45,7 +47,7 @@ /* c */ - d + d ) ( aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa @@ -57,6 +59,12 @@ { pkgs ? import ./.. { } , locationsXml }: - null + null + ) + ( + a: b: c: + { }: + a: b: c: + a ) ] diff --git a/tests/cases/pat_bind/out b/tests/cases/pat_bind/out index b484bcb..812edb9 100644 --- a/tests/cases/pat_bind/out +++ b/tests/cases/pat_bind/out @@ -4,13 +4,13 @@ { } @ /**/ a: - _ + _ ) ( { } /**/ @ a: - _ + _ ) ( { } @@ -18,7 +18,7 @@ @ /**/ a: - _ + _ ) (a @ { }: _) @@ -26,13 +26,13 @@ a @ /**/ { }: - _ + _ ) ( a /**/ @ { }: - _ + _ ) ( a @@ -40,6 +40,6 @@ @ /**/ { }: - _ + _ ) ] diff --git a/tests/cases/pattern/out b/tests/cases/pattern/out index f60344d..dbfb8a9 100644 --- a/tests/cases/pattern/out +++ b/tests/cases/pattern/out @@ -4,27 +4,27 @@ ( { /**/ }: - _ + _ ) ({ ... }: _) ( { ... /**/ }: - _ + _ ) ( { /**/ ... }: - _ + _ ) ( { /**/ ... /**/ }: - _ + _ ) ( @@ -32,7 +32,7 @@ , e , ... }: - _ + _ ) ( { b @@ -42,7 +42,7 @@ h */ }: - _ + _ ) ( { b @@ -52,7 +52,7 @@ */ ... }: - _ + _ ) ( { b @@ -65,7 +65,7 @@ h */ }: - _ + _ ) ( { b @@ -75,7 +75,7 @@ */ , ... }: - _ + _ ) ( { b @@ -88,7 +88,7 @@ h */ }: - _ + _ ) ( { b @@ -101,7 +101,7 @@ */ ... }: - _ + _ ) ( { b @@ -117,7 +117,7 @@ h */ }: - _ + _ ) ( { b @@ -127,7 +127,7 @@ e , ... }: - _ + _ ) ( { b @@ -140,7 +140,7 @@ h */ }: - _ + _ ) ( { b @@ -153,7 +153,7 @@ */ ... }: - _ + _ ) ( { b @@ -169,7 +169,7 @@ h */ }: - _ + _ ) ( { b @@ -182,7 +182,7 @@ */ , ... }: - _ + _ ) ( { b @@ -198,7 +198,7 @@ h */ }: - _ + _ ) ( { b @@ -214,7 +214,7 @@ */ ... }: - _ + _ ) ( { b @@ -233,7 +233,7 @@ h */ }: - _ + _ ) ( { b @@ -243,7 +243,7 @@ , e , ... }: - _ + _ ) ( { b @@ -256,7 +256,7 @@ h */ }: - _ + _ ) ( { b @@ -269,7 +269,7 @@ */ ... }: - _ + _ ) ( { b @@ -285,7 +285,7 @@ h */ }: - _ + _ ) ( { b @@ -298,7 +298,7 @@ */ , ... }: - _ + _ ) ( { b @@ -314,7 +314,7 @@ h */ }: - _ + _ ) ( { b @@ -330,7 +330,7 @@ */ ... }: - _ + _ ) ( { b @@ -349,7 +349,7 @@ h */ }: - _ + _ ) ( { b @@ -362,7 +362,7 @@ e , ... }: - _ + _ ) ( { b @@ -378,7 +378,7 @@ h */ }: - _ + _ ) ( { b @@ -394,7 +394,7 @@ */ ... }: - _ + _ ) ( { b @@ -413,7 +413,7 @@ h */ }: - _ + _ ) ( { b @@ -429,7 +429,7 @@ */ , ... }: - _ + _ ) ( { b @@ -448,7 +448,7 @@ h */ }: - _ + _ ) ( { b @@ -467,7 +467,7 @@ */ ... }: - _ + _ ) ( { b @@ -489,7 +489,7 @@ h */ }: - _ + _ ) ( { /* @@ -499,7 +499,7 @@ , e , ... }: - _ + _ ) ( { /* @@ -512,7 +512,7 @@ h */ }: - _ + _ ) ( { /* @@ -525,7 +525,7 @@ */ ... }: - _ + _ ) ( { /* @@ -541,7 +541,7 @@ h */ }: - _ + _ ) ( { /* @@ -554,7 +554,7 @@ */ , ... }: - _ + _ ) ( { /* @@ -570,7 +570,7 @@ h */ }: - _ + _ ) ( { /* @@ -586,7 +586,7 @@ */ ... }: - _ + _ ) ( { /* @@ -605,7 +605,7 @@ h */ }: - _ + _ ) ( { /* @@ -618,7 +618,7 @@ e , ... }: - _ + _ ) ( { /* @@ -634,7 +634,7 @@ h */ }: - _ + _ ) ( { /* @@ -650,7 +650,7 @@ */ ... }: - _ + _ ) ( { /* @@ -669,7 +669,7 @@ h */ }: - _ + _ ) ( { /* @@ -685,7 +685,7 @@ */ , ... }: - _ + _ ) ( { /* @@ -704,7 +704,7 @@ h */ }: - _ + _ ) ( { /* @@ -723,7 +723,7 @@ */ ... }: - _ + _ ) ( { /* @@ -745,7 +745,7 @@ h */ }: - _ + _ ) ( { /* @@ -758,7 +758,7 @@ , e , ... }: - _ + _ ) ( { /* @@ -774,7 +774,7 @@ h */ }: - _ + _ ) ( { /* @@ -790,7 +790,7 @@ */ ... }: - _ + _ ) ( { /* @@ -809,7 +809,7 @@ h */ }: - _ + _ ) ( { /* @@ -825,7 +825,7 @@ */ , ... }: - _ + _ ) ( { /* @@ -844,7 +844,7 @@ h */ }: - _ + _ ) ( { /* @@ -863,7 +863,7 @@ */ ... }: - _ + _ ) ( { /* @@ -885,7 +885,7 @@ h */ }: - _ + _ ) ( { /* @@ -901,7 +901,7 @@ e , ... }: - _ + _ ) ( { /* @@ -920,7 +920,7 @@ h */ }: - _ + _ ) ( { /* @@ -939,7 +939,7 @@ */ ... }: - _ + _ ) ( { /* @@ -961,7 +961,7 @@ h */ }: - _ + _ ) ( { /* @@ -980,7 +980,7 @@ */ , ... }: - _ + _ ) ( { /* @@ -1002,7 +1002,7 @@ h */ }: - _ + _ ) ( { /* @@ -1024,7 +1024,7 @@ */ ... }: - _ + _ ) ( { /* @@ -1049,7 +1049,7 @@ h */ }: - _ + _ ) ({ a ? null }: _) @@ -1092,7 +1092,7 @@ h */ }: - _ + _ ) ( @@ -1156,6 +1156,6 @@ j */ # - _ + _ ) ]