mirror of
https://github.com/RGBCube/alejandra
synced 2025-08-01 21:17:45 +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,
|
rnix::SyntaxKind::NODE_INHERIT => crate::rules::inherit::rule,
|
||||||
// ( a )
|
// ( a )
|
||||||
rnix::SyntaxKind::NODE_INHERIT_FROM => {
|
rnix::SyntaxKind::NODE_INHERIT_FROM => {
|
||||||
crate::rules::inherit_from::rule
|
crate::rules::paren::rule
|
||||||
}
|
}
|
||||||
rnix::SyntaxKind::NODE_KEY => crate::rules::default,
|
rnix::SyntaxKind::NODE_KEY => crate::rules::default,
|
||||||
// a = b;
|
// a = b;
|
||||||
|
|
|
@ -26,42 +26,70 @@ impl Children {
|
||||||
};
|
};
|
||||||
|
|
||||||
for child in node.children_with_tokens() {
|
for child in node.children_with_tokens() {
|
||||||
match child {
|
let text: String = child.to_string();
|
||||||
rnix::SyntaxElement::Node(node) => {
|
|
||||||
children.push(node.clone().into());
|
|
||||||
|
|
||||||
if pos.is_some() {
|
match child.kind() {
|
||||||
pos.as_mut().unwrap().update(&node.text().to_string());
|
rnix::SyntaxKind::NODE_PAREN => {
|
||||||
}
|
let mut simplified = child.into_node().unwrap();
|
||||||
}
|
|
||||||
rnix::SyntaxElement::Token(token) => {
|
while matches!(
|
||||||
match token.kind() {
|
simplified.kind(),
|
||||||
rnix::SyntaxKind::TOKEN_COMMENT => {
|
rnix::SyntaxKind::NODE_PAREN
|
||||||
children.push(
|
) {
|
||||||
crate::builder::make_isolated_token(
|
let mut children =
|
||||||
rnix::SyntaxKind::TOKEN_COMMENT,
|
crate::children2::new(build_ctx, &simplified);
|
||||||
&dedent_comment(
|
|
||||||
pos.as_ref().unwrap(),
|
let opener = children.next().unwrap();
|
||||||
token.text(),
|
let expression = children.next().unwrap();
|
||||||
),
|
let closer = children.next().unwrap();
|
||||||
)
|
|
||||||
.into(),
|
if !opener.has_inline_comment
|
||||||
);
|
&& !opener.has_comments
|
||||||
}
|
&& !expression.has_inline_comment
|
||||||
rnix::SyntaxKind::TOKEN_WHITESPACE => {
|
&& !expression.has_comments
|
||||||
if crate::utils::count_newlines(token.text()) > 0 {
|
&& !closer.has_inline_comment
|
||||||
children.push(token.clone().into());
|
&& !closer.has_comments
|
||||||
}
|
&& matches!(
|
||||||
}
|
expression.element.kind(),
|
||||||
_ => {
|
rnix::SyntaxKind::NODE_ATTR_SET
|
||||||
children.push(token.clone().into());
|
| 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() {
|
children.push(simplified.into());
|
||||||
pos.as_mut().unwrap().update(token.text());
|
}
|
||||||
|
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
|
// peek: expr
|
||||||
let child_expr = children.get_next().unwrap();
|
let child_expr = children.get_next().unwrap();
|
||||||
|
|
||||||
|
// Superfluous parens can be removed: `a = (x);` -> `a = x;`
|
||||||
let child_expr =
|
let child_expr =
|
||||||
if matches!(child_expr.kind(), rnix::SyntaxKind::NODE_PAREN) {
|
if matches!(child_expr.kind(), rnix::SyntaxKind::NODE_PAREN) {
|
||||||
let mut children: Vec<rnix::SyntaxElement> =
|
let mut children: Vec<rnix::SyntaxElement> =
|
||||||
|
|
|
@ -4,7 +4,6 @@ pub(crate) mod bin_op;
|
||||||
pub(crate) mod dynamic;
|
pub(crate) mod dynamic;
|
||||||
pub(crate) mod if_else;
|
pub(crate) mod if_else;
|
||||||
pub(crate) mod inherit;
|
pub(crate) mod inherit;
|
||||||
pub(crate) mod inherit_from;
|
|
||||||
pub(crate) mod key_value;
|
pub(crate) mod key_value;
|
||||||
pub(crate) mod lambda;
|
pub(crate) mod lambda;
|
||||||
pub(crate) mod let_in;
|
pub(crate) mod let_in;
|
||||||
|
|
|
@ -1,14 +1,6 @@
|
||||||
pub(crate) fn rule(
|
pub(crate) fn rule(
|
||||||
build_ctx: &crate::builder::BuildCtx,
|
build_ctx: &crate::builder::BuildCtx,
|
||||||
node: &rnix::SyntaxNode,
|
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> {
|
) -> std::collections::LinkedList<crate::builder::Step> {
|
||||||
let mut steps = std::collections::LinkedList::new();
|
let mut steps = std::collections::LinkedList::new();
|
||||||
|
|
||||||
|
@ -18,29 +10,6 @@ pub(crate) fn rule_with_configuration(
|
||||||
let expression = children.next().unwrap();
|
let expression = children.next().unwrap();
|
||||||
let closer = 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
|
let vertical = opener.has_inline_comment
|
||||||
|| opener.has_trivialities
|
|| opener.has_trivialities
|
||||||
|| expression.has_inline_comment
|
|| expression.has_inline_comment
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
rec /**/ {
|
rec /**/ {
|
||||||
|
|
||||||
a = (((4)));
|
a = (((4)));
|
||||||
|
a = (((a: b)));
|
||||||
|
|
||||||
a = {a = 1 ;};
|
a = {a = 1 ;};
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ rec
|
||||||
/**/
|
/**/
|
||||||
{
|
{
|
||||||
a = 4;
|
a = 4;
|
||||||
|
a = a: b;
|
||||||
|
|
||||||
a = {a = 1;};
|
a = {a = 1;};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue