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

fix(grainfmt): Refactor code formatter #1042

Merged
merged 121 commits into from
Dec 31, 2021
Merged

fix(grainfmt): Refactor code formatter #1042

merged 121 commits into from
Dec 31, 2021

Conversation

marcusroberts
Copy link
Member

I've rebuilt the formatter to use a context aware approach to determining comments to print. This saves the pre-scan of locations and comments and has much better performance.

I've also heavily refactored the code to really improve maintainability and now brings consistent formatting to anything that is iterated (e.g. top level statements, block statements, record fields etc). There are some improvements like supporting start of line comments too.

compiler/grainformat/comment_utils.re Outdated Show resolved Hide resolved
compiler/grainformat/comment_utils.re Outdated Show resolved Hide resolved
compiler/grainformat/comment_utils.re Outdated Show resolved Hide resolved
compiler/grainformat/comment_utils.re Outdated Show resolved Hide resolved
compiler/grainformat/comment_utils.re Outdated Show resolved Hide resolved
compiler/grainformat/reformat.re Outdated Show resolved Hide resolved
compiler/grainformat/reformat.re Outdated Show resolved Hide resolved
compiler/test/formatter_outputs/application.gr Outdated Show resolved Hide resolved
compiler/test/formatter_outputs/comments.gr Outdated Show resolved Hide resolved
compiler/test/formatter_outputs/comments.gr Outdated Show resolved Hide resolved
Copy link
Member

@phated phated left a comment

Choose a reason for hiding this comment

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

A few things. Haven't reviewed the huge reformat.re file yet.

Comment on lines -20 to +19
let single_ast_line = "x"
let single_ast_line = "x"
Copy link
Member

Choose a reason for hiding this comment

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

Why did this change if it uses // formatter-ignore?

Copy link
Member Author

Choose a reason for hiding this comment

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

Keen eye! There was a good reason for it to do with locations, I'll have another look though

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, because it's really hard (impossible) to add the spacing back. We don't know how much padding the formatter is adding around where we are inserting. So you can see it works in the line above but not here. I can't see how to fix it presently.

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 figured this was the best of a poor choice

compiler/grainformat/comment_utils.re Outdated Show resolved Hide resolved
compiler/grainformat/comment_utils.re Outdated Show resolved Hide resolved
compiler/grainformat/comment_utils.re Outdated Show resolved Hide resolved
compiler/grainformat/comment_utils.re Outdated Show resolved Hide resolved
compiler/grainformat/comment_utils.re Outdated Show resolved Hide resolved
compiler/grainformat/comment_utils.re Outdated Show resolved Hide resolved
compiler/grainformat/comment_utils.re Outdated Show resolved Hide resolved
compiler/grainformat/comment_utils.re Outdated Show resolved Hide resolved
compiler/src/parsing/parser.dyp Show resolved Hide resolved
Copy link
Member

@phated phated left a comment

Choose a reason for hiding this comment

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

@marcusroberts This was an absolute delight to read through. Awesome work! I had a few more pieces of advice in reformat.re

compiler/grainformat/reformat.re Outdated Show resolved Hide resolved
switch (comments) {
| [] => separator
| _ =>
let last_comment = List.nth(comments, List.length(comments) - 1);
Copy link
Member

Choose a reason for hiding this comment

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

Just musing: I wonder what the performance characteristics of your impl instead of List.hd(List.rev(comment)) (because you have to iterate the whole list to get the length and then you have to iterate it again to get the nth item?)

Copy link
Member Author

Choose a reason for hiding this comment

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

A good question, and I wondered the same. The number of comments in the list should be short, as this is usually scoped to a statement , expression etc. Worth changing do you think?

Copy link
Member

Choose a reason for hiding this comment

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

I want to defer to @ospencer because maybe he has more understanding of the implications of each.

Copy link
Member

Choose a reason for hiding this comment

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

This is probably much faster since it avoids all the allocations of creating a new list if you do List.rev. But of course, the fastest would be a recursive function that just pulls the last item 🙂

compiler/grainformat/reformat.re Outdated Show resolved Hide resolved
compiler/grainformat/reformat.re Outdated Show resolved Hide resolved
compiler/grainformat/reformat.re Outdated Show resolved Hide resolved
compiler/grainformat/reformat.re Outdated Show resolved Hide resolved
compiler/grainformat/reformat.re Outdated Show resolved Hide resolved
compiler/grainformat/reformat.re Outdated Show resolved Hide resolved
compiler/grainformat/reformat.re Outdated Show resolved Hide resolved
compiler/grainformat/reformat.re Outdated Show resolved Hide resolved
Copy link
Member

@ospencer ospencer left a comment

Choose a reason for hiding this comment

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

Fantastic work @marcusroberts!!!

Copy link
Member

@phated phated left a comment

Choose a reason for hiding this comment

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

Absolutely fantastic! Love the work that went into this. I'll merge once CI finishes.

@phated phated merged commit 9bc7a55 into main Dec 31, 2021
@phated phated deleted the marcus/formatter_v2 branch December 31, 2021 19:04
@github-actions github-actions bot mentioned this pull request May 31, 2022
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.

None yet

3 participants