-
Notifications
You must be signed in to change notification settings - Fork 656
☂️ Formatter API improvements #1726
Comments
We probably also want to explore to codegen the union formatter implementations |
It makes sense. The only drawback is that if we create new nodes out of it, we must implement also the I think we could also automate that part, at a certain extent. RomeJS used to create a builder for each node. If we decided to go in the same direction, we might be able to auto generate a simple scaffolding for new nodes. |
When finishing the trivia changes I had to decide what to do when formatting statements with problems. See https://github.com/rome/tools/pull/1801/files#r752272632 Basically we are trimming "whitespaces" around the statement; but keeping everything inside as it is. |
After writing a lot of boilerplate code, I vote for adding
This is more explicit, and we gain more control on the |
Handling of "manually inserted newlines"To align with prettier, we should collapse new lines appropriately. As @ematipico pointed out in #1882 (comment)
|
There are two long wrapping patterns that can have a common API:
We already have some boilerplate code: Ok(group_elements(format_elements!(
formatter.format_token(&self.l_brack_token()?)?,
soft_indent(format_elements![
join_elements(
soft_line_break_or_space(),
formatter.format_separated(elements)?
),
trailing_comma,
]),
formatter.format_token(&self.r_brack_token()?)?,
)))
|
The second example has a different pattern for each case, this means that we would not be able to create a utility for all of them, I think. |
I am going to take a look at creating some helpers inside our formatter API |
@leops is it something that you have been doing? |
Yes I should probably update this because some of these tasks ended up being implemented as part of the comments PR: |
CST to CST formatting might be possible but can be tracked as separate exploration task. The helpers will be tracked in #1996 |
Part of #1718
Umbrella task to update the
Formatter
after we made the improvements to our parser.Change formatter result type #1740
Rename
format_raw
toformat_verbatim
feat(rome_formatter): format_verbatim and unknown nodes #1976Decision: Add helpers for formatting nodes
A) Implement extension methods on
SyntaxResult
,Option
, andAstNode
/SyntaxToken
to format the element (to reduce the number of try operators). You can find some ideas here but please spend some time to think about alternatives (for example, should we add helpers to theFormatResult
type?).B) Provide helpers to format optional tokens, for example,
formatter.format_optional_token(token) -> FormatElement
and overloadformat_token
to accept anOption
:format_token<T: Into<Option<Token>>
? Or implement theformat
methods as extension methods?Implemented as part of feat(rome_formatter): retain comments in the formatter output #1916:
format_token
is generic over optional tokens, can take either aSyntaxToken
orOption<SyntaxToken>
and return aFormatElement
orOption<FormatElement>
respectivelyChange the formatter to visit every token, including
,
and;
.feat(rome_formatter): retain comments in the formatter output #1916
Re-evaluate CST-> CST printing? This should now be possible with explicit rules to which token trivia gets attached.
The text was updated successfully, but these errors were encountered: