1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-07-31 04:27:45 +00:00

feat: natural apply

This commit is contained in:
Kevin Amado 2022-02-01 19:24:54 -05:00
parent 863976c10f
commit 14ed5b9fc8
No known key found for this signature in database
GPG key ID: FFF341057F503148
4 changed files with 93 additions and 95 deletions

View file

@ -2,12 +2,10 @@ let
lockData = builtins.readFile ./flake.lock; lockData = builtins.readFile ./flake.lock;
lock = builtins.fromJSON lockData; lock = builtins.fromJSON lockData;
flakeCompat = lock.nodes.flakeCompat.locked; flakeCompat = lock.nodes.flakeCompat.locked;
flakeCompatSrc = flakeCompatSrc = builtins.fetchTarball {
builtins.fetchTarball url = "https://github.com/edolstra/flake-compat/archive/${flakeCompat.rev}.tar.gz";
{ sha256 = flakeCompat.narHash;
url = "https://github.com/edolstra/flake-compat/archive/${flakeCompat.rev}.tar.gz"; };
sha256 = flakeCompat.narHash;
};
flake = import flakeCompatSrc { src = ./.; }; flake = import flakeCompatSrc { src = ./.; };
in in
flake.defaultNix.defaultPackage flake.defaultNix.defaultPackage

109
flake.nix
View file

@ -15,62 +15,57 @@
}; };
outputs = outputs =
inputs: inputs:
inputs.flakeUtils.lib.eachSystem inputs.flakeUtils.lib.eachSystem [ "x86_64-darwin" "x86_64-linux" ] (
[ "x86_64-darwin" "x86_64-linux" ] system:
( let
system: nixpkgs = import inputs.nixpkgs { inherit system; };
let cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
nixpkgs = import inputs.nixpkgs { inherit system; }; treefmt = inputs.treefmt.defaultPackage.${system};
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml); fenix = inputs.fenix.packages.${system};
treefmt = inputs.treefmt.defaultPackage.${system}; fenixPlatform = nixpkgs.makeRustPlatform { inherit (fenix.latest) cargo rustc; };
fenix = inputs.fenix.packages.${system}; in
fenixPlatform = nixpkgs.makeRustPlatform { inherit (fenix.latest) cargo rustc; }; {
in checks = { defaultPackage = inputs.self.defaultPackage.${system}; };
{ defaultApp = {
checks = { defaultPackage = inputs.self.defaultPackage.${system}; }; type = "app";
defaultApp = { program = "${inputs.self.defaultPackage.${system}}/bin/alejandra";
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 devShell = nixpkgs.mkShell {
{ name = "Alejandra";
pname = cargoToml.package.name; packages = [
version = fenix.rust-analyzer
let fenix.latest.cargo
commit = inputs.self.shortRev or "dirty"; fenix.latest.clippy
date = inputs.self.lastModifiedDate or inputs.self.lastModified or "19700101"; fenix.latest.rust-src
in fenix.latest.rustc
"${builtins.substring 0 8 date}_${commit}"; fenix.latest.rustfmt
src = inputs.self.sourceInfo; nixpkgs.cargo-tarpaulin
cargoLock.lockFile = ./Cargo.lock; nixpkgs.jq
meta = { nixpkgs.nodejs
description = cargoToml.package.description; nixpkgs.nodePackages.prettier
homepage = "https://github.com/kamadorueda/alejandra"; nixpkgs.nodePackages.prettier-plugin-toml
license = nixpkgs.lib.licenses.unlicense; nixpkgs.shfmt
maintainers = [ nixpkgs.lib.maintainers.kamadorueda ]; 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
];
};
}
);
} }

View file

@ -12,46 +12,50 @@ pub fn rule(
build_ctx.config.layout() build_ctx.config.layout()
}; };
let child = children.get_next().unwrap(); // left
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);
});
let child = children.get_next().unwrap(); let child = children.get_next().unwrap();
match layout { match layout {
crate::config::Layout::Tall => { crate::config::Layout::Tall => {
steps.push_back(crate::builder::Step::FormatWider(child.element)); steps.push_back(crate::builder::Step::FormatWider(child.element));
steps.push_back(crate::builder::Step::Dedent);
} }
crate::config::Layout::Wide => { 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(|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 steps
} }

View file

@ -45,7 +45,8 @@ pub fn rule(
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child.element));
match layout { match layout {
crate::config::Layout::Tall => { 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_LIST
| rnix::SyntaxKind::NODE_PAREN | rnix::SyntaxKind::NODE_PAREN
| rnix::SyntaxKind::NODE_STRING = | rnix::SyntaxKind::NODE_STRING =