Skip to content

Conversation

@DavidGregory084
Copy link
Member

@DavidGregory084 DavidGregory084 commented Aug 29, 2025

Fixes #154.

This adds a combinator for Doc that indents text within the doc to the line position at the start of the Doc:

text("A").appendSpace(align(text("B").appendLine("C")))

==>

A B
  C

@DavidGregory084 DavidGregory084 marked this pull request as draft August 29, 2025 07:03
@codecov-commenter
Copy link

codecov-commenter commented Aug 29, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 85.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.98%. Comparing base (40a9cdf) to head (7e33de9).

Files with missing lines Patch % Lines
...main/java/com/opencastsoftware/prettier4j/Doc.java 85.00% 3 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@             Coverage Diff              @@
##               main     #155      +/-   ##
============================================
- Coverage     94.18%   93.98%   -0.20%     
- Complexity      283      286       +3     
============================================
  Files            12       12              
  Lines           946      965      +19     
  Branches        171      173       +2     
============================================
+ Hits            891      907      +16     
- Misses           44       47       +3     
  Partials         11       11              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…ignment and add tests that exercise the interaction between indentation, grouping and alignment
*
* Sets the indentation for line breaks within its inner {@link Doc} at the current line position.
*/
public static class Align extends Doc {
Copy link
Member Author

Choose a reason for hiding this comment

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

It may also be worth adding an absolute Position variant of Align that takes a user-provided position argument to be applied after line breaks

return align(this);
}

/**
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm a bit wary of adding too much stuff to this library, but it's possible to get "hanging" style indent by adding a variant of bracket with the following implementation:

        return group(align(
                left
                        .append(align(lineDoc.append(this).indent(indent).margin(marginDoc)))
                        .append(lineDoc.append(right))));

void testBracketFlatteningWithAlign() {
// Note: the arguments are aligned with the "functionCall" element because bracket doesn't support alignment.
// TODO: Consider adding a `hangingBracket` combinator that aligns the bracket docs with the starting line position
// and the arguments with the opening bracket doc.
Copy link
Member Author

Choose a reason for hiding this comment

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

hangingBracket refers to the variant of bracket I describe above

@DavidGregory084 DavidGregory084 changed the title WIP: Add Doc.align that aligns subsequent line breaks to the current line position Add Doc.align that aligns subsequent line breaks to the current line position Aug 29, 2025
@DavidGregory084 DavidGregory084 marked this pull request as ready for review August 29, 2025 12:00
} else if (entryDoc instanceof LineOr) {
// Reset line length
position = entryIndent;
position = 0;
Copy link
Member Author

Choose a reason for hiding this comment

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

This change is based on this observation that sending out the indent text below already manipulates the position to add the entryIndent.

I missed this while implementing Margin.

@DavidGregory084 DavidGregory084 merged commit 48a56fc into main Aug 29, 2025
2 checks passed
@FedericoPonzi
Copy link

FedericoPonzi commented Aug 29, 2025

Hey @DavidGregory084 when you have a moment, can you please release a new library version? Thanks! And thanks for fixing this so quickly :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Vertical alignment

4 participants