1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-08-01 13:07:47 +00:00

refactor: remove unnecessary code

This commit is contained in:
Kevin Amado 2022-02-25 23:04:46 -05:00
parent 5c3d7ab8b7
commit 9f33b633a8
19 changed files with 133 additions and 173 deletions

View file

@ -1,12 +1,5 @@
#[derive(Clone)]
pub(crate) struct Child {
pub element: rnix::SyntaxElement,
#[allow(dead_code)]
pub pos: crate::position::Position,
}
pub(crate) struct Children { pub(crate) struct Children {
children: Vec<Child>, children: Vec<rnix::SyntaxElement>,
current_index: usize, current_index: usize,
} }
@ -20,44 +13,34 @@ impl Children {
build_ctx: &crate::builder::BuildCtx, build_ctx: &crate::builder::BuildCtx,
node: &rnix::SyntaxNode, node: &rnix::SyntaxNode,
) -> Children { ) -> Children {
let mut children = Vec::new(); let mut children: Vec<rnix::SyntaxElement> = Vec::new();
let mut pos = build_ctx.pos_old.clone(); let mut pos = build_ctx.pos_old.clone();
for child in node.children_with_tokens() { for child in node.children_with_tokens() {
match child { match child {
rnix::SyntaxElement::Node(node) => { rnix::SyntaxElement::Node(node) => {
children.push(Child { children.push(node.clone().into());
element: node.clone().into(),
pos: pos.clone(),
});
pos.update(&node.text().to_string()); pos.update(&node.text().to_string());
} }
rnix::SyntaxElement::Token(token) => { rnix::SyntaxElement::Token(token) => {
match token.kind() { match token.kind() {
rnix::SyntaxKind::TOKEN_COMMENT => { rnix::SyntaxKind::TOKEN_COMMENT => {
children.push(Child { children.push(
element: crate::builder::make_isolated_token( crate::builder::make_isolated_token(
rnix::SyntaxKind::TOKEN_COMMENT, rnix::SyntaxKind::TOKEN_COMMENT,
&dedent_comment(&pos, token.text()), &dedent_comment(&pos, token.text()),
) )
.into(), .into(),
pos: pos.clone(), );
});
} }
rnix::SyntaxKind::TOKEN_WHITESPACE => { rnix::SyntaxKind::TOKEN_WHITESPACE => {
if crate::utils::count_newlines(token.text()) > 0 { if crate::utils::count_newlines(token.text()) > 0 {
children.push(Child { children.push(token.clone().into());
element: token.clone().into(),
pos: pos.clone(),
});
} }
} }
_ => { _ => {
children.push(Child { children.push(token.clone().into());
element: token.clone().into(),
pos: pos.clone(),
});
} }
} }
@ -69,7 +52,7 @@ impl Children {
Children { children, current_index: 0 } Children { children, current_index: 0 }
} }
pub fn get(&mut self, index: usize) -> Option<Child> { pub fn get(&mut self, index: usize) -> Option<rnix::SyntaxElement> {
if index + 1 > self.children.len() { if index + 1 > self.children.len() {
None None
} else { } else {
@ -77,13 +60,13 @@ impl Children {
} }
} }
pub fn get_next(&mut self) -> Option<Child> { pub fn get_next(&mut self) -> Option<rnix::SyntaxElement> {
let child = self.get(self.current_index); let child = self.get(self.current_index);
self.move_next(); self.move_next();
child child
} }
pub fn get_remaining(&mut self) -> Vec<Child> { pub fn get_remaining(&mut self) -> Vec<rnix::SyntaxElement> {
let remaining = &self.children[self.current_index..self.children.len()]; let remaining = &self.children[self.current_index..self.children.len()];
self.current_index = self.children.len(); self.current_index = self.children.len();
remaining.to_vec() remaining.to_vec()
@ -93,11 +76,11 @@ impl Children {
self.current_index < self.children.len() self.current_index < self.children.len()
} }
pub fn peek_next(&mut self) -> Option<Child> { pub fn peek_next(&mut self) -> Option<rnix::SyntaxElement> {
self.get(self.current_index) self.get(self.current_index)
} }
pub fn peek_prev(&mut self) -> Option<Child> { pub fn peek_prev(&mut self) -> Option<rnix::SyntaxElement> {
self.get(self.current_index - 1) self.get(self.current_index - 1)
} }
@ -110,38 +93,32 @@ impl Children {
} }
pub fn has_comments(&self) -> bool { pub fn has_comments(&self) -> bool {
self.children.iter().any(|child| { self.children
child.element.kind() == rnix::SyntaxKind::TOKEN_COMMENT .iter()
}) .any(|child| child.kind() == rnix::SyntaxKind::TOKEN_COMMENT)
} }
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.kind() == rnix::SyntaxKind::TOKEN_WHITESPACE
&& crate::utils::has_newlines( && crate::utils::has_newlines(
child.element.as_token().as_ref().unwrap().text(), child.as_token().as_ref().unwrap().text(),
) )
}) })
} }
pub fn drain_trivia<F: FnMut(Trivia)>(&mut self, mut callback: F) { pub fn drain_trivia<F: FnMut(Trivia)>(&mut self, mut callback: F) {
while let Some(child) = self.peek_next() { while let Some(child) = self.peek_next() {
match child.element.kind() { match child.kind() {
rnix::SyntaxKind::TOKEN_COMMENT => { rnix::SyntaxKind::TOKEN_COMMENT => {
callback(Trivia::Comment( callback(Trivia::Comment(
child.element.into_token().unwrap().text().to_string(), child.into_token().unwrap().text().to_string(),
)); ));
self.move_next(); self.move_next();
} }
rnix::SyntaxKind::TOKEN_WHITESPACE => { rnix::SyntaxKind::TOKEN_WHITESPACE => {
callback(Trivia::Whitespace( callback(Trivia::Whitespace(
child child.as_token().as_ref().unwrap().text().to_string(),
.element
.as_token()
.as_ref()
.unwrap()
.text()
.to_string(),
)); ));
self.move_next(); self.move_next();
} }

View file

@ -32,7 +32,7 @@ pub(crate) fn parse(
}); });
// expr // expr
if_else.if_expr = Some(children.get_next().unwrap().element); if_else.if_expr = Some(children.get_next().unwrap());
// /**/ // /**/
children.drain_trivia(|element| match element { children.drain_trivia(|element| match element {
@ -54,7 +54,7 @@ pub(crate) fn parse(
}); });
// expr // expr
if_else.then_expr = Some(children.get_next().unwrap().element); if_else.then_expr = Some(children.get_next().unwrap());
// /**/ // /**/
children.drain_trivia(|element| match element { children.drain_trivia(|element| match element {
@ -76,7 +76,7 @@ pub(crate) fn parse(
}); });
// expr // expr
if_else.else_expr = Some(children.get_next().unwrap().element); if_else.else_expr = Some(children.get_next().unwrap());
if_else if_else
} }

View file

@ -27,8 +27,8 @@ pub(crate) fn parse(
// x @ // x @
let child = children.peek_next().unwrap(); let child = children.peek_next().unwrap();
if let rnix::SyntaxKind::NODE_PAT_BIND = child.element.kind() { if let rnix::SyntaxKind::NODE_PAT_BIND = child.kind() {
pattern.initial_at = Some(child.element); pattern.initial_at = Some(child);
children.move_next(); children.move_next();
} }
@ -50,9 +50,9 @@ pub(crate) fn parse(
// Before an item we can have: comma, comments, whitespace // Before an item we can have: comma, comments, whitespace
loop { loop {
let child = children.peek_next().unwrap(); let child = children.peek_next().unwrap();
// eprintln!("before item {:?}", child.element.kind()); // eprintln!("before item {:?}", child.kind());
match child.element.kind() { match child.kind() {
rnix::SyntaxKind::NODE_PAT_ENTRY rnix::SyntaxKind::NODE_PAT_ENTRY
| rnix::SyntaxKind::TOKEN_CURLY_B_CLOSE | rnix::SyntaxKind::TOKEN_CURLY_B_CLOSE
| rnix::SyntaxKind::TOKEN_ELLIPSIS => { | rnix::SyntaxKind::TOKEN_ELLIPSIS => {
@ -62,8 +62,7 @@ pub(crate) fn parse(
children.move_next(); children.move_next();
} }
rnix::SyntaxKind::TOKEN_COMMENT => { rnix::SyntaxKind::TOKEN_COMMENT => {
let content = let content = child.into_token().unwrap().to_string();
child.element.into_token().unwrap().to_string();
argument.comments_before.push_back(content); argument.comments_before.push_back(content);
children.move_next(); children.move_next();
@ -77,8 +76,8 @@ pub(crate) fn parse(
// item // item
let child = children.peek_next().unwrap(); let child = children.peek_next().unwrap();
// eprintln!("item {:?}", child.element.kind()); // eprintln!("item {:?}", child.kind());
match child.element.kind() { match child.kind() {
rnix::SyntaxKind::TOKEN_CURLY_B_CLOSE => { rnix::SyntaxKind::TOKEN_CURLY_B_CLOSE => {
pattern.comments_before_curly_b_close = pattern.comments_before_curly_b_close =
argument.comments_before; argument.comments_before;
@ -86,7 +85,7 @@ pub(crate) fn parse(
} }
rnix::SyntaxKind::TOKEN_ELLIPSIS rnix::SyntaxKind::TOKEN_ELLIPSIS
| rnix::SyntaxKind::NODE_PAT_ENTRY => { | rnix::SyntaxKind::NODE_PAT_ENTRY => {
argument.item = Some(child.element); argument.item = Some(child);
children.move_next(); children.move_next();
} }
_ => {} _ => {}
@ -95,9 +94,9 @@ pub(crate) fn parse(
// After an item we can have: comma, comments, whitespace // After an item we can have: comma, comments, whitespace
loop { loop {
let child = children.peek_next().unwrap(); let child = children.peek_next().unwrap();
// eprintln!("after item {:?}", child.element.kind()); // eprintln!("after item {:?}", child.kind());
match child.element.kind() { match child.kind() {
rnix::SyntaxKind::NODE_PAT_ENTRY rnix::SyntaxKind::NODE_PAT_ENTRY
| rnix::SyntaxKind::TOKEN_ELLIPSIS | rnix::SyntaxKind::TOKEN_ELLIPSIS
| rnix::SyntaxKind::TOKEN_CURLY_B_CLOSE => { | rnix::SyntaxKind::TOKEN_CURLY_B_CLOSE => {
@ -107,16 +106,14 @@ pub(crate) fn parse(
children.move_next(); children.move_next();
} }
rnix::SyntaxKind::TOKEN_COMMENT => { rnix::SyntaxKind::TOKEN_COMMENT => {
let content = let content = child.into_token().unwrap().to_string();
child.element.into_token().unwrap().to_string();
children.move_next(); children.move_next();
argument.comment_after = Some(content); argument.comment_after = Some(content);
break; break;
} }
rnix::SyntaxKind::TOKEN_WHITESPACE => { rnix::SyntaxKind::TOKEN_WHITESPACE => {
let content = let content = child.into_token().unwrap().to_string();
child.element.into_token().unwrap().to_string();
children.move_next(); children.move_next();
if crate::utils::count_newlines(&content) > 0 { if crate::utils::count_newlines(&content) > 0 {
@ -143,8 +140,8 @@ pub(crate) fn parse(
// @ x // @ x
if let Some(child) = children.peek_next() { if let Some(child) = children.peek_next() {
if let rnix::SyntaxKind::NODE_PAT_BIND = child.element.kind() { if let rnix::SyntaxKind::NODE_PAT_BIND = child.kind() {
pattern.end_at = Some(child.element); pattern.end_at = Some(child);
} }
} }

View file

@ -14,9 +14,9 @@ pub(crate) fn rule(
let child = children.get_next().unwrap(); let child = children.get_next().unwrap();
if vertical { if vertical {
steps.push_back(crate::builder::Step::FormatWider(child.element)); steps.push_back(crate::builder::Step::FormatWider(child));
} else { } else {
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
} }
// /**/ // /**/
@ -35,24 +35,24 @@ pub(crate) fn rule(
let child = children.get_next().unwrap(); let child = children.get_next().unwrap();
if vertical { if vertical {
if let rnix::SyntaxKind::TOKEN_COMMENT if let rnix::SyntaxKind::TOKEN_COMMENT
| rnix::SyntaxKind::TOKEN_WHITESPACE = child_prev.element.kind() | rnix::SyntaxKind::TOKEN_WHITESPACE = child_prev.kind()
{ {
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);
} else if let rnix::SyntaxKind::NODE_ATTR_SET } else if let rnix::SyntaxKind::NODE_ATTR_SET
| rnix::SyntaxKind::NODE_LIST | rnix::SyntaxKind::NODE_LIST
| rnix::SyntaxKind::NODE_PAREN | rnix::SyntaxKind::NODE_PAREN
| rnix::SyntaxKind::NODE_STRING = child.element.kind() | rnix::SyntaxKind::NODE_STRING = child.kind()
{ {
steps.push_back(crate::builder::Step::Whitespace); steps.push_back(crate::builder::Step::Whitespace);
} else { } else {
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::FormatWider(child.element)); steps.push_back(crate::builder::Step::FormatWider(child));
} else { } else {
steps.push_back(crate::builder::Step::Whitespace); steps.push_back(crate::builder::Step::Whitespace);
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
} }
steps steps

View file

@ -12,7 +12,7 @@ pub(crate) fn rule(
// with // with
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));
// /**/ // /**/
let mut comment = false; let mut comment = false;
@ -36,14 +36,14 @@ pub(crate) fn rule(
// expr // expr
let child = children.get_next().unwrap(); let child = children.get_next().unwrap();
if vertical { if vertical {
steps.push_back(crate::builder::Step::FormatWider(child.element)); steps.push_back(crate::builder::Step::FormatWider(child));
} else { } else {
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
} }
// ; // ;
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));
// /**/ // /**/
let mut comment: bool = false; let mut comment: bool = false;
@ -62,16 +62,16 @@ pub(crate) fn rule(
if vertical { if vertical {
if { if {
matches!( matches!(
child.element.kind(), child.kind(),
rnix::SyntaxKind::NODE_ASSERT | rnix::SyntaxKind::NODE_WITH rnix::SyntaxKind::NODE_ASSERT | rnix::SyntaxKind::NODE_WITH
) )
} { } {
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::FormatWider(child.element)); steps.push_back(crate::builder::Step::FormatWider(child));
} else if comment } else if comment
|| !matches!( || !matches!(
child.element.kind(), child.kind(),
rnix::SyntaxKind::NODE_ATTR_SET rnix::SyntaxKind::NODE_ATTR_SET
| rnix::SyntaxKind::NODE_IDENT | rnix::SyntaxKind::NODE_IDENT
| rnix::SyntaxKind::NODE_PAREN | rnix::SyntaxKind::NODE_PAREN
@ -84,15 +84,15 @@ pub(crate) fn rule(
steps.push_back(crate::builder::Step::Indent); steps.push_back(crate::builder::Step::Indent);
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::FormatWider(child.element)); steps.push_back(crate::builder::Step::FormatWider(child));
steps.push_back(crate::builder::Step::Dedent); steps.push_back(crate::builder::Step::Dedent);
} else { } else {
steps.push_back(crate::builder::Step::Whitespace); steps.push_back(crate::builder::Step::Whitespace);
steps.push_back(crate::builder::Step::FormatWider(child.element)); steps.push_back(crate::builder::Step::FormatWider(child));
} }
} else { } else {
steps.push_back(crate::builder::Step::Whitespace); steps.push_back(crate::builder::Step::Whitespace);
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
} }
steps steps

View file

@ -32,13 +32,13 @@ pub(crate) fn rule(
// rec // rec
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.kind() {
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
children.move_next(); children.move_next();
if let rnix::SyntaxKind::TOKEN_COMMENT if let rnix::SyntaxKind::TOKEN_COMMENT
| rnix::SyntaxKind::TOKEN_WHITESPACE = | rnix::SyntaxKind::TOKEN_WHITESPACE =
children.peek_next().unwrap().element.kind() children.peek_next().unwrap().kind()
{ {
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);
@ -59,7 +59,7 @@ pub(crate) fn rule(
// { // {
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));
if vertical { if vertical {
steps.push_back(crate::builder::Step::Indent); steps.push_back(crate::builder::Step::Indent);
} }
@ -93,8 +93,7 @@ pub(crate) fn rule(
}); });
if let Some(child) = children.peek_next() { if let Some(child) = children.peek_next() {
if let rnix::SyntaxKind::TOKEN_CURLY_B_CLOSE = child.element.kind() if let rnix::SyntaxKind::TOKEN_CURLY_B_CLOSE = child.kind() {
{
break; break;
} }
@ -103,14 +102,12 @@ pub(crate) fn rule(
if vertical { if vertical {
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::FormatWider( steps.push_back(crate::builder::Step::FormatWider(child));
child.element,
));
} else { } else {
if item_index > 1 { if item_index > 1 {
steps.push_back(crate::builder::Step::Whitespace); steps.push_back(crate::builder::Step::Whitespace);
} }
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
} }
children.move_next(); children.move_next();
inline_next_comment = true; inline_next_comment = true;
@ -124,7 +121,7 @@ pub(crate) fn rule(
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::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
steps steps
} }

View file

@ -21,7 +21,7 @@ pub(crate) fn rule_with_configuration(
// expr // expr
let child = children.get_next().unwrap(); let child = children.get_next().unwrap();
if vertical { if vertical {
let kind = child.element.kind(); let kind = child.kind();
if (parent_kind == "bin_op_and_or_default" if (parent_kind == "bin_op_and_or_default"
&& matches!( && matches!(
@ -32,14 +32,14 @@ pub(crate) fn rule_with_configuration(
|| (parent_kind == "select" || (parent_kind == "select"
&& matches!(kind, rnix::SyntaxKind::NODE_SELECT)) && matches!(kind, rnix::SyntaxKind::NODE_SELECT))
{ {
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
} else { } else {
steps.push_back(crate::builder::Step::FormatWider(child.element)); steps.push_back(crate::builder::Step::FormatWider(child));
} }
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);
} else { } else {
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
} }
// /**/ // /**/
@ -57,7 +57,7 @@ pub(crate) fn rule_with_configuration(
if !vertical && parent_kind == "bin_op_and_or_default" { if !vertical && parent_kind == "bin_op_and_or_default" {
steps.push_back(crate::builder::Step::Whitespace); steps.push_back(crate::builder::Step::Whitespace);
} }
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
// /**/ // /**/
let mut comment = false; let mut comment = false;
@ -81,9 +81,9 @@ pub(crate) fn rule_with_configuration(
// expr // expr
let child = children.get_next().unwrap(); let child = children.get_next().unwrap();
if vertical { if vertical {
steps.push_back(crate::builder::Step::FormatWider(child.element)); steps.push_back(crate::builder::Step::FormatWider(child));
} else { } else {
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
} }
steps steps

View file

@ -12,7 +12,7 @@ pub(crate) fn rule(
// ${ // ${
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));
if vertical { if vertical {
steps.push_back(crate::builder::Step::Indent); steps.push_back(crate::builder::Step::Indent);
steps.push_back(crate::builder::Step::NewLine); steps.push_back(crate::builder::Step::NewLine);
@ -32,9 +32,9 @@ pub(crate) fn rule(
// expr // expr
let child = children.get_next().unwrap(); let child = children.get_next().unwrap();
if vertical { if vertical {
steps.push_back(crate::builder::Step::FormatWider(child.element)); steps.push_back(crate::builder::Step::FormatWider(child));
} else { } else {
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
} }
// /**/ // /**/
@ -54,7 +54,7 @@ pub(crate) fn rule(
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::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
steps steps
} }

View file

@ -12,7 +12,7 @@ pub(crate) fn rule(
// inherit // inherit
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));
if vertical { if vertical {
steps.push_back(crate::builder::Step::Indent); steps.push_back(crate::builder::Step::Indent);
} }
@ -33,16 +33,13 @@ pub(crate) fn rule(
if vertical { if vertical {
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::FormatWider( steps.push_back(crate::builder::Step::FormatWider(child));
child.element,
));
} else { } else {
if let rnix::SyntaxKind::TOKEN_SEMICOLON = child.element.kind() if let rnix::SyntaxKind::TOKEN_SEMICOLON = child.kind() {
{
} else { } else {
steps.push_back(crate::builder::Step::Whitespace); steps.push_back(crate::builder::Step::Whitespace);
} }
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
} }
} else { } else {
break; break;

View file

@ -13,9 +13,9 @@ pub(crate) fn rule(
// a // a
let child = children.get_next().unwrap(); let child = children.get_next().unwrap();
if vertical { if vertical {
steps.push_back(crate::builder::Step::FormatWider(child.element)); steps.push_back(crate::builder::Step::FormatWider(child));
} else { } else {
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
} }
// /**/ // /**/
@ -67,7 +67,7 @@ pub(crate) fn rule(
// = // =
let mut dedent = false; let mut dedent = false;
steps.push_back(crate::builder::Step::Format(child_equal.element)); steps.push_back(crate::builder::Step::Format(child_equal));
if vertical { if vertical {
if !comments_before.is_empty() || !comments_after.is_empty() { if !comments_before.is_empty() || !comments_after.is_empty() {
@ -76,7 +76,7 @@ pub(crate) fn rule(
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);
} else if matches!( } else if matches!(
child_expr.element.kind(), child_expr.kind(),
rnix::SyntaxKind::NODE_ASSERT rnix::SyntaxKind::NODE_ASSERT
| rnix::SyntaxKind::NODE_ATTR_SET | rnix::SyntaxKind::NODE_ATTR_SET
| rnix::SyntaxKind::NODE_PAREN | rnix::SyntaxKind::NODE_PAREN
@ -86,7 +86,7 @@ pub(crate) fn rule(
| rnix::SyntaxKind::NODE_STRING | rnix::SyntaxKind::NODE_STRING
| rnix::SyntaxKind::NODE_WITH | rnix::SyntaxKind::NODE_WITH
) || (matches!( ) || (matches!(
child_expr.element.kind(), child_expr.kind(),
rnix::SyntaxKind::NODE_APPLY rnix::SyntaxKind::NODE_APPLY
) && !newlines) ) && !newlines)
{ {
@ -110,13 +110,13 @@ pub(crate) fn rule(
// expr // expr
if vertical { if vertical {
steps.push_back(crate::builder::Step::FormatWider(child_expr.element)); steps.push_back(crate::builder::Step::FormatWider(child_expr));
if !comments_after.is_empty() { if !comments_after.is_empty() {
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);
} }
} else { } else {
steps.push_back(crate::builder::Step::Format(child_expr.element)); steps.push_back(crate::builder::Step::Format(child_expr));
} }
// /**/ // /**/
@ -128,7 +128,7 @@ pub(crate) fn rule(
// ; // ;
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));
if dedent { if dedent {
steps.push_back(crate::builder::Step::Dedent); steps.push_back(crate::builder::Step::Dedent);
} }

View file

@ -13,14 +13,14 @@ pub(crate) fn rule(
// a // a
let child = children.get_next().unwrap(); let child = children.get_next().unwrap();
if vertical { if vertical {
steps.push_back(crate::builder::Step::FormatWider(child.element)); steps.push_back(crate::builder::Step::FormatWider(child));
} else { } else {
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
} }
if let rnix::SyntaxKind::TOKEN_COMMENT if let rnix::SyntaxKind::TOKEN_COMMENT
| rnix::SyntaxKind::TOKEN_WHITESPACE = | rnix::SyntaxKind::TOKEN_WHITESPACE =
children.peek_next().unwrap().element.kind() children.peek_next().unwrap().kind()
{ {
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);
@ -38,7 +38,7 @@ pub(crate) fn rule(
// : // :
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));
// /**/ // /**/
let mut comment = false; let mut comment = false;
@ -57,7 +57,7 @@ pub(crate) fn rule(
if vertical { if vertical {
if comment if comment
|| !matches!( || !matches!(
child.element.kind(), child.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_LAMBDA
@ -68,7 +68,7 @@ pub(crate) fn rule(
) )
{ {
let should_indent = !matches!( let should_indent = !matches!(
child.element.kind(), child.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_LAMBDA
@ -83,17 +83,17 @@ pub(crate) fn rule(
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::FormatWider(child.element)); steps.push_back(crate::builder::Step::FormatWider(child));
if should_indent { if should_indent {
steps.push_back(crate::builder::Step::Dedent); steps.push_back(crate::builder::Step::Dedent);
} }
} else { } else {
steps.push_back(crate::builder::Step::Whitespace); steps.push_back(crate::builder::Step::Whitespace);
steps.push_back(crate::builder::Step::FormatWider(child.element)); steps.push_back(crate::builder::Step::FormatWider(child));
} }
} else { } else {
steps.push_back(crate::builder::Step::Whitespace); steps.push_back(crate::builder::Step::Whitespace);
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
} }
steps steps

View file

@ -25,7 +25,7 @@ pub(crate) fn rule(
// let // let
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));
if vertical { if vertical {
steps.push_back(crate::builder::Step::Indent); steps.push_back(crate::builder::Step::Indent);
} }
@ -58,7 +58,7 @@ pub(crate) fn rule(
}); });
if let Some(child) = children.peek_next() { if let Some(child) = children.peek_next() {
if let rnix::SyntaxKind::TOKEN_IN = child.element.kind() { if let rnix::SyntaxKind::TOKEN_IN = child.kind() {
break; break;
} }
@ -67,12 +67,10 @@ pub(crate) fn rule(
if vertical { if vertical {
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::FormatWider( steps.push_back(crate::builder::Step::FormatWider(child));
child.element,
));
} else { } else {
steps.push_back(crate::builder::Step::Whitespace); steps.push_back(crate::builder::Step::Whitespace);
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
} }
children.move_next(); children.move_next();
@ -105,11 +103,11 @@ pub(crate) fn rule(
// in // in
let mut dedent = false; let mut dedent = false;
steps.push_back(crate::builder::Step::Format(child_in.element)); steps.push_back(crate::builder::Step::Format(child_in));
if vertical { if vertical {
if child_comments.is_empty() if child_comments.is_empty()
&& matches!( && matches!(
child_expr.element.kind(), child_expr.kind(),
rnix::SyntaxKind::NODE_ATTR_SET rnix::SyntaxKind::NODE_ATTR_SET
| rnix::SyntaxKind::NODE_LET_IN | rnix::SyntaxKind::NODE_LET_IN
| rnix::SyntaxKind::NODE_LIST | rnix::SyntaxKind::NODE_LIST
@ -135,13 +133,13 @@ pub(crate) fn rule(
// expr // expr
if vertical { if vertical {
steps.push_back(crate::builder::Step::FormatWider(child_expr.element)); steps.push_back(crate::builder::Step::FormatWider(child_expr));
if dedent { if dedent {
steps.push_back(crate::builder::Step::Dedent); steps.push_back(crate::builder::Step::Dedent);
} }
} else { } else {
steps.push_back(crate::builder::Step::Whitespace); steps.push_back(crate::builder::Step::Whitespace);
steps.push_back(crate::builder::Step::Format(child_expr.element)); steps.push_back(crate::builder::Step::Format(child_expr));
} }
steps steps

View file

@ -20,7 +20,7 @@ pub(crate) fn rule(
// [ // [
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));
if vertical { if vertical {
steps.push_back(crate::builder::Step::Indent); steps.push_back(crate::builder::Step::Indent);
} }
@ -60,7 +60,7 @@ pub(crate) fn rule(
}); });
if let Some(child) = children.peek_next() { if let Some(child) = children.peek_next() {
let child_kind = child.element.kind(); let child_kind = child.kind();
if let rnix::SyntaxKind::TOKEN_SQUARE_B_CLOSE = child_kind { if let rnix::SyntaxKind::TOKEN_SQUARE_B_CLOSE = child_kind {
break; break;
@ -71,14 +71,12 @@ pub(crate) fn rule(
if vertical { if vertical {
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::FormatWider( steps.push_back(crate::builder::Step::FormatWider(child));
child.element,
));
} else { } else {
if item_index > 1 { if item_index > 1 {
steps.push_back(crate::builder::Step::Whitespace); steps.push_back(crate::builder::Step::Whitespace);
} }
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
} }
children.move_next(); children.move_next();
@ -93,7 +91,7 @@ pub(crate) fn rule(
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::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
steps steps
} }

View file

@ -13,7 +13,7 @@ pub(crate) fn rule(
// ( // (
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));
if vertical && has_comments_or_newlines { if vertical && has_comments_or_newlines {
steps.push_back(crate::builder::Step::Indent); steps.push_back(crate::builder::Step::Indent);
} }
@ -35,9 +35,9 @@ pub(crate) fn rule(
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::FormatWider(child.element)); steps.push_back(crate::builder::Step::FormatWider(child));
} else { } else {
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
} }
// /**/ // /**/
@ -57,7 +57,7 @@ pub(crate) fn rule(
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::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
steps steps
} }

View file

@ -12,9 +12,9 @@ pub(crate) fn rule(
let child = children.get_next().unwrap(); let child = children.get_next().unwrap();
if vertical { if vertical {
steps.push_back(crate::builder::Step::FormatWider(child.element)); steps.push_back(crate::builder::Step::FormatWider(child));
} else { } else {
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
} }
// /**/ // /**/
@ -38,9 +38,9 @@ pub(crate) fn rule(
let child = children.get_next().unwrap(); let child = children.get_next().unwrap();
if vertical { if vertical {
steps.push_back(crate::builder::Step::FormatWider(child.element)); steps.push_back(crate::builder::Step::FormatWider(child));
} else { } else {
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
} }
children.move_prev(); children.move_prev();

View file

@ -13,9 +13,9 @@ pub(crate) fn rule(
// expr // expr
let child = children.get_next().unwrap(); let child = children.get_next().unwrap();
if vertical { if vertical {
steps.push_back(crate::builder::Step::FormatWider(child.element)); steps.push_back(crate::builder::Step::FormatWider(child));
} else { } else {
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
} }
if children.has_next() { if children.has_next() {
@ -40,7 +40,7 @@ pub(crate) fn rule(
// operator // operator
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));
// /**/ // /**/
let mut comment = false; let mut comment = false;
@ -62,7 +62,7 @@ pub(crate) fn rule(
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);
} else if matches!( } else if matches!(
child.element.kind(), child.kind(),
rnix::SyntaxKind::NODE_ATTR_SET rnix::SyntaxKind::NODE_ATTR_SET
| rnix::SyntaxKind::NODE_IDENT | rnix::SyntaxKind::NODE_IDENT
| rnix::SyntaxKind::NODE_PAREN | rnix::SyntaxKind::NODE_PAREN
@ -73,7 +73,7 @@ pub(crate) fn rule(
| rnix::SyntaxKind::NODE_STRING, | rnix::SyntaxKind::NODE_STRING,
) || crate::builder::fits_in_single_line( ) || crate::builder::fits_in_single_line(
build_ctx, build_ctx,
child.element.clone(), child.clone(),
) { ) {
steps.push_back(crate::builder::Step::Whitespace); steps.push_back(crate::builder::Step::Whitespace);
} else { } else {
@ -84,9 +84,9 @@ pub(crate) fn rule(
} }
if vertical { if vertical {
steps.push_back(crate::builder::Step::FormatWider(child.element)); steps.push_back(crate::builder::Step::FormatWider(child));
} else { } else {
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
} }
if dedent { if dedent {
steps.push_back(crate::builder::Step::Dedent); steps.push_back(crate::builder::Step::Dedent);

View file

@ -22,12 +22,10 @@ pub(crate) fn rule(
if let Some(child) = children.get_next() { if let Some(child) = children.get_next() {
if vertical { if vertical {
steps.push_back(crate::builder::Step::FormatWider( steps.push_back(crate::builder::Step::FormatWider(child));
child.element,
));
steps.push_back(crate::builder::Step::NewLine); steps.push_back(crate::builder::Step::NewLine);
} else { } else {
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
} }
} }
} }

View file

@ -13,25 +13,23 @@ pub(crate) fn rule(
let mut children = crate::children::Children::new(build_ctx, node); let mut children = crate::children::Children::new(build_ctx, node);
let child = children.get_next().unwrap(); let child = children.get_next().unwrap();
let child_token = child.element.clone().into_token().unwrap(); let child_token = child.clone().into_token().unwrap();
let text = child_token.text(); let text = child_token.text();
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
if text == "\"" { if text == "\"" {
while let Some(child) = children.get_next() { while let Some(child) = children.get_next() {
if build_ctx.vertical { if build_ctx.vertical {
steps.push_back(crate::builder::Step::FormatWider( steps.push_back(crate::builder::Step::FormatWider(child));
child.element,
));
} else { } else {
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
} }
} }
} else { } else {
let elements: Vec<rnix::SyntaxElement> = children let elements: Vec<rnix::SyntaxElement> = children
.get_remaining() .get_remaining()
.iter() .iter()
.map(|child| child.element.clone()) .map(|child| child.clone())
.collect(); .collect();
let mut interpolations = elements let mut interpolations = elements

View file

@ -12,7 +12,7 @@ pub(crate) fn rule(
// ${ // ${
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));
if vertical { if vertical {
steps.push_back(crate::builder::Step::Indent); steps.push_back(crate::builder::Step::Indent);
steps.push_back(crate::builder::Step::NewLine); steps.push_back(crate::builder::Step::NewLine);
@ -32,9 +32,9 @@ pub(crate) fn rule(
// expr // expr
let child = children.get_next().unwrap(); let child = children.get_next().unwrap();
if vertical { if vertical {
steps.push_back(crate::builder::Step::FormatWider(child.element)); steps.push_back(crate::builder::Step::FormatWider(child));
} else { } else {
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
} }
// /**/ // /**/
@ -54,7 +54,7 @@ pub(crate) fn rule(
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::Format(child.element)); steps.push_back(crate::builder::Step::Format(child));
steps steps
} }