Skip to content

fix: keep delimiters and directives with their documents, make recursive sort reliable - #326

Merged
pascalre merged 3 commits into
masterfrom
feat/document-handling
Nov 19, 2026
Merged

pascalre merged 3 commits into
masterfrom
feat/document-handling

Conversation

@pascalre

Copy link
Copy Markdown
Owner

What changed

  • Delimiters and directives stay with their documents (958f9ae)
    New splitDocuments() in yaml-text.ts splits a text into documents, each with its own delimiter line, and returns directives (%YAML …, %TAG …) separately. The transformer joins the documents again, adds a leading --- only if needed, keeps the delimiter of a selection as it is and keeps empty documents. JsYamlAdapter.validateYaml() uses the same split. Removed the unused splitYaml(), getDelimiters() and removeLeadingLineBreakOfFirstElement().
  • Reliable "Recursively sort YAML files" (0c88b32)
    New controller module file-sorter.ts: sorts each file with sortDocuments(), reports a file that cannot be sorted and continues with the next one, asks for a folder when no uri is given. FileUtil now only finds files and passes the directory as glob's cwd. In package.json the command is hidden from the command palette and only offered for folders in the explorer.
  • Formatter stays silent (0e3e73b, refs No need to send notification when used as a formatter #65)
    "Yaml formatted successfully" moved from YamlUtil.formatYaml() into the Format YAML command.

Why

  • Delimiters were collected separately and assigned to documents by position. getDelimiters() added the leading --- that setLeadingDashes() added again, so several documents without a leading delimiter started with ---\n---, which is an additional empty document. With a leading --- # first, the comments shifted to the next document and the last one was lost.
  • Files with directives could not be processed at all: validateYaml() loaded %YAML 1.2 as a document of its own and failed, so the directive handling was unreachable. This also affected the Validate YAML command.
  • The recursive sort went through Controller, whose constructor throws without an active editor. From the command palette it got no uri and crashed. Each file was sorted as a single document, so every file with several documents failed. The directory was part of the glob pattern, where a backslash of a Windows path is an escape character.
  • formatYaml() showed its message once per document, also for "Format Document" and format on save, although 5.5.0 stopped notifications for the formatter (No need to send notification when used as a formatter #65).

Behavior changes

  • Directives are now kept instead of removed.
  • A leading --- # comment is kept as it is (with useLeadingDashes: true).
  • Recursively sort YAML files is no longer listed in the command palette and only appears on folders in the explorer.
  • "Format Document" / format on save no longer shows a success message.

Testing

  • New regression tests, each seen to fail before the fix:
    • document-transformer.test.ts: single leading delimiter, delimiters with comments, empty documents, directives (with and without useLeadingDashes), formatting several documents, no notification from formatDocuments()
    • file-sorter.test.ts: file with several documents, invalid file stays unchanged (plus the moved sortFile tests)
    • yaml-text.test.ts: splitDocuments(), including the cases ported from the former splitYaml() tests
  • Removed tests: those for the deleted functions (getDelimiters(): 7, splitYaml(): 5, ported) and the extension test "should remove yaml metadata tags", which did not await the command and therefore only compared the unchanged test file.
  • Locally verified: tsc, eslint, dependency-cruiser (no violations), 168 tests via mocha. The affected Electron tests were reproduced with the real test files and give the expected results.
  • Not verified: the full Electron suite (please rely on CI), and the file search on Windows (follows from the glob documentation).

🤖 Generated with Claude Code

https://claude.ai/code/session_01Jdb5is9YsMnteaxHryN26M

pascalre and others added 3 commits November 19, 2026 19:58
The delimiters were collected separately from the documents and later
assigned by position. getDelimiters() already added the leading `---`
that setLeadingDashes() added again, so several documents without a
leading delimiter started with `---\n---` (an additional empty
document). With a leading `--- # first`, the comments shifted to the
next document and the last one was lost.

Directives like `%YAML 1.2` were never handled: validateYaml() loaded
them as a document of their own and failed, so the directive handling
in the transformer was unreachable.

Replace splitYaml() and getDelimiters() with splitDocuments(), which
keeps every delimiter with its own document and returns the directives
separately. The transformer joins the documents again, adds a leading
delimiter only if needed, keeps the one of a selection as it is and
keeps empty documents.

Remove the unused splitYaml(), getDelimiters() and
removeLeadingLineBreakOfFirstElement() with their tests; the
splitYaml() tests are ported to splitDocuments(). Remove the directive
test of the extension suite: it did not await the command, so it
compared the unchanged test file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Jdb5is9YsMnteaxHryN26M
"Recursively sort YAML files" went through the Controller, whose
constructor throws without an active text editor, so the command
failed when no file was open. Run from the command palette, it got no
uri and crashed. Each file was sorted as a single document, so every
file with several documents failed. The directory was interpolated
into the glob pattern, where a backslash of a windows path is an
escape character.

Move the command into the new controller module file-sorter: it sorts
each file with sortDocuments(), reports a file that cannot be sorted
and continues with the next one, and asks for a folder when no uri is
given. FileUtil now only finds the files and passes the directory as
glob's cwd. The command is hidden from the command palette and only
offered for folders in the explorer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Jdb5is9YsMnteaxHryN26M
YamlUtil.formatYaml() showed "Yaml formatted successfully" itself, once
per document. The formatter uses it too, so "Format Document" and
format on save showed one notification per document, although 5.5.0
stopped notifications for the formatter (refs #65).

Remove the message from formatYaml() and show it in the command, once,
after the edits were applied. YamlUtil no longer depends on the VS Code
adapter.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Jdb5is9YsMnteaxHryN26M
@pascalre
pascalre merged commit cf6e5bc into master Nov 19, 2026
5 of 7 checks passed
@pascalre
pascalre deleted the feat/document-handling branch November 19, 2026 20:03
@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 25 complexity · -2 duplication

Metric Results
Complexity 25
Duplication -2

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.

1 participant