-
Notifications
You must be signed in to change notification settings - Fork 0
fix handling of multiple sequential edits #292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
I think lets hold off on merging this until we have all the bug fixes resolved from the release? This might require some in depth testing |
| const tree = components.syntaxTreeManager.getSyntaxTree(documentUri); | ||
|
|
||
| // Short-circuit if this is not a template (anymore) | ||
| if (document.cfnFileType === CloudFormationFileType.Other) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be cfnFileType !== CloudFormationFileType.Template ? we have gitsync, unknown, other and empty types now, don't think we need to do anything with gitsync files either
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Our current detection isn't perfect and we have lots of Completion tests that expect valid completions on templates even when cfnFileType !== CloudFormationFileType.Template
Bug: Syntax Tree Corruption with Incremental Edits
Problem
When users typed quickly or made multiple edits in a single change event, the syntax tree became corrupted with ERROR nodes and nodes containing ": null" text. This caused incorrect follow-up (after the first) code action edits and application.
Root Cause
The didChangeHandler was using the final document content to calculate edits for each incremental change. Since each change's range was calculated
against intermediate content states, applying them with the final content caused incorrect edit positions and tree corruption. This was seemingly not a problem for manual code edits as the handler was probably only receiving one incremental update at a time, but a code action with multiple edits triggers this bug.
Buggy code flow:
DocumentHandler.content = textDocument.getText()(final state)createEdit(content, change.text, start, end)content, but ranges assume intermediate statesERRORnodes and corrupted stringsSolution
Track the current content state and apply each change sequentially:
This ensures each incremental edit is applied correctly with proper content state, maintaining tree integrity while preserving performance through tree-sitter's incremental parsing.
Changes
• src/handlers/DocumentHandler.ts: Fixed to apply edits sequentially with correct content state
• src/context/syntaxtree/SyntaxTree.ts: Added getRootNode() public method for testing
• tst/unit/handlers/DocumentHandler.test.ts: Added test reproducing exact edit sequence from production logs, validates no tree corruption
• tst/resources/templates/sample_template_after_edits.json: Expected template state for test validation
Verification
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.