forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fmt): fmt comments with local insertions (FuelLabs#4192)
## Description Closes FuelLabs#3938 ### Problem I've submitted a few PRs (FuelLabs#4129, FuelLabs#4093, FuelLabs#4005) implementing a new way of formatting comments in tackling FuelLabs#3938, but after submitting FuelLabs#4129 and following that working on formatting storage comments in a local branch realized that the method of formatting previously by writing `write_comments(..)` between every span is getting very unwieldy. That method results in a lot of repeated calls to `write_comments()`, and in some cases where we have to check if some value exists, it would lead to even more vertical space taken up because we have to do something like this: ```rust ... if let Some(item) = my_item { write_comments( formatted_code, start..end, formatter, ) // do other things } ... ``` My sense is that this will probably lead to the formatter code being hard to maintain in future because of the length of each `Format` implementations, and having to work your way through a lot of conditional code and repeated calls to `write_comments(..)` isn't a very nice experience. In FuelLabs#4005, the comment formatting for `ItemAbi` alone led to **56 additional line insertions** in that PR (previously it was 54 LoC, which means the LoC doubled!) ### Solution We adapt the original approach of inserting comments, but this time on a local scale. That means we parse each node on its own, compare its unformatted and formatted versions, and find out where to insert comments found between spans. Unlike the original approach, this approach will parse and format while the AST node is being visited and formatted - that means the entire `handle_comments(..)` that reparses the entire `Module` can be deprecated once this PR is in. This is done mainly through the function `rewrite_with_comments(..)` that is called at the end of a `impl Format`. Essentially what this function does is it parses the unformatted and formatted versions of the currently visited node with `parse_snippet::<P: sway_parse::Parse + Format>(..)`, finding the comments between the unformatted spans, and inserting them into the formatted spans. Based on the `CommentKind` of the found comments, we do some formatting to decide how they should be written. Of course, this also means that unlike the previously proposed method of using `write_comments()` everywhere, where we have the indentation context, this new approach means that we yet again do not have the local context. To figure out indentation, we assume that each comment will always be 'pinned' to some node below it - if this node does not exist we try to 'pin' to a node above it instead. ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [x] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers.
- Loading branch information
1 parent
d735e99
commit 318fcde
Showing
22 changed files
with
454 additions
and
421 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.