Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

feature(formatter): Fill separators #2609

Merged
merged 30 commits into from
Jun 1, 2022
Merged

Conversation

NicholasLYang
Copy link
Contributor

@NicholasLYang NicholasLYang commented May 24, 2022

Summary

Adding a separator option for fill printing. Allows for JSX child list formatting as prettier fill prints JSX children with no spaces in between, when the children are a mixture of text and tags.

Test Plan

Added a doc test here. Existing tests for fill printing should still apply as well.

@NicholasLYang NicholasLYang temporarily deployed to aws May 24, 2022 20:22 Inactive
@github-actions
Copy link

github-actions bot commented May 24, 2022

Parser conformance results on ubuntu-latest

js/262

Test result main count This PR count Difference
Total 45878 45878 0
Passed 44938 44938 0
Failed 940 940 0
Panics 0 0 0
Coverage 97.95% 97.95% 0.00%

jsx/babel

Test result main count This PR count Difference
Total 39 39 0
Passed 36 36 0
Failed 3 3 0
Panics 0 0 0
Coverage 92.31% 92.31% 0.00%

symbols/microsoft

Test result main count This PR count Difference
Total 5946 5946 0
Passed 350 350 0
Failed 5591 5591 0
Panics 5 5 0
Coverage 5.89% 5.89% 0.00%

ts/babel

Test result main count This PR count Difference
Total 588 588 0
Passed 519 519 0
Failed 69 69 0
Panics 0 0 0
Coverage 88.27% 88.27% 0.00%

ts/microsoft

Test result main count This PR count Difference
Total 16257 16257 0
Passed 12391 12391 0
Failed 3866 3866 0
Panics 0 0 0
Coverage 76.22% 76.22% 0.00%

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented May 24, 2022

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: a0fc08a
Status: ✅  Deploy successful!
Preview URL: https://2b826283.tools-8rn.pages.dev

View logs

@github-actions
Copy link

github-actions bot commented May 24, 2022

"#
);

println!("{}", result.as_code());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe assert would be better here, since the function name is quick_test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, we should revert this line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not super important considering the quick_test function is ignored by default. It's just a way to test code quickly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies Nicholas, but even though this test is ignored, I use this test to check test things locally using my IDE (and probably other people too). If we remove the assertion, I will have to add it again.

Copy link
Contributor

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please go through my comments on the other PR and double check to address the review feedback.

/// let b = "Please do not commit memory bugs such as segfaults, buffer overflows, etc. otherwise you ";
/// let c = "<em>will</em>";
/// let d = " be reprimanded";
/// let expr = fill_elements(empty_element(), [token(a), token(b), token(c), token(d)]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please create a test with a spacer that has different "flat" and "expanded" representations and make sure the printer prints all these cases correctly

  • item, spacer, and next item fit
  • item and spacer fit, next item doesn't
  • only item fits

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I asked this in the JSX PR. What should the result be if the separator is printed differently? Because the fill printing should handle line breaking, not the separators themselves. Perhaps we should only allow non breaking separators?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, in fact, reading the fill printer code, at no point would we print a separator in expanded mode. Either it fits, in which case we print it flat, or we insert a hard line break. In theory we could replace that with printing the separator in expanded mode, but that'd mean users would have to provide a separator that breaks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I didn't see that comment.

Yes, we would need to change the logic inside of the printer to align it with Prettier's behaviour. The same as you did by allowing any other separator than space.

I quickly glanced through Prettier's fill use cases and the usage for arrays is interesting.

You can see that there's a use case where they use a hardline separator if the next element is separated by an empty line or if the element has a comment and a softline (softline_or_space) otherwise. This is something that we couldn't support with our current design because we only support a single separator.

Simply relying on the flat and expanded mode rather than hardcoding a line break also feels natural. It makes use of the fundamental principals of the printer and should also not be too hard to add. It should only require replacing the hard_line_break with the separator.

/// .as_code()
/// )
/// ```
pub fn fill_elements<TSep: Into<FormatElement>>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should TSep be Separator?

Copy link
Contributor Author

@NicholasLYang NicholasLYang May 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I stole this from one of the other functions. I can rename it if necessary

crates/rome_formatter/src/format_element.rs Show resolved Hide resolved
@NicholasLYang NicholasLYang temporarily deployed to aws May 25, 2022 22:09 Inactive
@NicholasLYang NicholasLYang temporarily deployed to aws May 26, 2022 17:10 Inactive
Comment on lines 1681 to 1682
fill.list.content.iter().any(FormatElement::will_break)
|| fill.separator.will_break()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit. Should be safe to remove the separator from here as it should never break in flat mode (or using fill is pointless)

@NicholasLYang NicholasLYang temporarily deployed to aws June 1, 2022 15:44 Inactive
@NicholasLYang NicholasLYang temporarily deployed to aws June 1, 2022 15:48 Inactive
@NicholasLYang NicholasLYang merged commit 010830c into main Jun 1, 2022
@NicholasLYang NicholasLYang deleted the feature/fill-separators branch June 1, 2022 16:26
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants