diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cb4b77..ee590d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,16 @@ Types of changes + assert stdenv.isDarwin -> libXaw != null; # fails to link otherwise + ``` +- Inline comments support for `if-then-else` expressions: + ```diff + if y ? ${a} + - then v x.${a} y.${a} + - # both have attr, use merge func + - else x.${a} + - # only x has attr + + then v x.${a} y.${a} # both have attr, use merge func + + else x.${a} # only x has attr + ``` ### Changed diff --git a/src/alejandra_engine/src/children2.rs b/src/alejandra_engine/src/children2.rs index a4818bd..954335d 100644 --- a/src/alejandra_engine/src/children2.rs +++ b/src/alejandra_engine/src/children2.rs @@ -1,10 +1,12 @@ use std::collections::LinkedList; +#[derive(Debug)] pub(crate) enum Trivia { Comment(String), Newlines(usize), } +#[derive(Debug)] pub(crate) struct Child { pub element: rnix::SyntaxElement, @@ -28,16 +30,26 @@ pub(crate) fn new( let mut inline_comment = None; let mut trivialities = LinkedList::new(); + let mut skip_next_newline = false; children.drain_trivia(|element| match element { crate::children::Trivia::Comment(text) => { - if trivialities.is_empty() && text.starts_with('#') { + if inline_comment.is_none() + && trivialities.is_empty() + && text.starts_with('#') + { inline_comment = Some(text); + skip_next_newline = true; } else { trivialities.push_back(Trivia::Comment(text)); } } crate::children::Trivia::Whitespace(text) => { - let newlines = crate::utils::count_newlines(&text); + let mut newlines = crate::utils::count_newlines(&text); + + if skip_next_newline && newlines > 0 { + newlines -= 1; + skip_next_newline = false; + } if newlines > 0 { trivialities.push_back(Trivia::Newlines(newlines)) diff --git a/src/alejandra_engine/src/rules/inherit.rs b/src/alejandra_engine/src/rules/inherit.rs index c9f97c8..981ae8a 100644 --- a/src/alejandra_engine/src/rules/inherit.rs +++ b/src/alejandra_engine/src/rules/inherit.rs @@ -4,45 +4,83 @@ pub(crate) fn rule( ) -> std::collections::LinkedList { let mut steps = std::collections::LinkedList::new(); - let mut children = crate::children::Children::new(build_ctx, node); + let children: Vec = + crate::children2::new(build_ctx, node).collect(); - let vertical = children.has_comments() - || children.has_newlines() - || build_ctx.vertical; + let vertical = build_ctx.vertical + || children + .iter() + .any(|child| child.has_inline_comment || child.has_trivialities); + + let children_count = children.len() - 1; + let mut children = children.into_iter(); // inherit - let child = children.get_next().unwrap(); - steps.push_back(crate::builder::Step::Format(child)); + let child = children.next().unwrap(); + steps.push_back(crate::builder::Step::Format(child.element)); if vertical { steps.push_back(crate::builder::Step::Indent); } - loop { - // /**/ - children.drain_trivia(|element| match element { - crate::children::Trivia::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::Trivia::Whitespace(_) => {} - }); + if let Some(text) = child.inline_comment { + steps.push_back(crate::builder::Step::Whitespace); + steps.push_back(crate::builder::Step::Comment(text)); + steps.push_back(crate::builder::Step::NewLine); + steps.push_back(crate::builder::Step::Pad); + } else if vertical { + steps.push_back(crate::builder::Step::NewLine); + steps.push_back(crate::builder::Step::Pad); + } - if let Some(child) = children.get_next() { - // expr - if vertical { + for trivia in child.trivialities { + match trivia { + crate::children2::Trivia::Comment(text) => { + steps.push_back(crate::builder::Step::Comment(text)); steps.push_back(crate::builder::Step::NewLine); steps.push_back(crate::builder::Step::Pad); - steps.push_back(crate::builder::Step::FormatWider(child)); - } else { - if let rnix::SyntaxKind::TOKEN_SEMICOLON = child.kind() { - } else { - steps.push_back(crate::builder::Step::Whitespace); + } + crate::children2::Trivia::Newlines(_) => {} + } + } + + for (index, child) in children.into_iter().enumerate() { + let not_last_child = index + 1 < children_count; + + if vertical { + steps.push_back(crate::builder::Step::FormatWider(child.element)); + + if let Some(text) = child.inline_comment { + steps.push_back(crate::builder::Step::Whitespace); + steps.push_back(crate::builder::Step::Comment(text)); + steps.push_back(crate::builder::Step::NewLine); + steps.push_back(crate::builder::Step::Pad); + } else if (not_last_child && !child.has_trivialities) + || matches!( + child.trivialities.front(), + Some(crate::children2::Trivia::Comment(_)) + ) + { + steps.push_back(crate::builder::Step::NewLine); + steps.push_back(crate::builder::Step::Pad); + } + + for trivia in child.trivialities { + match trivia { + crate::children2::Trivia::Comment(text) => { + steps.push_back(crate::builder::Step::Comment(text)); + } + crate::children2::Trivia::Newlines(_) => {} + } + if not_last_child { + steps.push_back(crate::builder::Step::NewLine); + steps.push_back(crate::builder::Step::Pad); } - steps.push_back(crate::builder::Step::Format(child)); } } else { - break; + if not_last_child { + steps.push_back(crate::builder::Step::Whitespace); + } + steps.push_back(crate::builder::Step::Format(child.element)); } } diff --git a/src/alejandra_engine/tests/cases/inherit/in b/src/alejandra_engine/tests/cases/inherit/in index 3e924ed..84dfa14 100644 --- a/src/alejandra_engine/tests/cases/inherit/in +++ b/src/alejandra_engine/tests/cases/inherit/in @@ -11,4 +11,19 @@ { inherit /*a*/ b d /*e*/ ; } { inherit /*a*/ b /*c*/ d ; } { inherit /*a*/ b /*c*/ d /*e*/ ; } + { + inherit # test + a # test + + b # test + c # test + d # test + + e + f + + g + h + ; + } ] diff --git a/src/alejandra_engine/tests/cases/inherit/out b/src/alejandra_engine/tests/cases/inherit/out index ee5526a..e76b649 100644 --- a/src/alejandra_engine/tests/cases/inherit/out +++ b/src/alejandra_engine/tests/cases/inherit/out @@ -86,4 +86,18 @@ */ ; } + { + inherit # test + a # test + + b # test + c # test + d # test + + e + f + g + h + ; + } ]