Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support inline comments on parens #227

Merged
merged 2 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ 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
```
- Inline comments support for `parenthesis` expressions:
```diff
- || (
- # Accept {} for tests that are unsupported
+ || ( # Accept {} for tests that are unsupported
isDerivation x
&& x ? meta.timeout
);
```

### Changed

Expand Down
88 changes: 52 additions & 36 deletions src/alejandra_engine/src/rules/paren.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
steps.push_back(crate::builder::Step::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);
}

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(_) => {}
}
crate::children::Trivia::Whitespace(_) => {}
});
}

// expr
let child = children.get_next().unwrap();
// 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));
}

if let Some(text) = expression.inline_comment {
steps.push_back(crate::builder::Step::Whitespace);
steps.push_back(crate::builder::Step::Comment(text));
}

// /**/
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));
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(_) => {}
}
crate::children::Trivia::Whitespace(_) => {}
});
}

// )
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
}
3 changes: 3 additions & 0 deletions src/alejandra_engine/tests/cases/paren/in
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
(
( # test
a # test
)
( ( c ) )
( ( c )/*e*/)
( ( c/*d*/) )
Expand Down
3 changes: 3 additions & 0 deletions src/alejandra_engine/tests/cases/paren/out
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
(
( # test
a # test
)
((c))
(
(c)
Expand Down