Repository navigation
fix(controller): stop dropping documents and double-applying formatter edits (#180) - #323
Merged
Merged
Conversation
…ransformer sortYamlWrapper() and formatYamlWrapper() duplicated the handling of yaml directives, delimiters and the loop over all documents, and mixed it with editor access. Move it into sortDocuments() and formatDocuments(), which work on plain text and accept the yaml operations as a dependency, so they can be tested without a VS Code instance. No behavior change. Characterization tests cover the moved code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Jdb5is9YsMnteaxHryN26M
The loop over all documents used forEach with `return []`, which only left the callback. A document that could not be sorted was dropped from the result, the delimiters shifted, and "Keys resorted successfully" was shown anyway. For a selection that could not be sorted, e.g. an indented section (refs #53), the result was an empty string, so the selection was deleted. sortDocuments() now returns null as soon as one document fails, and the controller applies no edits in that case. The success message is shown after the edits were applied. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Jdb5is9YsMnteaxHryN26M
The document formatting provider called the sort/format commands, which applied their edits via workspace.applyEdit() and also returned them. VS Code then applied the returned edits a second time, so formatting needed two undo steps (closes #180). The provider also ignored the document it was given and used the active editor and its selection. Move the provider into YamlFormattingProvider: it transforms the given document as a whole and only returns one edit with a validated full range. Register it through context.subscriptions so it is disposed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Jdb5is9YsMnteaxHryN26M
|
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 34 |
| Duplication | 0 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.



What changed
document-transformer.ts(055788f)sortYamlWrapper()andformatYamlWrapper()duplicated the handling of directives, delimiters and the loop over all documents. That logic now lives insortDocuments()/formatDocuments(). Both work on plain text, take the yaml operations as an injectable dependency and can be tested without a VS Code instance. No behavior change; characterization tests cover the moved code.8a31b23)sortDocuments()returnsnullas soon as one document fails, and the controller then applies no edits. The success message is shown only after the edits were applied.1005353, closes Weird double format #180)New
YamlFormattingProvider: it transforms the document VS Code passes in as a whole and returns exactly one edit with a validated full range. It is registered viacontext.subscriptions, so it is disposed on deactivation.Why
forEachwithreturn [], which only left the callback. A document that failed to sort was silently dropped, the delimiters shifted, and "Keys resorted successfully" was shown anyway. For a selection that could not be sorted, e.g. an indented section (refs Sorting an arbitrary section (with leading spaces) #53), the result was an empty string, so the selection was deleted.workspace.applyEdit()and also returned them. VS Code applied the returned edits a second time, so formatting needed two undo steps (Weird double format #180). The provider also ignored the document it was given and used the active editor.Behavior changes
Testing
document-transformer.test.ts; the two regression tests for dropped documents and the emptied selection failed before the fix.formatting-provider.test.ts: one edit with a full range, no self-applied edit (Weird double format #180), uses the given document instead of the active editor, no edits for invalid YAML.tsc,eslint, dependency-cruiser (only the known circular dependency), and 109 tests via mocha.formatting-provider.test.tsneeds a real VS Code instance and was only run against a mock, so please rely on CI.Manual check for #180: run "Format Document" on an unsorted YAML file, then undo once. The original content should be back.