diff --git a/default.nix b/default.nix index 4819a00..447f9f7 100644 --- a/default.nix +++ b/default.nix @@ -6,6 +6,6 @@ let url = "https://github.com/edolstra/flake-compat/archive/${flakeCompat.rev}.tar.gz"; sha256 = flakeCompat.narHash; }; - flake = import flakeCompatSrc { src = ./.; }; + flake = import flakeCompatSrc {src = ./.;}; in flake.defaultNix.defaultPackage diff --git a/flake.nix b/flake.nix index 47a58f8..bc7f1e4 100644 --- a/flake.nix +++ b/flake.nix @@ -26,7 +26,7 @@ cargoLock.lockFile = ./Cargo.lock; passthru.tests = { - version = nixpkgs.testVersion { package = super.alejandra; }; + version = nixpkgs.testVersion {package = super.alejandra;}; }; meta = { diff --git a/front/flake.nix b/front/flake.nix index 0dd38e5..5c8680b 100644 --- a/front/flake.nix +++ b/front/flake.nix @@ -10,7 +10,7 @@ system = "x86_64-linux"; fenix = inputs.fenix.packages.${system}; - nixpkgs = import inputs.nixpkgs { inherit system; }; + nixpkgs = import inputs.nixpkgs {inherit system;}; in { devShell.${system} = nixpkgs.mkShell { name = "alejandra"; diff --git a/src/children.rs b/src/children.rs index 398d7f4..5205911 100644 --- a/src/children.rs +++ b/src/children.rs @@ -37,11 +37,7 @@ impl Children { match token.kind() { rnix::SyntaxKind::TOKEN_WHITESPACE => { if with_newlines - && token - .text() - .chars() - .filter(|c| *c == '\n') - .count() + && crate::utils::count_newlines(token.text()) > 0 { children.push(Child { @@ -122,14 +118,9 @@ impl Children { pub fn has_newlines(&self) -> bool { self.children.iter().any(|child| { child.element.kind() == rnix::SyntaxKind::TOKEN_WHITESPACE - && child - .element - .clone() - .into_token() - .unwrap() - .text() - .chars() - .any(|c| c == '\n') + && crate::utils::has_newlines( + child.element.clone().into_token().unwrap().text(), + ) }) } @@ -179,17 +170,11 @@ impl Children { self.move_next(); } rnix::SyntaxKind::TOKEN_WHITESPACE => { - let count = child - .element - .clone() - .into_token() - .unwrap() - .text() - .chars() - .filter(|c| *c == '\n') - .count(); + let newlines_count = crate::utils::count_newlines( + child.element.clone().into_token().unwrap().text(), + ); - callback(DrainCommentOrNewline::Newline(count)); + callback(DrainCommentOrNewline::Newline(newlines_count)); self.move_next(); } _ => { diff --git a/src/lib.rs b/src/lib.rs index 474252e..0d67be4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,3 +7,4 @@ pub mod find; pub mod format; pub mod position; pub mod rules; +pub mod utils; diff --git a/src/rules/key_value.rs b/src/rules/key_value.rs index 1ed9b19..bbc6cf9 100644 --- a/src/rules/key_value.rs +++ b/src/rules/key_value.rs @@ -43,77 +43,62 @@ pub fn rule( steps.push_back(crate::builder::Step::Whitespace); } + // peek: = + let child_equal = children.get_next().unwrap(); + + // peek: /**/ + let mut comments_before = std::collections::LinkedList::new(); + let mut newlines = false; + children.drain_comments_and_newlines(|element| match element { + crate::children::DrainCommentOrNewline::Comment(text) => { + comments_before.push_back(crate::builder::Step::Comment(text)) + } + crate::children::DrainCommentOrNewline::Newline(newlines_count) => { + if newlines_count > 0 { + newlines = true; + } + } + }); + + // peek: expr + let child_expr = children.get_next().unwrap(); + + // peek: /**/ + let mut comments_after = std::collections::LinkedList::new(); + children.drain_comments_and_newlines(|element| match element { + crate::children::DrainCommentOrNewline::Comment(text) => { + comments_after.push_back(crate::builder::Step::Comment(text)) + } + crate::children::DrainCommentOrNewline::Newline(_) => {} + }); + // = let mut dedent = false; - let child = children.get_next().unwrap(); - steps.push_back(crate::builder::Step::Format(child.element)); + steps.push_back(crate::builder::Step::Format(child_equal.element)); + match layout { crate::config::Layout::Tall => { - let next = children.peek_next().unwrap(); - let next_kind = next.element.kind(); - - if false + if !comments_before.is_empty() || !comments_after.is_empty() { + dedent = true; + steps.push_back(crate::builder::Step::Indent); + steps.push_back(crate::builder::Step::NewLine); + steps.push_back(crate::builder::Step::Pad); + } else if false || matches!( - next_kind, + child_expr.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_STRING ) - || (matches!(next_kind, rnix::SyntaxKind::NODE_APPLY) - && matches!( - next.element - .clone() - .into_node() - .unwrap() - .children() - .collect::>() - .iter() - .rev() - .next() - .unwrap() - .kind(), - rnix::SyntaxKind::NODE_ATTR_SET - | rnix::SyntaxKind::NODE_PAREN - | rnix::SyntaxKind::NODE_LIST - | rnix::SyntaxKind::NODE_STRING - )) - || (matches!(next_kind, rnix::SyntaxKind::NODE_LAMBDA) - && !matches!( - next.element - .clone() - .into_node() - .unwrap() - .children() - .next() - .unwrap() - .kind(), - rnix::SyntaxKind::NODE_PATTERN - )) || (matches!( - next_kind, - rnix::SyntaxKind::NODE_ASSERT | rnix::SyntaxKind::NODE_WITH - ) && matches!( - next.element - .clone() - .into_node() - .unwrap() - .children() - .collect::>() - .iter() - .rev() - .next() - .unwrap() - .kind(), - rnix::SyntaxKind::NODE_ATTR_SET - | rnix::SyntaxKind::NODE_IDENT - | rnix::SyntaxKind::NODE_PAREN - | rnix::SyntaxKind::NODE_LET_IN - | rnix::SyntaxKind::NODE_LIST - | rnix::SyntaxKind::NODE_LITERAL - | rnix::SyntaxKind::NODE_STRING - )) + child_expr.element.kind(), + rnix::SyntaxKind::NODE_ASSERT + | rnix::SyntaxKind::NODE_APPLY + | rnix::SyntaxKind::NODE_WITH + ) && !newlines) { steps.push_back(crate::builder::Step::Whitespace); } else { @@ -129,38 +114,31 @@ pub fn rule( } // /**/ - children.drain_comments_and_newlines(|element| match element { - crate::children::DrainCommentOrNewline::Comment(text) => { - steps.push_back(crate::builder::Step::Comment(text)); - steps.push_back(crate::builder::Step::NewLine); - steps.push_back(crate::builder::Step::Pad); - } - crate::children::DrainCommentOrNewline::Newline(_) => {} - }); + for comment in comments_before { + steps.push_back(comment); + steps.push_back(crate::builder::Step::NewLine); + steps.push_back(crate::builder::Step::Pad); + } - // b - let child = children.get_next().unwrap(); + // expr match layout { crate::config::Layout::Tall => { - steps.push_back(crate::builder::Step::FormatWider(child.element)); + steps.push_back(crate::builder::Step::FormatWider( + child_expr.element, + )); + if !comments_after.is_empty() { + 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::Format(child_expr.element)); } } // /**/ - let mut comment = false; - children.drain_comments_and_newlines(|element| match element { - crate::children::DrainCommentOrNewline::Comment(text) => { - comment = true; - steps.push_back(crate::builder::Step::NewLine); - steps.push_back(crate::builder::Step::Pad); - steps.push_back(crate::builder::Step::Comment(text)); - } - crate::children::DrainCommentOrNewline::Newline(_) => {} - }); - if comment { + for comment in comments_after { + steps.push_back(comment); steps.push_back(crate::builder::Step::NewLine); steps.push_back(crate::builder::Step::Pad); } diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..d833162 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,7 @@ +pub fn has_newlines(string: &str) -> bool { + string.chars().any(|c| c == '\n') +} + +pub fn count_newlines(string: &str) -> usize { + string.chars().filter(|c| *c == '\n').count() +} diff --git a/tests/cases/key_value/in b/tests/cases/key_value/in index 0b227d1..218eed8 100644 --- a/tests/cases/key_value/in +++ b/tests/cases/key_value/in @@ -29,7 +29,8 @@ rec /**/ { /*d*/ ; - p = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa { } + p = + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa { } a; diff --git a/tests/cases/with/in b/tests/cases/with/in index 279754d..9a4f0a6 100644 --- a/tests/cases/with/in +++ b/tests/cases/with/in @@ -24,7 +24,8 @@ { a = with b;with b;with b; 1; } - {binPath = with pkgs; + {binPath = + with pkgs; makeBinPath ( [ rsync