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): comments in item abi (FuelLabs#4005)
## Description Part of FuelLabs#3938 This PR handles **comment formatting** for the abi, as well as comments before modules (this is to pass the test suite) This was a pretty tricky PR to tackle, since I cycled through several thought processes while trying to finish this. 1) I tried to solve the problem at the parse/lex level - cycled back to the thought of re-parsing the file to create a special commented AST - worked a bit on that before I scrapped the whole idea because it just didn't seem necessary. 2) I then started trying to solve it in the originally discussed way, attempting to format the comments within each `Format` implementation. This started off well - it was trivial to navigate within a span, but it was particularly tricky navigating _between_ spans, and for formatting the ABI, it turns out that trailing comments can appear after an attribute, or even after the fn signature. I initially tried to implement a way to 'look forward', but soon realized it was impossible unless we want to hold a reference to the unformatted code in each `Format` implementation (which I guess is what I ended up doing anyway) 3) So, I ended up looking for a way to 'look back', but still used 'look ahead' for comments: Comments will always be 'anchored' to the next span - meaning to say, when a trailing comment exists, it will 'belong' to the next span instead of the span(s) on the same line. This means that when we know the locations of 2 spans, we can easily call `write_comments` to insert the comments in between. While we mentioned that looking forward is impossible, one place we _can_ look forward though, is between comments in the unformatted code, if there are multiple comments to be written at once. This is basically what `collect_newlines_after_comment_span()` is doing. ### Other notable changes - Renamed `write_comments_from_comment_map` to `write_comments` to be more concise. - Introduce `CommentsContext` to hold both `CommentMap` and `unformatted_code`, instead of exposing `unformatted_code` at the Formatter level. - Changed generic `T` to `ItemKind` within attribute.rs. This is so we can have access to `span()`. - Lexing the commented token tree now gives us `CommentKind` info. ## 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. --------- Co-authored-by: Kaya Gökalp <[email protected]>
- Loading branch information
1 parent
d2f4e67
commit 8bef89c
Showing
15 changed files
with
498 additions
and
51 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
Oops, something went wrong.