1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-08-01 21:17:45 +00:00

feat: support inline comments on parens

This commit is contained in:
Kevin Amado 2022-02-28 16:48:22 -05:00
parent d839c3e5b2
commit efd9f93698
4 changed files with 71 additions and 40 deletions

View file

@ -46,6 +46,15 @@ Types of changes
+ then v x.${a} y.${a} # both have attr, use merge func
+ else x.${a} # only x has attr
```
- Inline comments support for `inherit` expressions:
```diff
inherit
(callPackage ../development/tools/ocaml/ocamlformat {})
- ocamlformat
- # latest version
+ ocamlformat # latest version
ocamlformat_0_11_0
```
### Changed

View file

@ -4,60 +4,76 @@ pub(crate) fn rule(
) -> std::collections::LinkedList<crate::builder::Step> {
let mut steps = std::collections::LinkedList::new();
let mut children = crate::children::Children::new(build_ctx, node);
let mut children = crate::children2::new(build_ctx, node);
let has_comments_or_newlines =
children.has_comments() || children.has_newlines();
let opener = children.next().unwrap();
let expression = children.next().unwrap();
let closer = children.next().unwrap();
let vertical = has_comments_or_newlines || build_ctx.vertical;
let vertical = opener.has_inline_comment
|| opener.has_trivialities
|| expression.has_inline_comment
|| expression.has_trivialities
|| closer.has_inline_comment
|| closer.has_trivialities;
// (
let child = children.get_next().unwrap();
steps.push_back(crate::builder::Step::Format(child));
if vertical && has_comments_or_newlines {
// opener
steps.push_back(crate::builder::Step::Format(opener.element));
if vertical {
steps.push_back(crate::builder::Step::Indent);
}
// /**/
children.drain_trivia(|element| match element {
crate::children::Trivia::Comment(text) => {
if let Some(text) = opener.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);
steps.push_back(crate::builder::Step::Comment(text));
}
crate::children::Trivia::Whitespace(_) => {}
});
// expr
let child = children.get_next().unwrap();
for trivia in opener.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);
}
crate::children2::Trivia::Newlines(_) => {}
}
}
// expression
if vertical {
if has_comments_or_newlines {
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
}
steps.push_back(crate::builder::Step::FormatWider(child));
steps.push_back(crate::builder::Step::FormatWider(expression.element));
} else {
steps.push_back(crate::builder::Step::Format(child));
steps.push_back(crate::builder::Step::Format(expression.element));
}
// /**/
children.drain_trivia(|element| match element {
crate::children::Trivia::Comment(text) => {
if let Some(text) = expression.inline_comment {
steps.push_back(crate::builder::Step::Whitespace);
steps.push_back(crate::builder::Step::Comment(text));
}
for trivia in expression.trivialities {
match trivia {
crate::children2::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(_) => {}
});
crate::children2::Trivia::Newlines(_) => {}
}
}
// )
let child = children.get_next().unwrap();
if vertical && has_comments_or_newlines {
// closer
if vertical {
steps.push_back(crate::builder::Step::Dedent);
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
}
steps.push_back(crate::builder::Step::Format(child));
steps.push_back(crate::builder::Step::Format(closer.element));
steps
}

View file

@ -1,4 +1,7 @@
(
( # test
a # test
)
( ( c ) )
( ( c )/*e*/)
( ( c/*d*/) )

View file

@ -1,4 +1,7 @@
(
( # test
a # test
)
((c))
(
(c)