From 56ed74357c80711e409470bf1cdcdeb2e203cfb6 Mon Sep 17 00:00:00 2001 From: Kevin Amado Date: Sat, 29 Jan 2022 18:02:47 -0500 Subject: [PATCH] feat: get `inherit` done right --- src/builder.rs | 2 +- src/rules/attr_set.rs | 7 +- src/rules/inherit.rs | 36 +- src/rules/inherit_from.rs | 64 -- src/rules/mod.rs | 1 - tests/cases/inherit/out | 12 +- tests/cases/inherit_from/in | 68 ++- tests/cases/inherit_from/out | 1108 +++++++++++++++++++++++++++++++++- 8 files changed, 1180 insertions(+), 118 deletions(-) delete mode 100644 src/rules/inherit_from.rs diff --git a/src/builder.rs b/src/builder.rs index 2e34d1f..0010191 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -178,7 +178,7 @@ fn format( rnix::SyntaxKind::NODE_IF_ELSE => crate::rules::if_else::rule, rnix::SyntaxKind::NODE_INHERIT => crate::rules::inherit::rule, rnix::SyntaxKind::NODE_INHERIT_FROM => { - crate::rules::inherit_from::rule + crate::rules::inherit::rule } rnix::SyntaxKind::NODE_KEY => crate::rules::default, rnix::SyntaxKind::NODE_KEY_VALUE => { diff --git a/src/rules/attr_set.rs b/src/rules/attr_set.rs index 50c8a11..8458951 100644 --- a/src/rules/attr_set.rs +++ b/src/rules/attr_set.rs @@ -10,7 +10,12 @@ pub fn rule( &crate::config::Layout::Tall } else if node .children() - .filter(|node| node.kind() == rnix::SyntaxKind::NODE_KEY_VALUE) + .filter(|node| match node.kind() { + rnix::SyntaxKind::NODE_KEY_VALUE + | rnix::SyntaxKind::NODE_INHERIT + | rnix::SyntaxKind::NODE_INHERIT_FROM => true, + _ => false, + }) .count() > 1 { diff --git a/src/rules/inherit.rs b/src/rules/inherit.rs index 3dc3582..c094dc0 100644 --- a/src/rules/inherit.rs +++ b/src/rules/inherit.rs @@ -30,17 +30,8 @@ pub fn rule( steps.push_back(crate::builder::Step::Comment(text)); }); - if let Some(child) = children.peek_next() { - let kind = child.element.kind(); - - if let rnix::SyntaxKind::TOKEN_COMMENT - | rnix::SyntaxKind::TOKEN_SEMICOLON = kind - { - break; - } - + if let Some(child) = children.get_next() { // expr - let child = children.get_next().unwrap(); match layout { crate::config::Layout::Tall => { steps.push_back(crate::builder::Step::NewLine); @@ -50,7 +41,12 @@ pub fn rule( )); } crate::config::Layout::Wide => { - steps.push_back(crate::builder::Step::Whitespace); + if let rnix::SyntaxKind::TOKEN_SEMICOLON = + child.element.kind() + { + } else { + steps.push_back(crate::builder::Step::Whitespace); + } steps .push_back(crate::builder::Step::Format(child.element)); } @@ -60,30 +56,12 @@ pub fn rule( } } - // /**/ - 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)); - }); - - if let rnix::SyntaxKind::TOKEN_COMMENT = - children.peek_prev().unwrap().element.kind() - { - steps.push_back(crate::builder::Step::NewLine); - steps.push_back(crate::builder::Step::Pad); - } else { - } - - // ; - let child = children.get_next().unwrap(); match layout { crate::config::Layout::Tall => { steps.push_back(crate::builder::Step::Dedent); } crate::config::Layout::Wide => {} } - steps.push_back(crate::builder::Step::Format(child.element)); steps } diff --git a/src/rules/inherit_from.rs b/src/rules/inherit_from.rs deleted file mode 100644 index 298c1fe..0000000 --- a/src/rules/inherit_from.rs +++ /dev/null @@ -1,64 +0,0 @@ -pub fn rule( - build_ctx: &crate::builder::BuildCtx, - node: &rnix::SyntaxNode, -) -> std::collections::LinkedList { - let mut steps = std::collections::LinkedList::new(); - - let mut children = crate::children::Children::new(build_ctx, node); - - let layout = if children.has_comments() { - &crate::config::Layout::Tall - } else { - build_ctx.config.layout() - }; - - let child = children.get_next().unwrap(); - steps.push_back(crate::builder::Step::Format(child.element)); - match layout { - crate::config::Layout::Tall => { - steps.push_back(crate::builder::Step::Indent); - steps.push_back(crate::builder::Step::NewLine); - steps.push_back(crate::builder::Step::Pad); - } - crate::config::Layout::Wide => { - steps.push_back(crate::builder::Step::Whitespace); - } - } - - children.drain_comments(|text| { - steps.push_back(crate::builder::Step::Comment(text)); - steps.push_back(crate::builder::Step::NewLine); - steps.push_back(crate::builder::Step::Pad); - }); - - let child = children.get_next().unwrap(); - match layout { - crate::config::Layout::Tall => { - steps.push_back(crate::builder::Step::FormatWider(child.element)); - } - crate::config::Layout::Wide => { - steps.push_back(crate::builder::Step::Format(child.element)); - } - } - - 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(); - match layout { - crate::config::Layout::Tall => { - steps.push_back(crate::builder::Step::Dedent); - steps.push_back(crate::builder::Step::NewLine); - steps.push_back(crate::builder::Step::Pad); - } - crate::config::Layout::Wide => { - steps.push_back(crate::builder::Step::Whitespace); - } - } - steps.push_back(crate::builder::Step::Format(child.element)); - - steps -} diff --git a/src/rules/mod.rs b/src/rules/mod.rs index e988d96..15cd897 100644 --- a/src/rules/mod.rs +++ b/src/rules/mod.rs @@ -5,7 +5,6 @@ pub mod bin_op; pub mod dynamic; pub mod if_else; pub mod inherit; -pub mod inherit_from; pub mod key_value; pub mod lambda; pub mod let_in; diff --git a/tests/cases/inherit/out b/tests/cases/inherit/out index 974a792..c468f2b 100644 --- a/tests/cases/inherit/out +++ b/tests/cases/inherit/out @@ -4,7 +4,8 @@ } { inherit - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + ; } { inherit b d; } { @@ -22,7 +23,8 @@ /* c */ - d; + d + ; } { inherit @@ -42,7 +44,8 @@ a */ b - d; + d + ; } { inherit @@ -65,7 +68,8 @@ /* c */ - d; + d + ; } { inherit diff --git a/tests/cases/inherit_from/in b/tests/cases/inherit_from/in index 52bb08c..691cf36 100644 --- a/tests/cases/inherit_from/in +++ b/tests/cases/inherit_from/in @@ -1,6 +1,66 @@ [ - { inherit ( b ) d; } - { inherit ( b /*c*/) d; } - { inherit (/*a*/ b ) d; } - { inherit (/*a*/ b /*c*/) d; } + { inherit ( c ) f h ; } + { inherit ( c ) f h /*i*/; } + { inherit ( c ) f /*g*/ h ; } + { inherit ( c ) f /*g*/ h /*i*/; } + { inherit ( c ) /*e*/ f h ; } + { inherit ( c ) /*e*/ f h /*i*/; } + { inherit ( c ) /*e*/ f /*g*/ h ; } + { inherit ( c ) /*e*/ f /*g*/ h /*i*/; } + { inherit ( c /*d*/) f h ; } + { inherit ( c /*d*/) f h /*i*/; } + { inherit ( c /*d*/) f /*g*/ h ; } + { inherit ( c /*d*/) f /*g*/ h /*i*/; } + { inherit ( c /*d*/) /*e*/ f h ; } + { inherit ( c /*d*/) /*e*/ f h /*i*/; } + { inherit ( c /*d*/) /*e*/ f /*g*/ h ; } + { inherit ( c /*d*/) /*e*/ f /*g*/ h /*i*/; } + { inherit (/*b*/ c ) f h ; } + { inherit (/*b*/ c ) f h /*i*/; } + { inherit (/*b*/ c ) f /*g*/ h ; } + { inherit (/*b*/ c ) f /*g*/ h /*i*/; } + { inherit (/*b*/ c ) /*e*/ f h ; } + { inherit (/*b*/ c ) /*e*/ f h /*i*/; } + { inherit (/*b*/ c ) /*e*/ f /*g*/ h ; } + { inherit (/*b*/ c ) /*e*/ f /*g*/ h /*i*/; } + { inherit (/*b*/ c /*d*/) f h ; } + { inherit (/*b*/ c /*d*/) f h /*i*/; } + { inherit (/*b*/ c /*d*/) f /*g*/ h ; } + { inherit (/*b*/ c /*d*/) f /*g*/ h /*i*/; } + { inherit (/*b*/ c /*d*/) /*e*/ f h ; } + { inherit (/*b*/ c /*d*/) /*e*/ f h /*i*/; } + { inherit (/*b*/ c /*d*/) /*e*/ f /*g*/ h ; } + { inherit (/*b*/ c /*d*/) /*e*/ f /*g*/ h /*i*/; } + { inherit /*a*/ ( c ) f h ; } + { inherit /*a*/ ( c ) f h /*i*/; } + { inherit /*a*/ ( c ) f /*g*/ h ; } + { inherit /*a*/ ( c ) f /*g*/ h /*i*/; } + { inherit /*a*/ ( c ) /*e*/ f h ; } + { inherit /*a*/ ( c ) /*e*/ f h /*i*/; } + { inherit /*a*/ ( c ) /*e*/ f /*g*/ h ; } + { inherit /*a*/ ( c ) /*e*/ f /*g*/ h /*i*/; } + { inherit /*a*/ ( c /*d*/) f h ; } + { inherit /*a*/ ( c /*d*/) f h /*i*/; } + { inherit /*a*/ ( c /*d*/) f /*g*/ h ; } + { inherit /*a*/ ( c /*d*/) f /*g*/ h /*i*/; } + { inherit /*a*/ ( c /*d*/) /*e*/ f h ; } + { inherit /*a*/ ( c /*d*/) /*e*/ f h /*i*/; } + { inherit /*a*/ ( c /*d*/) /*e*/ f /*g*/ h ; } + { inherit /*a*/ ( c /*d*/) /*e*/ f /*g*/ h /*i*/; } + { inherit /*a*/ (/*b*/ c ) f h ; } + { inherit /*a*/ (/*b*/ c ) f h /*i*/; } + { inherit /*a*/ (/*b*/ c ) f /*g*/ h ; } + { inherit /*a*/ (/*b*/ c ) f /*g*/ h /*i*/; } + { inherit /*a*/ (/*b*/ c ) /*e*/ f h ; } + { inherit /*a*/ (/*b*/ c ) /*e*/ f h /*i*/; } + { inherit /*a*/ (/*b*/ c ) /*e*/ f /*g*/ h ; } + { inherit /*a*/ (/*b*/ c ) /*e*/ f /*g*/ h /*i*/; } + { inherit /*a*/ (/*b*/ c /*d*/) f h ; } + { inherit /*a*/ (/*b*/ c /*d*/) f h /*i*/; } + { inherit /*a*/ (/*b*/ c /*d*/) f /*g*/ h ; } + { inherit /*a*/ (/*b*/ c /*d*/) f /*g*/ h /*i*/; } + { inherit /*a*/ (/*b*/ c /*d*/) /*e*/ f h ; } + { inherit /*a*/ (/*b*/ c /*d*/) /*e*/ f h /*i*/; } + { inherit /*a*/ (/*b*/ c /*d*/) /*e*/ f /*g*/ h ; } + { inherit /*a*/ (/*b*/ c /*d*/) /*e*/ f /*g*/ h /*i*/; } ] diff --git a/tests/cases/inherit_from/out b/tests/cases/inherit_from/out index b2a3f10..b430a4d 100644 --- a/tests/cases/inherit_from/out +++ b/tests/cases/inherit_from/out @@ -1,36 +1,1116 @@ [ - { inherit ( b ) d; } + { inherit ( c ) f h; } + { + inherit + ( c ) + f + h + /* + i + */ + ; + } + { + inherit + ( c ) + f + /* + g + */ + h + ; + } + { + inherit + ( c ) + f + /* + g + */ + h + /* + i + */ + ; + } + { + inherit + ( c ) + /* + e + */ + f + h + ; + } + { + inherit + ( c ) + /* + e + */ + f + h + /* + i + */ + ; + } + { + inherit + ( c ) + /* + e + */ + f + /* + g + */ + h + ; + } + { + inherit + ( c ) + /* + e + */ + f + /* + g + */ + h + /* + i + */ + ; + } { inherit ( - b + c /* - c + d */ - ) - d; + ) + f + h + ; + } + { + inherit + ( + c + /* + d + */ + ) + f + h + /* + i + */ + ; + } + { + inherit + ( + c + /* + d + */ + ) + f + /* + g + */ + h + ; + } + { + inherit + ( + c + /* + d + */ + ) + f + /* + g + */ + h + /* + i + */ + ; + } + { + inherit + ( + c + /* + d + */ + ) + /* + e + */ + f + h + ; + } + { + inherit + ( + c + /* + d + */ + ) + /* + e + */ + f + h + /* + i + */ + ; + } + { + inherit + ( + c + /* + d + */ + ) + /* + e + */ + f + /* + g + */ + h + ; + } + { + inherit + ( + c + /* + d + */ + ) + /* + e + */ + f + /* + g + */ + h + /* + i + */ + ; } { inherit ( /* - a + b */ - b - ) - d; + c + ) + f + h + ; } { inherit ( /* - a + b */ - b + c + ) + f + h + /* + i + */ + ; + } + { + inherit + ( /* - c + b */ - ) - d; + c + ) + f + /* + g + */ + h + ; + } + { + inherit + ( + /* + b + */ + c + ) + f + /* + g + */ + h + /* + i + */ + ; + } + { + inherit + ( + /* + b + */ + c + ) + /* + e + */ + f + h + ; + } + { + inherit + ( + /* + b + */ + c + ) + /* + e + */ + f + h + /* + i + */ + ; + } + { + inherit + ( + /* + b + */ + c + ) + /* + e + */ + f + /* + g + */ + h + ; + } + { + inherit + ( + /* + b + */ + c + ) + /* + e + */ + f + /* + g + */ + h + /* + i + */ + ; + } + { + inherit + ( + /* + b + */ + c + /* + d + */ + ) + f + h + ; + } + { + inherit + ( + /* + b + */ + c + /* + d + */ + ) + f + h + /* + i + */ + ; + } + { + inherit + ( + /* + b + */ + c + /* + d + */ + ) + f + /* + g + */ + h + ; + } + { + inherit + ( + /* + b + */ + c + /* + d + */ + ) + f + /* + g + */ + h + /* + i + */ + ; + } + { + inherit + ( + /* + b + */ + c + /* + d + */ + ) + /* + e + */ + f + h + ; + } + { + inherit + ( + /* + b + */ + c + /* + d + */ + ) + /* + e + */ + f + h + /* + i + */ + ; + } + { + inherit + ( + /* + b + */ + c + /* + d + */ + ) + /* + e + */ + f + /* + g + */ + h + ; + } + { + inherit + ( + /* + b + */ + c + /* + d + */ + ) + /* + e + */ + f + /* + g + */ + h + /* + i + */ + ; + } + { + inherit + /* + a + */ + ( c ) + f + h + ; + } + { + inherit + /* + a + */ + ( c ) + f + h + /* + i + */ + ; + } + { + inherit + /* + a + */ + ( c ) + f + /* + g + */ + h + ; + } + { + inherit + /* + a + */ + ( c ) + f + /* + g + */ + h + /* + i + */ + ; + } + { + inherit + /* + a + */ + ( c ) + /* + e + */ + f + h + ; + } + { + inherit + /* + a + */ + ( c ) + /* + e + */ + f + h + /* + i + */ + ; + } + { + inherit + /* + a + */ + ( c ) + /* + e + */ + f + /* + g + */ + h + ; + } + { + inherit + /* + a + */ + ( c ) + /* + e + */ + f + /* + g + */ + h + /* + i + */ + ; + } + { + inherit + /* + a + */ + ( + c + /* + d + */ + ) + f + h + ; + } + { + inherit + /* + a + */ + ( + c + /* + d + */ + ) + f + h + /* + i + */ + ; + } + { + inherit + /* + a + */ + ( + c + /* + d + */ + ) + f + /* + g + */ + h + ; + } + { + inherit + /* + a + */ + ( + c + /* + d + */ + ) + f + /* + g + */ + h + /* + i + */ + ; + } + { + inherit + /* + a + */ + ( + c + /* + d + */ + ) + /* + e + */ + f + h + ; + } + { + inherit + /* + a + */ + ( + c + /* + d + */ + ) + /* + e + */ + f + h + /* + i + */ + ; + } + { + inherit + /* + a + */ + ( + c + /* + d + */ + ) + /* + e + */ + f + /* + g + */ + h + ; + } + { + inherit + /* + a + */ + ( + c + /* + d + */ + ) + /* + e + */ + f + /* + g + */ + h + /* + i + */ + ; + } + { + inherit + /* + a + */ + ( + /* + b + */ + c + ) + f + h + ; + } + { + inherit + /* + a + */ + ( + /* + b + */ + c + ) + f + h + /* + i + */ + ; + } + { + inherit + /* + a + */ + ( + /* + b + */ + c + ) + f + /* + g + */ + h + ; + } + { + inherit + /* + a + */ + ( + /* + b + */ + c + ) + f + /* + g + */ + h + /* + i + */ + ; + } + { + inherit + /* + a + */ + ( + /* + b + */ + c + ) + /* + e + */ + f + h + ; + } + { + inherit + /* + a + */ + ( + /* + b + */ + c + ) + /* + e + */ + f + h + /* + i + */ + ; + } + { + inherit + /* + a + */ + ( + /* + b + */ + c + ) + /* + e + */ + f + /* + g + */ + h + ; + } + { + inherit + /* + a + */ + ( + /* + b + */ + c + ) + /* + e + */ + f + /* + g + */ + h + /* + i + */ + ; + } + { + inherit + /* + a + */ + ( + /* + b + */ + c + /* + d + */ + ) + f + h + ; + } + { + inherit + /* + a + */ + ( + /* + b + */ + c + /* + d + */ + ) + f + h + /* + i + */ + ; + } + { + inherit + /* + a + */ + ( + /* + b + */ + c + /* + d + */ + ) + f + /* + g + */ + h + ; + } + { + inherit + /* + a + */ + ( + /* + b + */ + c + /* + d + */ + ) + f + /* + g + */ + h + /* + i + */ + ; + } + { + inherit + /* + a + */ + ( + /* + b + */ + c + /* + d + */ + ) + /* + e + */ + f + h + ; + } + { + inherit + /* + a + */ + ( + /* + b + */ + c + /* + d + */ + ) + /* + e + */ + f + h + /* + i + */ + ; + } + { + inherit + /* + a + */ + ( + /* + b + */ + c + /* + d + */ + ) + /* + e + */ + f + /* + g + */ + h + ; + } + { + inherit + /* + a + */ + ( + /* + b + */ + c + /* + d + */ + ) + /* + e + */ + f + /* + g + */ + h + /* + i + */ + ; } ]