[snippets] Fix incorrect range traversal when resolving variables #1043
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.
Identify the Bug
This has been biting me for a while. Given the following snippet:
This is what I expect:
=
This produces a lovely banner comment… until I try it again with leading whitespace.
snippets-before.mov
When we expand a snippet, we know where the variables are within the snippet; they're described using
Point
s, but the origin of the coordinate system is the beginning of the snippet. To translate them into actual bufferPoint
s during snippet expansion, we “add” each point to thePoint
that marks the insertion point of the snippet: e.g., “start at (1, 2), then go right two columns and down one row.“When doing so, we need to remember that, when a snippet contains a newline and content after that newline, that second line of content will be indented by the same amount as the initial line. We know how much leading whitespace (if any) there was before the snippet trigger text. Folks tend to want a snippet to be indented an equal amount on each line unless they explicitly add/subtract space on subsequent lines; so if a snippet creates a new line of text, we make sure it starts with an equal amount of whitespace.
Description of the Change
For this reason, we were adding the two points incorrectly. Since the column position doesn't reset to
0
each time a row is advanced, we should've been using Point#translate, not Point#traverse.I'd have caught this earlier if I had thought to test the combination of variable expansion and leading whitespace.
Does it work?
snippets-after.mov
(I see other usages of
Point#traverse
in the same file, but those contexts are different, so I'll leave them be until they can be proven to be the source of a bug.)Alternate Designs
Since this was an incredibly simple change, I considered no alternate designs.
Possible Drawbacks
None.
Verification Process
Added a new spec for this behavior; all the existing specs pass. If the button's green, click it!
Release Notes