From 14ed5b9fc8e8042d797a0e42a77c3f208575cfc1 Mon Sep 17 00:00:00 2001 From: Kevin Amado Date: Tue, 1 Feb 2022 19:24:54 -0500 Subject: [PATCH] feat: natural apply --- default.nix | 10 ++-- flake.nix | 109 ++++++++++++++++++++--------------------- src/rules/apply.rs | 66 +++++++++++++------------ src/rules/key_value.rs | 3 +- 4 files changed, 93 insertions(+), 95 deletions(-) diff --git a/default.nix b/default.nix index fcf390b..99145dc 100644 --- a/default.nix +++ b/default.nix @@ -2,12 +2,10 @@ let lockData = builtins.readFile ./flake.lock; lock = builtins.fromJSON lockData; flakeCompat = lock.nodes.flakeCompat.locked; - flakeCompatSrc = - builtins.fetchTarball - { - url = "https://github.com/edolstra/flake-compat/archive/${flakeCompat.rev}.tar.gz"; - sha256 = flakeCompat.narHash; - }; + flakeCompatSrc = builtins.fetchTarball { + url = "https://github.com/edolstra/flake-compat/archive/${flakeCompat.rev}.tar.gz"; + sha256 = flakeCompat.narHash; + }; flake = import flakeCompatSrc { src = ./.; }; in flake.defaultNix.defaultPackage diff --git a/flake.nix b/flake.nix index 9c1987e..1b28b6a 100644 --- a/flake.nix +++ b/flake.nix @@ -15,62 +15,57 @@ }; 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"; + 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 ]; }; - 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.cargo - fenix.latest.clippy - fenix.latest.rust-src - fenix.latest.rustc - fenix.latest.rustfmt - nixpkgs.cargo-tarpaulin - nixpkgs.jq - nixpkgs.nodejs - nixpkgs.nodePackages.prettier - nixpkgs.nodePackages.prettier-plugin-toml - nixpkgs.shfmt - treefmt - ]; - }; - } - ); + }; + devShell = nixpkgs.mkShell { + name = "Alejandra"; + packages = [ + fenix.rust-analyzer + fenix.latest.cargo + fenix.latest.clippy + fenix.latest.rust-src + fenix.latest.rustc + fenix.latest.rustfmt + nixpkgs.cargo-tarpaulin + nixpkgs.jq + nixpkgs.nodejs + nixpkgs.nodePackages.prettier + nixpkgs.nodePackages.prettier-plugin-toml + nixpkgs.shfmt + treefmt + ]; + }; + } + ); } diff --git a/src/rules/apply.rs b/src/rules/apply.rs index 9efd0dd..8f24657 100644 --- a/src/rules/apply.rs +++ b/src/rules/apply.rs @@ -12,46 +12,50 @@ pub fn rule( build_ctx.config.layout() }; - let child = children.get_next().unwrap(); - match layout { - crate::config::Layout::Tall => { - match child.element.kind() { - rnix::SyntaxKind::NODE_APPLY => { - steps - .push_back(crate::builder::Step::Format(child.element)); - } - _ => { - steps.push_back(crate::builder::Step::FormatWider( - child.element, - )); - } - } - steps.push_back(crate::builder::Step::Indent); - steps.push_back(crate::builder::Step::NewLine); - steps.push_back(crate::builder::Step::Pad); - } - crate::config::Layout::Wide => { - steps.push_back(crate::builder::Step::Format(child.element)); - steps.push_back(crate::builder::Step::Whitespace); - } - } - - children.drain_comments(|text| { - steps.push_back(crate::builder::Step::Comment(text)); - steps.push_back(crate::builder::Step::NewLine); - steps.push_back(crate::builder::Step::Pad); - }); - + // left let child = children.get_next().unwrap(); match layout { crate::config::Layout::Tall => { steps.push_back(crate::builder::Step::FormatWider(child.element)); - steps.push_back(crate::builder::Step::Dedent); } crate::config::Layout::Wide => { steps.push_back(crate::builder::Step::Format(child.element)); } } + // /**/ + children.drain_comments(|text| { + steps.push_back(crate::builder::Step::NewLine); + steps.push_back(crate::builder::Step::Pad); + steps.push_back(crate::builder::Step::Comment(text)); + }); + + let child_prev = children.peek_prev().unwrap(); + + // right + let child = children.get_next().unwrap(); + match layout { + crate::config::Layout::Tall => { + if let rnix::SyntaxKind::TOKEN_COMMENT = child_prev.element.kind() { + steps.push_back(crate::builder::Step::NewLine); + steps.push_back(crate::builder::Step::Pad); + } else if let rnix::SyntaxKind::NODE_ATTR_SET + | rnix::SyntaxKind::NODE_LIST + | rnix::SyntaxKind::NODE_PAREN + | rnix::SyntaxKind::NODE_STRING = child.element.kind() + { + steps.push_back(crate::builder::Step::Whitespace); + } else { + steps.push_back(crate::builder::Step::NewLine); + steps.push_back(crate::builder::Step::Pad); + }; + steps.push_back(crate::builder::Step::FormatWider(child.element)); + } + crate::config::Layout::Wide => { + steps.push_back(crate::builder::Step::Whitespace); + steps.push_back(crate::builder::Step::Format(child.element)); + } + } + steps } diff --git a/src/rules/key_value.rs b/src/rules/key_value.rs index 1d05dca..3dd6e06 100644 --- a/src/rules/key_value.rs +++ b/src/rules/key_value.rs @@ -45,7 +45,8 @@ pub fn rule( steps.push_back(crate::builder::Step::Format(child.element)); match layout { crate::config::Layout::Tall => { - if let rnix::SyntaxKind::NODE_ATTR_SET + if let rnix::SyntaxKind::NODE_APPLY + | rnix::SyntaxKind::NODE_ATTR_SET | rnix::SyntaxKind::NODE_LIST | rnix::SyntaxKind::NODE_PAREN | rnix::SyntaxKind::NODE_STRING =