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 + then v x.${a} y.${a} # both have attr, use merge func
+ else x.${a} # only x has attr + 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 ### Changed

View file

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

View file

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

View file

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