1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-07-30 12:07:46 +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";
sha256 = flakeCompat.narHash;
};
flake = import flakeCompatSrc { src = ./.; };
flake = import flakeCompatSrc {src = ./.;};
in
flake.defaultNix.defaultPackage

View file

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

View file

@ -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";

View file

@ -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();
}
_ => {

View file

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

View file

@ -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::<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!(
next_kind,
rnix::SyntaxKind::NODE_ASSERT | rnix::SyntaxKind::NODE_WITH
) && 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_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);
}

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*/
;
p = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa { }
p =
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa { }
a;

View file

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