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

feat: respect newlines after =

This commit is contained in:
Kevin Amado 2022-02-18 13:42:57 -05:00
parent a2212283d8
commit efdc6fcbcb
No known key found for this signature in database
GPG key ID: FFF341057F503148
9 changed files with 83 additions and 110 deletions

View file

@ -6,6 +6,6 @@ let
url = "https://github.com/edolstra/flake-compat/archive/${flakeCompat.rev}.tar.gz"; url = "https://github.com/edolstra/flake-compat/archive/${flakeCompat.rev}.tar.gz";
sha256 = flakeCompat.narHash; sha256 = flakeCompat.narHash;
}; };
flake = import flakeCompatSrc { src = ./.; }; flake = import flakeCompatSrc {src = ./.;};
in in
flake.defaultNix.defaultPackage flake.defaultNix.defaultPackage

View file

@ -26,7 +26,7 @@
cargoLock.lockFile = ./Cargo.lock; cargoLock.lockFile = ./Cargo.lock;
passthru.tests = { passthru.tests = {
version = nixpkgs.testVersion { package = super.alejandra; }; version = nixpkgs.testVersion {package = super.alejandra;};
}; };
meta = { meta = {

View file

@ -10,7 +10,7 @@
system = "x86_64-linux"; system = "x86_64-linux";
fenix = inputs.fenix.packages.${system}; fenix = inputs.fenix.packages.${system};
nixpkgs = import inputs.nixpkgs { inherit system; }; nixpkgs = import inputs.nixpkgs {inherit system;};
in { in {
devShell.${system} = nixpkgs.mkShell { devShell.${system} = nixpkgs.mkShell {
name = "alejandra"; name = "alejandra";

View file

@ -37,11 +37,7 @@ impl Children {
match token.kind() { match token.kind() {
rnix::SyntaxKind::TOKEN_WHITESPACE => { rnix::SyntaxKind::TOKEN_WHITESPACE => {
if with_newlines if with_newlines
&& token && crate::utils::count_newlines(token.text())
.text()
.chars()
.filter(|c| *c == '\n')
.count()
> 0 > 0
{ {
children.push(Child { children.push(Child {
@ -122,14 +118,9 @@ impl Children {
pub fn has_newlines(&self) -> bool { pub fn has_newlines(&self) -> bool {
self.children.iter().any(|child| { self.children.iter().any(|child| {
child.element.kind() == rnix::SyntaxKind::TOKEN_WHITESPACE child.element.kind() == rnix::SyntaxKind::TOKEN_WHITESPACE
&& child && crate::utils::has_newlines(
.element child.element.clone().into_token().unwrap().text(),
.clone() )
.into_token()
.unwrap()
.text()
.chars()
.any(|c| c == '\n')
}) })
} }
@ -179,17 +170,11 @@ impl Children {
self.move_next(); self.move_next();
} }
rnix::SyntaxKind::TOKEN_WHITESPACE => { rnix::SyntaxKind::TOKEN_WHITESPACE => {
let count = child let newlines_count = crate::utils::count_newlines(
.element child.element.clone().into_token().unwrap().text(),
.clone() );
.into_token()
.unwrap()
.text()
.chars()
.filter(|c| *c == '\n')
.count();
callback(DrainCommentOrNewline::Newline(count)); callback(DrainCommentOrNewline::Newline(newlines_count));
self.move_next(); self.move_next();
} }
_ => { _ => {

View file

@ -7,3 +7,4 @@ pub mod find;
pub mod format; pub mod format;
pub mod position; pub mod position;
pub mod rules; pub mod rules;
pub mod utils;

View file

@ -43,77 +43,62 @@ pub fn rule(
steps.push_back(crate::builder::Step::Whitespace); 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 mut dedent = false;
let child = children.get_next().unwrap(); steps.push_back(crate::builder::Step::Format(child_equal.element));
steps.push_back(crate::builder::Step::Format(child.element));
match layout { match layout {
crate::config::Layout::Tall => { crate::config::Layout::Tall => {
let next = children.peek_next().unwrap(); if !comments_before.is_empty() || !comments_after.is_empty() {
let next_kind = next.element.kind(); dedent = true;
steps.push_back(crate::builder::Step::Indent);
if false steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
} else if false
|| matches!( || matches!(
next_kind, child_expr.element.kind(),
rnix::SyntaxKind::NODE_ATTR_SET rnix::SyntaxKind::NODE_ATTR_SET
| rnix::SyntaxKind::NODE_PAREN | rnix::SyntaxKind::NODE_PAREN
| rnix::SyntaxKind::NODE_LAMBDA
| rnix::SyntaxKind::NODE_LET_IN | rnix::SyntaxKind::NODE_LET_IN
| rnix::SyntaxKind::NODE_LIST | rnix::SyntaxKind::NODE_LIST
| rnix::SyntaxKind::NODE_STRING | rnix::SyntaxKind::NODE_STRING
) )
|| (matches!(next_kind, rnix::SyntaxKind::NODE_APPLY)
&& matches!(
next.element
.clone()
.into_node()
.unwrap()
.children()
.collect::<Vec<rnix::SyntaxNode>>()
.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!( || (matches!(
next_kind, child_expr.element.kind(),
rnix::SyntaxKind::NODE_ASSERT | rnix::SyntaxKind::NODE_WITH rnix::SyntaxKind::NODE_ASSERT
) && matches!( | rnix::SyntaxKind::NODE_APPLY
next.element | rnix::SyntaxKind::NODE_WITH
.clone() ) && !newlines)
.into_node()
.unwrap()
.children()
.collect::<Vec<rnix::SyntaxNode>>()
.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
))
{ {
steps.push_back(crate::builder::Step::Whitespace); steps.push_back(crate::builder::Step::Whitespace);
} else { } else {
@ -129,38 +114,31 @@ pub fn rule(
} }
// /**/ // /**/
children.drain_comments_and_newlines(|element| match element { for comment in comments_before {
crate::children::DrainCommentOrNewline::Comment(text) => { steps.push_back(comment);
steps.push_back(crate::builder::Step::Comment(text)); steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::NewLine); steps.push_back(crate::builder::Step::Pad);
steps.push_back(crate::builder::Step::Pad); }
}
crate::children::DrainCommentOrNewline::Newline(_) => {}
});
// b // expr
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_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 => { 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; for comment in comments_after {
children.drain_comments_and_newlines(|element| match element { steps.push_back(comment);
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 {
steps.push_back(crate::builder::Step::NewLine); steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad); steps.push_back(crate::builder::Step::Pad);
} }

7
src/utils.rs Normal file
View file

@ -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()
}

View file

@ -29,7 +29,8 @@ rec /**/ {
/*d*/ /*d*/
; ;
p = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa { } p =
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa { }
a; a;

View file

@ -24,7 +24,8 @@
{ a = with b;with b;with b; { a = with b;with b;with b;
1; 1;
} }
{binPath = with pkgs; {binPath =
with pkgs;
makeBinPath ( makeBinPath (
[ [
rsync rsync