From 8acbd26031df9d4377549250d5600f9dd5942a41 Mon Sep 17 00:00:00 2001 From: Kevin Amado Date: Wed, 26 Jan 2022 20:55:38 -0500 Subject: [PATCH] feat: do not indent binding --- README.md | 1 - flake.nix | 66 ++++++++++++------------ src/children.rs | 41 --------------- src/rules/key_value.rs | 23 ++++++--- tests/cases/attr_set/out | 13 ++--- tests/cases/key_value/out | 103 ++++++++++++++++++-------------------- 6 files changed, 98 insertions(+), 149 deletions(-) diff --git a/README.md b/README.md index f55db66..4c7a926 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,6 @@ Let's get Alejandra on our systems: Yet there are a few improvements to implement like: - Multiline strings indentation `'' ... ''`. - Multiline comments indentation `/* ... */`. - - Not indenting attr sets, lists, or parenthesis after binding `name = {`. - And many more as community feedback drives. Style is negotiable at this moment. diff --git a/flake.nix b/flake.nix index ed2cf35..14e85a2 100644 --- a/flake.nix +++ b/flake.nix @@ -1,11 +1,10 @@ { - inputs = - { - flakeCompat.url = github:edolstra/flake-compat; - flakeCompat.flake = false; - flakeUtils.url = "github:numtide/flake-utils"; - nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; - }; + inputs = { + flakeCompat.url = github:edolstra/flake-compat; + flakeCompat.flake = false; + flakeUtils.url = "github:numtide/flake-utils"; + nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; + }; outputs = inputs: inputs.flakeUtils.lib.eachDefaultSystem @@ -16,11 +15,10 @@ cargoToml = builtins.fromTOML ( builtins.readFile ./Cargo.toml ); in { - checks = - { - defaultPackage = inputs.self.defaultPackage.${ system }; - inherit ( inputs.self.packages.${ system } ) nixpkgsFormatted; - }; + checks = { + defaultPackage = inputs.self.defaultPackage.${ system }; + inherit ( inputs.self.packages.${ system } ) nixpkgsFormatted; + }; defaultApp = { type = "app"; program = "${ inputs.self.defaultPackage.${ system } }/bin/alejandra"; }; defaultPackage = nixpkgs.rustPlatform.buildRustPackage @@ -30,13 +28,12 @@ src = inputs.self.sourceInfo; cargoLock.lockFile = ./Cargo.lock; NIX_BUILD_CORES = 0; - meta = - { - description = cargoToml.package.description; - homepage = "https://github.com/kamadorueda/alejandra"; - license = nixpkgs.lib.licenses.unlicense; - maintainers = [ nixpkgs.lib.maintainers.kamadorueda ]; - }; + meta = { + description = cargoToml.package.description; + homepage = "https://github.com/kamadorueda/alejandra"; + license = nixpkgs.lib.licenses.unlicense; + maintainers = [ nixpkgs.lib.maintainers.kamadorueda ]; + }; }; devShell = nixpkgs.mkShell @@ -47,16 +44,15 @@ rustup toolchain install nightly ''; }; - packages = - { - nixpkgsFormatted = - nixpkgs.stdenv.mkDerivation - { - name = "nixpkgs-formatted"; - builder = - builtins.toFile - "builder.sh" - '' + packages = { + nixpkgsFormatted = + nixpkgs.stdenv.mkDerivation + { + name = "nixpkgs-formatted"; + builder = + builtins.toFile + "builder.sh" + '' source $stdenv/setup cp -rT $nixpkgs $out @@ -66,12 +62,12 @@ git diff --no-index $nixpkgs $out > $diff || true ''; - buildInputs = [ inputs.self.defaultPackage.${ system } nixpkgs.git ]; - nixpkgs = inputs.nixpkgs.sourceInfo.outPath; - NIX_BUILD_CORES = 0; - outputs = [ "diff" "out" ]; - }; - }; + buildInputs = [ inputs.self.defaultPackage.${ system } nixpkgs.git ]; + nixpkgs = inputs.nixpkgs.sourceInfo.outPath; + NIX_BUILD_CORES = 0; + outputs = [ "diff" "out" ]; + }; + }; } ); } diff --git a/src/children.rs b/src/children.rs index f0ee139..d46b58d 100644 --- a/src/children.rs +++ b/src/children.rs @@ -150,44 +150,3 @@ fn dedent_comment(pos: &crate::position::Position, text: &str) -> String { format!("/*{}*/", text) } } - -// fn dedent_string( -// pos: &crate::position::Position, -// node: &rnix::SyntaxNode, -// ) -> String { -// eprintln!("{}", text); -// if text.starts_with("\"") { -// text.to_string() -// } else { -// node.children_with_tokens().filter(|child| { -// child.kind() == rnix::SyntaxKind::TOKEN_STRING_CONTENT -// }).map(|child| { -// let lines = child.into_token().unwrap().lines(); -// "" -// " rustup toolchain install nightly" -// " " -// "" -// " " -// }); - -// // let padding_to_first_char = lines -// // TOKEN_STRING_CONTENT - -// let text = text[2..text.len() - 2] -// .lines() -// .enumerate() -// .map(|(index, line)| { -// if index > 0 { -// line.chars() -// .skip(if pos.column >= 1 { pos.column - 1 } else { 0 }) -// .collect::() -// } else { -// line.to_string() -// } -// }) -// .collect::>() -// .join("\n"); - -// format!("/*{}*/", text) -// } -// } diff --git a/src/rules/key_value.rs b/src/rules/key_value.rs index 06f710f..940fa2b 100644 --- a/src/rules/key_value.rs +++ b/src/rules/key_value.rs @@ -40,13 +40,23 @@ pub fn rule( } // = + let mut dedent = false; let child = children.get_next().unwrap(); steps.push_back(crate::builder::Step::Format(child.element)); match layout { crate::config::Layout::Tall => { - steps.push_back(crate::builder::Step::Indent); - steps.push_back(crate::builder::Step::NewLine); - steps.push_back(crate::builder::Step::Pad); + if let rnix::SyntaxKind::NODE_ATTR_SET + | rnix::SyntaxKind::NODE_LIST + | rnix::SyntaxKind::NODE_PAREN = + children.peek_next().unwrap().element.kind() + { + steps.push_back(crate::builder::Step::Whitespace); + } else { + dedent = true; + 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::Whitespace); @@ -88,11 +98,8 @@ pub fn rule( // ; let child = children.get_next().unwrap(); steps.push_back(crate::builder::Step::Format(child.element)); - match layout { - crate::config::Layout::Tall => { - steps.push_back(crate::builder::Step::Dedent); - } - crate::config::Layout::Wide => {} + if dedent { + steps.push_back(crate::builder::Step::Dedent); } steps diff --git a/tests/cases/attr_set/out b/tests/cases/attr_set/out index 1a21d5a..2308376 100644 --- a/tests/cases/attr_set/out +++ b/tests/cases/attr_set/out @@ -10,15 +10,10 @@ /*c*/ } { - a = - { - a = - { - a = - { - a = { a = { a = { a = { a = { a = { a = { }; }; }; }; }; }; }; - }; - }; + a = { + a = { + a = { a = { a = { a = { a = { a = { a = { a = { }; }; }; }; }; }; }; }; }; + }; } ] diff --git a/tests/cases/key_value/out b/tests/cases/key_value/out index 270df92..5799ad5 100644 --- a/tests/cases/key_value/out +++ b/tests/cases/key_value/out @@ -1,58 +1,51 @@ { a = { a = 1; }; - b = - { - a = - 1 - /*d*/ - ; - }; - c = - { - a = - /*c*/ - 1; - }; - d = - { - a = - /*c*/ - 1 - /*d*/ - ; - }; - e = - { - a - /*b*/ - = - 1; - }; - f = - { - a - /*b*/ - = - 1 - /*d*/ - ; - }; - h = - { - a - /*b*/ - = - /*c*/ - 1; - }; - i = - { - a - /*b*/ - = - /*c*/ - 1 - /*d*/ - ; - }; + b = { + a = + 1 + /*d*/ + ; + }; + c = { + a = + /*c*/ + 1; + }; + d = { + a = + /*c*/ + 1 + /*d*/ + ; + }; + e = { + a + /*b*/ + = + 1; + }; + f = { + a + /*b*/ + = + 1 + /*d*/ + ; + }; + h = { + a + /*b*/ + = + /*c*/ + 1; + }; + i = { + a + /*b*/ + = + /*c*/ + 1 + /*d*/ + ; + }; }