Repository navigation
Fix trailing newline bug - #1323
Conversation
| // Preserve trailing newline if original document had one and result doesn't | ||
| // This handles the case where adding content at the end removes the trailing newline | ||
| if content.ends_with('\n') && !result.ends_with('\n') { | ||
| result.push('\n'); | ||
| } |
There was a problem hiding this comment.
Huh, I'm kind of confused as to how this happens -- my understanding of tree-sitter-yaml is that our parse is "greedy" i.e. always includes the trailing newline, so I don't understand how we end up losing it.
(Also, do we need to do this newline check more generally, or only for the Add op?)
There was a problem hiding this comment.
Tree-sitter's feature byte span (node.start_byte() to node.end_byte()) ends at the feature's content, so a trailing newline after the feature is outside the span. Replacing only that span drops the trailing newline if the replacement doesn't include it. I'll update the code to fix it in all places along with more tests.
There was a problem hiding this comment.
I tried to reimplement the fix in a better way and fix it for Replace, ReplaceComment, RewriteFragment and Add operations. Also, I added more unit-tests.
…ure for almost all operations
Signed-off-by: William Woodruff <william@yossarian.net>
| let replacement_range = compute_replacement_range(&feature, document); | ||
| patched_content.replace_range(replacement_range, &patched_feature); |
There was a problem hiding this comment.
Not a blocker, just thinking out loud: it's not super ideal that we need these helpers now, since I could easily see us forgetting to call them in relevant places in the future. We might want to do more type state encoding here, i.e. have a RawSpan and AdjustedSpan or whatever and only allow replacements on ranges with the latter.
(That isn't necessarily the right approach either, just something that might help.)
Signed-off-by: William Woodruff <william@yossarian.net>
Signed-off-by: William Woodruff <william@yossarian.net>
Signed-off-by: William Woodruff <william@yossarian.net>
|
Thanks @mostafa! |
Pre-submission checks
Please check these boxes:
Mandatory: This PR corresponds to an issue (if not, please create
one first).
I hereby disclose the use of an LLM or other AI coding assistant in the
creation of this PR. PRs will not be rejected for using AI tools, but
will be rejected for undisclosed use.
If a checkbox is not applicable, you can leave it unchecked.
Summary
Fixed trailing newline removal when adding cooldown config to dependabot files by preserving the document's trailing newline in yamlpatch's
Op::Addhandler if the original document ended with one. This fixes #1315.Test Plan
Added a test case to the yamlpatch unit-tests.