Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This implements AST comments and updates the formatter to look for comments in the AST instead of a separate comments list.
This adds a new
:include_comments
toCode.string_to_quoted/2
andCode.string_to_quoted!/2
.Comments are attaches to AST nodes in three variants:
:leading_comments
: Comments that are located before a node, or in the same line.Examples:
:trailing_comments
: Comments that are located after a node, and before the end of the parent enclosing the node(or the root document).Examples:
:inner_comments
: Comments that are located inside an empty node.Examples:
A comment may be considered inner or trailing depending on wether the enclosing node is empty or not. For example, in the following code:
The comment is considered inner because the
do
block is empty. However, in the following code:The comment is considered trailing to
bar
because thedo
block is not empty.In the case no nodes are present in the AST but there are comments, they are inserted into the root
:__block__
node as:inner_comments
.Many of the ideas here come from Sourceror, which takes inspiration for the
recast
parser.Different from Sourceror, it introduces
:inner_comments
to remove the ambiguity between comments before theend
keyword, and comments after an expression. This also becomes important when working on the formatter code, as it is very awkward to inject inner comments after the call args were already converted to algebra docs.The current implementation in this PR adds an extra traversal step after the code is initially parsed. Due to this, and due to the logic to merge comments and AST requiring precise line information in every AST nodes, it requires to also set up the following options:
Where
&preserve_comments/5
is the same function used to extract comments for formatting.I first explored an alternative where we would convert comments into tokens in the tokenizer and then merge them in the parsing step to avoid an extra traversal and setting up extra options, but I couldn't figure out a maintainable way to do it. It seemed to require changing the definition of most expressions in the parser .yrl file, or similarly complex logic in a related file to the one in the
Code.Comments
module I introduce here.I'm open to suggestions and guidance if we want to move this to the parsing step instead.
In the current implementation, we perform an extra traversal to merge comments into the AST, which
TODO