From 958f9aef117a465d3312378d18830edeb0fefa0c Mon Sep 17 00:00:00 2001 From: Pascal Reitermann Date: Thu, 19 Nov 2026 19:58:58 +0000 Subject: [PATCH 1/3] fix(documents): keep delimiters and directives with their documents 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 Claude-Session: https://claude.ai/code/session_01Jdb5is9YsMnteaxHryN26M --- CHANGELOG.md | 3 + src/adapter/js-yaml-adapter.ts | 7 +- src/controller/document-transformer.ts | 92 +++++++++++-------- src/lib.ts | 15 --- .../controller/document-transformer.test.ts | 44 +++++++++ src/test/suite/extension.test.ts | 77 ---------------- src/test/suite/util/yaml-text.test.ts | 38 ++++++++ src/test/suite/util/yaml-util.test.ts | 73 --------------- src/util/yaml-text.ts | 41 ++++++++- src/util/yaml-util.ts | 44 --------- 10 files changed, 177 insertions(+), 257 deletions(-) create mode 100644 src/test/suite/util/yaml-text.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index fc4e628..87ccd49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased ๐Ÿ› Bugfixes +* Keep every document delimiter (`---`, including a comment behind it) in front of its own document. Before, several documents without a leading `---` got a duplicated `---` (an additional empty document), and with a leading `--- # comment` the comments moved to the next document or got lost +* Keep yaml directives like `%YAML 1.2` when sorting or formatting, and accept them when validating. Before, files with directives could not be processed +* Keep empty documents (`---` directly followed by `---`) instead of aborting * Setting `sortArrays` now sorts arrays with mixed item types deterministically: numbers (numerically), booleans and strings first, mappings and nested lists keep their order at the end. Before, such arrays were left in an arbitrary order * Setting `sortOrderReverse` now also applies to the default sort and to the keys following the keywords of a custom sort. Before, it only worked with `useCustomSortRecursively` * (closes [#53](https://github.com/pascalre/vscode-yaml-sort/issues/53)) Sort an indented selection, e.g. a part of a list or the children of a nested key, without having to unindent it first. The selection keeps its indentation diff --git a/src/adapter/js-yaml-adapter.ts b/src/adapter/js-yaml-adapter.ts index d21ed5d..7008821 100644 --- a/src/adapter/js-yaml-adapter.ts +++ b/src/adapter/js-yaml-adapter.ts @@ -2,7 +2,7 @@ import { dump, load } from "js-yaml" import { Settings } from "../settings" import { SortUtil } from "../util/sort-util" -import { removeTrailingCharacters, splitYaml } from "../util/yaml-text" +import { removeTrailingCharacters, splitDocuments } from "../util/yaml-text" /** * @param value Any loaded yaml value. @@ -24,8 +24,9 @@ export class JsYamlAdapter { * @returns true if yaml is valid */ validateYaml(text: string): boolean { - splitYaml(text).forEach((yaml) => { - load(yaml, { schema: this.settings.getSchema() }) + // directives are not part of a document's content, so they are not loaded (see splitDocuments) + splitDocuments(text).documents.forEach((document) => { + load(document.content, { schema: this.settings.getSchema() }) }) return true } diff --git a/src/controller/document-transformer.ts b/src/controller/document-transformer.ts index 6d344a5..8959d75 100644 --- a/src/controller/document-transformer.ts +++ b/src/controller/document-transformer.ts @@ -1,12 +1,12 @@ import { JsYamlAdapter } from "../adapter/js-yaml-adapter" -import { prependWhitespacesOnEachLine, removeLeadingLineBreakOfFirstElement, removeLeadingSpacesOnEachLine } from "../lib" -import { splitYaml, validateTextRange } from "../util/yaml-text" +import { prependWhitespacesOnEachLine, removeLeadingSpacesOnEachLine } from "../lib" +import { YamlDocument, YamlStream, splitDocuments, validateTextRange } from "../util/yaml-text" import { YamlUtil } from "../util/yaml-util" /** * The parts of YamlUtil a transformation depends on. Kept narrow so tests can pass a stub. */ -export type YamlOperations = Pick +export type YamlOperations = Pick export interface SortOptions { /** 0 for the default sort, 1 to 3 for the custom sort keywords. */ @@ -15,28 +15,48 @@ export interface SortOptions { isSelection: boolean } -interface PreparedText { - text: string - delimiters: string[] -} +/** + * Transforms the content of a single, non-empty document. Returns null if that fails. + */ +type ContentTransformation = (content: string) => string | null -const DIRECTIVE = /^%.*\n/m -const DIRECTIVES = /^%.*\n/gm +/** + * Puts the transformed documents back together. + * @param stream The directives and the documents, with their transformed content. + * @param leadingDashes Whether the first document should start with a delimiter. Directives always keep theirs. + * @returns The joined text. + */ +const joinDocuments = (stream: YamlStream, leadingDashes: boolean | undefined): string => { + const rendered = stream.documents.map((document, index) => { + let delimiter = document.delimiter + if (index === 0 && leadingDashes !== undefined && stream.directives === "") { + // undefined keeps the first delimiter as it is, e.g. for a selection + delimiter = leadingDashes ? (delimiter ?? "---") : undefined + } + return [delimiter, document.content].filter((line) => line).join("\n") + }) + return stream.directives + rendered.join("\n") +} /** - * Removes yaml directives (e.g. `%YAML 1.1`) and collects the document delimiters. - * @param text Text containing one or more yaml documents. - * @param delimiters Delimiters as returned by YamlUtil.getDelimiters(). - * @returns The text without directives and the delimiters to put in front of each document. + * Applies a transformation to every non-empty document. Empty documents are kept as they are. + * @returns The transformed stream, or null if one document could not be transformed. */ -const removeDirectives = (text: string, delimiters: RegExpMatchArray): PreparedText => { - let adjustedDelimiters = delimiters - if (DIRECTIVE.test(text)) { - adjustedDelimiters.shift() - adjustedDelimiters = removeLeadingLineBreakOfFirstElement(adjustedDelimiters) +const transformDocuments = (stream: YamlStream, transform: ContentTransformation): YamlStream | null => { + const documents: YamlDocument[] = [] + for (const document of stream.documents) { + if (document.content.trim() === "") { + documents.push({ delimiter: document.delimiter, content: "" }) + continue + } + const content = transform(document.content) + if (!content) { + // never return a partial result: it would drop documents or empty the selection + return null + } + documents.push({ delimiter: document.delimiter, content }) } - const withoutDirectives = text.replace(DIRECTIVES, "").replace(/^\n/, "") - return { text: withoutDirectives, delimiters: adjustedDelimiters } + return { directives: stream.directives, documents } } /** @@ -52,19 +72,16 @@ export const sortDocuments = (text: string, options: SortOptions, yaml: YamlOper validateTextRange(text) new JsYamlAdapter().validateYaml(text) - const prepared = removeDirectives(text, yaml.getDelimiters(text, !options.isSelection)) - let newText = "" - for (const unsortedYaml of splitYaml(prepared.text)) { + const sorted = transformDocuments(splitDocuments(text), (content) => { // an indented selection is sorted like a top-level document and indented again afterwards (see #53) - const indentation = options.isSelection ? YamlUtil.getNumberOfLeadingSpaces(unsortedYaml) : 0 - const sortedYaml = yaml.sortYaml(removeLeadingSpacesOnEachLine(unsortedYaml, indentation), options.customSort) - if (!sortedYaml) { - // never return a partial result: it would drop documents or empty the selection - return null - } - newText += (prepared.delimiters.shift() ?? "") + prependWhitespacesOnEachLine(sortedYaml, indentation) + const indentation = options.isSelection ? YamlUtil.getNumberOfLeadingSpaces(content) : 0 + const sortedContent = yaml.sortYaml(removeLeadingSpacesOnEachLine(content, indentation), options.customSort) + return sortedContent ? prependWhitespacesOnEachLine(sortedContent, indentation) : null + }) + if (!sorted) { + return null } - return options.isSelection ? newText : yaml.setLeadingDashes(newText) + return joinDocuments(sorted, options.isSelection ? undefined : yaml.settings.useLeadingDashes) } /** @@ -75,14 +92,9 @@ export const sortDocuments = (text: string, options: SortOptions, yaml: YamlOper * (the error has already been reported to the user). */ export const formatDocuments = (text: string, yaml: YamlOperations = new YamlUtil()): string | null => { - const prepared = removeDirectives(text, yaml.getDelimiters(text, true)) - let newText = "" - for (const unformattedYaml of splitYaml(prepared.text)) { - const formattedYaml = yaml.formatYaml(unformattedYaml, false) - if (!formattedYaml) { - return null - } - newText += (prepared.delimiters.shift() ?? "") + formattedYaml + const formatted = transformDocuments(splitDocuments(text), (content) => yaml.formatYaml(content, false)) + if (!formatted) { + return null } - return yaml.setLeadingDashes(newText) + return joinDocuments(formatted, yaml.settings.useLeadingDashes) } diff --git a/src/lib.ts b/src/lib.ts index fc86265..930c9a0 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -14,21 +14,6 @@ export function prependWhitespacesOnEachLine(text: string, count: number): strin return text.replace(/^(?!---)/mg, spaces) } -/** - * Removes the leading line break of the first element of an array. - * @param {RegExpMatchArray} delimiters Array for processing. - * @returns {RegExpMatchArray} - */ -export function removeLeadingLineBreakOfFirstElement(delimiters: RegExpMatchArray): RegExpMatchArray { - let firstDelimiter = delimiters.shift() - if (firstDelimiter) { - firstDelimiter = firstDelimiter.replace(/^\n/, "") - delimiters.unshift(firstDelimiter) - } - return delimiters -} - - /** * Removes up to a given count of leading spaces from every single line in a text. * The counterpart of prependWhitespacesOnEachLine(). diff --git a/src/test/suite/controller/document-transformer.test.ts b/src/test/suite/controller/document-transformer.test.ts index a28c3c9..8907538 100644 --- a/src/test/suite/controller/document-transformer.test.ts +++ b/src/test/suite/controller/document-transformer.test.ts @@ -73,6 +73,50 @@ suite("Test DocumentTransformer - sortDocuments() with an indented selection (#5 }) }) +suite("Test DocumentTransformer - delimiters and directives", () => { + const sort = (text: string, yaml?: YamlOperations) => sortDocuments(text, { customSort: 0, isSelection: false }, yaml) + + test("should add exactly one leading delimiter to several documents without one", () => { + equal(sort("b: 1\n---\nd: 1\nc: 2"), "---\nb: 1\n---\nc: 2\nd: 1") + }) + + test("should keep every delimiter with its comment in front of its own document", () => { + const text = "--- # first\nb: 1\n--- # second\nd: 1\nc: 2" + + equal(sort(text), "--- # first\nb: 1\n--- # second\nc: 2\nd: 1") + }) + + test("should drop only the leading delimiter when useLeadingDashes is false", () => { + const yamlutil = new YamlUtil() + yamlutil.settings.useLeadingDashes = false + + equal(sort("--- # first\nb: 1\n--- # second\nd: 1\nc: 2", yamlutil), "b: 1\n--- # second\nc: 2\nd: 1") + }) + + test("should keep an empty document", () => { + equal(sort("---\n---\nb: 1\na: 2"), "---\n---\na: 2\nb: 1") + }) + + test("should keep yaml directives in front of the first document", () => { + equal(sort("%YAML 1.2\n---\nb: 1\na: 2"), "%YAML 1.2\n---\na: 2\nb: 1") + }) + + test("should keep yaml directives when useLeadingDashes is false, because they require a delimiter", () => { + const yamlutil = new YamlUtil() + yamlutil.settings.useLeadingDashes = false + + equal(sort("%YAML 1.2\n---\nb: 1\n---\nd: 1\nc: 2", yamlutil), "%YAML 1.2\n---\nb: 1\n---\nc: 2\nd: 1") + }) + + test("should keep the delimiters of a selection as they are", () => { + equal(sortDocuments("b: 1\n---\nd: 1\nc: 2", { customSort: 0, isSelection: true }), "b: 1\n---\nc: 2\nd: 1") + }) + + test("should format several documents without a leading delimiter", () => { + equal(formatDocuments("b: 1\n---\na: 2"), "---\nb: 1\n---\na: 2") + }) +}) + suite("Test DocumentTransformer - formatDocuments()", () => { test("should format every document and keep the delimiters", () => { equal(formatDocuments("---\nkey1: value\n---\nkey2: value"), "---\nkey1: value\n---\nkey2: value") diff --git a/src/test/suite/extension.test.ts b/src/test/suite/extension.test.ts index 3ce18d8..8a4e757 100644 --- a/src/test/suite/extension.test.ts +++ b/src/test/suite/extension.test.ts @@ -5,7 +5,6 @@ import { Position, Range, Selection, Uri, commands, window, workspace } from "vs import { Controller } from "../../controller/controller" import { Settings } from "../../settings" -import { splitYaml } from "../../util/yaml-text" suite("Test getCustomSortKeywords", () => { test("should return values of `vscode-yaml-sort.customSortKeywords_1`", () => { @@ -135,80 +134,4 @@ suite("Test sortYamlWrapper", () => { equal(true, false) } }) - test("should remove yaml metadata tags (directives)", async () => { - const uri = Uri.parse(resolve("./src/test/files/testMetadataTags.yaml")) - const doc = await workspace.openTextDocument(uri) - await window.showTextDocument(doc, { preview: false }) - - const activeEditor = window.activeTextEditor - if (activeEditor) { - const actual = - "%YAML 1.1\n" + - "---\n" + - "key:\n" + - " key2: value" - const expected = - "key:\n" + - " key2: value" - - activeEditor.edit((builder) => builder.replace( - new Range( - new Position(0, 0), - new Position(activeEditor.document.lineCount + 1, 0)), - actual)) - - await commands.executeCommand("vscode-yaml-sort.sortYaml") - equal(activeEditor.document.getText(), expected) - } else { - equal(true, false) - } - }) -}) - -suite("Test splitYaml", () => { - test("should return the input string, when the input does not contain `---`", () => { - const actual = `\ -- Orange -- Apple` - deepEqual(splitYaml(actual), ["- Orange\n- Apple"]) - }) - test("should return the input document without the delimiters", () => { - const actual = `\ ---- -- Orange -- Apple` - deepEqual(splitYaml(actual), ["\n- Orange\n- Apple"]) - }) - test("should return an array with the yaml documents", () => { - const actual = `\ -- Orange -- Apple ---- -- Orange -- Apple` - deepEqual(splitYaml(actual), ["- Orange\n- Apple\n", "\n- Orange\n- Apple"]) - }) - test("Split multiple yaml documents with leading dashes", () => { - const actual = `\ ---- -- Orange -- Apple ---- -- Orange -- Apple` - - deepEqual(splitYaml(actual), - ["\n- Orange\n- Apple\n", "\n- Orange\n- Apple"]) - }) - test("Split multiple yaml documents with text behind delimiter", () => { - const actual = `\ ---- # comment 1 -- Orange -- Apple ---- text -- Orange -- Apple` - deepEqual(splitYaml(actual), - ["\n- Orange\n- Apple\n", "\n- Orange\n- Apple"]) - }) }) diff --git a/src/test/suite/util/yaml-text.test.ts b/src/test/suite/util/yaml-text.test.ts new file mode 100644 index 0000000..1ac5339 --- /dev/null +++ b/src/test/suite/util/yaml-text.test.ts @@ -0,0 +1,38 @@ +import { deepEqual } from "assert" + +import { splitDocuments } from "../../../util/yaml-text" + +suite("Test splitDocuments()", () => { + test("should return a single document without delimiter when the text contains no `---`", () => { + deepEqual(splitDocuments("- Orange\n- Apple"), { directives: "", documents: [{ content: "- Orange\n- Apple" }] }) + }) + + test("should keep the leading delimiter with the first document", () => { + deepEqual(splitDocuments("---\n- Orange"), { directives: "", documents: [{ delimiter: "---", content: "- Orange" }] }) + }) + + test("should keep every delimiter including its comment with its own document", () => { + deepEqual(splitDocuments("--- # one\na: 1\n--- text\nb: 2"), { + directives: "", + documents: [ + { delimiter: "--- # one", content: "a: 1\n" }, + { delimiter: "--- text", content: "b: 2" }, + ], + }) + }) + + test("should return a first document without delimiter when the text does not start with `---`", () => { + deepEqual(splitDocuments("a: 1\n---\nb: 2").documents, [{ content: "a: 1\n" }, { delimiter: "---", content: "b: 2" }]) + }) + + test("should keep an empty document", () => { + deepEqual(splitDocuments("---\n---\na: 1").documents, [{ delimiter: "---", content: "" }, { delimiter: "---", content: "a: 1" }]) + }) + + test("should return directives separately from the documents", () => { + deepEqual(splitDocuments("%YAML 1.2\n%TAG ! tag:example.com,2000:\n---\na: 1"), { + directives: "%YAML 1.2\n%TAG ! tag:example.com,2000:\n", + documents: [{ delimiter: "---", content: "a: 1" }], + }) + }) +}) diff --git a/src/test/suite/util/yaml-util.test.ts b/src/test/suite/util/yaml-util.test.ts index 8836606..c799461 100644 --- a/src/test/suite/util/yaml-util.test.ts +++ b/src/test/suite/util/yaml-util.test.ts @@ -445,79 +445,6 @@ suite("Test YamlUtil - transformMultilineValue()", () => { }) }) /* -suite("Test YamlUtil - getDelimiters()", () => { - test("should return `` when the input string does not contain a delimiter and isSelectionEmpty=true and useLeadingDashes=false", () => { - const doc = - 'yaml: data\n' + - 'spec: spec' - - equal(new YamlUtil().getDelimiters(doc, true), "") - }) - test("should return all delimiters except the first (return an empty string instead) when the input document starts with a delimiter and isSelectionEmpty=true and useLeadingDashes=false", () => { - const doc = - '--- text\n' + - 'yaml: data\n' + - '--- #comment\n' + - 'spec: spec' - const yamlutil = new YamlUtil() - yamlutil.settings.useLeadingDashes = false - const actual = yamlutil.getDelimiters(doc, true) - - deepequal(actual, ["", "\n--- #comment\n"]) - }) - test("should add leading dashes when useLeadingDashes=true and given document does not have leading dashes", () => { - const doc = - 'foo: bar\n' + - '---\n' + - 'foo2: baz' - const actual = new YamlUtil().getDelimiters(doc, true) - - deepequal(actual, ["---\n", "\n---\n"]) - }) - test("Get each delimiter (empty selection, leading linebreak)", () => { - const doc = - '\n' + - '--- text\n' + - 'yaml: data\n' + - '--- #comment\n' + - 'spec: spec---\n' + - '\n' - - const yamlutil = new YamlUtil() - yamlutil.settings.useLeadingDashes = false - const actual = yamlutil.getDelimiters(doc, true) - deepequal(actual, ["", "\n--- #comment\n"]) - }) - test("Get each delimiter (empty selection, leading text)", () => { - const doc = - 'bla\n' + - '--- text\n' + - 'test: bla' - const yamlutil = new YamlUtil() - yamlutil.settings.useLeadingDashes = false - const actual = yamlutil.getDelimiters(doc, true) - deepequal(actual, ["", "\n--- text\n"]) - }) - test("Get each delimiter (with selection, leading delimiter)", () => { - const doc = - '--- text\n' + - 'test: bla' - const yamlutil = new YamlUtil() - yamlutil.settings.useLeadingDashes = false - const actual = yamlutil.getDelimiters(doc, false) - deepEqual(actual, ["--- text\n"]) - }) - test("Get each delimiter (with selection, leading text)", () => { - const doc = - 'bla\n' + - '--- text\n' + - 'test: bla' - const yamlutil = new YamlUtil() - yamlutil.settings.useLeadingDashes = false - const actual = yamlutil.getDelimiters(doc, false) - deepEqual(actual, ["", "\n--- text\n"]) - }) -}) */ suite("Test YamlUtil - sortYaml() with quoted helm values", () => { test("GitHub issue #130: should not mangle a quoted helm value containing a dollar sign", () => { diff --git a/src/util/yaml-text.ts b/src/util/yaml-text.ts index 13ee034..753bfb9 100644 --- a/src/util/yaml-text.ts +++ b/src/util/yaml-text.ts @@ -27,10 +27,41 @@ export function validateTextRange(textRange: string) { } /** - * Splits a string, which contains multiple yaml documents. - * @param {string} multipleYamls String which contains multiple yaml documents. - * @returns {[string]} Array of yaml documents. + * A yaml document together with the delimiter line (`---`, optionally with a comment) in front of it. */ -export function splitYaml(multipleYamls: string): [string] { - return multipleYamls.split(/^---.*/m).filter((obj) => obj) as [string] +export interface YamlDocument { + /** The delimiter line without line break, undefined if the document has none. */ + delimiter?: string + /** The content of the document, without leading empty lines. */ + content: string +} + +export interface YamlStream { + /** Directives like `%YAML 1.2` in front of the first delimiter, including their line breaks. */ + directives: string + documents: YamlDocument[] +} + +/** + * Splits a text into its yaml documents, keeping every delimiter with its own document. + * @param text Text containing one or more yaml documents. + * @returns The directives and the documents. + */ +export const splitDocuments = (text: string): YamlStream => { + // a capturing group makes split() return the delimiters between the contents: + // [leading content, delimiter, content, delimiter, content, ...] + const parts = text.split(/^(---.*)$/m) + const withoutLeadingEmptyLines = (content: string) => content.replace(/^(?:[ \t]*\n)+/, "") + const leading = parts[0] + const stream: YamlStream = { directives: "", documents: [] } + + if (/^%/m.test(leading)) { + stream.directives = leading + } else if (leading.trim() !== "") { + stream.documents.push({ content: withoutLeadingEmptyLines(leading) }) + } + for (let i = 1; i < parts.length; i += 2) { + stream.documents.push({ delimiter: parts[i], content: withoutLeadingEmptyLines(parts[i + 1]) }) + } + return stream } diff --git a/src/util/yaml-util.ts b/src/util/yaml-util.ts index 54c33ba..9a92393 100644 --- a/src/util/yaml-util.ts +++ b/src/util/yaml-util.ts @@ -114,48 +114,4 @@ export class YamlUtil { } } - /** - * Returns all delimiters with comments. - * @param {string} multipleYamls String which contains multiple yaml documents. - * @param {boolean} isSelectionEmpty Specify if the text is an selection - * @param {boolean} useLeadingDashes Specify if the documents should have a leading delimiter. - * If set to false, it will add an empty array element at the beginning of the output. - * @returns {[string]} Array of yaml delimiters. - */ - getDelimiters(yamls: string, isSelectionEmpty: boolean): RegExpMatchArray { - // remove empty lines - let multipleYamls = yamls.trim() - multipleYamls = multipleYamls.replace(/^\n/, "") - let delimiters = multipleYamls.match(/^---.*/gm) - if (!delimiters) { - return [""] - } - - // append line break to every delimiter - delimiters = delimiters.map((delimiter) => `\n${delimiter}\n`) as RegExpMatchArray - - if (delimiters) { - if (isSelectionEmpty) { - if (!this.settings.useLeadingDashes && multipleYamls.startsWith("---")) { - delimiters.shift() - delimiters.unshift("") - } else if (this.settings.useLeadingDashes && !multipleYamls.startsWith("---")) { - delimiters.unshift("---\n") - } else { - delimiters.unshift("") - } - } else { - if (!multipleYamls.startsWith("---")) { - delimiters.unshift("") - } else { - let firstDelimiter = delimiters.shift() - if (firstDelimiter) { - firstDelimiter = firstDelimiter.replace(/^\n/, "") - delimiters.unshift(firstDelimiter) - } - } - } - } - return delimiters - } } From 0c88b32646f724e1c7866d84703327a4bd610df1 Mon Sep 17 00:00:00 2001 From: Pascal Reitermann Date: Thu, 19 Nov 2026 20:00:47 +0000 Subject: [PATCH 2/3] fix(files): sort yaml files in a directory reliably "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 Claude-Session: https://claude.ai/code/session_01Jdb5is9YsMnteaxHryN26M --- CHANGELOG.md | 1 + README.md | 2 +- package.json | 9 ++- src/controller/controller.ts | 23 +------ src/controller/file-sorter.ts | 48 +++++++++++++ src/extension.ts | 5 +- src/test/suite/controller/file-sorter.test.ts | 67 +++++++++++++++++++ src/test/suite/util/file-util.test.ts | 43 +----------- src/util/file-util.ts | 40 ++++------- 9 files changed, 142 insertions(+), 96 deletions(-) create mode 100644 src/controller/file-sorter.ts create mode 100644 src/test/suite/controller/file-sorter.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 87ccd49..a6a0c49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased ๐Ÿ› Bugfixes +* Command `Recursively sort YAML files` now sorts files with several documents, works without an open editor and is only offered for folders in the explorer. A file that cannot be sorted is reported and left unchanged. Files are now also found on Windows paths * Keep every document delimiter (`---`, including a comment behind it) in front of its own document. Before, several documents without a leading `---` got a duplicated `---` (an additional empty document), and with a leading `--- # comment` the comments moved to the next document or got lost * Keep yaml directives like `%YAML 1.2` when sorting or formatting, and accept them when validating. Before, files with directives could not be processed * Keep empty documents (`---` directly followed by `---`) instead of aborting diff --git a/README.md b/README.md index 896dfdf..d2c8147 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ This extension contributes the following commands: | `Custom Sort 2` | Same as `Custom Sort 1` | | `Custom Sort 3` | Same as `Custom Sort 1` | | `Format Document` | Formats a yaml document without sorting it. Also possible using the shortcut (โ‡งโŒฅF on Mac). | -| `Recursively sort YAML files` | Sorts all `.yaml` and `.yml` files in a directory and all its subdirectories. A newline at the end of a file will be retained. | +| `Recursively sort YAML files` | Sorts all `.yaml` and `.yml` files in a directory and all its subdirectories, including files with several documents. Run it from the context menu of a folder in the explorer. A newline at the end of a file will be retained. | | `Sort YAML` | Sorts a given YAML. You can either sort the whole YAML document or sort only a selection of the text. | | `Validate YAML` | Validates a given YAML. | diff --git a/package.json b/package.json index 6c1f6f7..ff88c58 100644 --- a/package.json +++ b/package.json @@ -260,10 +260,17 @@ } ], "menus": { + "commandPalette": [ + { + "command": "vscode-yaml-sort.sortYamlFilesInDirectory", + "when": "false" + } + ], "explorer/context": [ { "command": "vscode-yaml-sort.sortYamlFilesInDirectory", - "title": "Recursively sort YAML files" + "title": "Recursively sort YAML files", + "when": "explorerResourceIsFolder" } ] } diff --git a/src/controller/controller.ts b/src/controller/controller.ts index 42b5a14..c4b68af 100644 --- a/src/controller/controller.ts +++ b/src/controller/controller.ts @@ -1,16 +1,14 @@ -import { TextEdit, TextEditor, Uri, window } from "vscode" +import { TextEdit, TextEditor, window } from "vscode" import { formatDocuments, sortDocuments } from "./document-transformer" import { JsYamlAdapter } from "../adapter/js-yaml-adapter" import { Severity, VsCodeAdapter } from "../adapter/vs-code-adapter" import { Settings } from "../settings" import { ErrorUtil } from "../util/error-util" -import { FileUtil } from "../util/file-util" import { YamlUtil } from "../util/yaml-util" export class Controller { editor: TextEditor - fileutil = new FileUtil() yamlutil = new YamlUtil() jsyamladapter = new JsYamlAdapter() vscodeadapter = new VsCodeAdapter() @@ -37,25 +35,6 @@ export class Controller { return false } - /** - * Sorts all yaml files in a directory - * @param {Uri} uri Base URI - */ - sortYamlFiles(uri: Uri) { - const files = this.fileutil.getFiles(uri.fsPath) - files.forEach((file: string) => { - this.sortFile(file) - }) - } - - sortFile(file: string) { - try { - this.fileutil.sortFile(file) - } catch (error) { - this.errorutil.handleError(error) - } - } - formatYamlWrapper(): TextEdit[] { const newText = formatDocuments(VsCodeAdapter.getActiveDocument(this.editor), this.yamlutil) if (newText === null) { diff --git a/src/controller/file-sorter.ts b/src/controller/file-sorter.ts new file mode 100644 index 0000000..ad72664 --- /dev/null +++ b/src/controller/file-sorter.ts @@ -0,0 +1,48 @@ +import { readFileSync, writeFileSync } from "fs" + +import { Uri } from "vscode" + +import { YamlOperations, sortDocuments } from "./document-transformer" +import { Severity, VsCodeAdapter } from "../adapter/vs-code-adapter" +import { ErrorUtil } from "../util/error-util" +import { FileUtil } from "../util/file-util" +import { YamlUtil } from "../util/yaml-util" + +/** + * Sorts all yaml documents of a file and writes the result back. + * A trailing line break of the original file is retained (see #186). + * @param file Path of the file. + * @param yaml Yaml operations, injectable for tests. + * @throws Error if the file cannot be read, is not valid yaml or cannot be sorted. + */ +export const sortFile = (file: string, yaml: YamlOperations = new YamlUtil()): void => { + const text = readFileSync(file, "utf-8") + let sorted = sortDocuments(text, { customSort: 0, isSelection: false }, yaml) + if (sorted === null) { + throw new Error(`File ${file} could not be sorted`) + } + if (text.endsWith("\n") && !sorted.endsWith("\n")) { + sorted += "\n" + } + writeFileSync(file, sorted) +} + +/** + * Command handler: sorts every yaml file in a directory and its subdirectories. + * A file that cannot be sorted is reported and left unchanged; the other files are still sorted. + * @param uri The directory selected in the explorer. Undefined when the command is run from the command palette. + */ +export const sortYamlFilesInDirectory = (uri: Uri | undefined): void => { + const errorutil = new ErrorUtil() + if (!uri) { + new VsCodeAdapter().showMessage(Severity.ERROR, "Please run this command from the context menu of a folder in the explorer.") + return + } + for (const file of new FileUtil().getFiles(uri.fsPath)) { + try { + sortFile(file) + } catch (error) { + errorutil.handleError(error) + } + } +} diff --git a/src/extension.ts b/src/extension.ts index 9e5a271..7814445 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -2,6 +2,7 @@ import { ExtensionContext, Uri, commands } from "vscode" import { VsCodeAdapter } from "./adapter/vs-code-adapter" import { Controller } from "./controller/controller" +import { sortYamlFilesInDirectory } from "./controller/file-sorter" import { YamlFormattingProvider } from "./controller/formatting-provider" export function activate(context: ExtensionContext) { @@ -16,8 +17,8 @@ export function activate(context: ExtensionContext) { commands.registerCommand("vscode-yaml-sort.formatYaml", () => { new Controller().formatYamlWrapper() }), - commands.registerCommand("vscode-yaml-sort.sortYamlFilesInDirectory", (uri: Uri) => { - new Controller().sortYamlFiles(uri) + commands.registerCommand("vscode-yaml-sort.sortYamlFilesInDirectory", (uri?: Uri) => { + sortYamlFilesInDirectory(uri) }) ) for (const index of [1, 2, 3]) { diff --git a/src/test/suite/controller/file-sorter.test.ts b/src/test/suite/controller/file-sorter.test.ts new file mode 100644 index 0000000..6c0f24d --- /dev/null +++ b/src/test/suite/controller/file-sorter.test.ts @@ -0,0 +1,67 @@ +import { doesNotThrow, equal, ok, throws } from "assert" +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "fs" +import { tmpdir } from "os" +import { join } from "path" + +import { sortFile } from "../../../controller/file-sorter" + +suite("Test FileSorter - sortFile()", () => { + + test("should throw when file does not contain a valid yaml", () => { + throws(() => sortFile("./src/test/suite/util/resources/subfolder/foo2.yaml")) + }) + + test("should not throw when file contains a valid yaml", () => { + doesNotThrow(() => sortFile("./src/test/suite/util/resources/subfolder/.customyaml")) + }) + + test("should retain a trailing newline when the original file has one", () => { + const dir = mkdtempSync(join(tmpdir(), "yaml-sort-")) + const file = join(dir, "trailing-newline.yaml") + try { + writeFileSync(file, "zkey: zvalue\nakey: avalue\n") + sortFile(file) + const sorted = readFileSync(file, "utf-8") + ok(sorted.endsWith("\n")) + } finally { + rmSync(dir, { recursive: true, force: true }) + } + }) + + test("should not add a trailing newline when the original file has none", () => { + const dir = mkdtempSync(join(tmpdir(), "yaml-sort-")) + const file = join(dir, "no-trailing-newline.yaml") + try { + writeFileSync(file, "zkey: zvalue\nakey: avalue") + sortFile(file) + const sorted = readFileSync(file, "utf-8") + ok(!sorted.endsWith("\n")) + } finally { + rmSync(dir, { recursive: true, force: true }) + } + }) + + test("should sort every document of a file with several documents", () => { + const dir = mkdtempSync(join(tmpdir(), "yaml-sort-")) + const file = join(dir, "multiple-documents.yaml") + try { + writeFileSync(file, "b: 1\n---\nd: 1\nc: 2\n") + sortFile(file) + equal(readFileSync(file, "utf-8"), "---\nb: 1\n---\nc: 2\nd: 1\n") + } finally { + rmSync(dir, { recursive: true, force: true }) + } + }) + + test("should leave a file unchanged when it cannot be sorted", () => { + const dir = mkdtempSync(join(tmpdir(), "yaml-sort-")) + const file = join(dir, "invalid.yaml") + try { + writeFileSync(file, "a: [") + throws(() => sortFile(file)) + equal(readFileSync(file, "utf-8"), "a: [") + } finally { + rmSync(dir, { recursive: true, force: true }) + } + }) +}) diff --git a/src/test/suite/util/file-util.test.ts b/src/test/suite/util/file-util.test.ts index 285a6cb..2d3e4d9 100644 --- a/src/test/suite/util/file-util.test.ts +++ b/src/test/suite/util/file-util.test.ts @@ -1,7 +1,4 @@ -import { deepEqual, doesNotThrow, ok, throws } from "assert" -import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "fs" -import { tmpdir } from "os" -import { join } from "path" +import { deepEqual } from "assert" import { Settings } from "../../../settings" import { FileUtil } from "../../../util/file-util" @@ -35,41 +32,3 @@ suite("Test FileUtil - getFiles()", () => { deepEqual(fileutil.getFiles("./src/test/files/getYamlFilesInDirectory"), expected) }) }) - -suite("Test FileUtil - sortFile()", () => { - const fileutil = new FileUtil() - - test("should throw when file does not contain a valid yaml", () => { - throws(() => fileutil.sortFile("./src/test/suite/util/resources/subfolder/foo2.yaml")) - }) - - test("should not throw when file contains a valid yaml", () => { - doesNotThrow(() => fileutil.sortFile("./src/test/suite/util/resources/subfolder/.customyaml")) - }) - - test("should retain a trailing newline when the original file has one", () => { - const dir = mkdtempSync(join(tmpdir(), "yaml-sort-")) - const file = join(dir, "trailing-newline.yaml") - try { - writeFileSync(file, "zkey: zvalue\nakey: avalue\n") - fileutil.sortFile(file) - const sorted = readFileSync(file, "utf-8") - ok(sorted.endsWith("\n")) - } finally { - rmSync(dir, { recursive: true, force: true }) - } - }) - - test("should not add a trailing newline when the original file has none", () => { - const dir = mkdtempSync(join(tmpdir(), "yaml-sort-")) - const file = join(dir, "no-trailing-newline.yaml") - try { - writeFileSync(file, "zkey: zvalue\nakey: avalue") - fileutil.sortFile(file) - const sorted = readFileSync(file, "utf-8") - ok(!sorted.endsWith("\n")) - } finally { - rmSync(dir, { recursive: true, force: true }) - } - }) -}) diff --git a/src/util/file-util.ts b/src/util/file-util.ts index c6b6858..c3f3f1d 100644 --- a/src/util/file-util.ts +++ b/src/util/file-util.ts @@ -1,42 +1,26 @@ -import { readFileSync, writeFileSync } from "fs" +import { join } from "path" import { sync } from "glob" -import { YamlUtil } from "./yaml-util" import { Settings } from "../settings" export class FileUtil { settings: Settings - encoding: BufferEncoding = "utf-8" - globOptions = {dot: true} constructor(settings = new Settings()) { this.settings = settings } - - getFiles(path: string): string[] { - let files: string[] = [] - for (const extension of this.settings.getExtensions()) { - files = files.concat(sync(`${path}/**/*.${extension}`, this.globOptions)) - } - return files - } - sortFile(file: string) { - const yamlutil = new YamlUtil() - const text = readFileSync(file, this.encoding).toString() - let sortedYaml = yamlutil.sortYaml(text, 0) - - if (sortedYaml) { - sortedYaml = yamlutil.setLeadingDashes(sortedYaml) - // retain a trailing newline if the original file had one - // see https://github.com/pascalre/vscode-yaml-sort/issues/186 - if (text.endsWith("\n") && !sortedYaml.endsWith("\n")) { - sortedYaml += "\n" - } - writeFileSync(file, sortedYaml) - } else { - throw new Error(`File ${file} could not be sorted`) - } + /** + * Finds all files with one of the configured extensions in a directory and its subdirectories. + * @param path Directory to search. + * @returns Paths of the files, starting with the given directory. + */ + getFiles(path: string): string[] { + // the directory is passed as cwd instead of being part of the pattern: glob treats a backslash + // in a pattern as an escape character, so a windows path like C:\project would match nothing + return this.settings.getExtensions().flatMap((extension) => + sync(`**/*.${extension}`, { cwd: path, dot: true }).map((file) => join(path, file)) + ) } } From 0e3e73bb841b1b83a97f74d7213b764bd2dadb25 Mon Sep 17 00:00:00 2001 From: Pascal Reitermann Date: Thu, 19 Nov 2026 20:01:33 +0000 Subject: [PATCH 3/3] fix(formatter): stay silent when formatting through the formatter 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 Claude-Session: https://claude.ai/code/session_01Jdb5is9YsMnteaxHryN26M --- CHANGELOG.md | 1 + src/controller/controller.ts | 5 ++++- .../suite/controller/document-transformer.test.ts | 14 ++++++++++++++ src/util/yaml-util.ts | 2 -- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6a0c49..ac18ac0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased ๐Ÿ› Bugfixes +* (refs [#65](https://github.com/pascalre/vscode-yaml-sort/issues/65)) Formatting via "Format Document" or on save no longer shows "Yaml formatted successfully", once per document. The command `Format YAML` shows it once after the changes were applied * Command `Recursively sort YAML files` now sorts files with several documents, works without an open editor and is only offered for folders in the explorer. A file that cannot be sorted is reported and left unchanged. Files are now also found on Windows paths * Keep every document delimiter (`---`, including a comment behind it) in front of its own document. Before, several documents without a leading `---` got a duplicated `---` (an additional empty document), and with a leading `--- # comment` the comments moved to the next document or got lost * Keep yaml directives like `%YAML 1.2` when sorting or formatting, and accept them when validating. Before, files with directives could not be processed diff --git a/src/controller/controller.ts b/src/controller/controller.ts index c4b68af..2f82e18 100644 --- a/src/controller/controller.ts +++ b/src/controller/controller.ts @@ -40,7 +40,10 @@ export class Controller { if (newText === null) { return [] } - return this.applyEdits(newText) + const edits = this.applyEdits(newText) + // only the command reports success, the formatter stays silent (see #65) + this.vscodeadapter.showMessage(Severity.INFO, "Yaml formatted successfully") + return edits } applyEdits(text: string) { diff --git a/src/test/suite/controller/document-transformer.test.ts b/src/test/suite/controller/document-transformer.test.ts index 8907538..d565ebe 100644 --- a/src/test/suite/controller/document-transformer.test.ts +++ b/src/test/suite/controller/document-transformer.test.ts @@ -1,5 +1,8 @@ import { equal, throws } from "assert" +import { spy } from "sinon" +import { window } from "vscode" + import { YamlOperations, formatDocuments, sortDocuments } from "../../../controller/document-transformer" import { YamlUtil } from "../../../util/yaml-util" @@ -121,4 +124,15 @@ suite("Test DocumentTransformer - formatDocuments()", () => { test("should format every document and keep the delimiters", () => { equal(formatDocuments("---\nkey1: value\n---\nkey2: value"), "---\nkey1: value\n---\nkey2: value") }) + + test("should not show a message, so formatting on save stays silent (#65)", () => { + const info = spy(window, "showInformationMessage") + try { + formatDocuments("---\nkey1: value\n---\nkey2: value") + + equal(info.callCount, 0) + } finally { + info.restore() + } + }) }) diff --git a/src/util/yaml-util.ts b/src/util/yaml-util.ts index 9a92393..9ad0ff1 100644 --- a/src/util/yaml-util.ts +++ b/src/util/yaml-util.ts @@ -1,6 +1,5 @@ import { ErrorUtil } from "./error-util" import { JsYamlAdapter } from "../adapter/js-yaml-adapter" -import { Severity, VsCodeAdapter } from "../adapter/vs-code-adapter" import { ProcessorController } from "../controller/processor-controller" import { prependWhitespacesOnEachLine } from "../lib" import { Settings } from "../settings" @@ -106,7 +105,6 @@ export class YamlUtil { if (useLeadingDashes) { doc = `---\n${doc}` } - new VsCodeAdapter().showMessage(Severity.INFO, "Yaml formatted successfully") return doc } catch (error) { this.errorutil.handleError(error)