mirror of
https://github.com/RGBCube/alejandra
synced 2025-07-30 12:07:46 +00:00
feat: even more simplification
This commit is contained in:
parent
6acd441a1f
commit
3cd9911014
8 changed files with 63 additions and 69 deletions
|
@ -191,7 +191,7 @@ fn format(
|
|||
rnix::SyntaxKind::NODE_INHERIT => crate::rules::inherit::rule,
|
||||
// ( a )
|
||||
rnix::SyntaxKind::NODE_INHERIT_FROM => {
|
||||
crate::rules::inherit_from::rule
|
||||
crate::rules::paren::rule
|
||||
}
|
||||
rnix::SyntaxKind::NODE_KEY => crate::rules::default,
|
||||
// a = b;
|
||||
|
|
|
@ -26,42 +26,70 @@ impl Children {
|
|||
};
|
||||
|
||||
for child in node.children_with_tokens() {
|
||||
match child {
|
||||
rnix::SyntaxElement::Node(node) => {
|
||||
children.push(node.clone().into());
|
||||
let text: String = child.to_string();
|
||||
|
||||
if pos.is_some() {
|
||||
pos.as_mut().unwrap().update(&node.text().to_string());
|
||||
}
|
||||
}
|
||||
rnix::SyntaxElement::Token(token) => {
|
||||
match token.kind() {
|
||||
rnix::SyntaxKind::TOKEN_COMMENT => {
|
||||
children.push(
|
||||
crate::builder::make_isolated_token(
|
||||
rnix::SyntaxKind::TOKEN_COMMENT,
|
||||
&dedent_comment(
|
||||
pos.as_ref().unwrap(),
|
||||
token.text(),
|
||||
),
|
||||
)
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
rnix::SyntaxKind::TOKEN_WHITESPACE => {
|
||||
if crate::utils::count_newlines(token.text()) > 0 {
|
||||
children.push(token.clone().into());
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
children.push(token.clone().into());
|
||||
match child.kind() {
|
||||
rnix::SyntaxKind::NODE_PAREN => {
|
||||
let mut simplified = child.into_node().unwrap();
|
||||
|
||||
while matches!(
|
||||
simplified.kind(),
|
||||
rnix::SyntaxKind::NODE_PAREN
|
||||
) {
|
||||
let mut children =
|
||||
crate::children2::new(build_ctx, &simplified);
|
||||
|
||||
let opener = children.next().unwrap();
|
||||
let expression = children.next().unwrap();
|
||||
let closer = children.next().unwrap();
|
||||
|
||||
if !opener.has_inline_comment
|
||||
&& !opener.has_comments
|
||||
&& !expression.has_inline_comment
|
||||
&& !expression.has_comments
|
||||
&& !closer.has_inline_comment
|
||||
&& !closer.has_comments
|
||||
&& matches!(
|
||||
expression.element.kind(),
|
||||
rnix::SyntaxKind::NODE_ATTR_SET
|
||||
| rnix::SyntaxKind::NODE_IDENT
|
||||
| rnix::SyntaxKind::NODE_LIST
|
||||
| rnix::SyntaxKind::NODE_LITERAL
|
||||
| rnix::SyntaxKind::NODE_PAREN
|
||||
| rnix::SyntaxKind::NODE_PATH_WITH_INTERPOL
|
||||
| rnix::SyntaxKind::NODE_STRING
|
||||
)
|
||||
{
|
||||
simplified =
|
||||
expression.element.into_node().unwrap();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if pos.is_some() {
|
||||
pos.as_mut().unwrap().update(token.text());
|
||||
children.push(simplified.into());
|
||||
}
|
||||
rnix::SyntaxKind::TOKEN_COMMENT => {
|
||||
children.push(
|
||||
crate::builder::make_isolated_token(
|
||||
rnix::SyntaxKind::TOKEN_COMMENT,
|
||||
&dedent_comment(pos.as_ref().unwrap(), &text),
|
||||
)
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
rnix::SyntaxKind::TOKEN_WHITESPACE => {
|
||||
if crate::utils::count_newlines(&text) > 0 {
|
||||
children.push(child);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
children.push(child);
|
||||
}
|
||||
}
|
||||
|
||||
if pos.is_some() {
|
||||
pos.as_mut().unwrap().update(&text);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
pub(crate) fn rule(
|
||||
build_ctx: &crate::builder::BuildCtx,
|
||||
node: &rnix::SyntaxNode,
|
||||
) -> std::collections::LinkedList<crate::builder::Step> {
|
||||
crate::rules::paren::rule_with_configuration(build_ctx, node, false)
|
||||
}
|
|
@ -55,6 +55,8 @@ pub(crate) fn rule(
|
|||
|
||||
// peek: expr
|
||||
let child_expr = children.get_next().unwrap();
|
||||
|
||||
// Superfluous parens can be removed: `a = (x);` -> `a = x;`
|
||||
let child_expr =
|
||||
if matches!(child_expr.kind(), rnix::SyntaxKind::NODE_PAREN) {
|
||||
let mut children: Vec<rnix::SyntaxElement> =
|
||||
|
|
|
@ -4,7 +4,6 @@ pub(crate) mod bin_op;
|
|||
pub(crate) mod dynamic;
|
||||
pub(crate) mod if_else;
|
||||
pub(crate) mod inherit;
|
||||
pub(crate) mod inherit_from;
|
||||
pub(crate) mod key_value;
|
||||
pub(crate) mod lambda;
|
||||
pub(crate) mod let_in;
|
||||
|
|
|
@ -1,14 +1,6 @@
|
|||
pub(crate) fn rule(
|
||||
build_ctx: &crate::builder::BuildCtx,
|
||||
node: &rnix::SyntaxNode,
|
||||
) -> std::collections::LinkedList<crate::builder::Step> {
|
||||
rule_with_configuration(build_ctx, node, true)
|
||||
}
|
||||
|
||||
pub(crate) fn rule_with_configuration(
|
||||
build_ctx: &crate::builder::BuildCtx,
|
||||
node: &rnix::SyntaxNode,
|
||||
simplify: bool,
|
||||
) -> std::collections::LinkedList<crate::builder::Step> {
|
||||
let mut steps = std::collections::LinkedList::new();
|
||||
|
||||
|
@ -18,29 +10,6 @@ pub(crate) fn rule_with_configuration(
|
|||
let expression = children.next().unwrap();
|
||||
let closer = children.next().unwrap();
|
||||
|
||||
// Simplify this expression
|
||||
if simplify
|
||||
&& !opener.has_inline_comment
|
||||
&& !opener.has_comments
|
||||
&& !expression.has_inline_comment
|
||||
&& !expression.has_comments
|
||||
&& !closer.has_inline_comment
|
||||
&& !closer.has_comments
|
||||
&& matches!(
|
||||
expression.element.kind(),
|
||||
rnix::SyntaxKind::NODE_ATTR_SET
|
||||
| rnix::SyntaxKind::NODE_IDENT
|
||||
| rnix::SyntaxKind::NODE_LIST
|
||||
| rnix::SyntaxKind::NODE_LITERAL
|
||||
| rnix::SyntaxKind::NODE_PAREN
|
||||
| rnix::SyntaxKind::NODE_PATH_WITH_INTERPOL
|
||||
| rnix::SyntaxKind::NODE_STRING
|
||||
)
|
||||
{
|
||||
steps.push_back(crate::builder::Step::Format(expression.element));
|
||||
return steps;
|
||||
}
|
||||
|
||||
let vertical = opener.has_inline_comment
|
||||
|| opener.has_trivialities
|
||||
|| expression.has_inline_comment
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
rec /**/ {
|
||||
|
||||
a = (((4)));
|
||||
a = (((a: b)));
|
||||
|
||||
a = {a = 1 ;};
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ rec
|
|||
/**/
|
||||
{
|
||||
a = 4;
|
||||
a = a: b;
|
||||
|
||||
a = {a = 1;};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue