1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-08-01 04:57:44 +00:00

feat: keep attr sets new lines

This commit is contained in:
Kevin Amado 2022-02-06 20:20:23 -05:00
parent 50b2edb882
commit 9b66535246
No known key found for this signature in database
GPG key ID: FFF341057F503148
6 changed files with 271 additions and 32 deletions

View file

@ -9,10 +9,16 @@ pub struct Children {
current_index: usize, current_index: usize,
} }
pub enum DrainCommentOrNewline {
Comment(String),
Newline(usize),
}
impl Children { impl Children {
pub fn new( pub fn new_with_configuration(
build_ctx: &crate::builder::BuildCtx, build_ctx: &crate::builder::BuildCtx,
node: &rnix::SyntaxNode, node: &rnix::SyntaxNode,
with_newlines: bool,
) -> Children { ) -> Children {
let mut children = Vec::new(); let mut children = Vec::new();
@ -29,7 +35,21 @@ impl Children {
} }
rnix::SyntaxElement::Token(token) => { rnix::SyntaxElement::Token(token) => {
match token.kind() { match token.kind() {
rnix::SyntaxKind::TOKEN_WHITESPACE => {} rnix::SyntaxKind::TOKEN_WHITESPACE => {
if with_newlines
&& token
.text()
.chars()
.filter(|c| *c == '\n')
.count()
> 0
{
children.push(Child {
element: token.clone().into(),
pos: pos.clone(),
});
}
}
_ => { _ => {
children.push(Child { children.push(Child {
element: token.clone().into(), element: token.clone().into(),
@ -46,6 +66,13 @@ impl Children {
Children { children, current_index: 0 } Children { children, current_index: 0 }
} }
pub fn new(
build_ctx: &crate::builder::BuildCtx,
node: &rnix::SyntaxNode,
) -> Children {
Children::new_with_configuration(build_ctx, node, false)
}
pub fn get(&mut self, index: usize) -> Option<Child> { pub fn get(&mut self, index: usize) -> Option<Child> {
if index + 1 > self.children.len() { if index + 1 > self.children.len() {
None None
@ -92,6 +119,47 @@ 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()
.filter(|c| *c == '\n')
.count()
> 0
})
}
pub fn drain_newlines<F: FnMut(usize)>(&mut self, mut callback: F) {
let mut newlines = 0;
while let Some(child) = self.peek_next() {
match child.element.kind() {
rnix::SyntaxKind::TOKEN_WHITESPACE => {
newlines += child
.element
.into_token()
.unwrap()
.text()
.chars()
.filter(|c| *c == '\n')
.count();
self.move_next();
}
_ => {
break;
}
}
}
callback(newlines)
}
pub fn drain_comment<F: FnMut(String)>(&mut self, mut callback: F) { pub fn drain_comment<F: FnMut(String)>(&mut self, mut callback: F) {
if let Some(child) = self.peek_next() { if let Some(child) = self.peek_next() {
match child.element.kind() { match child.element.kind() {
@ -123,6 +191,42 @@ impl Children {
} }
} }
} }
pub fn drain_comments_and_newlines<F: FnMut(DrainCommentOrNewline)>(
&mut self,
mut callback: F,
) {
while let Some(child) = self.peek_next() {
match child.element.kind() {
rnix::SyntaxKind::TOKEN_COMMENT => {
callback(DrainCommentOrNewline::Comment(dedent_comment(
&child.pos,
child.element.into_token().unwrap().text(),
)));
self.move_next();
}
rnix::SyntaxKind::TOKEN_WHITESPACE => {
let count = child
.element
.clone()
.into_token()
.unwrap()
.text()
.chars()
.filter(|c| *c == '\n')
.count();
if count > 1 {
callback(DrainCommentOrNewline::Newline(count));
}
self.move_next();
}
_ => {
break;
}
}
}
}
} }
fn dedent_comment(pos: &crate::position::Position, text: &str) -> String { fn dedent_comment(pos: &crate::position::Position, text: &str) -> String {

View file

@ -4,20 +4,25 @@ pub fn rule(
) -> std::collections::LinkedList<crate::builder::Step> { ) -> std::collections::LinkedList<crate::builder::Step> {
let mut steps = std::collections::LinkedList::new(); let mut steps = std::collections::LinkedList::new();
let mut children = crate::children::Children::new(build_ctx, node); let mut children = crate::children::Children::new_with_configuration(
build_ctx, node, true,
);
let layout = if children.has_comments() { let items_count = node
&crate::config::Layout::Tall
} else if node
.children() .children()
.filter(|element| match element.kind() { .filter(|element| {
rnix::SyntaxKind::NODE_KEY_VALUE matches!(
| rnix::SyntaxKind::NODE_INHERIT element.kind(),
| rnix::SyntaxKind::NODE_INHERIT_FROM => true, rnix::SyntaxKind::NODE_KEY_VALUE
_ => false, | rnix::SyntaxKind::NODE_INHERIT
| rnix::SyntaxKind::NODE_INHERIT_FROM
)
}) })
.count() .count();
> 1
let layout = if items_count > 1
|| children.has_comments()
|| children.has_newlines()
{ {
&crate::config::Layout::Tall &crate::config::Layout::Tall
} else { } else {
@ -28,10 +33,29 @@ pub fn rule(
let child = children.peek_next().unwrap(); let child = children.peek_next().unwrap();
if let rnix::SyntaxKind::TOKEN_REC = child.element.kind() { if let rnix::SyntaxKind::TOKEN_REC = child.element.kind() {
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child.element));
steps.push_back(crate::builder::Step::Whitespace);
children.move_next(); children.move_next();
if let rnix::SyntaxKind::TOKEN_COMMENT
| rnix::SyntaxKind::TOKEN_WHITESPACE =
children.peek_next().unwrap().element.kind()
{
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
} else {
steps.push_back(crate::builder::Step::Whitespace);
}
} }
// /**/
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(_) => {}
});
// { // {
let child = children.get_next().unwrap(); let child = children.get_next().unwrap();
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child.element));
@ -42,24 +66,41 @@ pub fn rule(
crate::config::Layout::Wide => {} crate::config::Layout::Wide => {}
} }
loop { // /**/
// /**/ children.drain_comments_and_newlines(|element| match element {
children.drain_comments(|text| { crate::children::DrainCommentOrNewline::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);
steps.push_back(crate::builder::Step::Comment(text)); steps.push_back(crate::builder::Step::Comment(text));
}
crate::children::DrainCommentOrNewline::Newline(_) => {}
});
let mut item_index: usize = 0;
loop {
// /**/
children.drain_comments_and_newlines(|element| match element {
crate::children::DrainCommentOrNewline::Comment(text) => {
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 item_index > 0 && item_index < items_count {
steps.push_back(crate::builder::Step::NewLine);
}
}
}); });
if let Some(child) = children.peek_next() { if let Some(child) = children.peek_next() {
let kind = child.element.kind(); if let rnix::SyntaxKind::TOKEN_CURLY_B_CLOSE = child.element.kind()
if let rnix::SyntaxKind::TOKEN_COMMENT
| rnix::SyntaxKind::TOKEN_CURLY_B_CLOSE = kind
{ {
break; break;
} }
// item // item
item_index += 1;
match layout { match layout {
crate::config::Layout::Tall => { crate::config::Layout::Tall => {
steps.push_back(crate::builder::Step::NewLine); steps.push_back(crate::builder::Step::NewLine);
@ -76,18 +117,9 @@ pub fn rule(
} }
children.move_next(); children.move_next();
} else {
break;
} }
} }
// /**/
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 = children.get_next().unwrap(); let child = children.get_next().unwrap();
match layout { match layout {

View file

@ -2,6 +2,20 @@
{} {}
{/*a*/} {/*a*/}
{a=1;} {a=1;}
{/*a*/b=1;/*c*/}
{ b=1; }
{ b=1; /*c*/ }
{ /*a*/ b=1; }
{ /*a*/ b=1; /*c*/ }
rec { c=1; }
rec { c=1; /*d*/ }
rec { /*b*/ c=1; }
rec { /*b*/ c=1; /*d*/ }
rec /*a*/ { c=1; }
rec /*a*/ { c=1; /*d*/ }
rec /*a*/ { /*b*/ c=1; }
rec /*a*/ { /*b*/ c=1; /*d*/ }
{a=rec {a={a=rec {a={a=rec {a={a=rec {a={a=rec {a={};};};};};};};};};};} {a=rec {a={a=rec {a={a=rec {a={a=rec {a={a=rec {a={};};};};};};};};};};}
] ]

View file

@ -6,6 +6,19 @@
*/ */
} }
{ a = 1; } { a = 1; }
{ b = 1; }
{
b = 1;
/*
c
*/
}
{
/*
a
*/
b = 1;
}
{ {
/* /*
a a
@ -15,6 +28,68 @@
c c
*/ */
} }
rec { c = 1; }
rec {
c = 1;
/*
d
*/
}
rec {
/*
b
*/
c = 1;
}
rec {
/*
b
*/
c = 1;
/*
d
*/
}
rec
/*
a
*/
{
c = 1;
}
rec
/*
a
*/
{
c = 1;
/*
d
*/
}
rec
/*
a
*/
{
/*
b
*/
c = 1;
}
rec
/*
a
*/
{
/*
b
*/
c = 1;
/*
d
*/
}
{ {
a = rec { a = rec {
a = { a = {

View file

@ -1,6 +1,11 @@
{ rec /**/ {
a = {a = 1 ;}; a = {a = 1 ;};
b = {a = 1/*d*/;}; b = {a = 1/*d*/;};
c = {a =/*c*/1 ;}; c = {a =/*c*/1 ;};
d = {a =/*c*/1/*d*/;}; d = {a =/*c*/1/*d*/;};
e = {a/*b*/= 1 ;}; e = {a/*b*/= 1 ;};
@ -15,6 +20,10 @@
o = { pkgs o = { pkgs
, ... , ...
}: { }; }: { };
p = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa { } p = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa { }
a; a;
} }

View file

@ -1,5 +1,8 @@
rec
/**/
{ {
a = { a = 1; }; a = { a = 1; };
b = { b = {
a = a =
1 1
@ -8,6 +11,7 @@
*/ */
; ;
}; };
c = { c = {
a = a =
/* /*
@ -96,6 +100,7 @@
, ... , ...
}: }:
{ }; { };
p = p =
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa { } a; aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa { } a;
} }