Fix tree node start and end locations #2084
Merged
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.
Some tree types are guaranteed to have certain children, even if those children are empty. For example, SHAPE_STATEMENT always has a TRAIT_STATEMENT as its first child. Since trees with children have their start/end line and column locations computed from the respective locations of their first and last child, trees with 0 children ("empty" trees) will have a location of (0,0)-(0,0). If an empty child is the first or last child of its parent, the 0 location will bubble up to that parent.
This commit changes the way start/end line and column locations are computed for trees with children, such that empty children are ignored. This required a change to use statement sorting, which relied on the 0 location bubbling up to the parent to indicate to the formatter whether the use statement has a line comment.
Additional tests were added for the formatter and tree location computation. TokenTreeLeaf toString was also updated to include tree location, useful for debugging purposes.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.