Skip to content

fix(controller): stop dropping documents and double-applying formatter edits (#180) - #323

Merged
pascalre merged 3 commits into
masterfrom
feat/controller-edit-safety
Nov 19, 2026
Merged

pascalre merged 3 commits into
masterfrom
feat/controller-edit-safety

Conversation

@pascalre

Copy link
Copy Markdown
Owner

What changed

  • Refactor: multi-document handling moved into document-transformer.ts (055788f)
    sortYamlWrapper() and formatYamlWrapper() duplicated the handling of directives, delimiters and the loop over all documents. That logic now lives in sortDocuments() / 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.
  • Abort sorting when a document cannot be sorted (8a31b23)
    sortDocuments() returns null as soon as one document fails, and the controller then applies no edits. The success message is shown only after the edits were applied.
  • Formatter returns its edits instead of applying them itself (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 via context.subscriptions, so it is disposed on deactivation.

Why

  • The loop used forEach with return [], 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.
  • The formatting provider called the commands, which applied their edits via 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

  • "Format Document" with an active selection now formats the whole document (standard VS Code behavior). Selections are handled by the sort commands.
  • Format on save / "Format Document" no longer shows "Keys resorted successfully".

Testing

  • New tests in document-transformer.test.ts; the two regression tests for dropped documents and the emptied selection failed before the fix.
  • New tests in 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.
  • Locally verified: tsc, eslint, dependency-cruiser (only the known circular dependency), and 109 tests via mocha.
  • Not verified locally: the full Electron suite. formatting-provider.test.ts needs 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.

pascalre and others added 3 commits November 19, 2026 12:57
…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
@pascalre
pascalre merged commit fa0f06e into master Nov 19, 2026
3 of 6 checks passed
@pascalre
pascalre deleted the feat/controller-edit-safety branch November 19, 2026 13:28
@sonarqubecloud

Copy link
Copy Markdown

@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 34 complexity · 0 duplication

Metric Results
Complexity 34
Duplication 0

View in Codacy

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Weird double format

1 participant