From d0009b320c35de56ff09a6228face482d72a7cbc Mon Sep 17 00:00:00 2001 From: Pascal Reitermann Date: Fri, 20 Nov 2026 14:18:10 +0000 Subject: [PATCH 01/18] refactor: split the sources into a core and a vscode layer The sources were grouped by technical role (adapter, controller, util, processor), which hid the actual dependency direction: the yaml logic lived next to the VS Code glue, and helpers named `util` mixed both. Group them by layer instead: * src/core: the yaml sorting and formatting logic (settings, text and document handling, js-yaml adapter, processors) * src/vscode: everything that talks to the VS Code API (commands, formatter, notifications, editor access) * src/extension.ts stays the composition root The tests mirror this: test/unit for the core, test/integration for the tests that need a running VS Code, including the mocha runner. The integration runner still runs every test file, so coverage is unchanged. The launch configuration now points to the runner's index instead of the test folder. This commit only moves files and updates import paths. The core still imports VS Code through the settings and error reporting; the next commits remove these dependencies. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Jdb5ivdJ5L75kyEDAbuvMZ --- .vscode/launch.json | 2 +- src/{controller => core}/document-transformer.ts | 8 ++++---- src/{util => core}/file-util.ts | 2 +- src/{lib.ts => core/indentation.ts} | 0 src/{adapter => core}/js-yaml-adapter.ts | 6 +++--- src/{ => core}/processor/array-processor.ts | 0 src/{ => core}/processor/block-processor.ts | 0 src/{ => core}/processor/comment-processor.ts | 0 src/{ => core}/processor/generic-processor.ts | 0 src/{ => core}/processor/helm-processor.ts | 0 src/{ => core}/processor/octal-processor.ts | 0 .../processor}/processor-controller.ts | 14 +++++++------- src/{ => core}/processor/spacing-processor.ts | 0 .../processor/tabs-to-spaces-processor.ts | 0 src/{ => core}/settings.ts | 0 src/{util => core}/sort-util.ts | 2 +- src/{util => core}/yaml-text.ts | 0 src/{util => core}/yaml-util.ts | 10 +++++----- src/extension.ts | 8 ++++---- .../util/resources => files/file-finder}/bar.yml | 0 .../util/resources => files/file-finder}/foo.yaml | 0 .../resources => files/file-finder}/readFile.txt | 0 .../file-finder}/subfolder/.customyaml | 0 .../file-finder}/subfolder/foo2.yaml | 0 .../file-finder}/subfolder/text.txt | 0 .../controller => integration}/controller.test.ts | 2 +- .../{suite/util => integration}/error-util.test.ts | 2 +- src/test/{suite => integration}/extension.test.ts | 4 ++-- .../formatting-provider.test.ts | 2 +- src/test/{suite => integration}/index.ts | 0 .../vs-code-adapter.test.ts | 2 +- src/test/runTest.ts | 2 +- .../core}/document-transformer.test.ts | 6 +++--- .../controller => unit/core}/file-sorter.test.ts | 6 +++--- .../{suite/util => unit/core}/file-util.test.ts | 14 +++++++------- .../lib.test.ts => unit/core/indentation.test.ts} | 4 ++-- .../adapter => unit/core}/js-yaml-adapter.test.ts | 4 ++-- .../core}/processor/array-processor.test.ts | 2 +- .../core}/processor/block-processor.test.ts | 2 +- .../core}/processor/comment-processor.test.ts | 2 +- .../core}/processor/generic-processor.test.ts | 2 +- .../core}/processor/helm-processor.test.ts | 2 +- .../core}/processor/octal-processor.test.ts | 2 +- .../core/processor}/processor-controller.test.ts | 2 +- .../core}/processor/spacing-processor.test.ts | 2 +- .../processor/tabs-to-spaces-processor.test.ts} | 2 +- src/test/{suite => unit/core}/settings.test.ts | 2 +- .../{suite/util => unit/core}/sort-util.test.ts | 4 ++-- .../{suite/util => unit/core}/yaml-text.test.ts | 2 +- .../{suite/util => unit/core}/yaml-util.test.ts | 4 ++-- src/test/{suite => unit}/package-manifest.test.ts | 0 src/{controller => vscode}/controller.ts | 12 ++++++------ src/{util => vscode}/error-util.ts | 2 +- src/{controller => vscode}/file-sorter.ts | 12 ++++++------ src/{controller => vscode}/formatting-provider.ts | 8 ++++---- src/{adapter => vscode}/vs-code-adapter.ts | 2 +- 56 files changed, 83 insertions(+), 83 deletions(-) rename src/{controller => core}/document-transformer.ts (96%) rename src/{util => core}/file-util.ts (95%) rename src/{lib.ts => core/indentation.ts} (100%) rename src/{adapter => core}/js-yaml-adapter.ts (95%) rename src/{ => core}/processor/array-processor.ts (100%) rename src/{ => core}/processor/block-processor.ts (100%) rename src/{ => core}/processor/comment-processor.ts (100%) rename src/{ => core}/processor/generic-processor.ts (100%) rename src/{ => core}/processor/helm-processor.ts (100%) rename src/{ => core}/processor/octal-processor.ts (100%) rename src/{controller => core/processor}/processor-controller.ts (85%) rename src/{ => core}/processor/spacing-processor.ts (100%) rename src/{ => core}/processor/tabs-to-spaces-processor.ts (100%) rename src/{ => core}/settings.ts (100%) rename src/{util => core}/sort-util.ts (98%) rename src/{util => core}/yaml-text.ts (100%) rename src/{util => core}/yaml-util.ts (93%) rename src/test/{suite/util/resources => files/file-finder}/bar.yml (100%) rename src/test/{suite/util/resources => files/file-finder}/foo.yaml (100%) rename src/test/{suite/util/resources => files/file-finder}/readFile.txt (100%) rename src/test/{suite/util/resources => files/file-finder}/subfolder/.customyaml (100%) rename src/test/{suite/util/resources => files/file-finder}/subfolder/foo2.yaml (100%) rename src/test/{suite/util/resources => files/file-finder}/subfolder/text.txt (100%) rename src/test/{suite/controller => integration}/controller.test.ts (97%) rename src/test/{suite/util => integration}/error-util.test.ts (89%) rename src/test/{suite => integration}/extension.test.ts (97%) rename src/test/{suite/controller => integration}/formatting-provider.test.ts (96%) rename src/test/{suite => integration}/index.ts (100%) rename src/test/{suite/adapter => integration}/vs-code-adapter.test.ts (96%) rename src/test/{suite/controller => unit/core}/document-transformer.test.ts (97%) rename src/test/{suite/controller => unit/core}/file-sorter.test.ts (90%) rename src/test/{suite/util => unit/core}/file-util.test.ts (70%) rename src/test/{suite/lib.test.ts => unit/core/indentation.test.ts} (96%) rename src/test/{suite/adapter => unit/core}/js-yaml-adapter.test.ts (97%) rename src/test/{suite => unit/core}/processor/array-processor.test.ts (92%) rename src/test/{suite => unit/core}/processor/block-processor.test.ts (97%) rename src/test/{suite => unit/core}/processor/comment-processor.test.ts (99%) rename src/test/{suite => unit/core}/processor/generic-processor.test.ts (95%) rename src/test/{suite => unit/core}/processor/helm-processor.test.ts (98%) rename src/test/{suite => unit/core}/processor/octal-processor.test.ts (96%) rename src/test/{suite/controller => unit/core/processor}/processor-controller.test.ts (97%) rename src/test/{suite => unit/core}/processor/spacing-processor.test.ts (97%) rename src/test/{suite/processor/tabs-to-spaces-preprocessor.test.ts => unit/core/processor/tabs-to-spaces-processor.test.ts} (84%) rename src/test/{suite => unit/core}/settings.test.ts (99%) rename src/test/{suite/util => unit/core}/sort-util.test.ts (95%) rename src/test/{suite/util => unit/core}/yaml-text.test.ts (96%) rename src/test/{suite/util => unit/core}/yaml-util.test.ts (99%) rename src/test/{suite => unit}/package-manifest.test.ts (100%) rename src/{controller => vscode}/controller.ts (86%) rename src/{util => vscode}/error-util.ts (75%) rename src/{controller => vscode}/file-sorter.ts (84%) rename src/{controller => vscode}/formatting-provider.ts (86%) rename src/{adapter => vscode}/vs-code-adapter.ts (98%) diff --git a/.vscode/launch.json b/.vscode/launch.json index 9528312..c066745 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -20,7 +20,7 @@ "runtimeExecutable": "${execPath}", "args": [ "--extensionDevelopmentPath=${workspaceFolder}", - "--extensionTestsPath=${workspaceFolder}/out/test" + "--extensionTestsPath=${workspaceFolder}/out/test/integration/index" ], "outFiles": [ "${workspaceFolder}/out/test/**/*.js" diff --git a/src/controller/document-transformer.ts b/src/core/document-transformer.ts similarity index 96% rename from src/controller/document-transformer.ts rename to src/core/document-transformer.ts index bf7e9d2..3258957 100644 --- a/src/controller/document-transformer.ts +++ b/src/core/document-transformer.ts @@ -1,7 +1,7 @@ -import { JsYamlAdapter } from "../adapter/js-yaml-adapter" -import { prependWhitespacesOnEachLine, removeLeadingSpacesOnEachLine } from "../lib" -import { YamlDocument, YamlStream, splitDocuments, validateTextRange } from "../util/yaml-text" -import { YamlUtil } from "../util/yaml-util" +import { prependWhitespacesOnEachLine, removeLeadingSpacesOnEachLine } from "./indentation" +import { JsYamlAdapter } from "./js-yaml-adapter" +import { YamlDocument, YamlStream, splitDocuments, validateTextRange } from "./yaml-text" +import { YamlUtil } from "./yaml-util" /** * The parts of YamlUtil a transformation depends on. Kept narrow so tests can pass a stub. diff --git a/src/util/file-util.ts b/src/core/file-util.ts similarity index 95% rename from src/util/file-util.ts rename to src/core/file-util.ts index c3f3f1d..da16049 100644 --- a/src/util/file-util.ts +++ b/src/core/file-util.ts @@ -2,7 +2,7 @@ import { join } from "path" import { sync } from "glob" -import { Settings } from "../settings" +import { Settings } from "./settings" export class FileUtil { settings: Settings diff --git a/src/lib.ts b/src/core/indentation.ts similarity index 100% rename from src/lib.ts rename to src/core/indentation.ts diff --git a/src/adapter/js-yaml-adapter.ts b/src/core/js-yaml-adapter.ts similarity index 95% rename from src/adapter/js-yaml-adapter.ts rename to src/core/js-yaml-adapter.ts index 7008821..dd332f5 100644 --- a/src/adapter/js-yaml-adapter.ts +++ b/src/core/js-yaml-adapter.ts @@ -1,8 +1,8 @@ import { dump, load } from "js-yaml" -import { Settings } from "../settings" -import { SortUtil } from "../util/sort-util" -import { removeTrailingCharacters, splitDocuments } from "../util/yaml-text" +import { Settings } from "./settings" +import { SortUtil } from "./sort-util" +import { removeTrailingCharacters, splitDocuments } from "./yaml-text" /** * @param value Any loaded yaml value. diff --git a/src/processor/array-processor.ts b/src/core/processor/array-processor.ts similarity index 100% rename from src/processor/array-processor.ts rename to src/core/processor/array-processor.ts diff --git a/src/processor/block-processor.ts b/src/core/processor/block-processor.ts similarity index 100% rename from src/processor/block-processor.ts rename to src/core/processor/block-processor.ts diff --git a/src/processor/comment-processor.ts b/src/core/processor/comment-processor.ts similarity index 100% rename from src/processor/comment-processor.ts rename to src/core/processor/comment-processor.ts diff --git a/src/processor/generic-processor.ts b/src/core/processor/generic-processor.ts similarity index 100% rename from src/processor/generic-processor.ts rename to src/core/processor/generic-processor.ts diff --git a/src/processor/helm-processor.ts b/src/core/processor/helm-processor.ts similarity index 100% rename from src/processor/helm-processor.ts rename to src/core/processor/helm-processor.ts diff --git a/src/processor/octal-processor.ts b/src/core/processor/octal-processor.ts similarity index 100% rename from src/processor/octal-processor.ts rename to src/core/processor/octal-processor.ts diff --git a/src/controller/processor-controller.ts b/src/core/processor/processor-controller.ts similarity index 85% rename from src/controller/processor-controller.ts rename to src/core/processor/processor-controller.ts index b7fcf94..a2a5c86 100644 --- a/src/controller/processor-controller.ts +++ b/src/core/processor/processor-controller.ts @@ -1,10 +1,10 @@ -import { ArrayProcessor } from "../processor/array-processor" -import { BlockProcessor } from "../processor/block-processor" -import { CommentProcessor } from "../processor/comment-processor" -import { HelmProcessor } from "../processor/helm-processor" -import { OctalProcessor } from "../processor/octal-processor" -import { SpacingProcessor } from "../processor/spacing-processor" -import { TabsToSpacesProcessor } from "../processor/tabs-to-spaces-processor" +import { ArrayProcessor } from "./array-processor" +import { BlockProcessor } from "./block-processor" +import { CommentProcessor } from "./comment-processor" +import { HelmProcessor } from "./helm-processor" +import { OctalProcessor } from "./octal-processor" +import { SpacingProcessor } from "./spacing-processor" +import { TabsToSpacesProcessor } from "./tabs-to-spaces-processor" import { Settings } from "../settings" export class ProcessorController { diff --git a/src/processor/spacing-processor.ts b/src/core/processor/spacing-processor.ts similarity index 100% rename from src/processor/spacing-processor.ts rename to src/core/processor/spacing-processor.ts diff --git a/src/processor/tabs-to-spaces-processor.ts b/src/core/processor/tabs-to-spaces-processor.ts similarity index 100% rename from src/processor/tabs-to-spaces-processor.ts rename to src/core/processor/tabs-to-spaces-processor.ts diff --git a/src/settings.ts b/src/core/settings.ts similarity index 100% rename from src/settings.ts rename to src/core/settings.ts diff --git a/src/util/sort-util.ts b/src/core/sort-util.ts similarity index 98% rename from src/util/sort-util.ts rename to src/core/sort-util.ts index a061447..852221e 100644 --- a/src/util/sort-util.ts +++ b/src/core/sort-util.ts @@ -1,4 +1,4 @@ -import { Settings } from "../settings" +import { Settings } from "./settings" export class SortUtil { settings: Settings diff --git a/src/util/yaml-text.ts b/src/core/yaml-text.ts similarity index 100% rename from src/util/yaml-text.ts rename to src/core/yaml-text.ts diff --git a/src/util/yaml-util.ts b/src/core/yaml-util.ts similarity index 93% rename from src/util/yaml-util.ts rename to src/core/yaml-util.ts index ed50e86..8b639cd 100644 --- a/src/util/yaml-util.ts +++ b/src/core/yaml-util.ts @@ -1,8 +1,8 @@ -import { ErrorUtil } from "./error-util" -import { JsYamlAdapter } from "../adapter/js-yaml-adapter" -import { ProcessorController } from "../controller/processor-controller" -import { prependWhitespacesOnEachLine } from "../lib" -import { Settings } from "../settings" +import { prependWhitespacesOnEachLine } from "./indentation" +import { JsYamlAdapter } from "./js-yaml-adapter" +import { ErrorUtil } from "../vscode/error-util" +import { ProcessorController } from "./processor/processor-controller" +import { Settings } from "./settings" import { sortArraysRecursively } from "./sort-util" import { removeTrailingCharacters } from "./yaml-text" diff --git a/src/extension.ts b/src/extension.ts index 7814445..3958bf0 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,9 +1,9 @@ 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" +import { Controller } from "./vscode/controller" +import { sortYamlFilesInDirectory } from "./vscode/file-sorter" +import { YamlFormattingProvider } from "./vscode/formatting-provider" +import { VsCodeAdapter } from "./vscode/vs-code-adapter" export function activate(context: ExtensionContext) { context.subscriptions.push( diff --git a/src/test/suite/util/resources/bar.yml b/src/test/files/file-finder/bar.yml similarity index 100% rename from src/test/suite/util/resources/bar.yml rename to src/test/files/file-finder/bar.yml diff --git a/src/test/suite/util/resources/foo.yaml b/src/test/files/file-finder/foo.yaml similarity index 100% rename from src/test/suite/util/resources/foo.yaml rename to src/test/files/file-finder/foo.yaml diff --git a/src/test/suite/util/resources/readFile.txt b/src/test/files/file-finder/readFile.txt similarity index 100% rename from src/test/suite/util/resources/readFile.txt rename to src/test/files/file-finder/readFile.txt diff --git a/src/test/suite/util/resources/subfolder/.customyaml b/src/test/files/file-finder/subfolder/.customyaml similarity index 100% rename from src/test/suite/util/resources/subfolder/.customyaml rename to src/test/files/file-finder/subfolder/.customyaml diff --git a/src/test/suite/util/resources/subfolder/foo2.yaml b/src/test/files/file-finder/subfolder/foo2.yaml similarity index 100% rename from src/test/suite/util/resources/subfolder/foo2.yaml rename to src/test/files/file-finder/subfolder/foo2.yaml diff --git a/src/test/suite/util/resources/subfolder/text.txt b/src/test/files/file-finder/subfolder/text.txt similarity index 100% rename from src/test/suite/util/resources/subfolder/text.txt rename to src/test/files/file-finder/subfolder/text.txt diff --git a/src/test/suite/controller/controller.test.ts b/src/test/integration/controller.test.ts similarity index 97% rename from src/test/suite/controller/controller.test.ts rename to src/test/integration/controller.test.ts index dcc5ed2..04d10ae 100644 --- a/src/test/suite/controller/controller.test.ts +++ b/src/test/integration/controller.test.ts @@ -4,7 +4,7 @@ import { resolve } from "path" import { Uri, commands, window, workspace } from "vscode" -import { Controller } from "../../../controller/controller" +import { Controller } from "../../vscode/controller" suite("Test sortYamlFiles", () => { diff --git a/src/test/suite/util/error-util.test.ts b/src/test/integration/error-util.test.ts similarity index 89% rename from src/test/suite/util/error-util.test.ts rename to src/test/integration/error-util.test.ts index 3be2d68..d68200a 100644 --- a/src/test/suite/util/error-util.test.ts +++ b/src/test/integration/error-util.test.ts @@ -2,7 +2,7 @@ import { equal } from "assert" import { spy } from "sinon" -import { ErrorUtil } from "../../../util/error-util" +import { ErrorUtil } from "../../vscode/error-util" suite("Test ErrorUtil - handleError()", () => { test("when input is of type error, should execute showMessage function", () => { diff --git a/src/test/suite/extension.test.ts b/src/test/integration/extension.test.ts similarity index 97% rename from src/test/suite/extension.test.ts rename to src/test/integration/extension.test.ts index 8a4e757..032c107 100644 --- a/src/test/suite/extension.test.ts +++ b/src/test/integration/extension.test.ts @@ -3,8 +3,8 @@ import { resolve } from "path" import { Position, Range, Selection, Uri, commands, window, workspace } from "vscode" -import { Controller } from "../../controller/controller" -import { Settings } from "../../settings" +import { Settings } from "../../core/settings" +import { Controller } from "../../vscode/controller" suite("Test getCustomSortKeywords", () => { test("should return values of `vscode-yaml-sort.customSortKeywords_1`", () => { diff --git a/src/test/suite/controller/formatting-provider.test.ts b/src/test/integration/formatting-provider.test.ts similarity index 96% rename from src/test/suite/controller/formatting-provider.test.ts rename to src/test/integration/formatting-provider.test.ts index 2c877ca..14d7bfe 100644 --- a/src/test/suite/controller/formatting-provider.test.ts +++ b/src/test/integration/formatting-provider.test.ts @@ -2,7 +2,7 @@ import { deepEqual, equal } from "assert" import { window, workspace } from "vscode" -import { YamlFormattingProvider } from "../../../controller/formatting-provider" +import { YamlFormattingProvider } from "../../vscode/formatting-provider" suite("Test YamlFormattingProvider - provideDocumentFormattingEdits()", () => { const provider = new YamlFormattingProvider() diff --git a/src/test/suite/index.ts b/src/test/integration/index.ts similarity index 100% rename from src/test/suite/index.ts rename to src/test/integration/index.ts diff --git a/src/test/suite/adapter/vs-code-adapter.test.ts b/src/test/integration/vs-code-adapter.test.ts similarity index 96% rename from src/test/suite/adapter/vs-code-adapter.test.ts rename to src/test/integration/vs-code-adapter.test.ts index 59cb4c3..63709c7 100644 --- a/src/test/suite/adapter/vs-code-adapter.test.ts +++ b/src/test/integration/vs-code-adapter.test.ts @@ -4,7 +4,7 @@ import { resolve } from "path" import { spy } from "sinon" import { Uri, window, workspace } from "vscode" -import { Severity, VsCodeAdapter } from "../../../adapter/vs-code-adapter" +import { Severity, VsCodeAdapter } from "../../vscode/vs-code-adapter" suite("Test VsCodeAdapter - getActiveDocument()", () => { test("when text editor is active should open document", async () => { diff --git a/src/test/runTest.ts b/src/test/runTest.ts index 59d459d..c080c4c 100644 --- a/src/test/runTest.ts +++ b/src/test/runTest.ts @@ -10,7 +10,7 @@ async function main() { // The path to test runner // Passed to --extensionTestsPath - const extensionTestsPath = resolve(__dirname, "./suite/index") + const extensionTestsPath = resolve(__dirname, "./integration/index") // Download VS Code, unzip it and run the integration test await runTests({ extensionDevelopmentPath, extensionTestsPath }) diff --git a/src/test/suite/controller/document-transformer.test.ts b/src/test/unit/core/document-transformer.test.ts similarity index 97% rename from src/test/suite/controller/document-transformer.test.ts rename to src/test/unit/core/document-transformer.test.ts index 6e23184..1844546 100644 --- a/src/test/suite/controller/document-transformer.test.ts +++ b/src/test/unit/core/document-transformer.test.ts @@ -3,9 +3,9 @@ import { equal, throws } from "assert" import { spy } from "sinon" import { window } from "vscode" -import { YamlOperations, formatDocuments, sortDocuments } from "../../../controller/document-transformer" -import { Settings } from "../../../settings" -import { YamlUtil } from "../../../util/yaml-util" +import { YamlOperations, formatDocuments, sortDocuments } from "../../../core/document-transformer" +import { Settings } from "../../../core/settings" +import { YamlUtil } from "../../../core/yaml-util" /** * Real yaml operations, except that sorting fails for every document containing the marker. diff --git a/src/test/suite/controller/file-sorter.test.ts b/src/test/unit/core/file-sorter.test.ts similarity index 90% rename from src/test/suite/controller/file-sorter.test.ts rename to src/test/unit/core/file-sorter.test.ts index 6c0f24d..86e6176 100644 --- a/src/test/suite/controller/file-sorter.test.ts +++ b/src/test/unit/core/file-sorter.test.ts @@ -3,16 +3,16 @@ import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "fs" import { tmpdir } from "os" import { join } from "path" -import { sortFile } from "../../../controller/file-sorter" +import { sortFile } from "../../../vscode/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")) + throws(() => sortFile("./src/test/files/file-finder/subfolder/foo2.yaml")) }) test("should not throw when file contains a valid yaml", () => { - doesNotThrow(() => sortFile("./src/test/suite/util/resources/subfolder/.customyaml")) + doesNotThrow(() => sortFile("./src/test/files/file-finder/subfolder/.customyaml")) }) test("should retain a trailing newline when the original file has one", () => { diff --git a/src/test/suite/util/file-util.test.ts b/src/test/unit/core/file-util.test.ts similarity index 70% rename from src/test/suite/util/file-util.test.ts rename to src/test/unit/core/file-util.test.ts index 2d3e4d9..7844cb9 100644 --- a/src/test/suite/util/file-util.test.ts +++ b/src/test/unit/core/file-util.test.ts @@ -1,7 +1,7 @@ import { deepEqual } from "assert" -import { Settings } from "../../../settings" -import { FileUtil } from "../../../util/file-util" +import { FileUtil } from "../../../core/file-util" +import { Settings } from "../../../core/settings" suite("Test FileUtil - getFiles()", () => { const settings = new Settings() @@ -12,12 +12,12 @@ suite("Test FileUtil - getFiles()", () => { test("should return all files", () => { const expected = [ - "src/test/suite/util/resources/foo.yaml", - "src/test/suite/util/resources/subfolder/foo2.yaml", - "src/test/suite/util/resources/bar.yml", - "src/test/suite/util/resources/subfolder/.customyaml" + "src/test/files/file-finder/foo.yaml", + "src/test/files/file-finder/subfolder/foo2.yaml", + "src/test/files/file-finder/bar.yml", + "src/test/files/file-finder/subfolder/.customyaml" ] - deepEqual(fileutil.getFiles("./src/test/suite/util/resources"), expected) + deepEqual(fileutil.getFiles("./src/test/files/file-finder"), expected) }) test("should list all files with extension *.yaml or *.yml in a directory and all its subdirectories", () => { diff --git a/src/test/suite/lib.test.ts b/src/test/unit/core/indentation.test.ts similarity index 96% rename from src/test/suite/lib.test.ts rename to src/test/unit/core/indentation.test.ts index 6d7e3d3..10fe9e2 100644 --- a/src/test/suite/lib.test.ts +++ b/src/test/unit/core/indentation.test.ts @@ -3,8 +3,8 @@ import { equal, throws} from "assert" import { prependWhitespacesOnEachLine, removeLeadingSpacesOnEachLine -} from "../../lib" -import { removeTrailingCharacters } from "../../util/yaml-text" +} from "../../../core/indentation" +import { removeTrailingCharacters } from "../../../core/yaml-text" suite("Test removeTrailingCharacters", () => { const actual = "text" diff --git a/src/test/suite/adapter/js-yaml-adapter.test.ts b/src/test/unit/core/js-yaml-adapter.test.ts similarity index 97% rename from src/test/suite/adapter/js-yaml-adapter.test.ts rename to src/test/unit/core/js-yaml-adapter.test.ts index f0678ec..47d3c23 100644 --- a/src/test/suite/adapter/js-yaml-adapter.test.ts +++ b/src/test/unit/core/js-yaml-adapter.test.ts @@ -4,8 +4,8 @@ import { CLOUDFORMATION_SCHEMA } from "cloudformation-js-yaml-schema" import { HOMEASSISTANT_SCHEMA } from "homeassistant-js-yaml-schema" import { CORE_SCHEMA, YAMLException } from "js-yaml" -import { JsYamlAdapter } from "../../../adapter/js-yaml-adapter" -import { Settings } from "../../../settings" +import { JsYamlAdapter } from "../../../core/js-yaml-adapter" +import { Settings } from "../../../core/settings" suite("Test JsYamlAdapter - validateYaml()", () => { const jsyamladapter = new JsYamlAdapter(new Settings()) diff --git a/src/test/suite/processor/array-processor.test.ts b/src/test/unit/core/processor/array-processor.test.ts similarity index 92% rename from src/test/suite/processor/array-processor.test.ts rename to src/test/unit/core/processor/array-processor.test.ts index 1cb270a..f7d2815 100644 --- a/src/test/suite/processor/array-processor.test.ts +++ b/src/test/unit/core/processor/array-processor.test.ts @@ -1,6 +1,6 @@ import { equal } from "assert" -import { ArrayProcessor } from "../../../processor/array-processor" +import { ArrayProcessor } from "../../../../core/processor/array-processor" suite("Test ArrayProcessor - preprocess()", () => { test("should find all array values", () => { diff --git a/src/test/suite/processor/block-processor.test.ts b/src/test/unit/core/processor/block-processor.test.ts similarity index 97% rename from src/test/suite/processor/block-processor.test.ts rename to src/test/unit/core/processor/block-processor.test.ts index b2bfe4d..21e1742 100644 --- a/src/test/suite/processor/block-processor.test.ts +++ b/src/test/unit/core/processor/block-processor.test.ts @@ -1,6 +1,6 @@ import { equal } from "assert" -import { BlockProcessor } from "../../../processor/block-processor" +import { BlockProcessor } from "../../../../core/processor/block-processor" suite("Test BlockProcessor - preprocess()", () => { test("input string with block sequence > should be the same after pre and postprocessing", () => { diff --git a/src/test/suite/processor/comment-processor.test.ts b/src/test/unit/core/processor/comment-processor.test.ts similarity index 99% rename from src/test/suite/processor/comment-processor.test.ts rename to src/test/unit/core/processor/comment-processor.test.ts index a049459..4962470 100644 --- a/src/test/suite/processor/comment-processor.test.ts +++ b/src/test/unit/core/processor/comment-processor.test.ts @@ -1,6 +1,6 @@ import { deepEqual, equal } from "assert" -import { CommentProcessor } from "../../../processor/comment-processor" +import { CommentProcessor } from "../../../../core/processor/comment-processor" suite("Test CommentProcessor - findComments()", () => { test("should add all comments to array 'comments'", () => { diff --git a/src/test/suite/processor/generic-processor.test.ts b/src/test/unit/core/processor/generic-processor.test.ts similarity index 95% rename from src/test/suite/processor/generic-processor.test.ts rename to src/test/unit/core/processor/generic-processor.test.ts index 19e2d54..ed26dd6 100644 --- a/src/test/suite/processor/generic-processor.test.ts +++ b/src/test/unit/core/processor/generic-processor.test.ts @@ -1,6 +1,6 @@ import { equal } from "assert" -import { HelmProcessor } from "../../../processor/helm-processor" +import { HelmProcessor } from "../../../../core/processor/helm-processor" suite("Test GenericProcessor - replaceSubstituteWithValue()", () => { test("GitHub issue #154: Ensure a substitue is surrounded with the same quotation marks", () => { diff --git a/src/test/suite/processor/helm-processor.test.ts b/src/test/unit/core/processor/helm-processor.test.ts similarity index 98% rename from src/test/suite/processor/helm-processor.test.ts rename to src/test/unit/core/processor/helm-processor.test.ts index 1989ab5..7dbb3fb 100644 --- a/src/test/suite/processor/helm-processor.test.ts +++ b/src/test/unit/core/processor/helm-processor.test.ts @@ -1,6 +1,6 @@ import { equal, match } from "assert" -import { HelmProcessor } from "../../../processor/helm-processor" +import { HelmProcessor } from "../../../../core/processor/helm-processor" suite("Test HelmProcessor - preprocess()", () => { test("should replace all helm values", () => { diff --git a/src/test/suite/processor/octal-processor.test.ts b/src/test/unit/core/processor/octal-processor.test.ts similarity index 96% rename from src/test/suite/processor/octal-processor.test.ts rename to src/test/unit/core/processor/octal-processor.test.ts index 2b33b6f..3eda179 100644 --- a/src/test/suite/processor/octal-processor.test.ts +++ b/src/test/unit/core/processor/octal-processor.test.ts @@ -1,6 +1,6 @@ import { equal } from "assert" -import { OctalProcessor } from "../../../processor/octal-processor" +import { OctalProcessor } from "../../../../core/processor/octal-processor" suite("Test OctalProcessor - preprocess()", () => { test("should find one octal values", () => { diff --git a/src/test/suite/controller/processor-controller.test.ts b/src/test/unit/core/processor/processor-controller.test.ts similarity index 97% rename from src/test/suite/controller/processor-controller.test.ts rename to src/test/unit/core/processor/processor-controller.test.ts index d664e6d..c33c26f 100644 --- a/src/test/suite/controller/processor-controller.test.ts +++ b/src/test/unit/core/processor/processor-controller.test.ts @@ -1,6 +1,6 @@ import { equal, notEqual } from "assert" -import { ProcessorController } from "../../../controller/processor-controller" +import { ProcessorController } from "../../../../core/processor/processor-controller" suite("Test ProcessorController - preprocess() & postprocess()", () => { test("text should be the same after pre- and postprocessing", () => { diff --git a/src/test/suite/processor/spacing-processor.test.ts b/src/test/unit/core/processor/spacing-processor.test.ts similarity index 97% rename from src/test/suite/processor/spacing-processor.test.ts rename to src/test/unit/core/processor/spacing-processor.test.ts index 161f9c4..2819ecf 100644 --- a/src/test/suite/processor/spacing-processor.test.ts +++ b/src/test/unit/core/processor/spacing-processor.test.ts @@ -1,6 +1,6 @@ import { deepEqual, equal } from "assert" -import { SpacingProcessor } from "../../../processor/spacing-processor" +import { SpacingProcessor } from "../../../../core/processor/spacing-processor" suite("Test SpacingProcessor - getMatcher()", () => { test("should return regex with correct number of spaces", () => { diff --git a/src/test/suite/processor/tabs-to-spaces-preprocessor.test.ts b/src/test/unit/core/processor/tabs-to-spaces-processor.test.ts similarity index 84% rename from src/test/suite/processor/tabs-to-spaces-preprocessor.test.ts rename to src/test/unit/core/processor/tabs-to-spaces-processor.test.ts index 56f5797..7dd6c99 100644 --- a/src/test/suite/processor/tabs-to-spaces-preprocessor.test.ts +++ b/src/test/unit/core/processor/tabs-to-spaces-processor.test.ts @@ -1,6 +1,6 @@ import { equal } from "assert" -import { TabsToSpacesProcessor } from "../../../processor/tabs-to-spaces-processor" +import { TabsToSpacesProcessor } from "../../../../core/processor/tabs-to-spaces-processor" suite("Test TabsToSpacesPreprocessor - preprocess()", () => { const text = diff --git a/src/test/suite/settings.test.ts b/src/test/unit/core/settings.test.ts similarity index 99% rename from src/test/suite/settings.test.ts rename to src/test/unit/core/settings.test.ts index 3c2f393..fb75d01 100644 --- a/src/test/suite/settings.test.ts +++ b/src/test/unit/core/settings.test.ts @@ -5,7 +5,7 @@ import { HOMEASSISTANT_SCHEMA } from "homeassistant-js-yaml-schema" import { CORE_SCHEMA, DEFAULT_SCHEMA, FAILSAFE_SCHEMA, JSON_SCHEMA, Schema} from "js-yaml" import { Uri, workspace } from "vscode" -import { Settings } from "../../settings" +import { Settings } from "../../../core/settings" suite("Test Settings - getter", () => { const settings: Settings = new Settings() diff --git a/src/test/suite/util/sort-util.test.ts b/src/test/unit/core/sort-util.test.ts similarity index 95% rename from src/test/suite/util/sort-util.test.ts rename to src/test/unit/core/sort-util.test.ts index bc36801..bdb140b 100644 --- a/src/test/suite/util/sort-util.test.ts +++ b/src/test/unit/core/sort-util.test.ts @@ -1,7 +1,7 @@ import { deepEqual, equal } from "assert" -import { Settings } from "../../../settings" -import { SortUtil, sortArraysRecursively } from "../../../util/sort-util" +import { Settings } from "../../../core/settings" +import { SortUtil, sortArraysRecursively } from "../../../core/sort-util" suite("Test Sort - localeSort()", () => { test("when `locale` is `sv` and a is `รค` and b is `z` should return 1", () => { diff --git a/src/test/suite/util/yaml-text.test.ts b/src/test/unit/core/yaml-text.test.ts similarity index 96% rename from src/test/suite/util/yaml-text.test.ts rename to src/test/unit/core/yaml-text.test.ts index 1ac5339..b31dc87 100644 --- a/src/test/suite/util/yaml-text.test.ts +++ b/src/test/unit/core/yaml-text.test.ts @@ -1,6 +1,6 @@ import { deepEqual } from "assert" -import { splitDocuments } from "../../../util/yaml-text" +import { splitDocuments } from "../../../core/yaml-text" suite("Test splitDocuments()", () => { test("should return a single document without delimiter when the text contains no `---`", () => { diff --git a/src/test/suite/util/yaml-util.test.ts b/src/test/unit/core/yaml-util.test.ts similarity index 99% rename from src/test/suite/util/yaml-util.test.ts rename to src/test/unit/core/yaml-util.test.ts index 6e3530f..10c4c59 100644 --- a/src/test/suite/util/yaml-util.test.ts +++ b/src/test/unit/core/yaml-util.test.ts @@ -3,8 +3,8 @@ import { equal } from "assert" import { CLOUDFORMATION_SCHEMA } from "cloudformation-js-yaml-schema" import { CORE_SCHEMA } from "js-yaml" -import { Settings } from "../../../settings" -import { YamlUtil } from "../../../util/yaml-util" +import { Settings } from "../../../core/settings" +import { YamlUtil } from "../../../core/yaml-util" suite("Test sortArrays", () => { const yamlutil = new YamlUtil() diff --git a/src/test/suite/package-manifest.test.ts b/src/test/unit/package-manifest.test.ts similarity index 100% rename from src/test/suite/package-manifest.test.ts rename to src/test/unit/package-manifest.test.ts diff --git a/src/controller/controller.ts b/src/vscode/controller.ts similarity index 86% rename from src/controller/controller.ts rename to src/vscode/controller.ts index bae3ba5..ffafe0c 100644 --- a/src/controller/controller.ts +++ b/src/vscode/controller.ts @@ -1,11 +1,11 @@ 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 { YamlUtil } from "../util/yaml-util" +import { ErrorUtil } from "./error-util" +import { Severity, VsCodeAdapter } from "./vs-code-adapter" +import { formatDocuments, sortDocuments } from "../core/document-transformer" +import { JsYamlAdapter } from "../core/js-yaml-adapter" +import { Settings } from "../core/settings" +import { YamlUtil } from "../core/yaml-util" export class Controller { editor: TextEditor diff --git a/src/util/error-util.ts b/src/vscode/error-util.ts similarity index 75% rename from src/util/error-util.ts rename to src/vscode/error-util.ts index e1767b3..cc15435 100644 --- a/src/util/error-util.ts +++ b/src/vscode/error-util.ts @@ -1,4 +1,4 @@ -import { Severity, VsCodeAdapter } from "../adapter/vs-code-adapter" +import { Severity, VsCodeAdapter } from "./vs-code-adapter" export class ErrorUtil { vscodeadapter = new VsCodeAdapter() diff --git a/src/controller/file-sorter.ts b/src/vscode/file-sorter.ts similarity index 84% rename from src/controller/file-sorter.ts rename to src/vscode/file-sorter.ts index 406a24b..5eefd21 100644 --- a/src/controller/file-sorter.ts +++ b/src/vscode/file-sorter.ts @@ -2,12 +2,12 @@ import { readFileSync, writeFileSync } from "fs" import { Uri } from "vscode" -import { YamlOperations, sortDocuments } from "./document-transformer" -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" +import { ErrorUtil } from "./error-util" +import { Severity, VsCodeAdapter } from "./vs-code-adapter" +import { YamlOperations, sortDocuments } from "../core/document-transformer" +import { FileUtil } from "../core/file-util" +import { Settings } from "../core/settings" +import { YamlUtil } from "../core/yaml-util" /** * Sorts all yaml documents of a file and writes the result back. diff --git a/src/controller/formatting-provider.ts b/src/vscode/formatting-provider.ts similarity index 86% rename from src/controller/formatting-provider.ts rename to src/vscode/formatting-provider.ts index 757f470..8689214 100644 --- a/src/controller/formatting-provider.ts +++ b/src/vscode/formatting-provider.ts @@ -1,9 +1,9 @@ import { DocumentFormattingEditProvider, Range, TextDocument, TextEdit } from "vscode" -import { formatDocuments, sortDocuments } from "./document-transformer" -import { Settings } from "../settings" -import { ErrorUtil } from "../util/error-util" -import { YamlUtil } from "../util/yaml-util" +import { ErrorUtil } from "./error-util" +import { formatDocuments, sortDocuments } from "../core/document-transformer" +import { Settings } from "../core/settings" +import { YamlUtil } from "../core/yaml-util" /** * Formats (or, depending on `sortOnSave`, sorts) a whole yaml document. diff --git a/src/adapter/vs-code-adapter.ts b/src/vscode/vs-code-adapter.ts similarity index 98% rename from src/adapter/vs-code-adapter.ts rename to src/vscode/vs-code-adapter.ts index d3fea80..957cd5d 100644 --- a/src/adapter/vs-code-adapter.ts +++ b/src/vscode/vs-code-adapter.ts @@ -1,6 +1,6 @@ import { Disposable, DocumentFormattingEditProvider, Position, Range, Selection, TextEdit, TextEditor, WorkspaceEdit, languages, window, workspace } from "vscode" -import { Settings } from "../settings" +import { Settings } from "../core/settings" export enum Severity { INFO, ERROR From eec5872d387315e2ef0933119c5254fc9354ace2 Mon Sep 17 00:00:00 2001 From: Pascal Reitermann Date: Fri, 20 Nov 2026 14:19:28 +0000 Subject: [PATCH 02/18] refactor(settings): read settings without the VS Code API Settings imported `workspace` to read the configuration, which tied every class of the core to a running VS Code: `new Settings()` as a default parameter read the user's configuration, so even pure text transformations could only be tested inside VS Code. Settings now reads any ConfigurationReader (VS Code's WorkspaceConfiguration satisfies the interface) and falls back to DEFAULT_CONFIGURATION for missing values, so `new Settings()` yields the defaults. Lists are copied, so changing one snapshot no longer changes another one. Reading the configuration of a document moves to readSettings() in the vscode layer, replacing Settings.forDocument(). A test compares DEFAULT_CONFIGURATION with the defaults declared in package.json, so both cannot drift apart. The pure getCustomSortKeywords tests move from the integration tests to the settings unit tests. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Jdb5ivdJ5L75kyEDAbuvMZ --- src/core/settings.ts | 122 +++++++++++++-------- src/test/integration/configuration.test.ts | 24 ++++ src/test/integration/extension.test.ts | 22 ---- src/test/unit/core/settings.test.ts | 28 +++-- src/test/unit/package-manifest.test.ts | 20 +++- src/vscode/configuration.ts | 17 +++ src/vscode/controller.ts | 3 +- src/vscode/file-sorter.ts | 4 +- src/vscode/formatting-provider.ts | 4 +- 9 files changed, 160 insertions(+), 84 deletions(-) create mode 100644 src/test/integration/configuration.test.ts create mode 100644 src/vscode/configuration.ts diff --git a/src/core/settings.ts b/src/core/settings.ts index 18dfbea..64270b4 100644 --- a/src/core/settings.ts +++ b/src/core/settings.ts @@ -1,13 +1,56 @@ import { CLOUDFORMATION_SCHEMA } from "cloudformation-js-yaml-schema" import { HOMEASSISTANT_SCHEMA } from "homeassistant-js-yaml-schema" import { CORE_SCHEMA, DEFAULT_SCHEMA, FAILSAFE_SCHEMA, JSON_SCHEMA, Schema } from "js-yaml" -import { ConfigurationScope, WorkspaceConfiguration, workspace } from "vscode" -type QuotingType = "'" | "\"" +export type QuotingType = "'" | "\"" // js-yaml only supports these styles for the !!null tag const NULL_STYLES = ["lowercase", "uppercase", "camelcase", "canonical", "empty"] +/** + * The values used when the configuration has none. They must match the defaults declared in + * package.json, which a unit test verifies. + */ +export const DEFAULT_CONFIGURATION = { + customSortKeywords_1: ["apiVersion", "kind", "metadata", "spec", "data"], + customSortKeywords_2: [] as string[], + customSortKeywords_3: [] as string[], + emptyLinesUntilLevel: 0, + extensions: ["yaml", "yml"], + forceQuotes: false, + indent: 2, + lineWidth: 500, + locale: "en", + noArrayIndent: false, + noCompatMode: false, + notifySuccess: true, + nullStyle: "lowercase", + quotingType: "'" as QuotingType, + schema: "DEFAULT_SCHEMA", + sortArrays: false, + sortOnSave: 0, + sortOrderReverse: false, + useArrayProcessor: true, + useBlockProcessor: true, + useCommentProcessor: true, + useCustomSortRecursively: false, + useHelmProcessor: true, + useLeadingDashes: true, + useOctalProcessor: true, +} + +export type ConfigurationKey = keyof typeof DEFAULT_CONFIGURATION + +/** + * Read access to the extension's configuration section. VS Code's `WorkspaceConfiguration` + * satisfies it, so the core does not depend on the VS Code API. + */ +export interface ConfigurationReader { + get(key: string): T | undefined +} + +const NO_CONFIGURATION: ConfigurationReader = { get: () => undefined } + /** * A snapshot of the extension's settings. * @@ -44,49 +87,40 @@ export class Settings { useOctalProcessor: boolean /** - * @param configuration The extension's configuration section. Defaults to the global and workspace settings. + * @param configuration The extension's configuration section. Missing values fall back to + * DEFAULT_CONFIGURATION, so `new Settings()` yields the defaults. */ - constructor(configuration: WorkspaceConfiguration = workspace.getConfiguration(Settings.SECTION)) { - const read = (name: string) => configuration.get(name) as T - this.customSortKeywords1 = read("customSortKeywords_1") - this.customSortKeywords2 = read("customSortKeywords_2") - this.customSortKeywords3 = read("customSortKeywords_3") - this.emptyLinesUntilLevel = read("emptyLinesUntilLevel") - this.extensions = read("extensions") - this.forceQuotes = read("forceQuotes") - this.indent = read("indent") - this.lineWidth = read("lineWidth") - this.locale = read("locale") - this.nullStyle = read("nullStyle") - this.noArrayIndent = read("noArrayIndent") - this.noCompatMode = read("noCompatMode") - this.notifySuccess = read("notifySuccess") - this.quotingType = read("quotingType") - this.schema = Settings.getJsYamlSchemaFromString(read("schema")) - this.sortArrays = read("sortArrays") - this.sortOnSave = read("sortOnSave") - this.sortOrderReverse = read("sortOrderReverse") - this.useCustomSortRecursively = read("useCustomSortRecursively") - this.useLeadingDashes = read("useLeadingDashes") - this.useArrayProcessor = read("useArrayProcessor") - this.useBlockProcessor = read("useBlockProcessor") - this.useCommentProcessor = read("useCommentProcessor") - this.useHelmProcessor = read("useHelmProcessor") - this.useOctalProcessor = read("useOctalProcessor") - } - - /** - * Reads the settings that apply to a document: workspace folder settings in a multi-root - * workspace and language specific settings like `"[yaml]": { "vscode-yaml-sort.indent": 4 }`. - * @param document The document, or its uri and language id. - * @param getConfiguration Injectable for tests. - * @returns The settings for the document. - */ - static forDocument( - document: ConfigurationScope, - getConfiguration: (section?: string, scope?: ConfigurationScope) => WorkspaceConfiguration = workspace.getConfiguration - ): Settings { - return new Settings(getConfiguration(Settings.SECTION, document)) + constructor(configuration: ConfigurationReader = NO_CONFIGURATION) { + const read = (key: K): (typeof DEFAULT_CONFIGURATION)[K] => { + const value = configuration.get<(typeof DEFAULT_CONFIGURATION)[K]>(key) ?? DEFAULT_CONFIGURATION[key] + // copy lists, so changing a snapshot never changes the defaults or another snapshot + return (Array.isArray(value) ? [...value] : value) as (typeof DEFAULT_CONFIGURATION)[K] + } + this.customSortKeywords1 = read("customSortKeywords_1") + this.customSortKeywords2 = read("customSortKeywords_2") + this.customSortKeywords3 = read("customSortKeywords_3") + this.emptyLinesUntilLevel = read("emptyLinesUntilLevel") + this.extensions = read("extensions") + this.forceQuotes = read("forceQuotes") + this.indent = read("indent") + this.lineWidth = read("lineWidth") + this.locale = read("locale") + this.nullStyle = read("nullStyle") + this.noArrayIndent = read("noArrayIndent") + this.noCompatMode = read("noCompatMode") + this.notifySuccess = read("notifySuccess") + this.quotingType = read("quotingType") + this.schema = Settings.getJsYamlSchemaFromString(read("schema")) + this.sortArrays = read("sortArrays") + this.sortOnSave = read("sortOnSave") + this.sortOrderReverse = read("sortOrderReverse") + this.useCustomSortRecursively = read("useCustomSortRecursively") + this.useLeadingDashes = read("useLeadingDashes") + this.useArrayProcessor = read("useArrayProcessor") + this.useBlockProcessor = read("useBlockProcessor") + this.useCommentProcessor = read("useCommentProcessor") + this.useHelmProcessor = read("useHelmProcessor") + this.useOctalProcessor = read("useOctalProcessor") } /** diff --git a/src/test/integration/configuration.test.ts b/src/test/integration/configuration.test.ts new file mode 100644 index 0000000..39ae411 --- /dev/null +++ b/src/test/integration/configuration.test.ts @@ -0,0 +1,24 @@ +import { deepEqual, equal } from "assert" + +import { Uri, workspace } from "vscode" + +import { readSettings } from "../../vscode/configuration" + +suite("Test readSettings()", () => { + test("should read the configuration for the given document", () => { + const document = { uri: Uri.file("/project/a.yaml"), languageId: "yaml" } + const calls: unknown[][] = [] + const getConfiguration = (section?: string, scope?: unknown) => { + calls.push([section, scope]) + return workspace.getConfiguration(section) + } + + readSettings(document, getConfiguration) + + deepEqual(calls, [["vscode-yaml-sort", document]]) + }) + + test("should read the values of VS Code's configuration", () => { + equal(readSettings().indent, workspace.getConfiguration("vscode-yaml-sort").get("indent")) + }) +}) diff --git a/src/test/integration/extension.test.ts b/src/test/integration/extension.test.ts index 032c107..6c83e0c 100644 --- a/src/test/integration/extension.test.ts +++ b/src/test/integration/extension.test.ts @@ -3,30 +3,8 @@ import { resolve } from "path" import { Position, Range, Selection, Uri, commands, window, workspace } from "vscode" -import { Settings } from "../../core/settings" import { Controller } from "../../vscode/controller" -suite("Test getCustomSortKeywords", () => { - test("should return values of `vscode-yaml-sort.customSortKeywords_1`", () => { - const settings = new Settings() - settings.getCustomSortKeywords = function () { - return ["apiVersion", "kind", "metadata", "spec", "data"] - } - deepEqual(settings.getCustomSortKeywords(1), ["apiVersion", "kind", "metadata", "spec", "data"]) - }) - test("should return `[]` for custom keywords 2 and 3", () => { - deepEqual(new Settings().getCustomSortKeywords(2), []) - deepEqual(new Settings().getCustomSortKeywords(3), []) - }) - - test("should return [] when parameter is not in [1, 2, 3]", () => { - deepEqual(new Settings().getCustomSortKeywords(0), []) - deepEqual(new Settings().getCustomSortKeywords(4), []) - deepEqual(new Settings().getCustomSortKeywords(-1), []) - deepEqual(new Settings().getCustomSortKeywords(1.5), []) - }) -}) - suite("Test validateYaml", () => { test("do not fail when executing command", async () => { const uri = Uri.parse(resolve("./src/test/files/getYamlFilesInDirectory/file.yaml")) diff --git a/src/test/unit/core/settings.test.ts b/src/test/unit/core/settings.test.ts index fb75d01..2bf00f2 100644 --- a/src/test/unit/core/settings.test.ts +++ b/src/test/unit/core/settings.test.ts @@ -3,9 +3,8 @@ import { deepEqual, equal } from "assert" import { CLOUDFORMATION_SCHEMA } from "cloudformation-js-yaml-schema" import { HOMEASSISTANT_SCHEMA } from "homeassistant-js-yaml-schema" import { CORE_SCHEMA, DEFAULT_SCHEMA, FAILSAFE_SCHEMA, JSON_SCHEMA, Schema} from "js-yaml" -import { Uri, workspace } from "vscode" -import { Settings } from "../../../core/settings" +import { DEFAULT_CONFIGURATION, Settings } from "../../../core/settings" suite("Test Settings - getter", () => { const settings: Settings = new Settings() @@ -84,6 +83,9 @@ suite("Test Settings - getCustomSortKeywords()", () => { test("when index is not in [1, 2, 3] return `[]`", () => { deepEqual(settings.getCustomSortKeywords(0), []) + deepEqual(settings.getCustomSortKeywords(4), []) + deepEqual(settings.getCustomSortKeywords(-1), []) + deepEqual(settings.getCustomSortKeywords(1.5), []) }) }) @@ -129,17 +131,19 @@ suite("Test Settings - doSortOnSave()", () => { }) }) -suite("Test Settings - forDocument()", () => { - test("should read the configuration for the given document", () => { - const document = { uri: Uri.file("/project/a.yaml"), languageId: "yaml" } - const calls: unknown[][] = [] - const getConfiguration = (section?: string, scope?: unknown) => { - calls.push([section, scope]) - return workspace.getConfiguration(section) - } +suite("Test Settings - configuration", () => { + test("should use the configured values", () => { + const configuration = { indent: 4, customSortKeywords_2: ["b", "a"] } as Record + const settings = new Settings({ get: (key: string) => configuration[key] as T | undefined }) - Settings.forDocument(document, getConfiguration) + equal(settings.indent, 4) + deepEqual(settings.getCustomSortKeywords(2), ["b", "a"]) + equal(settings.lineWidth, DEFAULT_CONFIGURATION.lineWidth) + }) + + test("should not share lists between snapshots and the defaults", () => { + new Settings().getCustomSortKeywords(1).push("changed") - deepEqual(calls, [["vscode-yaml-sort", document]]) + deepEqual(new Settings().getCustomSortKeywords(1), ["apiVersion", "kind", "metadata", "spec", "data"]) }) }) diff --git a/src/test/unit/package-manifest.test.ts b/src/test/unit/package-manifest.test.ts index 9ce8c51..dec15e6 100644 --- a/src/test/unit/package-manifest.test.ts +++ b/src/test/unit/package-manifest.test.ts @@ -2,15 +2,18 @@ import { deepEqual, ok } from "assert" import { readFileSync } from "fs" import { join } from "path" +import { DEFAULT_CONFIGURATION, Settings } from "../../core/settings" + interface Manifest { activationEvents: string[] contributes: { commands: { command: string }[] + configuration: { properties: Record } menus: { commandPalette: { command: string, when: string }[] } } } -// out/test/suite -> project root +// out/test/unit -> project root const manifest = JSON.parse(readFileSync(join(__dirname, "..", "..", "..", "package.json"), "utf-8")) as Manifest suite("Test package.json - activation events", () => { @@ -42,3 +45,18 @@ suite("Test package.json - command palette", () => { } }) }) + +suite("Test package.json - configuration", () => { + const properties = manifest.contributes.configuration.properties + const prefix = `${Settings.SECTION}.` + + test("should declare every setting with the extension's section", () => { + ok(Object.keys(properties).every((key) => key.startsWith(prefix))) + }) + + test("should declare the same defaults as the core settings", () => { + const declared = Object.fromEntries(Object.entries(properties).map(([key, property]) => [key.slice(prefix.length), property.default])) + + deepEqual(declared, DEFAULT_CONFIGURATION) + }) +}) diff --git a/src/vscode/configuration.ts b/src/vscode/configuration.ts new file mode 100644 index 0000000..6df8c7d --- /dev/null +++ b/src/vscode/configuration.ts @@ -0,0 +1,17 @@ +import { ConfigurationScope, WorkspaceConfiguration, workspace } from "vscode" + +import { Settings } from "../core/settings" + +export type ConfigurationProvider = (section: string, scope?: ConfigurationScope) => WorkspaceConfiguration + +/** + * Reads the settings that apply to a document: workspace folder settings in a multi-root + * workspace and language specific settings like `"[yaml]": { "vscode-yaml-sort.indent": 4 }`. + * @param scope The document, or its uri and language id. Undefined reads the global and workspace settings. + * @param getConfiguration Injectable for tests. + * @returns A snapshot of the settings. + */ +export const readSettings = ( + scope?: ConfigurationScope, + getConfiguration: ConfigurationProvider = workspace.getConfiguration +): Settings => new Settings(getConfiguration(Settings.SECTION, scope)) diff --git a/src/vscode/controller.ts b/src/vscode/controller.ts index ffafe0c..96090a0 100644 --- a/src/vscode/controller.ts +++ b/src/vscode/controller.ts @@ -1,5 +1,6 @@ import { TextEdit, TextEditor, window } from "vscode" +import { readSettings } from "./configuration" import { ErrorUtil } from "./error-util" import { Severity, VsCodeAdapter } from "./vs-code-adapter" import { formatDocuments, sortDocuments } from "../core/document-transformer" @@ -21,7 +22,7 @@ export class Controller { } this.editor = window.activeTextEditor // one snapshot of the settings that apply to the edited document - this.settings = Settings.forDocument(this.editor.document) + this.settings = readSettings(this.editor.document) this.yamlutil = new YamlUtil(this.settings) this.jsyamladapter = new JsYamlAdapter(this.settings) this.vscodeadapter = new VsCodeAdapter(this.settings) diff --git a/src/vscode/file-sorter.ts b/src/vscode/file-sorter.ts index 5eefd21..6fd1518 100644 --- a/src/vscode/file-sorter.ts +++ b/src/vscode/file-sorter.ts @@ -2,11 +2,11 @@ import { readFileSync, writeFileSync } from "fs" import { Uri } from "vscode" +import { readSettings } from "./configuration" import { ErrorUtil } from "./error-util" import { Severity, VsCodeAdapter } from "./vs-code-adapter" import { YamlOperations, sortDocuments } from "../core/document-transformer" import { FileUtil } from "../core/file-util" -import { Settings } from "../core/settings" import { YamlUtil } from "../core/yaml-util" /** @@ -39,7 +39,7 @@ export const sortYamlFilesInDirectory = (uri: Uri | undefined): void => { new VsCodeAdapter().showMessage(Severity.ERROR, "Please run this command from the context menu of a folder in the explorer.") return } - const settingsFor = (resource: Uri) => Settings.forDocument({ uri: resource, languageId: "yaml" }) + const settingsFor = (resource: Uri) => readSettings({ uri: resource, languageId: "yaml" }) for (const file of new FileUtil(settingsFor(uri)).getFiles(uri.fsPath)) { try { sortFile(file, new YamlUtil(settingsFor(Uri.file(file)))) diff --git a/src/vscode/formatting-provider.ts b/src/vscode/formatting-provider.ts index 8689214..68db857 100644 --- a/src/vscode/formatting-provider.ts +++ b/src/vscode/formatting-provider.ts @@ -1,8 +1,8 @@ import { DocumentFormattingEditProvider, Range, TextDocument, TextEdit } from "vscode" +import { readSettings } from "./configuration" import { ErrorUtil } from "./error-util" import { formatDocuments, sortDocuments } from "../core/document-transformer" -import { Settings } from "../core/settings" import { YamlUtil } from "../core/yaml-util" /** @@ -19,7 +19,7 @@ export class YamlFormattingProvider implements DocumentFormattingEditProvider { * @returns One edit replacing the whole document, or no edit if the document cannot be processed. */ provideDocumentFormattingEdits(document: TextDocument): TextEdit[] { - const settings = Settings.forDocument(document) + const settings = readSettings(document) const yamlutil = new YamlUtil(settings) const text = document.getText() let newText: string | null From ab54172982171a5eb18f9b27b25d96caf9f91e25 Mon Sep 17 00:00:00 2001 From: Pascal Reitermann Date: Fri, 20 Nov 2026 14:20:53 +0000 Subject: [PATCH 03/18] refactor(core): throw errors instead of reporting them to the user YamlUtil caught every error, showed it in VS Code through ErrorUtil and returned null. The document transformer and its callers then had to treat null as "failed, already reported", and the core depended on the VS Code API for its error handling. The core now throws: sortYaml(), formatYaml(), sortDocuments() and formatDocuments() return a string or throw. The vscode layer, which already caught the validation errors, reports them, so every error is reported exactly once, at the boundary to the user. Sorting a directory reports the original error instead of the generic "could not be sorted". transformDocuments() still never returns a partial result, because the first document that cannot be transformed aborts the whole text. The test for #65 (formatting stays silent) is no longer meaningful for the core, which cannot show messages at all; it now checks that an invalid document aborts formatting. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Jdb5ivdJ5L75kyEDAbuvMZ --- src/core/document-transformer.ts | 53 ++++------ src/core/yaml-util.ts | 98 +++++++++---------- .../unit/core/document-transformer.test.ts | 29 +++--- src/test/unit/core/yaml-util.test.ts | 8 +- src/vscode/controller.ts | 12 +-- src/vscode/file-sorter.ts | 3 - src/vscode/formatting-provider.ts | 5 +- 7 files changed, 89 insertions(+), 119 deletions(-) diff --git a/src/core/document-transformer.ts b/src/core/document-transformer.ts index 3258957..6b68b57 100644 --- a/src/core/document-transformer.ts +++ b/src/core/document-transformer.ts @@ -16,9 +16,10 @@ export interface SortOptions { } /** - * Transforms the content of a single, non-empty document. Returns null if that fails. + * Transforms the content of a single, non-empty document. + * @throws Error if the content cannot be transformed. */ -type ContentTransformation = (content: string) => string | null +type ContentTransformation = (content: string) => string /** * Puts the transformed documents back together. @@ -40,35 +41,27 @@ const joinDocuments = (stream: YamlStream, leadingDashes: boolean | undefined): /** * 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. + * The transformation throws for a document it cannot transform, so there never is a partial + * result that drops documents or empties a selection. + * @returns The transformed stream. */ -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 }) - } - return { directives: stream.directives, documents } -} +const transformDocuments = (stream: YamlStream, transform: ContentTransformation): YamlStream => ({ + directives: stream.directives, + documents: stream.documents.map((document): YamlDocument => ({ + delimiter: document.delimiter, + content: document.content.trim() === "" ? "" : transform(document.content), + })), +}) /** * Sorts every yaml document in a text. * @param text Text containing one or more yaml documents. * @param options Sort options. * @param yaml Yaml operations, injectable for tests. - * @returns The sorted text, or null if a document could not be sorted - * (the error has already been reported to the user). - * @throws Error if the text is not valid yaml or the selection ends invalid. + * @returns The sorted text. + * @throws Error if the text is not valid yaml, the selection ends invalid or a document cannot be sorted. */ -export const sortDocuments = (text: string, options: SortOptions, yaml: YamlOperations = new YamlUtil()): string | null => { +export const sortDocuments = (text: string, options: SortOptions, yaml: YamlOperations = new YamlUtil()): string => { validateTextRange(text) new JsYamlAdapter(yaml.settings).validateYaml(text) @@ -76,11 +69,8 @@ export const sortDocuments = (text: string, options: SortOptions, yaml: YamlOper // an indented selection is sorted like a top-level document and indented again afterwards (see #53) const indentation = options.isSelection ? YamlUtil.getNumberOfLeadingSpaces(content) : 0 const sortedContent = yaml.sortYaml(removeLeadingSpacesOnEachLine(content, indentation), options.customSort) - return sortedContent ? prependWhitespacesOnEachLine(sortedContent, indentation) : null + return prependWhitespacesOnEachLine(sortedContent, indentation) }) - if (!sorted) { - return null - } return joinDocuments(sorted, options.isSelection ? undefined : yaml.settings.useLeadingDashes) } @@ -88,13 +78,10 @@ export const sortDocuments = (text: string, options: SortOptions, yaml: YamlOper * Formats every yaml document in a text without sorting it. * @param text Text containing one or more yaml documents. * @param yaml Yaml operations, injectable for tests. - * @returns The formatted text, or null if a document could not be formatted - * (the error has already been reported to the user). + * @returns The formatted text. + * @throws Error if a document is not valid yaml. */ -export const formatDocuments = (text: string, yaml: YamlOperations = new YamlUtil()): string | null => { +export const formatDocuments = (text: string, yaml: YamlOperations = new YamlUtil()): string => { const formatted = transformDocuments(splitDocuments(text), (content) => yaml.formatYaml(content, false)) - if (!formatted) { - return null - } return joinDocuments(formatted, yaml.settings.useLeadingDashes) } diff --git a/src/core/yaml-util.ts b/src/core/yaml-util.ts index 8b639cd..07fa9c1 100644 --- a/src/core/yaml-util.ts +++ b/src/core/yaml-util.ts @@ -1,6 +1,5 @@ import { prependWhitespacesOnEachLine } from "./indentation" import { JsYamlAdapter } from "./js-yaml-adapter" -import { ErrorUtil } from "../vscode/error-util" import { ProcessorController } from "./processor/processor-controller" import { Settings } from "./settings" import { sortArraysRecursively } from "./sort-util" @@ -16,7 +15,6 @@ const isMapping = (value: unknown): value is Record => export class YamlUtil { settings: Settings jsyamladapter: JsYamlAdapter - errorutil = new ErrorUtil() constructor(settings = new Settings()) { this.settings = settings @@ -50,40 +48,45 @@ export class YamlUtil { return result } - sortYaml(unsortedYaml: string, customSort = 0): string | null { - try { - const processor = new ProcessorController(unsortedYaml, this.settings) - processor.preprocess() - const unsortedYamlWithoutTabs = processor.text - - const doc = this.jsyamladapter.load(unsortedYamlWithoutTabs) - - if (this.settings.sortArrays) { - sortArraysRecursively(doc) - } - - let sortedYaml = "" - let remainder = doc - if (customSort > 0 && !this.settings.useCustomSortRecursively && isMapping(doc)) { - // check for the key itself, values like 0, false or null must be ordered too - const keywords = this.settings.getCustomSortKeywords(customSort) - .filter((key) => Object.prototype.hasOwnProperty.call(doc, key)) - sortedYaml = keywords.map((key) => this.dumpKeyword(key, doc[key], customSort)).join("") - // the remaining keys are sorted as usual; build them anew instead of deleting from the loaded document - remainder = Object.fromEntries(Object.entries(doc).filter(([key]) => !keywords.includes(key))) - } + /** + * Sorts a single yaml document. + * @param unsortedYaml The content of one document, without delimiter. + * @param customSort 0 for the default sort, 1 to 3 for the custom sort keywords. + * @returns The sorted document. + * @throws YAMLException if the document is not valid yaml. + */ + sortYaml(unsortedYaml: string, customSort = 0): string { + const processor = new ProcessorController(unsortedYaml, this.settings) + processor.preprocess() - // either sort whole yaml or sort the rest of the yaml (which can be empty) and add it to the sortedYaml - sortedYaml += this.jsyamladapter.dumpYaml(remainder, true, customSort) + const doc = this.jsyamladapter.load(processor.text) + if (this.settings.sortArrays) { + sortArraysRecursively(doc) + } - processor.text = sortedYaml - processor.postprocess() + processor.text = this.dumpSorted(doc, customSort) + processor.postprocess() + return processor.text + } - return processor.text - } catch (error) { - this.errorutil.handleError(error) - return null + /** + * Dumps a loaded document with sorted keys. A custom sort that is not applied recursively puts + * the custom sort keywords on top and sorts the remaining keys as usual. + * @param doc The loaded document. + * @param customSort 0 for the default sort, 1 to 3 for the custom sort keywords. + * @returns The dumped document. + */ + dumpSorted(doc: unknown, customSort: number): string { + if (customSort === 0 || this.settings.useCustomSortRecursively || !isMapping(doc)) { + return this.jsyamladapter.dumpYaml(doc, true, customSort) } + // check for the key itself, values like 0, false or null must be ordered too + const keywords = this.settings.getCustomSortKeywords(customSort) + .filter((key) => Object.prototype.hasOwnProperty.call(doc, key)) + // build the remaining keys anew instead of deleting them from the loaded document + const remainder = Object.fromEntries(Object.entries(doc).filter(([key]) => !keywords.includes(key))) + const dumpedKeywords = keywords.map((key) => this.dumpKeyword(key, doc[key], customSort)).join("") + return dumpedKeywords + this.jsyamladapter.dumpYaml(remainder, true, customSort) } /** @@ -102,26 +105,17 @@ export class YamlUtil { } /** - * Formats a yaml document (without sorting). - * @param {string} yaml Yaml to be formatted. - * @returns {string} Formatted yaml. + * Formats a single yaml document without sorting it. + * @param yaml The content of one document, without delimiter. + * @param useLeadingDashes Whether the result should start with a delimiter. + * @returns The formatted document. + * @throws YAMLException if the document is not valid yaml. */ - formatYaml(yaml: string, useLeadingDashes: boolean): string | null { - try { - const processor = new ProcessorController(yaml, this.settings) - processor.preprocess() - let doc = this.jsyamladapter.dumpYaml(this.jsyamladapter.load(processor.text), false, 0) - processor.text = doc - processor.postprocess() - doc = processor.text - if (useLeadingDashes) { - doc = `---\n${doc}` - } - return doc - } catch (error) { - this.errorutil.handleError(error) - return null - } + formatYaml(yaml: string, useLeadingDashes: boolean): string { + const processor = new ProcessorController(yaml, this.settings) + processor.preprocess() + processor.text = this.jsyamladapter.dumpYaml(this.jsyamladapter.load(processor.text), false, 0) + processor.postprocess() + return useLeadingDashes ? `---\n${processor.text}` : processor.text } - } diff --git a/src/test/unit/core/document-transformer.test.ts b/src/test/unit/core/document-transformer.test.ts index 1844546..21d939e 100644 --- a/src/test/unit/core/document-transformer.test.ts +++ b/src/test/unit/core/document-transformer.test.ts @@ -1,8 +1,5 @@ import { equal, throws } from "assert" -import { spy } from "sinon" -import { window } from "vscode" - import { YamlOperations, formatDocuments, sortDocuments } from "../../../core/document-transformer" import { Settings } from "../../../core/settings" import { YamlUtil } from "../../../core/yaml-util" @@ -13,7 +10,12 @@ import { YamlUtil } from "../../../core/yaml-util" const failingSortFor = (marker: string): YamlOperations => { const yamlutil = new YamlUtil() const original = yamlutil.sortYaml.bind(yamlutil) - yamlutil.sortYaml = (text: string, customSort?: number) => (text.includes(marker) ? null : original(text, customSort)) + yamlutil.sortYaml = (text: string, customSort?: number) => { + if (text.includes(marker)) { + throw new Error(`cannot sort ${marker}`) + } + return original(text, customSort) + } return yamlutil } @@ -38,15 +40,15 @@ suite("Test DocumentTransformer - sortDocuments()", () => { }) suite("Test DocumentTransformer - sortDocuments() when a document cannot be sorted", () => { - test("should return null instead of dropping the failing document", () => { + test("should throw instead of dropping the failing document", () => { const text = "---\nb: 1\na: 2\n---\nfail: 1\nc: 2\n---\ne: 1\nd: 2" - equal(sortDocuments(text, { customSort: 0, isSelection: false }, failingSortFor("fail")), null) + throws(() => sortDocuments(text, { customSort: 0, isSelection: false }, failingSortFor("fail")), /cannot sort fail/) }) - test("should return null instead of an empty text for a selection", () => { + test("should throw instead of returning an empty text for a selection", () => { // an empty result would replace the selection with nothing - equal(sortDocuments(" fail: 1\n a: 2", { customSort: 0, isSelection: true }, failingSortFor("fail")), null) + throws(() => sortDocuments(" fail: 1\n a: 2", { customSort: 0, isSelection: true }, failingSortFor("fail")), /cannot sort fail/) }) }) @@ -126,15 +128,8 @@ suite("Test DocumentTransformer - formatDocuments()", () => { 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() - } + test("should throw on invalid yaml instead of dropping the document", () => { + throws(() => formatDocuments("---\nkey1: value\n---\nkey2: [value")) }) }) diff --git a/src/test/unit/core/yaml-util.test.ts b/src/test/unit/core/yaml-util.test.ts index 10c4c59..be5c9fe 100644 --- a/src/test/unit/core/yaml-util.test.ts +++ b/src/test/unit/core/yaml-util.test.ts @@ -1,4 +1,4 @@ -import { equal } from "assert" +import { equal, throws } from "assert" import { CLOUDFORMATION_SCHEMA } from "cloudformation-js-yaml-schema" import { CORE_SCHEMA } from "js-yaml" @@ -399,9 +399,9 @@ suite("Test formatYaml", () => { equal(yamlutil.formatYaml(actual, true), expected) }) - test("should return `null` on invalid yaml", () => { + test("should throw on invalid yaml", () => { const yamlutil = new YamlUtil() - equal(yamlutil.formatYaml("key: 1\nkey: 1", true), null) + throws(() => yamlutil.formatYaml("key: 1\nkey: 1", true), /duplicated mapping key/) }) }) @@ -470,7 +470,7 @@ suite("Test YamlUtil - sortYaml() with kustomize patches", () => { const sorted = util.sortYaml(kustomize) - equal(sorted !== null && sorted.includes("patch: |-\n - op: replace\n path: /subjects/0/namespace\n value: monitor"), true) + equal(sorted.includes("patch: |-\n - op: replace\n path: /subjects/0/namespace\n value: monitor"), true) }) }) diff --git a/src/vscode/controller.ts b/src/vscode/controller.ts index 96090a0..47bd9b3 100644 --- a/src/vscode/controller.ts +++ b/src/vscode/controller.ts @@ -41,8 +41,11 @@ export class Controller { } formatYamlWrapper(): TextEdit[] { - const newText = formatDocuments(VsCodeAdapter.getActiveDocument(this.editor), this.yamlutil) - if (newText === null) { + let newText: string + try { + newText = formatDocuments(VsCodeAdapter.getActiveDocument(this.editor), this.yamlutil) + } catch (error) { + this.errorutil.handleError(error) return [] } const edits = this.applyEdits(newText) @@ -59,16 +62,13 @@ export class Controller { sortYamlWrapper(customSort = 0): TextEdit[] { const text = VsCodeAdapter.getText(this.editor, VsCodeAdapter.getRange(this.editor)) - let newText: string | null + let newText: string try { newText = sortDocuments(text, { customSort, isSelection: !this.editor.selection.isEmpty }, this.yamlutil) } catch (error) { this.errorutil.handleError(error) return [] } - if (newText === null) { - return [] - } const edits = this.applyEdits(newText) this.vscodeadapter.showMessage(Severity.INFO, "Keys resorted successfully") return edits diff --git a/src/vscode/file-sorter.ts b/src/vscode/file-sorter.ts index 6fd1518..b5dea01 100644 --- a/src/vscode/file-sorter.ts +++ b/src/vscode/file-sorter.ts @@ -19,9 +19,6 @@ import { YamlUtil } from "../core/yaml-util" 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" } diff --git a/src/vscode/formatting-provider.ts b/src/vscode/formatting-provider.ts index 68db857..e4af24a 100644 --- a/src/vscode/formatting-provider.ts +++ b/src/vscode/formatting-provider.ts @@ -22,7 +22,7 @@ export class YamlFormattingProvider implements DocumentFormattingEditProvider { const settings = readSettings(document) const yamlutil = new YamlUtil(settings) const text = document.getText() - let newText: string | null + let newText: string try { newText = settings.doSortOnSave() ? sortDocuments(text, { customSort: settings.sortOnSave, isSelection: false }, yamlutil) @@ -31,9 +31,6 @@ export class YamlFormattingProvider implements DocumentFormattingEditProvider { this.errorutil.handleError(error) return [] } - if (newText === null) { - return [] - } const fullRange = document.validateRange(new Range(0, 0, document.lineCount, 0)) return [TextEdit.replace(fullRange, newText)] } From 00bab42fd1fc6967495f3a0e6a55fc9a5318084f Mon Sep 17 00:00:00 2001 From: Pascal Reitermann Date: Fri, 20 Nov 2026 14:21:48 +0000 Subject: [PATCH 04/18] refactor: name modules after their purpose instead of util Several classes carried generic names that said nothing about their job (YamlUtil, SortUtil, FileUtil, ErrorUtil, Controller, ProcessorController), and one module mixed a pure file operation with a VS Code command. Rename them after what they do: * YamlUtil -> YamlSorter (core/yaml-sorter.ts) * SortUtil -> SortOrder (core/sort-order.ts) * FileUtil -> YamlFileFinder (core/yaml-file-finder.ts) * ProcessorController -> ProcessorPipeline (core/processor/processor-pipeline.ts) * ErrorUtil -> ErrorReporter (vscode/error-reporter.ts) * Controller -> EditorCommands (vscode/editor-commands.ts) Split controller/file-sorter: sortFile() only uses node's fs and moves to core/file-sorter.ts, the command handler that walks a directory stays in vscode/directory-sorter.ts. Properties and variables follow camelCase (jsYamlAdapter, vsCodeAdapter, errorReporter). No behavior changes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Jdb5ivdJ5L75kyEDAbuvMZ --- src/core/document-transformer.ts | 12 +- src/core/file-sorter.ts | 20 ++ src/core/js-yaml-adapter.ts | 6 +- ...or-controller.ts => processor-pipeline.ts} | 16 +- src/core/{sort-util.ts => sort-order.ts} | 6 +- .../{file-util.ts => yaml-file-finder.ts} | 2 +- src/core/{yaml-util.ts => yaml-sorter.ts} | 26 +-- src/extension.ts | 12 +- ...roller.test.ts => editor-commands.test.ts} | 8 +- src/test/integration/error-reporter.test.ts | 18 ++ src/test/integration/error-util.test.ts | 18 -- src/test/integration/extension.test.ts | 6 +- src/test/integration/vs-code-adapter.test.ts | 18 +- .../unit/core/document-transformer.test.ts | 24 +-- src/test/unit/core/file-sorter.test.ts | 2 +- src/test/unit/core/js-yaml-adapter.test.ts | 28 +-- .../core/processor/generic-processor.test.ts | 22 +- .../core/processor/helm-processor.test.ts | 74 +++---- ...ler.test.ts => processor-pipeline.test.ts} | 30 +-- .../{sort-util.test.ts => sort-order.test.ts} | 8 +- ...-util.test.ts => yaml-file-finder.test.ts} | 10 +- ...{yaml-util.test.ts => yaml-sorter.test.ts} | 204 +++++++++--------- src/vscode/directory-sorter.ts | 29 +++ .../{controller.ts => editor-commands.ts} | 38 ++-- .../{error-util.ts => error-reporter.ts} | 6 +- src/vscode/file-sorter.ts | 47 ---- src/vscode/formatting-provider.ts | 14 +- 27 files changed, 353 insertions(+), 351 deletions(-) create mode 100644 src/core/file-sorter.ts rename src/core/processor/{processor-controller.ts => processor-pipeline.ts} (89%) rename src/core/{sort-util.ts => sort-order.ts} (93%) rename src/core/{file-util.ts => yaml-file-finder.ts} (96%) rename src/core/{yaml-util.ts => yaml-sorter.ts} (84%) rename src/test/integration/{controller.test.ts => editor-commands.test.ts} (90%) create mode 100644 src/test/integration/error-reporter.test.ts delete mode 100644 src/test/integration/error-util.test.ts rename src/test/unit/core/processor/{processor-controller.test.ts => processor-pipeline.test.ts} (78%) rename src/test/unit/core/{sort-util.test.ts => sort-order.test.ts} (92%) rename src/test/unit/core/{file-util.test.ts => yaml-file-finder.test.ts} (75%) rename src/test/unit/core/{yaml-util.test.ts => yaml-sorter.test.ts} (69%) create mode 100644 src/vscode/directory-sorter.ts rename src/vscode/{controller.ts => editor-commands.ts} (66%) rename src/vscode/{error-util.ts => error-reporter.ts} (52%) delete mode 100644 src/vscode/file-sorter.ts diff --git a/src/core/document-transformer.ts b/src/core/document-transformer.ts index 6b68b57..d93364a 100644 --- a/src/core/document-transformer.ts +++ b/src/core/document-transformer.ts @@ -1,12 +1,12 @@ import { prependWhitespacesOnEachLine, removeLeadingSpacesOnEachLine } from "./indentation" import { JsYamlAdapter } from "./js-yaml-adapter" +import { YamlSorter } from "./yaml-sorter" import { YamlDocument, YamlStream, splitDocuments, validateTextRange } from "./yaml-text" -import { YamlUtil } from "./yaml-util" /** - * The parts of YamlUtil a transformation depends on. Kept narrow so tests can pass a stub. + * The parts of YamlSorter 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. */ @@ -61,13 +61,13 @@ const transformDocuments = (stream: YamlStream, transform: ContentTransformation * @returns The sorted text. * @throws Error if the text is not valid yaml, the selection ends invalid or a document cannot be sorted. */ -export const sortDocuments = (text: string, options: SortOptions, yaml: YamlOperations = new YamlUtil()): string => { +export const sortDocuments = (text: string, options: SortOptions, yaml: YamlOperations = new YamlSorter()): string => { validateTextRange(text) new JsYamlAdapter(yaml.settings).validateYaml(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(content) : 0 + const indentation = options.isSelection ? YamlSorter.getNumberOfLeadingSpaces(content) : 0 const sortedContent = yaml.sortYaml(removeLeadingSpacesOnEachLine(content, indentation), options.customSort) return prependWhitespacesOnEachLine(sortedContent, indentation) }) @@ -81,7 +81,7 @@ export const sortDocuments = (text: string, options: SortOptions, yaml: YamlOper * @returns The formatted text. * @throws Error if a document is not valid yaml. */ -export const formatDocuments = (text: string, yaml: YamlOperations = new YamlUtil()): string => { +export const formatDocuments = (text: string, yaml: YamlOperations = new YamlSorter()): string => { const formatted = transformDocuments(splitDocuments(text), (content) => yaml.formatYaml(content, false)) return joinDocuments(formatted, yaml.settings.useLeadingDashes) } diff --git a/src/core/file-sorter.ts b/src/core/file-sorter.ts new file mode 100644 index 0000000..fcaad03 --- /dev/null +++ b/src/core/file-sorter.ts @@ -0,0 +1,20 @@ +import { readFileSync, writeFileSync } from "fs" + +import { YamlOperations, sortDocuments } from "./document-transformer" +import { YamlSorter } from "./yaml-sorter" + +/** + * 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 YamlSorter()): void => { + const text = readFileSync(file, "utf-8") + let sorted = sortDocuments(text, { customSort: 0, isSelection: false }, yaml) + if (text.endsWith("\n") && !sorted.endsWith("\n")) { + sorted += "\n" + } + writeFileSync(file, sorted) +} diff --git a/src/core/js-yaml-adapter.ts b/src/core/js-yaml-adapter.ts index dd332f5..d830cc3 100644 --- a/src/core/js-yaml-adapter.ts +++ b/src/core/js-yaml-adapter.ts @@ -1,7 +1,7 @@ import { dump, load } from "js-yaml" import { Settings } from "./settings" -import { SortUtil } from "./sort-util" +import { SortOrder } from "./sort-order" import { removeTrailingCharacters, splitDocuments } from "./yaml-text" /** @@ -87,12 +87,12 @@ export class JsYamlAdapter { */ getKeyComparator(sortKeys: boolean, custom: number): boolean | ((a: string, b: string) => number) { if (custom > 0 && this.settings.useCustomSortRecursively) { - const sort = new SortUtil(this.settings, custom) + const sort = new SortOrder(this.settings, custom) return (a: string, b: string) => sort.customSort(a, b) } if (sortKeys && this.settings.sortOrderReverse) { // mirror js-yaml's default order, which compares by code point - return (a: string, b: string) => SortUtil.compare(b, a) + return (a: string, b: string) => SortOrder.compare(b, a) } return sortKeys } diff --git a/src/core/processor/processor-controller.ts b/src/core/processor/processor-pipeline.ts similarity index 89% rename from src/core/processor/processor-controller.ts rename to src/core/processor/processor-pipeline.ts index a2a5c86..e144173 100644 --- a/src/core/processor/processor-controller.ts +++ b/src/core/processor/processor-pipeline.ts @@ -7,11 +7,11 @@ import { SpacingProcessor } from "./spacing-processor" import { TabsToSpacesProcessor } from "./tabs-to-spaces-processor" import { Settings } from "../settings" -export class ProcessorController { +export class ProcessorPipeline { arrayprocessor!: ArrayProcessor blockprocessor!: BlockProcessor commentprocessor!: CommentProcessor - helmprocessor!: HelmProcessor + helmProcessor!: HelmProcessor octalprocessor!: OctalProcessor spacingprocessor!: SpacingProcessor tabstospacespreprocessor!: TabsToSpacesProcessor @@ -41,9 +41,9 @@ export class ProcessorController { } if (this.settings.useHelmProcessor) { - this.helmprocessor = new HelmProcessor(this.text) - this.helmprocessor.preprocess() - this.text = this.helmprocessor.text + this.helmProcessor = new HelmProcessor(this.text) + this.helmProcessor.preprocess() + this.text = this.helmProcessor.text } if (this.settings.useOctalProcessor) { @@ -73,9 +73,9 @@ export class ProcessorController { } if (this.settings.useHelmProcessor) { - this.helmprocessor.text = this.text - this.helmprocessor.postprocess() - this.text = this.helmprocessor.text + this.helmProcessor.text = this.text + this.helmProcessor.postprocess() + this.text = this.helmProcessor.text } if (this.settings.useArrayProcessor) { diff --git a/src/core/sort-util.ts b/src/core/sort-order.ts similarity index 93% rename from src/core/sort-util.ts rename to src/core/sort-order.ts index 852221e..c0ea523 100644 --- a/src/core/sort-util.ts +++ b/src/core/sort-order.ts @@ -1,6 +1,6 @@ import { Settings } from "./settings" -export class SortUtil { +export class SortOrder { settings: Settings custom = 0 @@ -16,7 +16,7 @@ export class SortUtil { const indexB = sortOrder.indexOf(b) if (indexA > -1 && indexB > -1) { - return SortUtil.compare(indexA, indexB) + return SortOrder.compare(indexA, indexB) } if (indexA !== -1 && indexB === -1) { return -1 @@ -76,7 +76,7 @@ const compareArrayItems = (a: unknown, b: unknown): number => { // Array.prototype.sort is stable, so these items keep their order return 0 } - return SortUtil.compare(a as number | string, b as number | string) + return SortOrder.compare(a as number | string, b as number | string) } /** diff --git a/src/core/file-util.ts b/src/core/yaml-file-finder.ts similarity index 96% rename from src/core/file-util.ts rename to src/core/yaml-file-finder.ts index da16049..cad02b6 100644 --- a/src/core/file-util.ts +++ b/src/core/yaml-file-finder.ts @@ -4,7 +4,7 @@ import { sync } from "glob" import { Settings } from "./settings" -export class FileUtil { +export class YamlFileFinder { settings: Settings constructor(settings = new Settings()) { diff --git a/src/core/yaml-util.ts b/src/core/yaml-sorter.ts similarity index 84% rename from src/core/yaml-util.ts rename to src/core/yaml-sorter.ts index 07fa9c1..089e37f 100644 --- a/src/core/yaml-util.ts +++ b/src/core/yaml-sorter.ts @@ -1,8 +1,8 @@ import { prependWhitespacesOnEachLine } from "./indentation" import { JsYamlAdapter } from "./js-yaml-adapter" -import { ProcessorController } from "./processor/processor-controller" +import { ProcessorPipeline } from "./processor/processor-pipeline" import { Settings } from "./settings" -import { sortArraysRecursively } from "./sort-util" +import { sortArraysRecursively } from "./sort-order" import { removeTrailingCharacters } from "./yaml-text" /** @@ -12,13 +12,13 @@ import { removeTrailingCharacters } from "./yaml-text" const isMapping = (value: unknown): value is Record => typeof value === "object" && value !== null && !Array.isArray(value) -export class YamlUtil { +export class YamlSorter { settings: Settings - jsyamladapter: JsYamlAdapter + jsYamlAdapter: JsYamlAdapter constructor(settings = new Settings()) { this.settings = settings - this.jsyamladapter = new JsYamlAdapter(settings) + this.jsYamlAdapter = new JsYamlAdapter(settings) } static getNumberOfLeadingSpaces(text: string): number { @@ -56,10 +56,10 @@ export class YamlUtil { * @throws YAMLException if the document is not valid yaml. */ sortYaml(unsortedYaml: string, customSort = 0): string { - const processor = new ProcessorController(unsortedYaml, this.settings) + const processor = new ProcessorPipeline(unsortedYaml, this.settings) processor.preprocess() - const doc = this.jsyamladapter.load(processor.text) + const doc = this.jsYamlAdapter.load(processor.text) if (this.settings.sortArrays) { sortArraysRecursively(doc) } @@ -78,7 +78,7 @@ export class YamlUtil { */ dumpSorted(doc: unknown, customSort: number): string { if (customSort === 0 || this.settings.useCustomSortRecursively || !isMapping(doc)) { - return this.jsyamladapter.dumpYaml(doc, true, customSort) + return this.jsYamlAdapter.dumpYaml(doc, true, customSort) } // check for the key itself, values like 0, false or null must be ordered too const keywords = this.settings.getCustomSortKeywords(customSort) @@ -86,7 +86,7 @@ export class YamlUtil { // build the remaining keys anew instead of deleting them from the loaded document const remainder = Object.fromEntries(Object.entries(doc).filter(([key]) => !keywords.includes(key))) const dumpedKeywords = keywords.map((key) => this.dumpKeyword(key, doc[key], customSort)).join("") - return dumpedKeywords + this.jsyamladapter.dumpYaml(remainder, true, customSort) + return dumpedKeywords + this.jsYamlAdapter.dumpYaml(remainder, true, customSort) } /** @@ -97,8 +97,8 @@ export class YamlUtil { * @returns The yaml lines for the keyword, ending with a line break. */ dumpKeyword(key: string, value: unknown, customSort: number): string { - const dumpedValue = this.jsyamladapter.dumpValue(value, true, customSort) - if (YamlUtil.isValueMultiline(dumpedValue)) { + const dumpedValue = this.jsYamlAdapter.dumpValue(value, true, customSort) + if (YamlSorter.isValueMultiline(dumpedValue)) { return `${key}:\n${this.transformMultilineValue(dumpedValue)}\n` } return `${key}: ${dumpedValue}\n` @@ -112,9 +112,9 @@ export class YamlUtil { * @throws YAMLException if the document is not valid yaml. */ formatYaml(yaml: string, useLeadingDashes: boolean): string { - const processor = new ProcessorController(yaml, this.settings) + const processor = new ProcessorPipeline(yaml, this.settings) processor.preprocess() - processor.text = this.jsyamladapter.dumpYaml(this.jsyamladapter.load(processor.text), false, 0) + processor.text = this.jsYamlAdapter.dumpYaml(this.jsYamlAdapter.load(processor.text), false, 0) processor.postprocess() return useLeadingDashes ? `---\n${processor.text}` : processor.text } diff --git a/src/extension.ts b/src/extension.ts index 3958bf0..8512b68 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,7 +1,7 @@ import { ExtensionContext, Uri, commands } from "vscode" -import { Controller } from "./vscode/controller" -import { sortYamlFilesInDirectory } from "./vscode/file-sorter" +import { sortYamlFilesInDirectory } from "./vscode/directory-sorter" +import { EditorCommands } from "./vscode/editor-commands" import { YamlFormattingProvider } from "./vscode/formatting-provider" import { VsCodeAdapter } from "./vscode/vs-code-adapter" @@ -9,13 +9,13 @@ export function activate(context: ExtensionContext) { context.subscriptions.push( VsCodeAdapter.registerFormatter(new YamlFormattingProvider()), commands.registerCommand("vscode-yaml-sort.sortYaml", () => { - new Controller().sortYamlWrapper() + new EditorCommands().sortYamlWrapper() }), commands.registerCommand("vscode-yaml-sort.validateYaml", () => { - new Controller().validateYamlWrapper() + new EditorCommands().validateYamlWrapper() }), commands.registerCommand("vscode-yaml-sort.formatYaml", () => { - new Controller().formatYamlWrapper() + new EditorCommands().formatYamlWrapper() }), commands.registerCommand("vscode-yaml-sort.sortYamlFilesInDirectory", (uri?: Uri) => { sortYamlFilesInDirectory(uri) @@ -24,7 +24,7 @@ export function activate(context: ExtensionContext) { for (const index of [1, 2, 3]) { context.subscriptions.push( commands.registerCommand(`vscode-yaml-sort.customSortYaml_${index}`, () => { - new Controller().sortYamlWrapper(index) + new EditorCommands().sortYamlWrapper(index) }) ) } diff --git a/src/test/integration/controller.test.ts b/src/test/integration/editor-commands.test.ts similarity index 90% rename from src/test/integration/controller.test.ts rename to src/test/integration/editor-commands.test.ts index 04d10ae..54afac0 100644 --- a/src/test/integration/controller.test.ts +++ b/src/test/integration/editor-commands.test.ts @@ -4,7 +4,7 @@ import { resolve } from "path" import { Uri, commands, window, workspace } from "vscode" -import { Controller } from "../../vscode/controller" +import { EditorCommands } from "../../vscode/editor-commands" suite("Test sortYamlFiles", () => { @@ -30,7 +30,7 @@ suite("Test validateYamlWrapper", () => { const uri = Uri.parse(resolve("./src/test/files/testValidateYamlWrapper.yaml")) const doc = await workspace.openTextDocument(uri) await window.showTextDocument(doc, { preview: false }) - equal(new Controller().validateYamlWrapper(), true) + equal(new EditorCommands().validateYamlWrapper(), true) }) }) @@ -47,7 +47,7 @@ suite("Test formatYamlWrapper", () => { "key:\n" + " key2: value" - equal(new Controller().formatYamlWrapper()[0].newText, expected) + equal(new EditorCommands().formatYamlWrapper()[0].newText, expected) } else { equal(true, false) } @@ -65,7 +65,7 @@ suite("Test formatYamlWrapper", () => { "---\n" + "key2: value" - equal(new Controller().formatYamlWrapper()[0].newText, expected) + equal(new EditorCommands().formatYamlWrapper()[0].newText, expected) } else { equal(true, false) } diff --git a/src/test/integration/error-reporter.test.ts b/src/test/integration/error-reporter.test.ts new file mode 100644 index 0000000..3322a51 --- /dev/null +++ b/src/test/integration/error-reporter.test.ts @@ -0,0 +1,18 @@ +import { equal } from "assert" + +import { spy } from "sinon" + +import { ErrorReporter } from "../../vscode/error-reporter" + +suite("Test ErrorReporter - handleError()", () => { + test("when input is of type error, should execute showMessage function", () => { + const errorReporter = new ErrorReporter() + const spyObject = spy(errorReporter.vsCodeAdapter, "showMessage") + + errorReporter.handleError("no error") + equal(spyObject.called, false) + + errorReporter.handleError(new Error()) + equal(spyObject.called, true) + }) +}) \ No newline at end of file diff --git a/src/test/integration/error-util.test.ts b/src/test/integration/error-util.test.ts deleted file mode 100644 index d68200a..0000000 --- a/src/test/integration/error-util.test.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { equal } from "assert" - -import { spy } from "sinon" - -import { ErrorUtil } from "../../vscode/error-util" - -suite("Test ErrorUtil - handleError()", () => { - test("when input is of type error, should execute showMessage function", () => { - const errorutil = new ErrorUtil() - const spyObject = spy(errorutil.vscodeadapter, "showMessage") - - errorutil.handleError("no error") - equal(spyObject.called, false) - - errorutil.handleError(new Error()) - equal(spyObject.called, true) - }) -}) \ No newline at end of file diff --git a/src/test/integration/extension.test.ts b/src/test/integration/extension.test.ts index 6c83e0c..8efea2b 100644 --- a/src/test/integration/extension.test.ts +++ b/src/test/integration/extension.test.ts @@ -3,7 +3,7 @@ import { resolve } from "path" import { Position, Range, Selection, Uri, commands, window, workspace } from "vscode" -import { Controller } from "../../vscode/controller" +import { EditorCommands } from "../../vscode/editor-commands" suite("Test validateYaml", () => { test("do not fail when executing command", async () => { @@ -61,7 +61,7 @@ suite("Test sortYamlWrapper", () => { new Position(activeEditor.document.lineCount + 1, 0)), actual)) - notDeepEqual(new Controller().sortYamlWrapper(), []) + notDeepEqual(new EditorCommands().sortYamlWrapper(), []) } else { equal(true, false) } @@ -85,7 +85,7 @@ suite("Test sortYamlWrapper", () => { actual)) activeEditor.selection = new Selection(0, 0, 0, 4) - deepEqual(new Controller().sortYamlWrapper(), []) + deepEqual(new EditorCommands().sortYamlWrapper(), []) } else { equal(true, false) } diff --git a/src/test/integration/vs-code-adapter.test.ts b/src/test/integration/vs-code-adapter.test.ts index 63709c7..328a860 100644 --- a/src/test/integration/vs-code-adapter.test.ts +++ b/src/test/integration/vs-code-adapter.test.ts @@ -25,33 +25,33 @@ suite("Test VsCodeAdapter - getActiveDocument()", () => { }) suite("Test VsCodeAdapter - showMessage()", () => { - const vscodeadapter = new VsCodeAdapter() + const vsCodeAdapter = new VsCodeAdapter() test("when Severity.ERROR should show error message", () => { const spyError = spy(window, "showErrorMessage") equal(spyError.called, false) - vscodeadapter.showMessage(Severity.ERROR, "Test message") + vsCodeAdapter.showMessage(Severity.ERROR, "Test message") equal(spyError.called, true) }) test("when Severity.INFO should show info message", () => { - const spyNotify = spy(vscodeadapter, "notify") + const spyNotify = spy(vsCodeAdapter, "notify") equal(spyNotify.called, false) - vscodeadapter.showMessage(Severity.INFO, "Test message") + vsCodeAdapter.showMessage(Severity.INFO, "Test message") equal(spyNotify.called, true) }) }) suite("Test VsCodeAdapter - notify()", () => { test("when notifySuccess should show info message", () => { - const vscodeadapter = new VsCodeAdapter() + const vsCodeAdapter = new VsCodeAdapter() const spyObject = spy(window, "showInformationMessage") - vscodeadapter.settings.notifySuccess = false - vscodeadapter.notify("Test message") + vsCodeAdapter.settings.notifySuccess = false + vsCodeAdapter.notify("Test message") equal(spyObject.called, false) - vscodeadapter.settings.notifySuccess = true - vscodeadapter.notify("Test message") + vsCodeAdapter.settings.notifySuccess = true + vsCodeAdapter.notify("Test message") equal(spyObject.called, true) }) }) diff --git a/src/test/unit/core/document-transformer.test.ts b/src/test/unit/core/document-transformer.test.ts index 21d939e..b915116 100644 --- a/src/test/unit/core/document-transformer.test.ts +++ b/src/test/unit/core/document-transformer.test.ts @@ -2,21 +2,21 @@ import { equal, throws } from "assert" import { YamlOperations, formatDocuments, sortDocuments } from "../../../core/document-transformer" import { Settings } from "../../../core/settings" -import { YamlUtil } from "../../../core/yaml-util" +import { YamlSorter } from "../../../core/yaml-sorter" /** * Real yaml operations, except that sorting fails for every document containing the marker. */ const failingSortFor = (marker: string): YamlOperations => { - const yamlutil = new YamlUtil() - const original = yamlutil.sortYaml.bind(yamlutil) - yamlutil.sortYaml = (text: string, customSort?: number) => { + const yamlSorter = new YamlSorter() + const original = yamlSorter.sortYaml.bind(yamlSorter) + yamlSorter.sortYaml = (text: string, customSort?: number) => { if (text.includes(marker)) { throw new Error(`cannot sort ${marker}`) } return original(text, customSort) } - return yamlutil + return yamlSorter } suite("Test DocumentTransformer - sortDocuments()", () => { @@ -93,10 +93,10 @@ suite("Test DocumentTransformer - delimiters and directives", () => { }) test("should drop only the leading delimiter when useLeadingDashes is false", () => { - const yamlutil = new YamlUtil() - yamlutil.settings.useLeadingDashes = false + const yamlSorter = new YamlSorter() + yamlSorter.settings.useLeadingDashes = false - equal(sort("--- # first\nb: 1\n--- # second\nd: 1\nc: 2", yamlutil), "b: 1\n--- # second\nc: 2\nd: 1") + equal(sort("--- # first\nb: 1\n--- # second\nd: 1\nc: 2", yamlSorter), "b: 1\n--- # second\nc: 2\nd: 1") }) test("should keep an empty document", () => { @@ -108,10 +108,10 @@ suite("Test DocumentTransformer - delimiters and directives", () => { }) test("should keep yaml directives when useLeadingDashes is false, because they require a delimiter", () => { - const yamlutil = new YamlUtil() - yamlutil.settings.useLeadingDashes = false + const yamlSorter = new YamlSorter() + yamlSorter.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") + equal(sort("%YAML 1.2\n---\nb: 1\n---\nd: 1\nc: 2", yamlSorter), "%YAML 1.2\n---\nb: 1\n---\nc: 2\nd: 1") }) test("should keep the delimiters of a selection as they are", () => { @@ -138,6 +138,6 @@ suite("Test DocumentTransformer - injected settings", () => { const settings = new Settings() settings.schema = Settings.getJsYamlSchemaFromString("CLOUDFORMATION_SCHEMA") - equal(sortDocuments("b: !Ref x\na: 1", { customSort: 0, isSelection: false }, new YamlUtil(settings)), "---\na: 1\nb: !Ref x") + equal(sortDocuments("b: !Ref x\na: 1", { customSort: 0, isSelection: false }, new YamlSorter(settings)), "---\na: 1\nb: !Ref x") }) }) diff --git a/src/test/unit/core/file-sorter.test.ts b/src/test/unit/core/file-sorter.test.ts index 86e6176..5cb3861 100644 --- a/src/test/unit/core/file-sorter.test.ts +++ b/src/test/unit/core/file-sorter.test.ts @@ -3,7 +3,7 @@ import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "fs" import { tmpdir } from "os" import { join } from "path" -import { sortFile } from "../../../vscode/file-sorter" +import { sortFile } from "../../../core/file-sorter" suite("Test FileSorter - sortFile()", () => { diff --git a/src/test/unit/core/js-yaml-adapter.test.ts b/src/test/unit/core/js-yaml-adapter.test.ts index 47d3c23..5ba9371 100644 --- a/src/test/unit/core/js-yaml-adapter.test.ts +++ b/src/test/unit/core/js-yaml-adapter.test.ts @@ -8,7 +8,7 @@ import { JsYamlAdapter } from "../../../core/js-yaml-adapter" import { Settings } from "../../../core/settings" suite("Test JsYamlAdapter - validateYaml()", () => { - const jsyamladapter = new JsYamlAdapter(new Settings()) + const jsYamlAdapter = new JsYamlAdapter(new Settings()) test("when text is a valid yaml document should return true", () => { const actual = @@ -20,7 +20,7 @@ suite("Test JsYamlAdapter - validateYaml()", () => { " kitty:\n" + " age: 3" - equal(jsyamladapter.validateYaml(actual), true) + equal(jsYamlAdapter.validateYaml(actual), true) }) test("when text are two seperated valid yaml documents should return true", () => { @@ -34,13 +34,13 @@ suite("Test JsYamlAdapter - validateYaml()", () => { " kitty:\n" + " age: 3" - equal(jsyamladapter.validateYaml(actual), true) + equal(jsYamlAdapter.validateYaml(actual), true) }) test("when text is an invalid yaml document should throw YAMLException", () => { const actual = "network: ethernets:" - throws(() => jsyamladapter.validateYaml(actual), YAMLException) + throws(() => jsYamlAdapter.validateYaml(actual), YAMLException) }) test("when indentation is not correct should throw YAMLException", () => { const actual = @@ -48,7 +48,7 @@ suite("Test JsYamlAdapter - validateYaml()", () => { "bob\n" + " age:23" - throws(() => jsyamladapter.validateYaml(actual), YAMLException) + throws(() => jsYamlAdapter.validateYaml(actual), YAMLException) }) test("when text contains duplicate key should throw YAMLException", () => { const actual = @@ -58,39 +58,39 @@ suite("Test JsYamlAdapter - validateYaml()", () => { " bob:\n" + " age: 25" - throws(() => jsyamladapter.validateYaml(actual), YAMLException) + throws(() => jsYamlAdapter.validateYaml(actual), YAMLException) }) test("when text is a yaml document using CLOUDFORMATION_SCHEMA and schema is CORE_SCHEMA should throw YAMLException", () => { const actual = "RoleName: !Sub \"AdministratorAccess\"" - jsyamladapter.settings.getSchema = function () { + jsYamlAdapter.settings.getSchema = function () { return CORE_SCHEMA } - throws(() => jsyamladapter.validateYaml(actual), YAMLException) + throws(() => jsYamlAdapter.validateYaml(actual), YAMLException) }) test("when text is a yaml document using CLOUDFORMATION_SCHEMA and schema is CLOUDFORMATION_SCHEMA should return true", () => { const actual = "RoleName: !Sub \"AdministratorAccess\"" - jsyamladapter.settings.getSchema = function () { + jsYamlAdapter.settings.getSchema = function () { return CLOUDFORMATION_SCHEMA } - equal(jsyamladapter.validateYaml(actual), true) + equal(jsYamlAdapter.validateYaml(actual), true) }) test("when text is a yaml document using HOMEASSISTANT_SCHEMA and schema is CORE_SCHEMA should throw YAMLException", () => { const actual = "password: !env_var PASSWORD default_password" - jsyamladapter.settings.getSchema = function () { + jsYamlAdapter.settings.getSchema = function () { return CORE_SCHEMA } - throws(() => jsyamladapter.validateYaml(actual), YAMLException) + throws(() => jsYamlAdapter.validateYaml(actual), YAMLException) }) test("when text is a yaml document using HOMEASSISTANT_SCHEMA and schema is HOMEASSISTANT_SCHEMA should return true", () => { const actual = "password: !env_var PASSWORD default_password" - jsyamladapter.settings.getSchema = function () { + jsYamlAdapter.settings.getSchema = function () { return HOMEASSISTANT_SCHEMA } - equal(jsyamladapter.validateYaml(actual), true) + equal(jsYamlAdapter.validateYaml(actual), true) }) }) diff --git a/src/test/unit/core/processor/generic-processor.test.ts b/src/test/unit/core/processor/generic-processor.test.ts index ed26dd6..a2a9619 100644 --- a/src/test/unit/core/processor/generic-processor.test.ts +++ b/src/test/unit/core/processor/generic-processor.test.ts @@ -5,24 +5,24 @@ import { HelmProcessor } from "../../../../core/processor/helm-processor" suite("Test GenericProcessor - replaceSubstituteWithValue()", () => { test("GitHub issue #154: Ensure a substitue is surrounded with the same quotation marks", () => { const text = "TABLE_SOURCES: 'vscode-yaml-sort.helm.0 TABLE'" - const helmprocessor = new HelmProcessor(text) + const helmProcessor = new HelmProcessor(text) - helmprocessor.replaceSubstituteWithValue("vscode-yaml-sort.helm.0", "{{ LEVEL }}") + helmProcessor.replaceSubstituteWithValue("vscode-yaml-sort.helm.0", "{{ LEVEL }}") const expected = "TABLE_SOURCES: '{{ LEVEL }} TABLE'" - equal(helmprocessor.text, expected) + equal(helmProcessor.text, expected) }) test("GitHub issue #130: dollar signs in a value must not be interpreted as replacement patterns", () => { const text = "label: vscode-yaml-sort.helm.0" - const helmprocessor = new HelmProcessor(text) + const helmProcessor = new HelmProcessor(text) - helmprocessor.replaceSubstituteWithValue("vscode-yaml-sort.helm.0", "{{''$''}}") + helmProcessor.replaceSubstituteWithValue("vscode-yaml-sort.helm.0", "{{''$''}}") const expected = "label: {{''$''}}" - equal(helmprocessor.text, expected) + equal(helmProcessor.text, expected) }) }) @@ -30,13 +30,13 @@ suite("Test GenericProcessor - substitutes with more than ten matches", () => { test("postprocess restores every value at its own key when more than ten substitutes exist", () => { // vscode-yaml-sort.helm.1 must not match the prefix of vscode-yaml-sort.helm.10 or .11 const lines = Array.from({ length: 12 }, (_, i) => `key${i}: "{{ .Values.value${i} }}"`) - const helmprocessor = new HelmProcessor(lines.join("\n")) + const helmProcessor = new HelmProcessor(lines.join("\n")) - helmprocessor.preprocess() + helmProcessor.preprocess() // simulate sorting, which moves the higher substitutes in front of the lower ones - helmprocessor.text = helmprocessor.text.split("\n").reverse().join("\n") - helmprocessor.postprocess() + helmProcessor.text = helmProcessor.text.split("\n").reverse().join("\n") + helmProcessor.postprocess() - equal(helmprocessor.text, [...lines].reverse().join("\n")) + equal(helmProcessor.text, [...lines].reverse().join("\n")) }) }) diff --git a/src/test/unit/core/processor/helm-processor.test.ts b/src/test/unit/core/processor/helm-processor.test.ts index 7dbb3fb..c2fd266 100644 --- a/src/test/unit/core/processor/helm-processor.test.ts +++ b/src/test/unit/core/processor/helm-processor.test.ts @@ -12,36 +12,36 @@ suite("Test HelmProcessor - preprocess()", () => { "app: clientsso\n" + "name: {{ .Values.ui.environment.keycloakClientId }}\n" + "namespace: {{ required 'Keycloak namespace required' .Values.keycloak.namespace }}\n" - const helmprocessor = new HelmProcessor(text) + const helmProcessor = new HelmProcessor(text) - helmprocessor.preprocess() + helmProcessor.preprocess() - equal(helmprocessor.store.size, 2) - equal(helmprocessor.store.get("vscode-yaml-sort.helm.0"), "{{ .Values.ui.environment.keycloakClientId }}") - equal(helmprocessor.store.get("vscode-yaml-sort.helm.1"), "{{ required 'Keycloak namespace required' .Values.keycloak.namespace }}") + equal(helmProcessor.store.size, 2) + equal(helmProcessor.store.get("vscode-yaml-sort.helm.0"), "{{ .Values.ui.environment.keycloakClientId }}") + equal(helmProcessor.store.get("vscode-yaml-sort.helm.1"), "{{ required 'Keycloak namespace required' .Values.keycloak.namespace }}") }) }) suite("Test HelmProcessor - createSubstitute()", () => { test("should store each value under a consecutive substitute", () => { - const helmprocessor = new HelmProcessor("") + const helmProcessor = new HelmProcessor("") - equal(helmprocessor.createSubstitute("{{ .a }}"), "vscode-yaml-sort.helm.0") - equal(helmprocessor.createSubstitute("{{ .b }}"), "vscode-yaml-sort.helm.1") + equal(helmProcessor.createSubstitute("{{ .a }}"), "vscode-yaml-sort.helm.0") + equal(helmProcessor.createSubstitute("{{ .b }}"), "vscode-yaml-sort.helm.1") - equal(helmprocessor.store.size, 2) - equal(helmprocessor.store.get("vscode-yaml-sort.helm.0"), "{{ .a }}") - equal(helmprocessor.store.get("vscode-yaml-sort.helm.1"), "{{ .b }}") + equal(helmProcessor.store.size, 2) + equal(helmProcessor.store.get("vscode-yaml-sort.helm.0"), "{{ .a }}") + equal(helmProcessor.store.get("vscode-yaml-sort.helm.1"), "{{ .b }}") }) }) suite("Test HelmProcessor - preprocess() keeps surrounding text", () => { test("should keep trailing newline character", () => { - const helmprocessor = new HelmProcessor("foo: {{ .bar }}\n") + const helmProcessor = new HelmProcessor("foo: {{ .bar }}\n") - helmprocessor.preprocess() + helmProcessor.preprocess() - equal(helmprocessor.text, "foo: vscode-yaml-sort.helm.0\n") + equal(helmProcessor.text, "foo: vscode-yaml-sort.helm.0\n") }) }) @@ -55,15 +55,15 @@ suite("Test HelmProcessor - postprocess()", () => { "app: clientsso\n" + "name: {{ .Values.ui.environment.keycloakClientId }}\n" + "namespace: {{ required 'Keycloak namespace required' .Values.keycloak.namespace }}\n" - const helmprocessor = new HelmProcessor(text) + const helmProcessor = new HelmProcessor(text) - helmprocessor.preprocess() + helmProcessor.preprocess() - match(helmprocessor.text, /vscode-yaml-sort.helm/) + match(helmProcessor.text, /vscode-yaml-sort.helm/) - helmprocessor.postprocess() + helmProcessor.postprocess() - equal(helmprocessor.text, text) + equal(helmProcessor.text, text) }) }) @@ -73,51 +73,51 @@ suite("Test HelmProcessor - replaceSubstituteWithValue()", () => { "key1: 'vscode-yaml-sort.helm.0'\n" + "key2: \"vscode-yaml-sort.helm.1\"\n" + "key3: vscode-yaml-sort.helm.2" - const helmprocessor = new HelmProcessor(text) + const helmProcessor = new HelmProcessor(text) - helmprocessor.replaceSubstituteWithValue("vscode-yaml-sort.helm.0", "test0") - helmprocessor.replaceSubstituteWithValue("vscode-yaml-sort.helm.1", "test1") - helmprocessor.replaceSubstituteWithValue("vscode-yaml-sort.helm.2", "test2") + helmProcessor.replaceSubstituteWithValue("vscode-yaml-sort.helm.0", "test0") + helmProcessor.replaceSubstituteWithValue("vscode-yaml-sort.helm.1", "test1") + helmProcessor.replaceSubstituteWithValue("vscode-yaml-sort.helm.2", "test2") const expected = "key1: test0\n" + "key2: test1\n" + "key3: test2" - equal(helmprocessor.text, expected) + equal(helmProcessor.text, expected) }) }) suite("Test HelmProcessor - GitHub issue #130", () => { test("preprocess should keep the quotation marks of a quoted helm value in the store", () => { const text = "section:\n label: '{{''$''}}'" - const helmprocessor = new HelmProcessor(text) + const helmProcessor = new HelmProcessor(text) - helmprocessor.preprocess() + helmProcessor.preprocess() - equal(helmprocessor.store.size, 1) - equal(helmprocessor.store.get("vscode-yaml-sort.helm.0"), "'{{''$''}}'") - equal(helmprocessor.text, "section:\n label: vscode-yaml-sort.helm.0") + equal(helmProcessor.store.size, 1) + equal(helmProcessor.store.get("vscode-yaml-sort.helm.0"), "'{{''$''}}'") + equal(helmProcessor.text, "section:\n label: vscode-yaml-sort.helm.0") }) test("postprocess should restore a quoted helm value containing a dollar sign", () => { const text = "section:\n label: '{{''$''}}'" - const helmprocessor = new HelmProcessor(text) + const helmProcessor = new HelmProcessor(text) - helmprocessor.preprocess() - helmprocessor.postprocess() + helmProcessor.preprocess() + helmProcessor.postprocess() - equal(helmprocessor.text, text) + equal(helmProcessor.text, text) }) }) suite("Test HelmProcessor - GitHub issue #283", () => { test("should keep double quotes around a helm value", () => { const text = "foo: \"{{ .Fighters}}\"" - const helmprocessor = new HelmProcessor(text) + const helmProcessor = new HelmProcessor(text) - helmprocessor.preprocess() - helmprocessor.postprocess() + helmProcessor.preprocess() + helmProcessor.postprocess() - equal(helmprocessor.text, text) + equal(helmProcessor.text, text) }) }) diff --git a/src/test/unit/core/processor/processor-controller.test.ts b/src/test/unit/core/processor/processor-pipeline.test.ts similarity index 78% rename from src/test/unit/core/processor/processor-controller.test.ts rename to src/test/unit/core/processor/processor-pipeline.test.ts index c33c26f..117bff6 100644 --- a/src/test/unit/core/processor/processor-controller.test.ts +++ b/src/test/unit/core/processor/processor-pipeline.test.ts @@ -1,14 +1,14 @@ import { equal, notEqual } from "assert" -import { ProcessorController } from "../../../../core/processor/processor-controller" +import { ProcessorPipeline } from "../../../../core/processor/processor-pipeline" -suite("Test ProcessorController - preprocess() & postprocess()", () => { +suite("Test ProcessorPipeline - preprocess() & postprocess()", () => { test("text should be the same after pre- and postprocessing", () => { const text = "foo: {{ .Value }}\n" + "# comment\n" + "bar: baz" - const processor = new ProcessorController(text) + const processor = new ProcessorPipeline(text) processor.preprocess() @@ -25,7 +25,7 @@ suite("Test ProcessorController - preprocess() & postprocess()", () => { test("test setting useArrayProcessor = true", () => { const text = "test: [ \"CMD\", \"pg_isready\", \"-q\", \"-d\", \"DB_NAME\", \"-U\", \"DB_USER\" ]" - const processor = new ProcessorController(text) + const processor = new ProcessorPipeline(text) processor.preprocess() @@ -33,7 +33,7 @@ suite("Test ProcessorController - preprocess() & postprocess()", () => { }) test("test setting useArrayProcessor = false", () => { let text = "test: [ \"CMD\", \"pg_isready\", \"-q\", \"-d\", \"DB_NAME\", \"-U\", \"DB_USER\" ]" - const processor = new ProcessorController(text) + const processor = new ProcessorPipeline(text) processor.settings.useArrayProcessor = false processor.preprocess() @@ -52,7 +52,7 @@ suite("Test ProcessorController - preprocess() & postprocess()", () => { const text = "# comment\n" + "bar: baz" - const processor = new ProcessorController(text) + const processor = new ProcessorPipeline(text) processor.preprocess() @@ -62,7 +62,7 @@ suite("Test ProcessorController - preprocess() & postprocess()", () => { const text = "# comment\n" + "bar: baz" - const processor = new ProcessorController(text) + const processor = new ProcessorPipeline(text) processor.settings.useCommentProcessor = false processor.preprocess() @@ -79,20 +79,20 @@ suite("Test ProcessorController - preprocess() & postprocess()", () => { const text = "foo: {{ .Value }}\n" + "bar: baz" - const processor = new ProcessorController(text) + const processor = new ProcessorPipeline(text) processor.preprocess() - equal(processor.helmprocessor.store.size, 1) + equal(processor.helmProcessor.store.size, 1) }) test("test setting useHelmProcessor = false", () => { let text = "foo: {{ .Value }}" - const processor = new ProcessorController(text) + const processor = new ProcessorPipeline(text) processor.settings.useHelmProcessor = false processor.preprocess() - equal(processor.helmprocessor, undefined) + equal(processor.helmProcessor, undefined) text = "foo: vscode-yaml-sort.helm.0" processor.text = text @@ -106,16 +106,16 @@ suite("Test ProcessorController - preprocess() & postprocess()", () => { const text = "foo: {{ .Value }}\n" + "bar: baz" - const processor = new ProcessorController(text) + const processor = new ProcessorPipeline(text) processor.preprocess() - equal(processor.helmprocessor.store.size, 1) + equal(processor.helmProcessor.store.size, 1) }) test("test setting useOctalProcessor = false", () => { let text = "foo: 0644" - const processor = new ProcessorController(text) + const processor = new ProcessorPipeline(text) processor.settings.useOctalProcessor = false processor.preprocess() @@ -138,7 +138,7 @@ suite("Test ProcessorController - preprocess() & postprocess()", () => { " some command\n" + " # comment 2\n" + " some other command" - const processor = new ProcessorController(text) + const processor = new ProcessorPipeline(text) processor.preprocess() processor.postprocess() diff --git a/src/test/unit/core/sort-util.test.ts b/src/test/unit/core/sort-order.test.ts similarity index 92% rename from src/test/unit/core/sort-util.test.ts rename to src/test/unit/core/sort-order.test.ts index bdb140b..8a1d290 100644 --- a/src/test/unit/core/sort-util.test.ts +++ b/src/test/unit/core/sort-order.test.ts @@ -1,20 +1,20 @@ import { deepEqual, equal } from "assert" import { Settings } from "../../../core/settings" -import { SortUtil, sortArraysRecursively } from "../../../core/sort-util" +import { SortOrder, sortArraysRecursively } from "../../../core/sort-order" suite("Test Sort - localeSort()", () => { test("when `locale` is `sv` and a is `รค` and b is `z` should return 1", () => { const settings = new Settings() settings.locale = "sv" - const sort = new SortUtil(settings) + const sort = new SortOrder(settings) equal(sort.localeSort("รค", "z"), 1) }) test("when `locale` is `en` and a is `รค` and b is `z` should return -1", () => { const settings = new Settings() settings.locale = "en" - const sort = new SortUtil(settings) + const sort = new SortOrder(settings) equal(sort.localeSort("รค", "z"), -1) }) }) @@ -24,7 +24,7 @@ suite("Test Sort - customSort()", () => { settings.getCustomSortKeywords = function () { return ["kind", "data"] } - const sort = new SortUtil(settings) + const sort = new SortOrder(settings) sort.custom = 1 test("when `custom` is `1` and keywords are `['kind', 'data']` and a is `data` and b is `kind` should return 1", () => { diff --git a/src/test/unit/core/file-util.test.ts b/src/test/unit/core/yaml-file-finder.test.ts similarity index 75% rename from src/test/unit/core/file-util.test.ts rename to src/test/unit/core/yaml-file-finder.test.ts index 7844cb9..e805e60 100644 --- a/src/test/unit/core/file-util.test.ts +++ b/src/test/unit/core/yaml-file-finder.test.ts @@ -1,14 +1,14 @@ import { deepEqual } from "assert" -import { FileUtil } from "../../../core/file-util" import { Settings } from "../../../core/settings" +import { YamlFileFinder } from "../../../core/yaml-file-finder" -suite("Test FileUtil - getFiles()", () => { +suite("Test YamlFileFinder - getFiles()", () => { const settings = new Settings() settings.getExtensions = function () { return ["yaml", "yml", "customyaml"] } - const fileutil = new FileUtil(settings) + const fileFinder = new YamlFileFinder(settings) test("should return all files", () => { const expected = [ @@ -17,7 +17,7 @@ suite("Test FileUtil - getFiles()", () => { "src/test/files/file-finder/bar.yml", "src/test/files/file-finder/subfolder/.customyaml" ] - deepEqual(fileutil.getFiles("./src/test/files/file-finder"), expected) + deepEqual(fileFinder.getFiles("./src/test/files/file-finder"), expected) }) test("should list all files with extension *.yaml or *.yml in a directory and all its subdirectories", () => { @@ -29,6 +29,6 @@ suite("Test FileUtil - getFiles()", () => { "src/test/files/getYamlFilesInDirectory/folder1/file.yaml", "src/test/files/getYamlFilesInDirectory/file.yml" ] - deepEqual(fileutil.getFiles("./src/test/files/getYamlFilesInDirectory"), expected) + deepEqual(fileFinder.getFiles("./src/test/files/getYamlFilesInDirectory"), expected) }) }) diff --git a/src/test/unit/core/yaml-util.test.ts b/src/test/unit/core/yaml-sorter.test.ts similarity index 69% rename from src/test/unit/core/yaml-util.test.ts rename to src/test/unit/core/yaml-sorter.test.ts index be5c9fe..22a9405 100644 --- a/src/test/unit/core/yaml-util.test.ts +++ b/src/test/unit/core/yaml-sorter.test.ts @@ -4,30 +4,30 @@ import { CLOUDFORMATION_SCHEMA } from "cloudformation-js-yaml-schema" import { CORE_SCHEMA } from "js-yaml" import { Settings } from "../../../core/settings" -import { YamlUtil } from "../../../core/yaml-util" +import { YamlSorter } from "../../../core/yaml-sorter" suite("Test sortArrays", () => { - const yamlutil = new YamlUtil() + const yamlSorter = new YamlSorter() const text = "array:\n" + " - python\n" + " - perl" test("when sortArrays is set to false, should not sort arrays", () => { - const actual = yamlutil.sortYaml(text) + const actual = yamlSorter.sortYaml(text) equal(actual, text) }) test("when sortArrays is set to true, should sort arrays", () => { - yamlutil.settings.sortArrays = true - const actual = yamlutil.sortYaml("foo") + yamlSorter.settings.sortArrays = true + const actual = yamlSorter.sortYaml("foo") const expected = "foo" equal(actual, expected) }) test("when sortArrays is set to true, should sort arrays", () => { - yamlutil.settings.sortArrays = true - const actual = yamlutil.sortYaml(text) + yamlSorter.settings.sortArrays = true + const actual = yamlSorter.sortYaml(text) const expected = "array:\n" + " - perl\n" + @@ -36,13 +36,13 @@ suite("Test sortArrays", () => { }) test("when sortArrays is set to true, should sort nested arrays", () => { - yamlutil.settings.sortArrays = true + yamlSorter.settings.sortArrays = true const text = "foo:\n" + " array:\n" + " - python\n" + " - perl" - const actual = yamlutil.sortYaml(text) + const actual = yamlSorter.sortYaml(text) const expected = "foo:\n" + " array:\n" + @@ -52,16 +52,16 @@ suite("Test sortArrays", () => { }) }) -suite("Test YamlUtil - setLeadingDashes()", () => { +suite("Test YamlSorter - setLeadingDashes()", () => { test("when setting useLeadingDashes is `true` should prepend `---\n` to text", () => { - const yamlutil = new YamlUtil() + const yamlSorter = new YamlSorter() const text = "lorem ipsum" - yamlutil.settings.useLeadingDashes = false - equal(yamlutil.setLeadingDashes(text), text) + yamlSorter.settings.useLeadingDashes = false + equal(yamlSorter.setLeadingDashes(text), text) - yamlutil.settings.useLeadingDashes = true + yamlSorter.settings.useLeadingDashes = true const expected = "---\nlorem ipsum" - equal(yamlutil.setLeadingDashes(text), expected) + equal(yamlSorter.setLeadingDashes(text), expected) }) }) @@ -91,8 +91,8 @@ suite("Test dumpYaml", () => { const settings = new Settings() settings.useCustomSortRecursively = true - const yamlutil = new YamlUtil(settings) - equal(yamlutil.sortYaml(actual, 1), expected) + const yamlSorter = new YamlSorter(settings) + equal(yamlSorter.sortYaml(actual, 1), expected) }) test("when locale is `en` should sort character `รค` over `z`", () => { @@ -107,8 +107,8 @@ suite("Test dumpYaml", () => { const settings = new Settings() settings.locale = "en" settings.useCustomSortRecursively = true - const yamlutil = new YamlUtil(settings) - equal(yamlutil.sortYaml(actual, 1), expected) + const yamlSorter = new YamlSorter(settings) + equal(yamlSorter.sortYaml(actual, 1), expected) }) test("when locale is `sv` should sort character `z` over `รค`", () => { @@ -122,8 +122,8 @@ suite("Test dumpYaml", () => { const settings = new Settings() settings.locale = "sv" - const yamlutil = new YamlUtil(settings) - equal(yamlutil.sortYaml(actual, 1), expected) + const yamlSorter = new YamlSorter(settings) + equal(yamlSorter.sortYaml(actual, 1), expected) }) test("should ignore case when sorting", () => { @@ -137,8 +137,8 @@ suite("Test dumpYaml", () => { const settings = new Settings() settings.useCustomSortRecursively = true - const yamlutil = new YamlUtil(settings) - equal(yamlutil.sortYaml(actual, 1), expected) + const yamlSorter = new YamlSorter(settings) + equal(yamlSorter.sortYaml(actual, 1), expected) }) }) @@ -169,8 +169,8 @@ suite("Test dumpYaml", () => { const settings = new Settings() settings.useCustomSortRecursively = true - const yamlutil = new YamlUtil(settings) - equal(yamlutil.sortYaml(actual, 1), expected) + const yamlSorter = new YamlSorter(settings) + equal(yamlSorter.sortYaml(actual, 1), expected) }) test("should sort with by locale behaviour", () => { const actual = @@ -183,15 +183,15 @@ suite("Test dumpYaml", () => { const settings = new Settings() settings.useCustomSortRecursively = true - const yamlutil = new YamlUtil(settings) - equal(yamlutil.sortYaml(actual, 1), expected) + const yamlSorter = new YamlSorter(settings) + equal(yamlSorter.sortYaml(actual, 1), expected) expected = "z: value\n" + "รค: value" - yamlutil.settings.locale = "sv" - equal(yamlutil.sortYaml(actual, 1), expected) + yamlSorter.settings.locale = "sv" + equal(yamlSorter.sortYaml(actual, 1), expected) }) test("should ignore case when sorting", () => { const actual = @@ -204,8 +204,8 @@ suite("Test dumpYaml", () => { const settings = new Settings() settings.useCustomSortRecursively = true - const yamlutil = new YamlUtil(settings) - equal(yamlutil.sortYaml(actual, 1), expected) + const yamlSorter = new YamlSorter(settings) + equal(yamlSorter.sortYaml(actual, 1), expected) }) }) @@ -220,8 +220,8 @@ suite("Test sortYaml", () => { "spec: spec\n" + "data: data\n" - const yamlutil = new YamlUtil() - equal(yamlutil.sortYaml(actual, 1), expected) + const yamlSorter = new YamlSorter() + equal(yamlSorter.sortYaml(actual, 1), expected) actual = "data: data\n" + @@ -233,7 +233,7 @@ suite("Test sortYaml", () => { " - aa: b\n" + "data: data\n" - equal(yamlutil.sortYaml(actual, 1), expected) + equal(yamlSorter.sortYaml(actual, 1), expected) actual = "data:\n" + @@ -253,7 +253,7 @@ suite("Test sortYaml", () => { " skills:\n" + " - pascal\n" - equal(yamlutil.sortYaml(actual, 1), expected) + equal(yamlSorter.sortYaml(actual, 1), expected) actual = "data: data\n" + @@ -267,7 +267,7 @@ suite("Test sortYaml", () => { " - b\n" + "data: data\n" - equal(yamlutil.sortYaml(actual, 1), expected) + equal(yamlSorter.sortYaml(actual, 1), expected) }) test("should wrap words after 500 characters (`vscode-yaml-sort.lineWidth`)", () => { @@ -289,14 +289,14 @@ suite("Test sortYaml", () => { "aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo\n" + " dolores et e" - const yamlutil = new YamlUtil() - equal(yamlutil.sortYaml(actual, 1), expected) + const yamlSorter = new YamlSorter() + equal(yamlSorter.sortYaml(actual, 1), expected) }) test("should not format a date in CORE_SCHEMA", () => { const actual = "AWSTemplateFormatVersion: 2010-09-09" let expected = "AWSTemplateFormatVersion: 2010-09-09T00:00:00.000Z" - let yamlutil = new YamlUtil() - equal(yamlutil.sortYaml(actual, 0), expected) + let yamlSorter = new YamlSorter() + equal(yamlSorter.sortYaml(actual, 0), expected) expected = actual @@ -304,9 +304,9 @@ suite("Test sortYaml", () => { settings.getSchema = function () { return CORE_SCHEMA } - yamlutil = new YamlUtil(settings) + yamlSorter = new YamlSorter(settings) - equal(yamlutil.sortYaml(actual, 0), expected) + equal(yamlSorter.sortYaml(actual, 0), expected) }) test("should sort a yaml with CLOUDFORMATION_SCHEMA", () => { const actual = @@ -329,8 +329,8 @@ suite("Test sortYaml", () => { settings.getQuotingType = function () { return "\"" } - const yamlutil = new YamlUtil(settings) - equal(yamlutil.sortYaml(actual, 0), expected) + const yamlSorter = new YamlSorter(settings) + equal(yamlSorter.sortYaml(actual, 0), expected) }) test("compatibility with older yaml versions should be configurable", () => { const actual = @@ -348,15 +348,15 @@ suite("Test sortYaml", () => { settings.getQuotingType = function () { return "\"" } - const yamlutil = new YamlUtil(settings) - equal(yamlutil.sortYaml(actual, 0), expected) + const yamlSorter = new YamlSorter(settings) + equal(yamlSorter.sortYaml(actual, 0), expected) expected = "key:\n" + " off: egg\n" + " on: foo" - yamlutil.settings.noCompatMode = true - equal(yamlutil.sortYaml(actual, 0), expected) + yamlSorter.settings.noCompatMode = true + equal(yamlSorter.sortYaml(actual, 0), expected) }) }) @@ -382,8 +382,8 @@ suite("Test formatYaml", () => { const settings = new Settings() settings.useLeadingDashes = false - let yamlutil = new YamlUtil(settings) - equal(yamlutil.formatYaml(actual, false), expected) + let yamlSorter = new YamlSorter(settings) + equal(yamlSorter.formatYaml(actual, false), expected) expected = "---\n" + @@ -395,36 +395,36 @@ suite("Test formatYaml", () => { " kitty:\n" + " age: 3" - yamlutil = new YamlUtil() - equal(yamlutil.formatYaml(actual, true), expected) + yamlSorter = new YamlSorter() + equal(yamlSorter.formatYaml(actual, true), expected) }) test("should throw on invalid yaml", () => { - const yamlutil = new YamlUtil() - throws(() => yamlutil.formatYaml("key: 1\nkey: 1", true), /duplicated mapping key/) + const yamlSorter = new YamlSorter() + throws(() => yamlSorter.formatYaml("key: 1\nkey: 1", true), /duplicated mapping key/) }) }) -suite("Test YamlUtil - getNumberOfLeadingSpaces()", () => { +suite("Test YamlSorter - getNumberOfLeadingSpaces()", () => { test("should return the number of leading spaces of 'text'", () => { - equal(YamlUtil.getNumberOfLeadingSpaces(" foo"), 2) - equal(YamlUtil.getNumberOfLeadingSpaces("foo"), 0) - equal(YamlUtil.getNumberOfLeadingSpaces("foo "), 0) + equal(YamlSorter.getNumberOfLeadingSpaces(" foo"), 2) + equal(YamlSorter.getNumberOfLeadingSpaces("foo"), 0) + equal(YamlSorter.getNumberOfLeadingSpaces("foo "), 0) }) }) -suite("Test YamlUtil - isValueMultiline", () => { +suite("Test YamlSorter - isValueMultiline", () => { test("when text starts with '-' should return true", () => { const text = "- foo: bar\n" + " bar: baz" - equal(YamlUtil.isValueMultiline(text), true) + equal(YamlSorter.isValueMultiline(text), true) }) }) -suite("Test YamlUtil - transformMultilineValue()", () => { - const yamlutil = new YamlUtil() - yamlutil.settings.indent = 2 +suite("Test YamlSorter - transformMultilineValue()", () => { + const yamlSorter = new YamlSorter() + yamlSorter.settings.indent = 2 test("Should prepend spaces on each line", () => { const text = "- foo: bar\n" + @@ -432,7 +432,7 @@ suite("Test YamlUtil - transformMultilineValue()", () => { const expected = " - foo: bar\n" + " bar: baz" - equal(yamlutil.transformMultilineValue(text), expected) + equal(yamlSorter.transformMultilineValue(text), expected) }) test("When text ends with newline should remove trailing indent", () => { const text = @@ -441,31 +441,31 @@ suite("Test YamlUtil - transformMultilineValue()", () => { const expected = " - foo: bar\n" + " bar: baz\n" - equal(yamlutil.transformMultilineValue(text), expected) + equal(yamlSorter.transformMultilineValue(text), expected) }) }) /* */ -suite("Test YamlUtil - sortYaml() with quoted helm values", () => { +suite("Test YamlSorter - sortYaml() with quoted helm values", () => { test("GitHub issue #130: should not mangle a quoted helm value containing a dollar sign", () => { - const yamlutil = new YamlUtil() + const yamlSorter = new YamlSorter() const text = "section:\n label: '{{''$''}}'" - equal(yamlutil.sortYaml(text), text) + equal(yamlSorter.sortYaml(text), text) }) }) -suite("Test YamlUtil - sortYaml() with double-quoted helm values", () => { +suite("Test YamlSorter - sortYaml() with double-quoted helm values", () => { test("GitHub issue #283: should keep double quotes around a helm value", () => { - const yamlutil = new YamlUtil() + const yamlSorter = new YamlSorter() const text = "foo: \"{{ .Fighters}}\"" - equal(yamlutil.sortYaml(text), text) + equal(yamlSorter.sortYaml(text), text) }) }) -suite("Test YamlUtil - sortYaml() with kustomize patches", () => { +suite("Test YamlSorter - sortYaml() with kustomize patches", () => { test("GitHub issue #188: should keep a block scalar containing colons intact while sorting", () => { - const util = new YamlUtil() + const util = new YamlSorter() const kustomize = "apiVersion: kustomize.config.k8s.io/v1beta1\nkind: Kustomization\nnamespace: monitor\nresources:\n - bundle.yaml\n\npatches:\n - target:\n kind: ClusterRoleBinding\n name: prometheus-operator\n patch: |-\n - op: replace\n path: /subjects/0/namespace\n value: monitor" const sorted = util.sortYaml(kustomize) @@ -474,23 +474,23 @@ suite("Test YamlUtil - sortYaml() with kustomize patches", () => { }) }) -suite("Test YamlUtil - sortYaml() with inline comments", () => { +suite("Test YamlSorter - sortYaml() with inline comments", () => { test("GitHub issue #222: should keep an inline comment while sorting", () => { - const instance = new YamlUtil() + const instance = new YamlSorter() const game = "game:\n core:\n serviceRpcTimeoutMillis: 6900000 # Extra-long for debugging locally" equal(instance.sortYaml(game), game) }) }) -suite("Test YamlUtil - sortYaml() with docker-compose files", () => { +suite("Test YamlSorter - sortYaml() with docker-compose files", () => { test("GitHub issue #221: should keep a comment above a service while sorting with forceQuotes", () => { const composeSettings = new Settings() composeSettings.forceQuotes = true composeSettings.getQuotingType = function () { return "\"" } - const composeUtil = new YamlUtil(composeSettings) + const composeUtil = new YamlSorter(composeSettings) const composeYaml = "services:\n" + " # https://hub.docker.com/_/mysql\n" + @@ -504,68 +504,68 @@ suite("Test YamlUtil - sortYaml() with docker-compose files", () => { }) }) -suite("Test YamlUtil - sortYaml() keeps substituted values intact", () => { - const yamlutil = new YamlUtil() +suite("Test YamlSorter - sortYaml() keeps substituted values intact", () => { + const yamlSorter = new YamlSorter() test("should keep more than ten helm values at their keys", () => { const lines = Array.from({ length: 12 }, (_, i) => `key${String(i).padStart(2, "0")}: "{{ .Values.value${i} }}"`) - equal(yamlutil.sortYaml([...lines].reverse().join("\n")), lines.join("\n")) + equal(yamlSorter.sortYaml([...lines].reverse().join("\n")), lines.join("\n")) }) test("should keep an octal value when the same digits appear quoted earlier", () => { const text = "a: '0755'\nz: 0755" - equal(yamlutil.sortYaml(text), text) + equal(yamlSorter.sortYaml(text), text) }) }) -suite("Test YamlUtil - sortYaml() with custom sort and scalar values", () => { - const yamlutil = new YamlUtil() +suite("Test YamlSorter - sortYaml() with custom sort and scalar values", () => { + const yamlSorter = new YamlSorter() test("should keep number and boolean values of custom sort keywords", () => { - equal(yamlutil.sortYaml("spec: x\nkind: true\napiVersion: 3", 1), "apiVersion: 3\nkind: true\nspec: x\n") + equal(yamlSorter.sortYaml("spec: x\nkind: true\napiVersion: 3", 1), "apiVersion: 3\nkind: true\nspec: x\n") }) test("should keep an empty mapping as value of a custom sort keyword", () => { - equal(yamlutil.sortYaml("spec: x\nmetadata: {}", 1), "metadata: {}\nspec: x\n") + equal(yamlSorter.sortYaml("spec: x\nmetadata: {}", 1), "metadata: {}\nspec: x\n") }) test("should order custom sort keywords even if their values are falsy", () => { const text = "spec: x\nmetadata: null\nkind: false\napiVersion: 0" - equal(yamlutil.sortYaml(text, 1), "apiVersion: 0\nkind: false\nmetadata: null\nspec: x\n") + equal(yamlSorter.sortYaml(text, 1), "apiVersion: 0\nkind: false\nmetadata: null\nspec: x\n") }) test("should not add a value to a document containing only a comment", () => { - equal(yamlutil.sortYaml("# only a comment"), "\n# only a comment") + equal(yamlSorter.sortYaml("# only a comment"), "\n# only a comment") }) test("should keep a document containing only a scalar", () => { - equal(yamlutil.sortYaml("42"), "42") + equal(yamlSorter.sortYaml("42"), "42") }) }) -suite("Test YamlUtil - sortYaml() with sortOrderReverse", () => { +suite("Test YamlSorter - sortYaml() with sortOrderReverse", () => { const settings = new Settings() settings.sortOrderReverse = true - const yamlutil = new YamlUtil(settings) + const yamlSorter = new YamlSorter(settings) test("should sort keys in reverse order on every level", () => { - equal(yamlutil.sortYaml("a: 1\nc:\n x: 1\n z: 2\nb: 2"), "c:\n z: 2\n x: 1\nb: 2\na: 1") + equal(yamlSorter.sortYaml("a: 1\nc:\n x: 1\n z: 2\nb: 2"), "c:\n z: 2\n x: 1\nb: 2\na: 1") }) test("should sort the remaining keys of a custom sort in reverse order", () => { - equal(yamlutil.sortYaml("a: 1\nkind: x\nb: 2", 1), "kind: x\nb: 2\na: 1") + equal(yamlSorter.sortYaml("a: 1\nkind: x\nb: 2", 1), "kind: x\nb: 2\na: 1") }) }) -suite("Test YamlUtil - injected settings reach every processing step", () => { +suite("Test YamlSorter - injected settings reach every processing step", () => { test("should use emptyLinesUntilLevel of the injected settings", () => { const settings = new Settings() settings.emptyLinesUntilLevel = 1 - equal(new YamlUtil(settings).sortYaml("b: 1\na: 2"), "a: 2\n\nb: 1") + equal(new YamlSorter(settings).sortYaml("b: 1\na: 2"), "a: 2\n\nb: 1") }) test("should use the processor switches of the injected settings", () => { @@ -573,33 +573,33 @@ suite("Test YamlUtil - injected settings reach every processing step", () => { settings.useCommentProcessor = false // without the comment processor js-yaml drops the comment - equal(new YamlUtil(settings).sortYaml("b: 1\n# comment\na: 2"), "a: 2\nb: 1") + equal(new YamlSorter(settings).sortYaml("b: 1\n# comment\na: 2"), "a: 2\nb: 1") }) test("should format with the indentation of the injected settings", () => { const settings = new Settings() settings.indent = 4 - equal(new YamlUtil(settings).formatYaml("a:\n b: 1", false), "a:\n b: 1") + equal(new YamlSorter(settings).formatYaml("a:\n b: 1", false), "a:\n b: 1") }) }) -suite("Test YamlUtil - sortYaml() with custom sort keeps the document intact", () => { - const yamlutil = new YamlUtil() +suite("Test YamlSorter - sortYaml() with custom sort keeps the document intact", () => { + const yamlSorter = new YamlSorter() test("should put the keywords in their order before the remaining keys", () => { - equal(yamlutil.sortYaml("data: 1\nkind: x\nb:\n c: 1", 1), "kind: x\ndata: 1\nb:\n c: 1") + equal(yamlSorter.sortYaml("data: 1\nkind: x\nb:\n c: 1", 1), "kind: x\ndata: 1\nb:\n c: 1") }) test("should keep a document that only contains keywords", () => { - equal(yamlutil.sortYaml("kind: x", 1), "kind: x\n") + equal(yamlSorter.sortYaml("kind: x", 1), "kind: x\n") }) test("should leave a list document unchanged", () => { - equal(yamlutil.sortYaml("- kind: x\n- a: 1", 1), "- kind: x\n- a: 1") + equal(yamlSorter.sortYaml("- kind: x\n- a: 1", 1), "- kind: x\n- a: 1") }) test("should leave a scalar document unchanged", () => { - equal(yamlutil.sortYaml("42", 1), "42") + equal(yamlSorter.sortYaml("42", 1), "42") }) }) diff --git a/src/vscode/directory-sorter.ts b/src/vscode/directory-sorter.ts new file mode 100644 index 0000000..d0b0c3a --- /dev/null +++ b/src/vscode/directory-sorter.ts @@ -0,0 +1,29 @@ +import { Uri } from "vscode" + +import { readSettings } from "./configuration" +import { ErrorReporter } from "./error-reporter" +import { Severity, VsCodeAdapter } from "./vs-code-adapter" +import { sortFile } from "../core/file-sorter" +import { YamlFileFinder } from "../core/yaml-file-finder" +import { YamlSorter } from "../core/yaml-sorter" + +/** + * 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 errorReporter = new ErrorReporter() + if (!uri) { + new VsCodeAdapter().showMessage(Severity.ERROR, "Please run this command from the context menu of a folder in the explorer.") + return + } + const settingsFor = (resource: Uri) => readSettings({ uri: resource, languageId: "yaml" }) + for (const file of new YamlFileFinder(settingsFor(uri)).getFiles(uri.fsPath)) { + try { + sortFile(file, new YamlSorter(settingsFor(Uri.file(file)))) + } catch (error) { + errorReporter.handleError(error) + } + } +} diff --git a/src/vscode/controller.ts b/src/vscode/editor-commands.ts similarity index 66% rename from src/vscode/controller.ts rename to src/vscode/editor-commands.ts index 47bd9b3..c66c643 100644 --- a/src/vscode/controller.ts +++ b/src/vscode/editor-commands.ts @@ -1,20 +1,20 @@ import { TextEdit, TextEditor, window } from "vscode" import { readSettings } from "./configuration" -import { ErrorUtil } from "./error-util" +import { ErrorReporter } from "./error-reporter" import { Severity, VsCodeAdapter } from "./vs-code-adapter" import { formatDocuments, sortDocuments } from "../core/document-transformer" import { JsYamlAdapter } from "../core/js-yaml-adapter" import { Settings } from "../core/settings" -import { YamlUtil } from "../core/yaml-util" +import { YamlSorter } from "../core/yaml-sorter" -export class Controller { +export class EditorCommands { editor: TextEditor settings: Settings - yamlutil: YamlUtil - jsyamladapter: JsYamlAdapter - vscodeadapter: VsCodeAdapter - errorutil = new ErrorUtil() + yamlSorter: YamlSorter + jsYamlAdapter: JsYamlAdapter + vsCodeAdapter: VsCodeAdapter + errorReporter = new ErrorReporter() constructor() { if (!window.activeTextEditor) { @@ -23,19 +23,19 @@ export class Controller { this.editor = window.activeTextEditor // one snapshot of the settings that apply to the edited document this.settings = readSettings(this.editor.document) - this.yamlutil = new YamlUtil(this.settings) - this.jsyamladapter = new JsYamlAdapter(this.settings) - this.vscodeadapter = new VsCodeAdapter(this.settings) + this.yamlSorter = new YamlSorter(this.settings) + this.jsYamlAdapter = new JsYamlAdapter(this.settings) + this.vsCodeAdapter = new VsCodeAdapter(this.settings) } validateYamlWrapper(): boolean { const text = VsCodeAdapter.getActiveDocument(this.editor) try { - this.jsyamladapter.validateYaml(text) - this.vscodeadapter.showMessage(Severity.INFO, "YAML is valid.") + this.jsYamlAdapter.validateYaml(text) + this.vsCodeAdapter.showMessage(Severity.INFO, "YAML is valid.") return true } catch (error) { - this.errorutil.handleError(error) + this.errorReporter.handleError(error) } return false } @@ -43,14 +43,14 @@ export class Controller { formatYamlWrapper(): TextEdit[] { let newText: string try { - newText = formatDocuments(VsCodeAdapter.getActiveDocument(this.editor), this.yamlutil) + newText = formatDocuments(VsCodeAdapter.getActiveDocument(this.editor), this.yamlSorter) } catch (error) { - this.errorutil.handleError(error) + this.errorReporter.handleError(error) return [] } const edits = this.applyEdits(newText) // only the command reports success, the formatter stays silent (see #65) - this.vscodeadapter.showMessage(Severity.INFO, "Yaml formatted successfully") + this.vsCodeAdapter.showMessage(Severity.INFO, "Yaml formatted successfully") return edits } @@ -64,13 +64,13 @@ export class Controller { const text = VsCodeAdapter.getText(this.editor, VsCodeAdapter.getRange(this.editor)) let newText: string try { - newText = sortDocuments(text, { customSort, isSelection: !this.editor.selection.isEmpty }, this.yamlutil) + newText = sortDocuments(text, { customSort, isSelection: !this.editor.selection.isEmpty }, this.yamlSorter) } catch (error) { - this.errorutil.handleError(error) + this.errorReporter.handleError(error) return [] } const edits = this.applyEdits(newText) - this.vscodeadapter.showMessage(Severity.INFO, "Keys resorted successfully") + this.vsCodeAdapter.showMessage(Severity.INFO, "Keys resorted successfully") return edits } } diff --git a/src/vscode/error-util.ts b/src/vscode/error-reporter.ts similarity index 52% rename from src/vscode/error-util.ts rename to src/vscode/error-reporter.ts index cc15435..7409189 100644 --- a/src/vscode/error-util.ts +++ b/src/vscode/error-reporter.ts @@ -1,11 +1,11 @@ import { Severity, VsCodeAdapter } from "./vs-code-adapter" -export class ErrorUtil { - vscodeadapter = new VsCodeAdapter() +export class ErrorReporter { + vsCodeAdapter = new VsCodeAdapter() handleError(error: unknown) { if (error instanceof Error) { - this.vscodeadapter.showMessage(Severity.ERROR, error.message) + this.vsCodeAdapter.showMessage(Severity.ERROR, error.message) } } } \ No newline at end of file diff --git a/src/vscode/file-sorter.ts b/src/vscode/file-sorter.ts deleted file mode 100644 index b5dea01..0000000 --- a/src/vscode/file-sorter.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { readFileSync, writeFileSync } from "fs" - -import { Uri } from "vscode" - -import { readSettings } from "./configuration" -import { ErrorUtil } from "./error-util" -import { Severity, VsCodeAdapter } from "./vs-code-adapter" -import { YamlOperations, sortDocuments } from "../core/document-transformer" -import { FileUtil } from "../core/file-util" -import { YamlUtil } from "../core/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 (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 - } - const settingsFor = (resource: Uri) => readSettings({ uri: resource, languageId: "yaml" }) - for (const file of new FileUtil(settingsFor(uri)).getFiles(uri.fsPath)) { - try { - sortFile(file, new YamlUtil(settingsFor(Uri.file(file)))) - } catch (error) { - errorutil.handleError(error) - } - } -} diff --git a/src/vscode/formatting-provider.ts b/src/vscode/formatting-provider.ts index e4af24a..170def6 100644 --- a/src/vscode/formatting-provider.ts +++ b/src/vscode/formatting-provider.ts @@ -1,9 +1,9 @@ import { DocumentFormattingEditProvider, Range, TextDocument, TextEdit } from "vscode" import { readSettings } from "./configuration" -import { ErrorUtil } from "./error-util" +import { ErrorReporter } from "./error-reporter" import { formatDocuments, sortDocuments } from "../core/document-transformer" -import { YamlUtil } from "../core/yaml-util" +import { YamlSorter } from "../core/yaml-sorter" /** * Formats (or, depending on `sortOnSave`, sorts) a whole yaml document. @@ -12,7 +12,7 @@ import { YamlUtil } from "../core/yaml-util" * so this provider must never apply them on its own (see #180). */ export class YamlFormattingProvider implements DocumentFormattingEditProvider { - errorutil = new ErrorUtil() + errorReporter = new ErrorReporter() /** * @param document The document VS Code asks to format. It may differ from the active editor. @@ -20,15 +20,15 @@ export class YamlFormattingProvider implements DocumentFormattingEditProvider { */ provideDocumentFormattingEdits(document: TextDocument): TextEdit[] { const settings = readSettings(document) - const yamlutil = new YamlUtil(settings) + const yamlSorter = new YamlSorter(settings) const text = document.getText() let newText: string try { newText = settings.doSortOnSave() - ? sortDocuments(text, { customSort: settings.sortOnSave, isSelection: false }, yamlutil) - : formatDocuments(text, yamlutil) + ? sortDocuments(text, { customSort: settings.sortOnSave, isSelection: false }, yamlSorter) + : formatDocuments(text, yamlSorter) } catch (error) { - this.errorutil.handleError(error) + this.errorReporter.handleError(error) return [] } const fullRange = document.validateRange(new Range(0, 0, document.lineCount, 0)) From 2e64aa6974829dee2f72b5c4036671b02406fd89 Mon Sep 17 00:00:00 2001 From: Pascal Reitermann Date: Fri, 20 Nov 2026 14:22:35 +0000 Subject: [PATCH 05/18] test: run the core's unit tests without VS Code Every test, including the pure text transformations, could only run inside a downloaded VS Code under xvfb. Now that the core no longer imports VS Code, its tests run in plain node. * npm run test:unit runs src/test/unit with mocha in about a second; CI runs it before the integration tests, so a broken core fails fast * npm run test keeps running all tests inside VS Code for coverage * dependency-cruiser fails when the core or a unit test imports VS Code or the vscode layer, so the split cannot erode silently * CI installs with npm ci from the lock file and no longer installs yarn and vsce, which no step used * CONTRIBUTING describes the layers and the test commands Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Jdb5ivdJ5L75kyEDAbuvMZ --- .dependency-cruiser.js | 18 ++++++++++++++++++ .github/workflows/action.yml | 8 ++++---- CONTRIBUTING.md | 16 ++++++++++++++-- package.json | 2 ++ 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/.dependency-cruiser.js b/.dependency-cruiser.js index 98fd858..3b4ed76 100644 --- a/.dependency-cruiser.js +++ b/.dependency-cruiser.js @@ -1,6 +1,24 @@ /** @type {import('dependency-cruiser').IConfiguration} */ module.exports = { forbidden: [ + /* architecture of this extension: */ + { + name: 'core-independent-of-vscode', + severity: 'error', + comment: + 'The core contains the yaml logic and must run without VS Code, so it can be unit tested in plain node ' + + 'and reused. Talk to VS Code from src/vscode instead and pass the values into the core.', + from: { path: '^src/core' }, + to: { path: '^node_modules/@types/vscode/|^src/vscode/|^src/extension' } + }, + { + name: 'unit-tests-independent-of-vscode', + severity: 'error', + comment: + 'Unit tests run in plain node (npm run test:unit). Tests that need VS Code belong to src/test/integration.', + from: { path: '^src/test/unit' }, + to: { path: '^node_modules/@types/vscode/|^src/vscode/|^src/extension' } + }, /* rules from the 'recommended' preset: */ { name: 'no-circular', diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml index 3e19a43..286289b 100644 --- a/.github/workflows/action.yml +++ b/.github/workflows/action.yml @@ -25,10 +25,7 @@ jobs: node-version: ${{env.NODE_VERSION}} - name: install - run: | - npm install - npm install -g yarn - npm install -g vsce + run: npm ci - name: lint run: npm run lint @@ -36,6 +33,9 @@ jobs: - name: static code analysis run: npm run code-analysis + - name: unit tests + run: npm run test:unit + - name: test uses: GabrielBB/xvfb-action@v1 with: diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 99c4fc1..7ebc6cb 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -7,10 +7,22 @@ When opening an issue, describe your issue as clear as possible. If useful, prov ## Workflow This project uses the [GitHub Flow](https://docs.github.com/en/get-started/quickstart/github-flow). +### Architecture +The sources are split into two layers: + +| Folder | Content | May use the VS Code API | +| --- | --- | --- | +| `src/core` | Sorting, formatting and validating yaml: settings, documents, processors, js-yaml | no | +| `src/vscode` | Commands, formatter, notifications, reading the configuration | yes | + +`src/extension.ts` registers the commands and the formatter. The core reports problems by throwing errors, the +vscode layer shows them to the user. `npm run code-analysis` fails if the core or a unit test imports VS Code. + ### Testing & Linting The following commands support you at developing good code: -* `npm run code-analysis`: Run the static code analysis -* `npm run test`: Run the tests +* `npm run code-analysis`: Run the static code analysis, including the layer rules above +* `npm run test:unit`: Run the unit tests of the core in plain node, which takes a few seconds +* `npm run test`: Run all tests, including the integration tests in `src/test/integration`, inside VS Code * `npm run lint`: Run the linter ### Commit changes diff --git a/package.json b/package.json index 10b13c4..c157835 100644 --- a/package.json +++ b/package.json @@ -333,6 +333,8 @@ "pretest": "npm run compile", "sonar": "node sonar-project.js", "test": "node ./out/test/runTest.js", + "pretest:unit": "npm run compile", + "test:unit": "mocha --ui tdd \"out/test/unit/**/*.test.js\"", "vscode:prepublish": "npm run compile", "watch": "tsc -watch -p ./" }, From 71fc6ce6d1a4cc6e174b225144221e21cf9276e8 Mon Sep 17 00:00:00 2001 From: Pascal Reitermann Date: Fri, 20 Nov 2026 14:24:36 +0000 Subject: [PATCH 06/18] fix(commands): apply edits to the command's document and await them Fixes several problems of the editor commands: * "Format YAML" with a selection replaced only the selected lines with the formatted whole document, so the rest of the document appeared twice. Formatting now always replaces the whole document. * The edits were applied to whichever editor was active when they were applied, not to the editor the command read from. * The success message was shown before VS Code applied the edits, and also when VS Code rejected them. The commands now await the edit and report a rejected edit as an error. * Running a command with a key binding while no editor was open threw "No texteditor is active" as an unhandled error. The command now asks to open a yaml file. * ErrorReporter silently ignored thrown values that are no Error. They are now shown as text. EditorCommands takes the editor as a parameter (defaulting to the active one) and returns the applied edits asynchronously. VsCodeAdapter loses the helpers this made unused (getText, getEdits, applyEdits, getActiveDocument); getFullDocumentRange() now takes a document and validates the range, which the formatter shares. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Jdb5ivdJ5L75kyEDAbuvMZ --- src/extension.ts | 35 ++++---- src/test/integration/editor-commands.test.ts | 30 ++++++- src/test/integration/error-reporter.test.ts | 22 +++-- src/test/integration/extension.test.ts | 4 +- src/test/integration/vs-code-adapter.test.ts | 25 ++---- src/vscode/editor-commands.ts | 93 +++++++++++++------- src/vscode/error-reporter.ts | 14 ++- src/vscode/formatting-provider.ts | 6 +- src/vscode/vs-code-adapter.ts | 58 ++++-------- 9 files changed, 161 insertions(+), 126 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 8512b68..72495b0 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,31 +1,34 @@ -import { ExtensionContext, Uri, commands } from "vscode" +import { ExtensionContext, Uri, commands, window } from "vscode" import { sortYamlFilesInDirectory } from "./vscode/directory-sorter" import { EditorCommands } from "./vscode/editor-commands" import { YamlFormattingProvider } from "./vscode/formatting-provider" import { VsCodeAdapter } from "./vscode/vs-code-adapter" +/** + * Runs a command on the active editor, or explains why it cannot run, e.g. when the command is + * started with a key binding while no editor is open. + */ +const withActiveEditor = (command: (editorCommands: EditorCommands) => T) => (): T | undefined => { + const editor = window.activeTextEditor + if (!editor) { + window.showErrorMessage("Open a yaml file to run this command.") + return undefined + } + return command(new EditorCommands(editor)) +} + export function activate(context: ExtensionContext) { context.subscriptions.push( VsCodeAdapter.registerFormatter(new YamlFormattingProvider()), - commands.registerCommand("vscode-yaml-sort.sortYaml", () => { - new EditorCommands().sortYamlWrapper() - }), - commands.registerCommand("vscode-yaml-sort.validateYaml", () => { - new EditorCommands().validateYamlWrapper() - }), - commands.registerCommand("vscode-yaml-sort.formatYaml", () => { - new EditorCommands().formatYamlWrapper() - }), - commands.registerCommand("vscode-yaml-sort.sortYamlFilesInDirectory", (uri?: Uri) => { - sortYamlFilesInDirectory(uri) - }) + commands.registerCommand("vscode-yaml-sort.sortYaml", withActiveEditor((editor) => editor.sortYamlWrapper())), + commands.registerCommand("vscode-yaml-sort.validateYaml", withActiveEditor((editor) => editor.validateYamlWrapper())), + commands.registerCommand("vscode-yaml-sort.formatYaml", withActiveEditor((editor) => editor.formatYamlWrapper())), + commands.registerCommand("vscode-yaml-sort.sortYamlFilesInDirectory", (uri?: Uri) => sortYamlFilesInDirectory(uri)) ) for (const index of [1, 2, 3]) { context.subscriptions.push( - commands.registerCommand(`vscode-yaml-sort.customSortYaml_${index}`, () => { - new EditorCommands().sortYamlWrapper(index) - }) + commands.registerCommand(`vscode-yaml-sort.customSortYaml_${index}`, withActiveEditor((editor) => editor.sortYamlWrapper(index))) ) } } diff --git a/src/test/integration/editor-commands.test.ts b/src/test/integration/editor-commands.test.ts index 54afac0..c892d0e 100644 --- a/src/test/integration/editor-commands.test.ts +++ b/src/test/integration/editor-commands.test.ts @@ -2,7 +2,7 @@ import { equal } from "assert" import { readFileSync, writeFileSync } from "fs" import { resolve } from "path" -import { Uri, commands, window, workspace } from "vscode" +import { Selection, Uri, commands, window, workspace } from "vscode" import { EditorCommands } from "../../vscode/editor-commands" @@ -47,7 +47,7 @@ suite("Test formatYamlWrapper", () => { "key:\n" + " key2: value" - equal(new EditorCommands().formatYamlWrapper()[0].newText, expected) + equal((await new EditorCommands().formatYamlWrapper())[0].newText, expected) } else { equal(true, false) } @@ -65,7 +65,7 @@ suite("Test formatYamlWrapper", () => { "---\n" + "key2: value" - equal(new EditorCommands().formatYamlWrapper()[0].newText, expected) + equal((await new EditorCommands().formatYamlWrapper())[0].newText, expected) } else { equal(true, false) } @@ -82,4 +82,26 @@ suite("Test formatYamlWrapper", () => { equal(true, false) } }) -}) \ No newline at end of file +}) + +suite("Test formatYamlWrapper with a selection", () => { + test("should format the whole document instead of replacing the selection with it", async () => { + const document = await workspace.openTextDocument({ language: "yaml", content: "b: 1\na: 2" }) + const editor = await window.showTextDocument(document, { preview: false }) + editor.selection = new Selection(0, 0, 0, 5) + + await new EditorCommands(editor).formatYamlWrapper() + + equal(document.getText(), "---\nb: 1\na: 2") + }) +}) + +suite("Test editor commands without an active editor", () => { + test("should not fail when no editor is open", async () => { + await commands.executeCommand("workbench.action.closeAllEditors") + + await commands.executeCommand("vscode-yaml-sort.sortYaml") + await commands.executeCommand("vscode-yaml-sort.formatYaml") + await commands.executeCommand("vscode-yaml-sort.validateYaml") + }) +}) diff --git a/src/test/integration/error-reporter.test.ts b/src/test/integration/error-reporter.test.ts index 3322a51..d588807 100644 --- a/src/test/integration/error-reporter.test.ts +++ b/src/test/integration/error-reporter.test.ts @@ -1,18 +1,26 @@ -import { equal } from "assert" +import { deepEqual } from "assert" import { spy } from "sinon" import { ErrorReporter } from "../../vscode/error-reporter" +import { Severity } from "../../vscode/vs-code-adapter" suite("Test ErrorReporter - handleError()", () => { - test("when input is of type error, should execute showMessage function", () => { + test("should show the message of an error", () => { const errorReporter = new ErrorReporter() - const spyObject = spy(errorReporter.vsCodeAdapter, "showMessage") + const showMessage = spy(errorReporter.vsCodeAdapter, "showMessage") + + errorReporter.handleError(new Error("broken")) + + deepEqual(showMessage.args, [[Severity.ERROR, "broken"]]) + }) + + test("should show a thrown value that is no error instead of swallowing it", () => { + const errorReporter = new ErrorReporter() + const showMessage = spy(errorReporter.vsCodeAdapter, "showMessage") errorReporter.handleError("no error") - equal(spyObject.called, false) - errorReporter.handleError(new Error()) - equal(spyObject.called, true) + deepEqual(showMessage.args, [[Severity.ERROR, "no error"]]) }) -}) \ No newline at end of file +}) diff --git a/src/test/integration/extension.test.ts b/src/test/integration/extension.test.ts index 8efea2b..de86998 100644 --- a/src/test/integration/extension.test.ts +++ b/src/test/integration/extension.test.ts @@ -61,7 +61,7 @@ suite("Test sortYamlWrapper", () => { new Position(activeEditor.document.lineCount + 1, 0)), actual)) - notDeepEqual(new EditorCommands().sortYamlWrapper(), []) + notDeepEqual(await new EditorCommands().sortYamlWrapper(), []) } else { equal(true, false) } @@ -85,7 +85,7 @@ suite("Test sortYamlWrapper", () => { actual)) activeEditor.selection = new Selection(0, 0, 0, 4) - deepEqual(new EditorCommands().sortYamlWrapper(), []) + deepEqual(await new EditorCommands().sortYamlWrapper(), []) } else { equal(true, false) } diff --git a/src/test/integration/vs-code-adapter.test.ts b/src/test/integration/vs-code-adapter.test.ts index 328a860..f3fff16 100644 --- a/src/test/integration/vs-code-adapter.test.ts +++ b/src/test/integration/vs-code-adapter.test.ts @@ -1,26 +1,15 @@ -import { equal, fail } from "assert" -import { resolve } from "path" +import { equal } from "assert" import { spy } from "sinon" -import { Uri, window, workspace } from "vscode" +import { window, workspace } from "vscode" import { Severity, VsCodeAdapter } from "../../vscode/vs-code-adapter" -suite("Test VsCodeAdapter - getActiveDocument()", () => { - test("when text editor is active should open document", async () => { - const uri = Uri.parse(resolve("./src/test/files/testGetActiveDocument.yaml")) - const doc = await workspace.openTextDocument(uri) - await window.showTextDocument(doc, { preview: false }) - - if (window.activeTextEditor) { - const expected = - "key:\n" + - " key2: value" - - equal(VsCodeAdapter.getActiveDocument(window.activeTextEditor), expected) - } else { - fail("window.activeTextEditor is not set") - } +suite("Test VsCodeAdapter - getFullDocumentRange()", () => { + test("should cover the whole document", async () => { + const document = await workspace.openTextDocument({ language: "yaml", content: "a: 1\nb: 2\n" }) + + equal(document.getText(VsCodeAdapter.getFullDocumentRange(document)), "a: 1\nb: 2\n") }) }) diff --git a/src/vscode/editor-commands.ts b/src/vscode/editor-commands.ts index c66c643..0234936 100644 --- a/src/vscode/editor-commands.ts +++ b/src/vscode/editor-commands.ts @@ -1,4 +1,4 @@ -import { TextEdit, TextEditor, window } from "vscode" +import { Range, TextEdit, TextEditor, WorkspaceEdit, window, workspace } from "vscode" import { readSettings } from "./configuration" import { ErrorReporter } from "./error-reporter" @@ -8,69 +8,96 @@ import { JsYamlAdapter } from "../core/js-yaml-adapter" import { Settings } from "../core/settings" import { YamlSorter } from "../core/yaml-sorter" +/** + * The commands that work on the document of one editor. + */ export class EditorCommands { - editor: TextEditor - settings: Settings - yamlSorter: YamlSorter - jsYamlAdapter: JsYamlAdapter - vsCodeAdapter: VsCodeAdapter - errorReporter = new ErrorReporter() + readonly editor: TextEditor + readonly settings: Settings + readonly yamlSorter: YamlSorter + readonly jsYamlAdapter: JsYamlAdapter + readonly vsCodeAdapter: VsCodeAdapter + readonly errorReporter = new ErrorReporter() - constructor() { - if (!window.activeTextEditor) { + /** + * @param editor The editor to work on. Defaults to the active editor. + * @throws Error if no editor is given and none is active. + */ + constructor(editor: TextEditor | undefined = window.activeTextEditor) { + if (!editor) { throw new Error("No texteditor is active") } - this.editor = window.activeTextEditor + this.editor = editor // one snapshot of the settings that apply to the edited document - this.settings = readSettings(this.editor.document) + this.settings = readSettings(editor.document) this.yamlSorter = new YamlSorter(this.settings) this.jsYamlAdapter = new JsYamlAdapter(this.settings) this.vsCodeAdapter = new VsCodeAdapter(this.settings) } + /** + * Validates the whole document and reports the result. + * @returns True if the document is valid yaml. + */ validateYamlWrapper(): boolean { - const text = VsCodeAdapter.getActiveDocument(this.editor) try { - this.jsYamlAdapter.validateYaml(text) - this.vsCodeAdapter.showMessage(Severity.INFO, "YAML is valid.") - return true + this.jsYamlAdapter.validateYaml(this.editor.document.getText()) } catch (error) { this.errorReporter.handleError(error) + return false } - return false + this.vsCodeAdapter.showMessage(Severity.INFO, "YAML is valid.") + return true } - formatYamlWrapper(): TextEdit[] { + /** + * Formats the whole document, regardless of a selection. + * @returns The applied edits, or none if the document could not be formatted. + */ + async formatYamlWrapper(): Promise { + const document = this.editor.document let newText: string try { - newText = formatDocuments(VsCodeAdapter.getActiveDocument(this.editor), this.yamlSorter) + newText = formatDocuments(document.getText(), this.yamlSorter) } catch (error) { this.errorReporter.handleError(error) return [] } - 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 + return this.replace(VsCodeAdapter.getFullDocumentRange(document), newText, "Yaml formatted successfully") } - applyEdits(text: string) { - const edits = VsCodeAdapter.getEdits(this.editor, text) - VsCodeAdapter.applyEdits([edits]) - return [edits] - } - - sortYamlWrapper(customSort = 0): TextEdit[] { - const text = VsCodeAdapter.getText(this.editor, VsCodeAdapter.getRange(this.editor)) + /** + * Sorts the selected lines, or the whole document if nothing is selected. + * @param customSort 0 for the default sort, 1 to 3 for the custom sort keywords. + * @returns The applied edits, or none if the text could not be sorted. + */ + async sortYamlWrapper(customSort = 0): Promise { + const range = VsCodeAdapter.getRange(this.editor) let newText: string try { - newText = sortDocuments(text, { customSort, isSelection: !this.editor.selection.isEmpty }, this.yamlSorter) + newText = sortDocuments(this.editor.document.getText(range), { customSort, isSelection: !this.editor.selection.isEmpty }, this.yamlSorter) } catch (error) { this.errorReporter.handleError(error) return [] } - const edits = this.applyEdits(newText) - this.vsCodeAdapter.showMessage(Severity.INFO, "Keys resorted successfully") - return edits + return this.replace(range, newText, "Keys resorted successfully") + } + + /** + * Replaces a range of the editor's document and reports success once the edit is applied. + * @returns The applied edits, or none if VS Code rejected them. + */ + private async replace(range: Range, text: string, successMessage: string): Promise { + const edit = TextEdit.replace(range, text) + const workspaceEdit = new WorkspaceEdit() + // the edit belongs to this editor's document, even if another editor became active meanwhile + workspaceEdit.set(this.editor.document.uri, [edit]) + if (!await workspace.applyEdit(workspaceEdit)) { + this.vsCodeAdapter.showMessage(Severity.ERROR, "The changes could not be applied to the document.") + return [] + } + this.vsCodeAdapter.showMessage(Severity.INFO, successMessage) + return [edit] } } diff --git a/src/vscode/error-reporter.ts b/src/vscode/error-reporter.ts index 7409189..de639ee 100644 --- a/src/vscode/error-reporter.ts +++ b/src/vscode/error-reporter.ts @@ -1,11 +1,17 @@ import { Severity, VsCodeAdapter } from "./vs-code-adapter" +/** + * Shows errors from the core to the user. + */ export class ErrorReporter { vsCodeAdapter = new VsCodeAdapter() + /** + * @param error Anything that was thrown. Values that are no Error are shown as text, + * so no failure goes unnoticed. + */ handleError(error: unknown) { - if (error instanceof Error) { - this.vsCodeAdapter.showMessage(Severity.ERROR, error.message) - } + const message = error instanceof Error ? error.message : String(error) + this.vsCodeAdapter.showMessage(Severity.ERROR, message) } -} \ No newline at end of file +} diff --git a/src/vscode/formatting-provider.ts b/src/vscode/formatting-provider.ts index 170def6..f8a4f4e 100644 --- a/src/vscode/formatting-provider.ts +++ b/src/vscode/formatting-provider.ts @@ -1,7 +1,8 @@ -import { DocumentFormattingEditProvider, Range, TextDocument, TextEdit } from "vscode" +import { DocumentFormattingEditProvider, TextDocument, TextEdit } from "vscode" import { readSettings } from "./configuration" import { ErrorReporter } from "./error-reporter" +import { VsCodeAdapter } from "./vs-code-adapter" import { formatDocuments, sortDocuments } from "../core/document-transformer" import { YamlSorter } from "../core/yaml-sorter" @@ -31,7 +32,6 @@ export class YamlFormattingProvider implements DocumentFormattingEditProvider { this.errorReporter.handleError(error) return [] } - const fullRange = document.validateRange(new Range(0, 0, document.lineCount, 0)) - return [TextEdit.replace(fullRange, newText)] + return [TextEdit.replace(VsCodeAdapter.getFullDocumentRange(document), newText)] } } diff --git a/src/vscode/vs-code-adapter.ts b/src/vscode/vs-code-adapter.ts index 957cd5d..c9a51fb 100644 --- a/src/vscode/vs-code-adapter.ts +++ b/src/vscode/vs-code-adapter.ts @@ -1,4 +1,4 @@ -import { Disposable, DocumentFormattingEditProvider, Position, Range, Selection, TextEdit, TextEditor, WorkspaceEdit, languages, window, workspace } from "vscode" +import { Disposable, DocumentFormattingEditProvider, Range, Selection, TextDocument, TextEditor, languages, window } from "vscode" import { Settings } from "../core/settings" @@ -31,57 +31,37 @@ export class VsCodeAdapter { return languages.registerDocumentFormattingEditProvider("yaml", formatter) } - static getText(textEditor: TextEditor, range: Range) { - return textEditor.document.getText(range) - } - - static getFullDocumentRange(textEditor: TextEditor) { - return new Range( - new Position(0, 0), - new Position(textEditor.document.lineCount + 1, 0)) + /** + * @param document A text document. + * @returns The range from the beginning to the end of the document. + */ + static getFullDocumentRange(document: TextDocument): Range { + return document.validateRange(new Range(0, 0, document.lineCount, 0)) } - static getSelectedRange(textEditor: TextEditor) { + /** + * @param textEditor An editor with a non-empty selection. + * @returns The selection, extended to whole lines. A selection that ends on the first character + * of a line does not include that line. + */ + static getSelectedRange(textEditor: TextEditor): Selection { let endLine = textEditor.selection.end.line - // if selection ends on the first character on a new line ignore this line if (textEditor.selection.end.character === 0) { endLine-- } - - // ensure selection covers whole start and end line return new Selection( textEditor.selection.start.line, 0, endLine, textEditor.document.lineAt(endLine).range.end.character) } /** - * Applys edits to a text editor - * @param activeEditor Editor to apply the changes - * @param edits Changes to apply - */ - static applyEdits(edit: [TextEdit]) { - if (window.activeTextEditor) { - const workspaceEdit = new WorkspaceEdit() - workspaceEdit.set(window.activeTextEditor.document.uri, edit) - workspace.applyEdit(workspaceEdit) - } - } - - static getActiveDocument(textEditor: TextEditor) { - return textEditor.document.getText() - } - - static getRange(textEditor: TextEditor) { + * @param textEditor An editor. + * @returns The selected lines, or the whole document if nothing is selected. + */ + static getRange(textEditor: TextEditor): Range { if (textEditor.selection.isEmpty) { - return VsCodeAdapter.getFullDocumentRange(textEditor) - } else { - return VsCodeAdapter.getSelectedRange(textEditor) + return VsCodeAdapter.getFullDocumentRange(textEditor.document) } + return VsCodeAdapter.getSelectedRange(textEditor) } - - static getEdits(textEditor: TextEditor, text: string) { - const range = VsCodeAdapter.getRange(textEditor) - return TextEdit.replace(range, text) - } - } From 930058096062d8294c745ac4f9ffcecf9f44d8be Mon Sep 17 00:00:00 2001 From: Pascal Reitermann Date: Fri, 20 Nov 2026 14:25:32 +0000 Subject: [PATCH 07/18] fix(sort): accept a whole document that ends with a colon sortDocuments() checked every text for an invalid selection ending (`:`, `|` or `>`), also whole documents. A valid document like b: 1 a: could therefore not be sorted, neither with the command, the formatter with sortOnSave nor the directory sort, and the user was told that the selection is invalid although nothing was selected. Only check the ending of a selection, which can cut a value in half. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Jdb5ivdJ5L75kyEDAbuvMZ --- CHANGELOG.md | 1 + src/core/document-transformer.ts | 6 ++++-- src/test/unit/core/document-transformer.test.ts | 5 +++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 90c9327..ba459ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased ๐Ÿ› Bugfixes +* Sorting a whole document whose last key has no value (e.g. `key:`) no longer fails with "YAML selection is invalid". This check now only applies to selections * The editor commands (sort, custom sort, format, validate) are only offered in the command palette for yaml files. The condition was declared at a place VS Code ignores * (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 diff --git a/src/core/document-transformer.ts b/src/core/document-transformer.ts index d93364a..dad7dec 100644 --- a/src/core/document-transformer.ts +++ b/src/core/document-transformer.ts @@ -59,10 +59,12 @@ const transformDocuments = (stream: YamlStream, transform: ContentTransformation * @param options Sort options. * @param yaml Yaml operations, injectable for tests. * @returns The sorted text. - * @throws Error if the text is not valid yaml, the selection ends invalid or a document cannot be sorted. + * @throws Error if the text is not valid yaml, a selection ends invalid or a document cannot be sorted. */ export const sortDocuments = (text: string, options: SortOptions, yaml: YamlOperations = new YamlSorter()): string => { - validateTextRange(text) + if (options.isSelection) { + validateTextRange(text) + } new JsYamlAdapter(yaml.settings).validateYaml(text) const sorted = transformDocuments(splitDocuments(text), (content) => { diff --git a/src/test/unit/core/document-transformer.test.ts b/src/test/unit/core/document-transformer.test.ts index b915116..8053e8b 100644 --- a/src/test/unit/core/document-transformer.test.ts +++ b/src/test/unit/core/document-transformer.test.ts @@ -37,6 +37,11 @@ suite("Test DocumentTransformer - sortDocuments()", () => { test("should throw when a selection ends with a colon", () => { throws(() => sortDocuments("key:", { customSort: 0, isSelection: true }), /YAML selection is invalid/) }) + + test("should sort a whole document whose last key has no value", () => { + // only a selection can be cut off in the middle of a value, a whole document ending with a colon is valid + equal(sortDocuments("b: 1\na:", { customSort: 0, isSelection: false }), "---\na: null\nb: 1") + }) }) suite("Test DocumentTransformer - sortDocuments() when a document cannot be sorted", () => { From f5fd1ce9b0813c1272b4d4163a27aeafed00be52 Mon Sep 17 00:00:00 2001 From: Pascal Reitermann Date: Fri, 20 Nov 2026 14:26:17 +0000 Subject: [PATCH 08/18] fix(comments): keep inline comments of values with apostrophes getInlineComment() toggled its quote state on every quote character. An apostrophe in a plain value, as in key: it's fine # comment opened a "quoted value" that never closed, so the comment was not found and got lost while sorting. An escaped double quote (\") closed a double quoted value too early. A quote now only starts a quoted value at the beginning of a value (line start, after whitespace, `[`, `{` or `,`). Inside double quotes a backslash escapes the next character, inside single quotes `''` is an escaped quote, as defined by the yaml spec. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Jdb5ivdJ5L75kyEDAbuvMZ --- CHANGELOG.md | 1 + src/core/processor/comment-processor.ts | 35 +++++++++++++++---- .../core/processor/comment-processor.test.ts | 22 ++++++++++-- 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba459ce..17dd365 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased ๐Ÿ› Bugfixes +* Keep the inline comment of a value that contains an apostrophe (`key: it's # comment`) or an escaped quote (`key: "a \\" b" # comment`). Before, the comment was dropped while sorting * Sorting a whole document whose last key has no value (e.g. `key:`) no longer fails with "YAML selection is invalid". This check now only applies to selections * The editor commands (sort, custom sort, format, validate) are only offered in the command palette for yaml files. The condition was declared at a place VS Code ignores * (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 diff --git a/src/core/processor/comment-processor.ts b/src/core/processor/comment-processor.ts index e7b3027..c029362 100644 --- a/src/core/processor/comment-processor.ts +++ b/src/core/processor/comment-processor.ts @@ -44,16 +44,29 @@ export class CommentProcessor { } } + /** + * Finds the comment at the end of a line, ignoring `#` inside quoted values. + * @param line A line of yaml. + * @returns The comment starting with `#`, or undefined if the line has none. + */ static getInlineComment(line: string): string | undefined { - let inDoubleQuotes = false - let inSingleQuotes = false + let quote: string | undefined for (let i = 0; i < line.length; i++) { - if (line[i] === "\"" && !inSingleQuotes) { - inDoubleQuotes = !inDoubleQuotes - } else if (line[i] === "'" && !inDoubleQuotes) { - inSingleQuotes = !inSingleQuotes - } else if (line[i] === "#" && i > 0 && (line[i - 1] === " " || line[i - 1] === "\t") && !inDoubleQuotes && !inSingleQuotes) { + const char = line[i] + if (quote !== undefined) { + if (quote === "\"" && char === "\\") { + // skip the escaped character, e.g. \" + i++ + } else if (quote === "'" && char === "'" && line[i + 1] === "'") { + // '' is an escaped single quote + i++ + } else if (char === quote) { + quote = undefined + } + } else if ((char === "\"" || char === "'") && CommentProcessor.startsValue(line, i)) { + quote = char + } else if (char === "#" && i > 0 && (line[i - 1] === " " || line[i - 1] === "\t")) { return line.slice(i) } } @@ -61,6 +74,14 @@ export class CommentProcessor { return undefined } + /** + * A quote only starts a quoted value at the beginning of a value, e.g. after `key: `, `- ` or `[`. + * Elsewhere it is part of a plain value, like the apostrophe in `key: it's`. + */ + static startsValue(line: string, index: number): boolean { + return index === 0 || /[\s[{,]/.test(line[index - 1]) + } + postprocess(): string { this.reverseComments() this.store.forEach(comment => { diff --git a/src/test/unit/core/processor/comment-processor.test.ts b/src/test/unit/core/processor/comment-processor.test.ts index 4962470..6c52c0f 100644 --- a/src/test/unit/core/processor/comment-processor.test.ts +++ b/src/test/unit/core/processor/comment-processor.test.ts @@ -500,16 +500,34 @@ suite("Test CommentProcessor - getInlineComment()", () => { { line: "key: 'value # quoted'", expectedComment: undefined }, { line: "key: value#noComment", expectedComment: undefined }, { line: "key: \"a\" # comment", expectedComment: "# comment" }, - { line: "# full line comment", expectedComment: undefined } + { line: "# full line comment", expectedComment: undefined }, + // an apostrophe inside a plain value does not start a quoted value + { line: "key: it's fine # comment", expectedComment: "# comment" }, + { line: "key: don't # comment 'quoted'", expectedComment: "# comment 'quoted'" }, + // escaped quotes do not end a quoted value + { line: "key: \"a \\\" # b\" # comment", expectedComment: "# comment" }, + { line: "key: 'a '' # b' # comment", expectedComment: "# comment" }, + { line: "- 'a # b' # comment", expectedComment: "# comment" }, + { line: "key: [\"a # b\", 'c # d'] # comment", expectedComment: "# comment" } ] detectionCases.forEach(({ line, expectedComment }) => { - test("should only detect comments outside of quotes", () => { + test(`should only detect comments outside of quotes: ${line}`, () => { equal(CommentProcessor.getInlineComment(line), expectedComment) }) }) }) +suite("Test CommentProcessor - inline comments of values with apostrophes", () => { + test("should keep the inline comment of a value containing an apostrophe", () => { + const processor = new CommentProcessor("c: it's fine # note\nb: 1") + processor.findComments() + processor.text = "b: 1\nc: it's fine" + + equal(processor.postprocess(), "b: 1\nc: it's fine # note") + }) +}) + suite("Test CommentProcessor - GitHub issue #221", () => { const composeCommentCases: { unsortedYaml: string, sortedYaml: string, expectedYaml: string }[] = [ { From e8b9421e7e4655c6b33f7fcac854d2a6e6eaedcc Mon Sep 17 00:00:00 2001 From: Pascal Reitermann Date: Fri, 20 Nov 2026 14:27:46 +0000 Subject: [PATCH 09/18] fix(sort): dump custom sort keywords with js-yaml The keywords of a custom sort were written by hand as `${key}: value`, and a heuristic decided whether the dumped value belongs on the next line. This produced * invalid yaml for a keyword that needs quotes: `'a: b': 1` became `a: b: 1` * a scalar containing a colon on its own line (`kind:\n http://x`), because the heuristic took it for a mapping * an extra trailing line break when every key was a keyword, unlike the default sort Each keyword is now dumped as a single-key mapping by js-yaml, which quotes keys and places values the same way as for all other keys. The heuristic (isValueMultiline, transformMultilineValue) and the unused setLeadingDashes() are removed together with their tests; the characterization tests that recorded the trailing line break expect none now. dumpYaml() also no longer turns an empty mapping into an empty string: a document `{}` became an empty document. The remaining keys of a custom sort are skipped when there are none instead. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Jdb5ivdJ5L75kyEDAbuvMZ --- CHANGELOG.md | 4 +- src/core/js-yaml-adapter.ts | 11 +--- src/core/yaml-sorter.ts | 39 +++-------- src/test/unit/core/yaml-sorter.test.ts | 91 +++++++++++--------------- 4 files changed, 51 insertions(+), 94 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 17dd365..92c74df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,9 @@ ## Unreleased ๐Ÿ› Bugfixes -* Keep the inline comment of a value that contains an apostrophe (`key: it's # comment`) or an escaped quote (`key: "a \\" b" # comment`). Before, the comment was dropped while sorting +* Custom sort writes the keywords like every other key: a keyword that needs quotes is quoted (before, `'a: b': 1` became the invalid `a: b: 1`), a value containing a colon stays on the line of its keyword instead of moving to the next line, and the result no longer ends with an extra line break when every key is a keyword +* Keep a document that only contains an empty mapping (`{}`) when sorting or formatting. Before, it became an empty document +* Keep the inline comment of a value that contains an apostrophe (`key: it's # comment`) or an escaped quote (`key: "a \" b" # comment`). Before, the comment was dropped while sorting * Sorting a whole document whose last key has no value (e.g. `key:`) no longer fails with "YAML selection is invalid". This check now only applies to selections * The editor commands (sort, custom sort, format, validate) are only offered in the command palette for yaml files. The condition was declared at a place VS Code ignores * (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 diff --git a/src/core/js-yaml-adapter.ts b/src/core/js-yaml-adapter.ts index d830cc3..35ebb7f 100644 --- a/src/core/js-yaml-adapter.ts +++ b/src/core/js-yaml-adapter.ts @@ -4,13 +4,6 @@ import { Settings } from "./settings" import { SortOrder } from "./sort-order" import { removeTrailingCharacters, splitDocuments } from "./yaml-text" -/** - * @param value Any loaded yaml value. - * @returns True for a mapping without keys, e.g. an empty document. - */ -const isEmptyMapping = (value: unknown): boolean => - typeof value === "object" && value !== null && !Array.isArray(value) && Object.keys(value).length === 0 - export class JsYamlAdapter { settings: Settings @@ -41,7 +34,7 @@ export class JsYamlAdapter { /** * Dumps a whole yaml document with the user specific settings. - * An empty document (no content or only comments) is dumped as an empty string. + * An empty document (no content or only comments) is dumped as an empty string, an empty mapping as `{}`. * @param text Loaded yaml document which should be dumped. * @param sortKeys If set to true, the function will sort the keys in the document. * @param custom 0 for the default sort, 1 to 3 for the custom sort keywords. @@ -49,7 +42,7 @@ export class JsYamlAdapter { */ dumpYaml(text: unknown, sortKeys: boolean, custom: number): string { // js-yaml loads an empty document or one with only comments as null or undefined - if (text === undefined || text === null || isEmptyMapping(text)) { + if (text === undefined || text === null) { return "" } return this.dumpValue(text, sortKeys, custom) diff --git a/src/core/yaml-sorter.ts b/src/core/yaml-sorter.ts index 089e37f..03783e9 100644 --- a/src/core/yaml-sorter.ts +++ b/src/core/yaml-sorter.ts @@ -1,9 +1,7 @@ -import { prependWhitespacesOnEachLine } from "./indentation" import { JsYamlAdapter } from "./js-yaml-adapter" import { ProcessorPipeline } from "./processor/processor-pipeline" import { Settings } from "./settings" import { sortArraysRecursively } from "./sort-order" -import { removeTrailingCharacters } from "./yaml-text" /** * @param value Any loaded yaml value. @@ -29,25 +27,6 @@ export class YamlSorter { } } - static isValueMultiline(text: string) { - return (!text.startsWith("|") && text.includes(":")) || text.startsWith("-") - } - - setLeadingDashes(text: string) { - if (this.settings.useLeadingDashes) { - return `---\n${text}` - } - return text - } - - transformMultilineValue(text: string) { - let result = prependWhitespacesOnEachLine(text, this.settings.indent) - if (text.endsWith("\n")) { - result = removeTrailingCharacters(result, this.settings.indent) - } - return result - } - /** * Sorts a single yaml document. * @param unsortedYaml The content of one document, without delimiter. @@ -85,23 +64,23 @@ export class YamlSorter { .filter((key) => Object.prototype.hasOwnProperty.call(doc, key)) // build the remaining keys anew instead of deleting them from the loaded document const remainder = Object.fromEntries(Object.entries(doc).filter(([key]) => !keywords.includes(key))) - const dumpedKeywords = keywords.map((key) => this.dumpKeyword(key, doc[key], customSort)).join("") - return dumpedKeywords + this.jsYamlAdapter.dumpYaml(remainder, true, customSort) + const parts = keywords.map((key) => this.dumpKeyword(key, doc[key], customSort)) + if (Object.keys(remainder).length > 0) { + parts.push(this.jsYamlAdapter.dumpYaml(remainder, true, customSort)) + } + return parts.join("\n") } /** - * Dumps one top-level keyword of a custom sort together with its value. + * Dumps one top-level keyword of a custom sort together with its value. js-yaml dumps both, so + * the keyword is quoted if necessary and the value keeps its usual style. * @param key The keyword. * @param value Its value. * @param customSort 1 to 3, the custom sort that is applied. - * @returns The yaml lines for the keyword, ending with a line break. + * @returns The yaml lines for the keyword, without trailing line break. */ dumpKeyword(key: string, value: unknown, customSort: number): string { - const dumpedValue = this.jsYamlAdapter.dumpValue(value, true, customSort) - if (YamlSorter.isValueMultiline(dumpedValue)) { - return `${key}:\n${this.transformMultilineValue(dumpedValue)}\n` - } - return `${key}: ${dumpedValue}\n` + return this.jsYamlAdapter.dumpValue({ [key]: value }, true, customSort) } /** diff --git a/src/test/unit/core/yaml-sorter.test.ts b/src/test/unit/core/yaml-sorter.test.ts index 22a9405..2e560ab 100644 --- a/src/test/unit/core/yaml-sorter.test.ts +++ b/src/test/unit/core/yaml-sorter.test.ts @@ -52,19 +52,6 @@ suite("Test sortArrays", () => { }) }) -suite("Test YamlSorter - setLeadingDashes()", () => { - test("when setting useLeadingDashes is `true` should prepend `---\n` to text", () => { - const yamlSorter = new YamlSorter() - const text = "lorem ipsum" - yamlSorter.settings.useLeadingDashes = false - equal(yamlSorter.setLeadingDashes(text), text) - - yamlSorter.settings.useLeadingDashes = true - const expected = "---\nlorem ipsum" - equal(yamlSorter.setLeadingDashes(text), expected) - }) -}) - suite("Test dumpYaml", () => { test("when useCustomSortRecursively is set to`true` should recursively use customSort", () => { const actual = @@ -218,7 +205,7 @@ suite("Test sortYaml", () => { let expected = "spec: spec\n" + - "data: data\n" + "data: data" const yamlSorter = new YamlSorter() equal(yamlSorter.sortYaml(actual, 1), expected) @@ -231,7 +218,7 @@ suite("Test sortYaml", () => { expected = "spec:\n" + " - aa: b\n" + - "data: data\n" + "data: data" equal(yamlSorter.sortYaml(actual, 1), expected) @@ -251,7 +238,7 @@ suite("Test sortYaml", () => { "data:\n" + " job: Developer\n" + " skills:\n" + - " - pascal\n" + " - pascal" equal(yamlSorter.sortYaml(actual, 1), expected) @@ -265,7 +252,7 @@ suite("Test sortYaml", () => { "spec:\n" + " - a\n" + " - b\n" + - "data: data\n" + "data: data" equal(yamlSorter.sortYaml(actual, 1), expected) }) @@ -413,39 +400,6 @@ suite("Test YamlSorter - getNumberOfLeadingSpaces()", () => { }) }) -suite("Test YamlSorter - isValueMultiline", () => { - test("when text starts with '-' should return true", () => { - const text = - "- foo: bar\n" + - " bar: baz" - equal(YamlSorter.isValueMultiline(text), true) - }) -}) - -suite("Test YamlSorter - transformMultilineValue()", () => { - const yamlSorter = new YamlSorter() - yamlSorter.settings.indent = 2 - test("Should prepend spaces on each line", () => { - const text = - "- foo: bar\n" + - " bar: baz" - const expected = - " - foo: bar\n" + - " bar: baz" - equal(yamlSorter.transformMultilineValue(text), expected) - }) - test("When text ends with newline should remove trailing indent", () => { - const text = - "- foo: bar\n" + - " bar: baz\n" - const expected = - " - foo: bar\n" + - " bar: baz\n" - equal(yamlSorter.transformMultilineValue(text), expected) - }) -}) -/* -*/ suite("Test YamlSorter - sortYaml() with quoted helm values", () => { test("GitHub issue #130: should not mangle a quoted helm value containing a dollar sign", () => { const yamlSorter = new YamlSorter() @@ -524,17 +478,17 @@ suite("Test YamlSorter - sortYaml() with custom sort and scalar values", () => { const yamlSorter = new YamlSorter() test("should keep number and boolean values of custom sort keywords", () => { - equal(yamlSorter.sortYaml("spec: x\nkind: true\napiVersion: 3", 1), "apiVersion: 3\nkind: true\nspec: x\n") + equal(yamlSorter.sortYaml("spec: x\nkind: true\napiVersion: 3", 1), "apiVersion: 3\nkind: true\nspec: x") }) test("should keep an empty mapping as value of a custom sort keyword", () => { - equal(yamlSorter.sortYaml("spec: x\nmetadata: {}", 1), "metadata: {}\nspec: x\n") + equal(yamlSorter.sortYaml("spec: x\nmetadata: {}", 1), "metadata: {}\nspec: x") }) test("should order custom sort keywords even if their values are falsy", () => { const text = "spec: x\nmetadata: null\nkind: false\napiVersion: 0" - equal(yamlSorter.sortYaml(text, 1), "apiVersion: 0\nkind: false\nmetadata: null\nspec: x\n") + equal(yamlSorter.sortYaml(text, 1), "apiVersion: 0\nkind: false\nmetadata: null\nspec: x") }) test("should not add a value to a document containing only a comment", () => { @@ -544,6 +498,35 @@ suite("Test YamlSorter - sortYaml() with custom sort and scalar values", () => { test("should keep a document containing only a scalar", () => { equal(yamlSorter.sortYaml("42"), "42") }) + + test("should keep a scalar value containing a colon on the line of its keyword", () => { + equal(yamlSorter.sortYaml("spec: x\nkind: http://example.com", 1), "kind: http://example.com\nspec: x") + }) + + test("should keep a block scalar value of a keyword", () => { + equal(yamlSorter.sortYaml("b: 1\nkind: |\n line1\n line2", 1), "kind: |\n line1\n line2\nb: 1") + }) + + test("should quote a keyword that needs quotes", () => { + const settings = new Settings() + settings.customSortKeywords1 = ["a: b"] + + equal(new YamlSorter(settings).sortYaml("c: 2\n'a: b': 1", 1), "'a: b': 1\nc: 2") + }) + + test("should end without a line break when every key is a keyword, like the default sort", () => { + equal(yamlSorter.sortYaml("kind: x\napiVersion: v1", 1), "apiVersion: v1\nkind: x") + }) +}) + +suite("Test YamlSorter - sortYaml() with an empty mapping", () => { + test("should keep a document containing only an empty mapping", () => { + equal(new YamlSorter().sortYaml("{}"), "{}") + }) + + test("should keep a document containing only an empty mapping when formatting", () => { + equal(new YamlSorter().formatYaml("{}", false), "{}") + }) }) suite("Test YamlSorter - sortYaml() with sortOrderReverse", () => { @@ -592,7 +575,7 @@ suite("Test YamlSorter - sortYaml() with custom sort keeps the document intact", }) test("should keep a document that only contains keywords", () => { - equal(yamlSorter.sortYaml("kind: x", 1), "kind: x\n") + equal(yamlSorter.sortYaml("kind: x", 1), "kind: x") }) test("should leave a list document unchanged", () => { From 993d8346ed864801bbcd8539db6e1cef5aa939a8 Mon Sep 17 00:00:00 2001 From: Pascal Reitermann Date: Fri, 20 Nov 2026 14:29:11 +0000 Subject: [PATCH 10/18] fix(spacing): add empty lines before restoring hidden values The SpacingProcessor ran last, after the block scalars and comments were put back. With emptyLinesUntilLevel set, it therefore * inserted empty lines between the lines of a block scalar that look like keys, which changed the value of the scalar: a: | foo: bar -> a: | baz: 1 (empty line) foo: bar ... * inserted the empty line between a comment and its key instead of above the comment. The pipeline now adds the empty lines first, while block scalars are still hidden behind their substitutes, and restores the comments afterwards directly above their keys. ProcessorPipeline is rewritten as an ordered list of processors that are preprocessed in order and restored in reverse order, replacing ten nearly identical if-blocks. CommentProcessor gets preprocess() for the common interface. The pipeline tests check the resulting text instead of the pipeline's private processor fields. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Jdb5ivdJ5L75kyEDAbuvMZ --- CHANGELOG.md | 1 + src/core/processor/comment-processor.ts | 7 + src/core/processor/processor-pipeline.ts | 120 +++++++---------- .../core/processor/processor-pipeline.test.ts | 121 +++++++++--------- src/test/unit/core/yaml-sorter.test.ts | 15 +++ 5 files changed, 129 insertions(+), 135 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 92c74df..3682e89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased ๐Ÿ› Bugfixes +* Setting `emptyLinesUntilLevel` no longer adds empty lines inside block scalars (`key: |`), which changed their value, and no longer separates a comment from the key below it * Custom sort writes the keywords like every other key: a keyword that needs quotes is quoted (before, `'a: b': 1` became the invalid `a: b: 1`), a value containing a colon stays on the line of its keyword instead of moving to the next line, and the result no longer ends with an extra line break when every key is a keyword * Keep a document that only contains an empty mapping (`{}`) when sorting or formatting. Before, it became an empty document * Keep the inline comment of a value that contains an apostrophe (`key: it's # comment`) or an escaped quote (`key: "a \" b" # comment`). Before, the comment was dropped while sorting diff --git a/src/core/processor/comment-processor.ts b/src/core/processor/comment-processor.ts index c029362..591ced5 100644 --- a/src/core/processor/comment-processor.ts +++ b/src/core/processor/comment-processor.ts @@ -9,6 +9,13 @@ export class CommentProcessor { this.lines = this.text.split("\n") } + /** + * Remembers the comments. The text itself stays unchanged, js-yaml drops the comments. + */ + preprocess() { + this.findComments() + } + /** * Finds all comments in a given yaml. Stores full line comments with their following line * and inline comments with the code they belong to. diff --git a/src/core/processor/processor-pipeline.ts b/src/core/processor/processor-pipeline.ts index e144173..626a1a7 100644 --- a/src/core/processor/processor-pipeline.ts +++ b/src/core/processor/processor-pipeline.ts @@ -7,91 +7,69 @@ import { SpacingProcessor } from "./spacing-processor" import { TabsToSpacesProcessor } from "./tabs-to-spaces-processor" import { Settings } from "../settings" +/** + * A processor that hides parts of a document from js-yaml and restores them afterwards. + */ +interface ReversibleProcessor { + text: string + preprocess(): void + postprocess(): void +} + +/** + * Runs the processors around loading and dumping a document with js-yaml. + * + * `preprocess()` applies the enabled processors in a fixed order, `postprocess()` restores their + * values in the reverse order, so a processor always sees the text the way it left it. + */ export class ProcessorPipeline { - arrayprocessor!: ArrayProcessor - blockprocessor!: BlockProcessor - commentprocessor!: CommentProcessor - helmProcessor!: HelmProcessor - octalprocessor!: OctalProcessor - spacingprocessor!: SpacingProcessor - tabstospacespreprocessor!: TabsToSpacesProcessor text: string settings: Settings + private applied: ReversibleProcessor[] = [] constructor(text: string, settings = new Settings()) { this.text = text this.settings = settings } - preprocess() { - this.tabstospacespreprocessor = new TabsToSpacesProcessor(this.text, this.settings) - this.tabstospacespreprocessor.preprocess() - this.text = this.tabstospacespreprocessor.text - - if (this.settings.useBlockProcessor) { - this.blockprocessor = new BlockProcessor(this.text) - this.blockprocessor.preprocess() - this.text = this.blockprocessor.text - } - - if (this.settings.useArrayProcessor) { - this.arrayprocessor = new ArrayProcessor(this.text) - this.arrayprocessor.preprocess() - this.text = this.arrayprocessor.text - } - - if (this.settings.useHelmProcessor) { - this.helmProcessor = new HelmProcessor(this.text) - this.helmProcessor.preprocess() - this.text = this.helmProcessor.text - } - - if (this.settings.useOctalProcessor) { - this.octalprocessor = new OctalProcessor(this.text) - this.octalprocessor.preprocess() - this.text = this.octalprocessor.text - } + /** + * @returns Factories for the enabled processors, in the order of preprocessing. + */ + private enabledProcessors(): ((text: string) => ReversibleProcessor)[] { + const processors: [boolean, (text: string) => ReversibleProcessor][] = [ + [this.settings.useBlockProcessor, (text) => new BlockProcessor(text)], + [this.settings.useArrayProcessor, (text) => new ArrayProcessor(text)], + [this.settings.useHelmProcessor, (text) => new HelmProcessor(text)], + [this.settings.useOctalProcessor, (text) => new OctalProcessor(text)], + [this.settings.useCommentProcessor, (text) => new CommentProcessor(text)], + ] + return processors.filter(([enabled]) => enabled).map(([, create]) => create) + } - if (this.settings.useCommentProcessor) { - this.commentprocessor = new CommentProcessor(this.text) - this.commentprocessor.findComments() - this.text = this.commentprocessor.text - } + preprocess() { + const tabsToSpaces = new TabsToSpacesProcessor(this.text, this.settings) + tabsToSpaces.preprocess() + this.text = tabsToSpaces.text + // each processor works on the text the previous one left + this.applied = this.enabledProcessors().map((create) => { + const processor = create(this.text) + processor.preprocess() + this.text = processor.text + return processor + }) } postprocess() { - if (this.settings.useCommentProcessor) { - this.commentprocessor.text = this.text - this.commentprocessor.postprocess() - this.text = this.commentprocessor.text - } - - if (this.settings.useOctalProcessor) { - this.octalprocessor.text = this.text - this.octalprocessor.postprocess() - this.text = this.octalprocessor.text - } + // add empty lines first: the hidden values, e.g. the content of block scalars, must not get + // any, and comments are put directly above their key afterwards + const spacing = new SpacingProcessor(this.text, this.settings) + spacing.postprocess() + this.text = spacing.text - if (this.settings.useHelmProcessor) { - this.helmProcessor.text = this.text - this.helmProcessor.postprocess() - this.text = this.helmProcessor.text + for (const processor of [...this.applied].reverse()) { + processor.text = this.text + processor.postprocess() + this.text = processor.text } - - if (this.settings.useArrayProcessor) { - this.arrayprocessor.text = this.text - this.arrayprocessor.postprocess() - this.text = this.arrayprocessor.text - } - - if (this.settings.useBlockProcessor) { - this.blockprocessor.text = this.text - this.blockprocessor.postprocess() - this.text = this.blockprocessor.text - } - - this.spacingprocessor = new SpacingProcessor(this.text, this.settings) - this.spacingprocessor.postprocess() - this.text = this.spacingprocessor.text } } diff --git a/src/test/unit/core/processor/processor-pipeline.test.ts b/src/test/unit/core/processor/processor-pipeline.test.ts index 117bff6..4bd3414 100644 --- a/src/test/unit/core/processor/processor-pipeline.test.ts +++ b/src/test/unit/core/processor/processor-pipeline.test.ts @@ -1,6 +1,16 @@ -import { equal, notEqual } from "assert" +import { equal, notEqual, ok } from "assert" import { ProcessorPipeline } from "../../../../core/processor/processor-pipeline" +import { Settings } from "../../../../core/settings" + +/** + * @returns Settings with all processors enabled, except the given one. + */ +const settingsWithout = (setting: "useArrayProcessor" | "useCommentProcessor" | "useHelmProcessor" | "useOctalProcessor" | "useBlockProcessor"): Settings => { + const settings = new Settings() + settings[setting] = false + return settings +} suite("Test ProcessorPipeline - preprocess() & postprocess()", () => { test("text should be the same after pre- and postprocessing", () => { @@ -23,108 +33,92 @@ suite("Test ProcessorPipeline - preprocess() & postprocess()", () => { equal(processor.text, text) }) - test("test setting useArrayProcessor = true", () => { - const text = "test: [ \"CMD\", \"pg_isready\", \"-q\", \"-d\", \"DB_NAME\", \"-U\", \"DB_USER\" ]" - const processor = new ProcessorPipeline(text) + test("should replace tabs with spaces", () => { + const processor = new ProcessorPipeline("a:\n\tb: 1") processor.preprocess() - equal(processor.arrayprocessor.store.size, 1) + equal(processor.text, "a:\n b: 1") }) - test("test setting useArrayProcessor = false", () => { - let text = "test: [ \"CMD\", \"pg_isready\", \"-q\", \"-d\", \"DB_NAME\", \"-U\", \"DB_USER\" ]" + + test("should hide and restore arrays when useArrayProcessor = true", () => { + const text = "test: [ \"CMD\", \"pg_isready\" ]" const processor = new ProcessorPipeline(text) - processor.settings.useArrayProcessor = false processor.preprocess() - equal(processor.arrayprocessor, undefined) - - text = "test: vscode-yaml-sort.array.0" - processor.text = text - + equal(processor.text, "test: vscode-yaml-sort.array.0") processor.postprocess() - equal(processor.text, text) }) - test("test setting useCommentProcessor = true", () => { - const text = - "# comment\n" + - "bar: baz" - const processor = new ProcessorPipeline(text) + test("should leave arrays alone when useArrayProcessor = false", () => { + const text = "test: [ \"CMD\", \"pg_isready\" ]" + const processor = new ProcessorPipeline(text, settingsWithout("useArrayProcessor")) processor.preprocess() - equal(processor.commentprocessor.store.length, 1) + equal(processor.text, text) }) - test("test setting useCommentProcessor = false", () => { - const text = - "# comment\n" + - "bar: baz" - const processor = new ProcessorPipeline(text) - processor.settings.useCommentProcessor = false - processor.preprocess() - - equal(processor.commentprocessor, undefined) + test("should restore comments when useCommentProcessor = true", () => { + const processor = new ProcessorPipeline("# comment\nbar: baz") + processor.preprocess() processor.text = "bar: baz" processor.postprocess() - notEqual(processor.text, text) + equal(processor.text, "# comment\nbar: baz") }) - test("test setting useHelmProcessor = true", () => { - const text = - "foo: {{ .Value }}\n" + - "bar: baz" - const processor = new ProcessorPipeline(text) + test("should drop comments when useCommentProcessor = false", () => { + const processor = new ProcessorPipeline("# comment\nbar: baz", settingsWithout("useCommentProcessor")) processor.preprocess() + processor.text = "bar: baz" + processor.postprocess() - equal(processor.helmProcessor.store.size, 1) + equal(processor.text, "bar: baz") }) - test("test setting useHelmProcessor = false", () => { - let text = "foo: {{ .Value }}" - const processor = new ProcessorPipeline(text) - processor.settings.useHelmProcessor = false + + test("should hide and restore helm values when useHelmProcessor = true", () => { + const processor = new ProcessorPipeline("foo: {{ .Value }}\nbar: baz") processor.preprocess() - equal(processor.helmProcessor, undefined) + equal(processor.text, "foo: vscode-yaml-sort.helm.0\nbar: baz") + }) - text = "foo: vscode-yaml-sort.helm.0" - processor.text = text + test("should leave helm values alone when useHelmProcessor = false", () => { + const processor = new ProcessorPipeline("foo: {{ .Value }}", settingsWithout("useHelmProcessor")) - processor.postprocess() + processor.preprocess() - equal(processor.text, text) + equal(processor.text, "foo: {{ .Value }}") }) - test("test setting useHelmProcessor = true", () => { - const text = - "foo: {{ .Value }}\n" + - "bar: baz" - const processor = new ProcessorPipeline(text) + test("should leave octal values alone when useOctalProcessor = false", () => { + const processor = new ProcessorPipeline("foo: 0644", settingsWithout("useOctalProcessor")) processor.preprocess() - equal(processor.helmProcessor.store.size, 1) + equal(processor.text, "foo: 0644") }) - test("test setting useOctalProcessor = false", () => { - let text = "foo: 0644" - const processor = new ProcessorPipeline(text) - processor.settings.useOctalProcessor = false + test("should hide block scalars when useBlockProcessor = true", () => { + const processor = new ProcessorPipeline("a: |\n text") processor.preprocess() - equal(processor.octalprocessor, undefined) + ok(processor.text.startsWith("a: vscode-yaml-sort.block.0")) + }) - text = "foo: vscode-yaml-sort.octal.0" - processor.text = text + test("should restore the values in reverse order, so nested substitutes are resolved", () => { + // the helm value is inside the array, which the array processor hides first + const text = "a: [ \"{{ .Value }}\", b ]" + const processor = new ProcessorPipeline(text) + processor.preprocess() processor.postprocess() equal(processor.text, text) @@ -138,12 +132,11 @@ suite("Test ProcessorPipeline - preprocess() & postprocess()", () => { " some command\n" + " # comment 2\n" + " some other command" - const processor = new ProcessorPipeline(text) - - processor.preprocess() - processor.postprocess() + const processor = new ProcessorPipeline(text) - equal(processor.text, text) + processor.preprocess() + processor.postprocess() - }) + equal(processor.text, text) + }) }) diff --git a/src/test/unit/core/yaml-sorter.test.ts b/src/test/unit/core/yaml-sorter.test.ts index 2e560ab..d9fdd35 100644 --- a/src/test/unit/core/yaml-sorter.test.ts +++ b/src/test/unit/core/yaml-sorter.test.ts @@ -586,3 +586,18 @@ suite("Test YamlSorter - sortYaml() with custom sort keeps the document intact", equal(yamlSorter.sortYaml("42", 1), "42") }) }) + +suite("Test YamlSorter - sortYaml() with emptyLinesUntilLevel", () => { + const settings = new Settings() + settings.emptyLinesUntilLevel = 2 + const yamlSorter = new YamlSorter(settings) + + test("should not add empty lines inside a block scalar, which would change its value", () => { + equal(yamlSorter.sortYaml("b: 1\na: |\n foo: bar\n baz: 1"), "a: |\n foo: bar\n baz: 1\n\nb: 1") + }) + + test("should add the empty line above the comment of a key, not between them", () => { + equal(yamlSorter.sortYaml("b: 1\n# note\na: 1"), "# note\na: 1\n\nb: 1") + }) +}) + From 885268a3ff8a765809a24ef0c2f8f0331bdb1b5a Mon Sep 17 00:00:00 2001 From: Pascal Reitermann Date: Fri, 20 Nov 2026 14:29:40 +0000 Subject: [PATCH 11/18] fix(selection): keep empty lines of an indented selection empty An indented selection is sorted unindented and indented again afterwards (#53). prependWhitespacesOnEachLine() also indented empty lines, so every empty line of the selection, e.g. inside a block scalar, ended up with trailing whitespaces. Empty lines are no longer indented. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Jdb5ivdJ5L75kyEDAbuvMZ --- CHANGELOG.md | 1 + src/core/indentation.ts | 5 +++-- src/test/unit/core/indentation.test.ts | 7 +++++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3682e89..e4cd178 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased ๐Ÿ› Bugfixes +* Sorting an indented selection no longer adds trailing whitespaces to its empty lines * Setting `emptyLinesUntilLevel` no longer adds empty lines inside block scalars (`key: |`), which changed their value, and no longer separates a comment from the key below it * Custom sort writes the keywords like every other key: a keyword that needs quotes is quoted (before, `'a: b': 1` became the invalid `a: b: 1`), a value containing a colon stays on the line of its keyword instead of moving to the next line, and the result no longer ends with an extra line break when every key is a keyword * Keep a document that only contains an empty mapping (`{}`) when sorting or formatting. Before, it became an empty document diff --git a/src/core/indentation.ts b/src/core/indentation.ts index 930c9a0..368ebbb 100644 --- a/src/core/indentation.ts +++ b/src/core/indentation.ts @@ -1,6 +1,6 @@ /** * Prepends a given count of whitespaces to every single line in a text. - * Lines with yaml seperators (---) will not be indented + * Lines with yaml separators (---) and empty lines will not be indented * @param {string} text Text which should get some leading whitespaces on each line. * @param {number} count The number of whitesspaces to prepend on each line of the returned string. * @returns {string} Input Text, which has the given count of whitespaces prepended on each single line. @@ -11,7 +11,8 @@ export function prependWhitespacesOnEachLine(text: string, count: number): strin } const spaces = " ".repeat(count) - return text.replace(/^(?!---)/mg, spaces) + // empty lines stay empty instead of getting trailing whitespaces + return text.replace(/^(?!---|$)/mg, spaces) } /** diff --git a/src/test/unit/core/indentation.test.ts b/src/test/unit/core/indentation.test.ts index 10fe9e2..3feca0e 100644 --- a/src/test/unit/core/indentation.test.ts +++ b/src/test/unit/core/indentation.test.ts @@ -47,8 +47,11 @@ suite("Test prependWhitespacesOnEachLine", () => { throws(() => prependWhitespacesOnEachLine(actual, -1)) }) const actual2 = "text\n" - test("should return ` text\\n ` when `text\\n` and 2 are passed", () => { - equal(prependWhitespacesOnEachLine(actual2, 2), " text\n ") + test("should not indent the empty line after a trailing line break", () => { + equal(prependWhitespacesOnEachLine(actual2, 2), " text\n") + }) + test("should not add trailing whitespaces to empty lines", () => { + equal(prependWhitespacesOnEachLine("a: |\n line1\n\n line2", 2), " a: |\n line1\n\n line2") }) }) From 87b8e92d5cf2b341c35d0a869bfaeae8fdc479ab Mon Sep 17 00:00:00 2001 From: Pascal Reitermann Date: Fri, 20 Nov 2026 14:30:24 +0000 Subject: [PATCH 12/18] fix(files): skip dependencies and unchanged files in directory sort Running "Recursively sort YAML files" on a project folder also sorted every yaml file below node_modules, i.e. it rewrote the files of installed dependencies. It also rewrote every file, even when nothing changed, which touches modification times and triggers file watchers, and processed a file twice if its extension was configured twice. * YamlFileFinder ignores node_modules and .git * configured extensions are de-duplicated * sortFile() only writes a file whose content changed The README describes the skipped folders. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Jdb5ivdJ5L75kyEDAbuvMZ --- CHANGELOG.md | 1 + README.md | 2 +- src/core/file-sorter.ts | 7 ++++-- src/core/yaml-file-finder.ts | 13 +++++++--- src/test/unit/core/file-sorter.test.ts | 16 +++++++++++- src/test/unit/core/yaml-file-finder.test.ts | 28 +++++++++++++++++++++ 6 files changed, 59 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4cd178..d5cd37d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased ๐Ÿ› Bugfixes +* Command `Recursively sort YAML files` skips the folders `node_modules` and `.git`, so it no longer rewrites the yaml files of installed dependencies. Files that are already sorted are no longer written, and a file is processed once even if its extension is configured twice * Sorting an indented selection no longer adds trailing whitespaces to its empty lines * Setting `emptyLinesUntilLevel` no longer adds empty lines inside block scalars (`key: |`), which changed their value, and no longer separates a comment from the key below it * Custom sort writes the keywords like every other key: a keyword that needs quotes is quoted (before, `'a: b': 1` became the invalid `a: b: 1`), a value containing a colon stays on the line of its keyword instead of moving to the next line, and the result no longer ends with an extra line break when every key is a keyword diff --git a/README.md b/README.md index 6f852f2..9e8f912 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, 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. | +| `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. Files in `node_modules` and `.git` are skipped, files that are already sorted are not rewritten. | | `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/src/core/file-sorter.ts b/src/core/file-sorter.ts index fcaad03..495cd4a 100644 --- a/src/core/file-sorter.ts +++ b/src/core/file-sorter.ts @@ -4,7 +4,7 @@ import { YamlOperations, sortDocuments } from "./document-transformer" import { YamlSorter } from "./yaml-sorter" /** - * Sorts all yaml documents of a file and writes the result back. + * Sorts all yaml documents of a file and writes the result back, if it differs. * 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. @@ -16,5 +16,8 @@ export const sortFile = (file: string, yaml: YamlOperations = new YamlSorter()): if (text.endsWith("\n") && !sorted.endsWith("\n")) { sorted += "\n" } - writeFileSync(file, sorted) + // an unchanged file keeps its modification time, so watchers and build tools are not triggered + if (sorted !== text) { + writeFileSync(file, sorted) + } } diff --git a/src/core/yaml-file-finder.ts b/src/core/yaml-file-finder.ts index cad02b6..0aa96be 100644 --- a/src/core/yaml-file-finder.ts +++ b/src/core/yaml-file-finder.ts @@ -4,6 +4,9 @@ import { sync } from "glob" import { Settings } from "./settings" +/** Folders whose files belong to other projects or tools and must never be rewritten. */ +const IGNORED_FOLDERS = ["**/node_modules/**", "**/.git/**"] + export class YamlFileFinder { settings: Settings @@ -12,15 +15,17 @@ export class YamlFileFinder { } /** - * Finds all files with one of the configured extensions in a directory and its subdirectories. + * Finds all files with one of the configured extensions in a directory and its subdirectories, + * except for installed dependencies (node_modules) and git internals. * @param path Directory to search. - * @returns Paths of the files, starting with the given directory. + * @returns Paths of the files, starting with the given directory. Each file is listed once. */ getFiles(path: string): string[] { + const extensions = [...new Set(this.settings.getExtensions())] // 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)) + return extensions.flatMap((extension) => + sync(`**/*.${extension}`, { cwd: path, dot: true, ignore: IGNORED_FOLDERS }).map((file) => join(path, file)) ) } } diff --git a/src/test/unit/core/file-sorter.test.ts b/src/test/unit/core/file-sorter.test.ts index 5cb3861..bcfd34a 100644 --- a/src/test/unit/core/file-sorter.test.ts +++ b/src/test/unit/core/file-sorter.test.ts @@ -1,5 +1,5 @@ import { doesNotThrow, equal, ok, throws } from "assert" -import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "fs" +import { chmodSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "fs" import { tmpdir } from "os" import { join } from "path" @@ -64,4 +64,18 @@ suite("Test FileSorter - sortFile()", () => { rmSync(dir, { recursive: true, force: true }) } }) + + test("should not write a file that is already sorted", () => { + const dir = mkdtempSync(join(tmpdir(), "yaml-sort-")) + const file = join(dir, "sorted.yaml") + try { + writeFileSync(file, "---\na: 1\nb: 2\n") + // a write to a read-only file would throw + chmodSync(file, 0o444) + + doesNotThrow(() => sortFile(file)) + } finally { + rmSync(dir, { recursive: true, force: true }) + } + }) }) diff --git a/src/test/unit/core/yaml-file-finder.test.ts b/src/test/unit/core/yaml-file-finder.test.ts index e805e60..b886907 100644 --- a/src/test/unit/core/yaml-file-finder.test.ts +++ b/src/test/unit/core/yaml-file-finder.test.ts @@ -1,4 +1,7 @@ import { deepEqual } from "assert" +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "fs" +import { tmpdir } from "os" +import { join } from "path" import { Settings } from "../../../core/settings" import { YamlFileFinder } from "../../../core/yaml-file-finder" @@ -32,3 +35,28 @@ suite("Test YamlFileFinder - getFiles()", () => { deepEqual(fileFinder.getFiles("./src/test/files/getYamlFilesInDirectory"), expected) }) }) + +suite("Test YamlFileFinder - getFiles() in a project", () => { + let dir: string + + setup(() => { + dir = mkdtempSync(join(tmpdir(), "yaml-finder-")) + mkdirSync(join(dir, "node_modules", "dependency"), { recursive: true }) + writeFileSync(join(dir, "node_modules", "dependency", "config.yaml"), "a: 1") + writeFileSync(join(dir, "app.yaml"), "a: 1") + }) + + teardown(() => rmSync(dir, { recursive: true, force: true })) + + test("should not return files of installed dependencies in node_modules", () => { + deepEqual(new YamlFileFinder().getFiles(dir), [join(dir, "app.yaml")]) + }) + + test("should return every file once, even if an extension is configured twice", () => { + const settings = new Settings() + settings.extensions = ["yaml", "yaml"] + + deepEqual(new YamlFileFinder(settings).getFiles(dir), [join(dir, "app.yaml")]) + }) +}) + From 751a5800e10e0a86677fb946ba4f5b8568ef18a4 Mon Sep 17 00:00:00 2001 From: Pascal Reitermann Date: Fri, 20 Nov 2026 14:31:29 +0000 Subject: [PATCH 13/18] fix(octal): keep octal values with comments and in lists The OctalProcessor only protected a value like 0755 when it directly followed `key: ` and ended the line. js-yaml loads every other 0755 as the decimal number 755, so sorting changed mode: 0755 # rwxr-xr-x -> mode: 755 # rwxr-xr-x - 0755 -> - 755 Also match list items and values followed by a comment. A value that only starts with such digits, like 0755-1, is still left alone. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Jdb5ivdJ5L75kyEDAbuvMZ --- CHANGELOG.md | 1 + src/core/processor/octal-processor.ts | 4 ++- .../core/processor/octal-processor.test.ts | 29 +++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d5cd37d..e4635a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased ๐Ÿ› Bugfixes +* Keep the leading zero of octal values like `0755` that are followed by a comment or are items of a list. Before, they became `755` * Command `Recursively sort YAML files` skips the folders `node_modules` and `.git`, so it no longer rewrites the yaml files of installed dependencies. Files that are already sorted are no longer written, and a file is processed once even if its extension is configured twice * Sorting an indented selection no longer adds trailing whitespaces to its empty lines * Setting `emptyLinesUntilLevel` no longer adds empty lines inside block scalars (`key: |`), which changed their value, and no longer separates a comment from the key below it diff --git a/src/core/processor/octal-processor.ts b/src/core/processor/octal-processor.ts index fc498a4..13a8f97 100644 --- a/src/core/processor/octal-processor.ts +++ b/src/core/processor/octal-processor.ts @@ -2,6 +2,8 @@ import { GenericProcessor } from "./generic-processor" export class OctalProcessor extends GenericProcessor { constructor(text: string) { - super("octal", /(?<=:\s)(0[0-7]{3})$/gm, text) + // js-yaml loads a value like 0755 as the decimal number 755, which drops the leading zero of a file mode. + // match it as value of a key or a list item, optionally followed by a comment + super("octal", /(?<=(?::|^[ \t]*-)[ \t]+)0[0-7]{3}(?=[ \t]*(?:#.*)?$)/gm, text) } } diff --git a/src/test/unit/core/processor/octal-processor.test.ts b/src/test/unit/core/processor/octal-processor.test.ts index 3eda179..740428d 100644 --- a/src/test/unit/core/processor/octal-processor.test.ts +++ b/src/test/unit/core/processor/octal-processor.test.ts @@ -64,3 +64,32 @@ suite("Test OctalProcessor - matched occurrence", () => { equal(octalprocessor.text, text) }) }) + +suite("Test OctalProcessor - octal values in other positions", () => { + const cases: [string, string][] = [ + ["with an inline comment", "mode: 0755 # rwxr-xr-x"], + ["as list item", "modes:\n - 0755\n - 0644"], + ] + + cases.forEach(([name, text]) => { + test(`should keep the leading zero of an octal value ${name}`, () => { + const octalprocessor = new OctalProcessor(text) + + octalprocessor.preprocess() + + equal(/\b0[0-7]{3}\b/.test(octalprocessor.text), false) + octalprocessor.postprocess() + equal(octalprocessor.text, text) + }) + }) + + test("should not touch a value that only starts with an octal number", () => { + const text = "id: 0755-1" + const octalprocessor = new OctalProcessor(text) + + octalprocessor.preprocess() + + equal(octalprocessor.text, text) + }) +}) + From 3a7ddde1cb341aca81e29c83d961bcc4839a0541 Mon Sep 17 00:00:00 2001 From: Pascal Reitermann Date: Fri, 20 Nov 2026 14:31:36 +0000 Subject: [PATCH 14/18] fix(tabs): only replace tabs in the indentation TabsToSpacesProcessor replaced every tab of a document with spaces, also inside values: `key: "ab"` became `key: a b`, which changed the value. Only tabs in the leading whitespace of a line are replaced, which is what yaml forbids. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Jdb5ivdJ5L75kyEDAbuvMZ --- CHANGELOG.md | 1 + src/core/processor/tabs-to-spaces-processor.ts | 7 +++++-- .../core/processor/tabs-to-spaces-processor.test.ts | 13 ++++++++++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4635a0..9cfd328 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased ๐Ÿ› Bugfixes +* Keep tabs inside values, e.g. in a quoted string. Only tabs in the indentation are replaced with spaces * Keep the leading zero of octal values like `0755` that are followed by a comment or are items of a list. Before, they became `755` * Command `Recursively sort YAML files` skips the folders `node_modules` and `.git`, so it no longer rewrites the yaml files of installed dependencies. Files that are already sorted are no longer written, and a file is processed once even if its extension is configured twice * Sorting an indented selection no longer adds trailing whitespaces to its empty lines diff --git a/src/core/processor/tabs-to-spaces-processor.ts b/src/core/processor/tabs-to-spaces-processor.ts index 4c78918..ec28b5d 100644 --- a/src/core/processor/tabs-to-spaces-processor.ts +++ b/src/core/processor/tabs-to-spaces-processor.ts @@ -1,5 +1,8 @@ import { Settings } from "../settings" +/** + * Replaces tabs in the indentation with spaces, because yaml does not allow tabs for indentation. + */ export class TabsToSpacesProcessor { text: string settings: Settings @@ -11,7 +14,7 @@ export class TabsToSpacesProcessor { preprocess() { const spaces = " ".repeat(this.settings.indent) - this.text = this.text.replace(/\t/mg, spaces) + // only the indentation: a tab inside a value, e.g. in a quoted string, is part of the value + this.text = this.text.replace(/^[ \t]+/gm, (indentation) => indentation.replace(/\t/g, spaces)) } } - diff --git a/src/test/unit/core/processor/tabs-to-spaces-processor.test.ts b/src/test/unit/core/processor/tabs-to-spaces-processor.test.ts index 7dd6c99..3d9f673 100644 --- a/src/test/unit/core/processor/tabs-to-spaces-processor.test.ts +++ b/src/test/unit/core/processor/tabs-to-spaces-processor.test.ts @@ -23,4 +23,15 @@ suite("Test TabsToSpacesPreprocessor - preprocess()", () => { tabstospacespreprocessor.preprocess() equal(tabstospacespreprocessor.text, expected) }) -}) \ No newline at end of file +}) + +suite("Test TabsToSpacesPreprocessor - tabs inside values", () => { + test("should keep tabs that are not part of the indentation, e.g. inside a quoted string", () => { + const processor = new TabsToSpacesProcessor("a:\n\tb: \"x\ty\"") + + processor.preprocess() + + equal(processor.text, "a:\n b: \"x\ty\"") + }) +}) + From 04e1e35b233227867994a0941cdae168fad64e67 Mon Sep 17 00:00:00 2001 From: Pascal Reitermann Date: Fri, 20 Nov 2026 14:31:47 +0000 Subject: [PATCH 15/18] fix(test): print why the integration tests could not run runTest.ts caught every error of runTests() and only printed "Failed to run tests", so a failed VS Code download or a crashed test host left no hint in the CI log. The error is printed now. The no-op `.then().catch()` chain is replaced by a single catch. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Jdb5ivdJ5L75kyEDAbuvMZ --- src/test/runTest.ts | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/test/runTest.ts b/src/test/runTest.ts index c080c4c..f1db7ad 100644 --- a/src/test/runTest.ts +++ b/src/test/runTest.ts @@ -2,24 +2,22 @@ import { resolve } from "path" import { runTests } from "@vscode/test-electron" -async function main() { - try { - // The folder containing the Extension Manifest package.json - // Passed to `--extensionDevelopmentPath` - const extensionDevelopmentPath = resolve(__dirname, "../../") +const main = async (): Promise => { + // The folder containing the Extension Manifest package.json + // Passed to `--extensionDevelopmentPath` + const extensionDevelopmentPath = resolve(__dirname, "../../") - // The path to test runner - // Passed to --extensionTestsPath - const extensionTestsPath = resolve(__dirname, "./integration/index") + // The path to the test runner + // Passed to --extensionTestsPath + const extensionTestsPath = resolve(__dirname, "./integration/index") - // Download VS Code, unzip it and run the integration test - await runTests({ extensionDevelopmentPath, extensionTestsPath }) - } catch (err) { - /* istanbul ignore next */ - console.error("Failed to run tests") - /* istanbul ignore next */ - process.exit(1) - } + // Download VS Code, unzip it and run the integration test + await runTests({ extensionDevelopmentPath, extensionTestsPath }) } -main().then().catch() +main().catch((error: unknown) => { + /* istanbul ignore next */ + console.error("Failed to run tests:", error) + /* istanbul ignore next */ + process.exit(1) +}) From 707ae988462a1a675dc97e6f51aa44644c0ca319 Mon Sep 17 00:00:00 2001 From: Pascal Reitermann Date: Fri, 20 Nov 2026 14:32:44 +0000 Subject: [PATCH 16/18] fix(comments): find a comment's key by its name, not as a substring When sorting changes the line a comment belongs to, e.g. js-yaml drops the quotes of `a: "x"`, the comment is placed by its key. That search took the last occurrence of the key's name anywhere in the text, so # about a a: "x" data: 2 became `a: x`, `# about a`, `data: 2`: the comment moved to `data`, which contains an `a`. Inline comments moved the same way. Search for the last line that starts with the key and a colon, first with the original indentation, then with any indentation. The plain substring search stays as last resort, so a comment is rather misplaced than lost. The line searches share findLastLine(). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Jdb5ivdJ5L75kyEDAbuvMZ --- CHANGELOG.md | 1 + src/core/processor/comment-processor.ts | 52 +++++++++++-------- .../core/processor/comment-processor.test.ts | 30 +++++++++++ 3 files changed, 62 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9cfd328..402ae8e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased ๐Ÿ› Bugfixes +* Keep a comment at its key when sorting changes the key's value, e.g. removes its quotes. Before, the comment could move to another key that contains the key's name, like `data` for `a` * Keep tabs inside values, e.g. in a quoted string. Only tabs in the indentation are replaced with spaces * Keep the leading zero of octal values like `0755` that are followed by a comment or are items of a list. Before, they became `755` * Command `Recursively sort YAML files` skips the folders `node_modules` and `.git`, so it no longer rewrites the yaml files of installed dependencies. Files that are already sorted are no longer written, and a file is processed once even if its extension is configured twice diff --git a/src/core/processor/comment-processor.ts b/src/core/processor/comment-processor.ts index 591ced5..eb9ff05 100644 --- a/src/core/processor/comment-processor.ts +++ b/src/core/processor/comment-processor.ts @@ -188,18 +188,15 @@ export class CommentProcessor { return this.searchFuzzyForKeyword(text) } - searchExactMatch(text: string): number { - // an empty anchor keeps its previous behavior - if (text === "") { - return this.text.lastIndexOf(text) - } - // comments must be inserted above the exact matching line (see #221). - // a substring search would also find `mysql5:` inside a deeper indented - // line like ` mysql5:` of a `depends_on` block + /** + * @param matches Condition for a line. + * @returns The offset of the last line of the text that meets the condition, or -1. + */ + findLastLine(matches: (line: string) => boolean): number { let result = -1 let offset = 0 for (const line of this.text.split("\n")) { - if (line === text) { + if (matches(line)) { result = offset } offset += line.length + 1 @@ -207,25 +204,38 @@ export class CommentProcessor { return result } + searchExactMatch(text: string): number { + // an empty anchor keeps its previous behavior + if (text === "") { + return this.text.lastIndexOf(text) + } + // comments must be inserted above the exact matching line (see #221). + // a substring search would also find `mysql5:` inside a deeper indented + // line like ` mysql5:` of a `depends_on` block + return this.findLastLine((line) => line === text) + } + searchFuzzyForTrimmedText(text: string): number { // like searchExactMatch(), but the line may have more or less // surrounding whitespaces than the anchor (see #139) const trimmedText = text.trim() - let result = -1 - let offset = 0 - for (const line of this.text.split("\n")) { - if (line.trim() === trimmedText) { - result = offset - } - offset += line.length + 1 - } - return result + return this.findLastLine((line) => line.trim() === trimmedText) } searchFuzzyForKeyword(text: string): number { - // the value of a line may change while sorting, - // so only search for the keyword in front of the first colon - return this.text.lastIndexOf(text.split(":")[0]) + // the value of a line may change while sorting, e.g. quotes are removed, + // so search for the line with the same key in front of the first colon + const key = text.split(":")[0] + const sameKey = this.findLastLine((line) => line.startsWith(`${key}:`)) + if (CommentProcessor.isCommentFound(sameKey)) { + return sameKey + } + const sameKeyIndented = this.findLastLine((line) => line.trim().startsWith(`${key.trim()}:`)) + if (CommentProcessor.isCommentFound(sameKeyIndented)) { + return sameKeyIndented + } + // last resort, e.g. for a key that js-yaml quotes: rather misplace a comment than lose it + return this.text.lastIndexOf(key) } static isCommentFound(index: number): boolean { diff --git a/src/test/unit/core/processor/comment-processor.test.ts b/src/test/unit/core/processor/comment-processor.test.ts index 6c52c0f..7bac4af 100644 --- a/src/test/unit/core/processor/comment-processor.test.ts +++ b/src/test/unit/core/processor/comment-processor.test.ts @@ -250,6 +250,36 @@ suite("Test CommentProcessor - searchFuzzyForKeyword()", () => { equal(commentprocessor.searchFuzzyForKeyword(line), 17) }) + + test("should find the line of the key, not the key as part of another key", () => { + const commentprocessor = new CommentProcessor("a: x\ndata: 2") + + equal(commentprocessor.searchFuzzyForKeyword("a: \"x\""), 0) + }) + + test("should find the key with a different indentation", () => { + const commentprocessor = new CommentProcessor("b:\n a: x\n data: 2") + + equal(commentprocessor.searchFuzzyForKeyword(" a: \"x\""), 3) + }) +}) + +suite("Test CommentProcessor - comments of values that change while sorting", () => { + test("should keep a comment above its key when the quotes of the value are removed", () => { + const processor = new CommentProcessor("# about a\na: \"x\"\ndata: 2") + processor.findComments() + processor.text = "a: x\ndata: 2" + + equal(processor.postprocess(), "# about a\na: x\ndata: 2") + }) + + test("should keep an inline comment at its key when the quotes of the value are removed", () => { + const processor = new CommentProcessor("a: \"x\" # about a\ndata: 2") + processor.findComments() + processor.text = "a: x\ndata: 2" + + equal(processor.postprocess(), "a: x # about a\ndata: 2") + }) }) suite("Test CommentProcessor - isCommentFound()", () => { From 3b2504cbec9d6ca60491904e7b1f1b67414a46a8 Mon Sep 17 00:00:00 2001 From: Pascal Reitermann Date: Fri, 20 Nov 2026 14:32:59 +0000 Subject: [PATCH 17/18] docs(changelog): describe the editor command fixes and the new layers Add the user-visible fixes of the editor commands (format with a selection, edits and success message, running without an editor) and an internal section about the core/vscode split and the unit tests. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Jdb5ivdJ5L75kyEDAbuvMZ --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 402ae8e..89d32db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased ๐Ÿ› Bugfixes +* Command `Format YAML` with a selection formats the whole document. Before, it replaced the selected lines with the formatted document, so the rest of the document appeared twice. The editor commands apply their changes to the document they were run on and show the success message only after VS Code applied them. Running a command without an open editor shows a hint instead of an internal error * Keep a comment at its key when sorting changes the key's value, e.g. removes its quotes. Before, the comment could move to another key that contains the key's name, like `data` for `a` * Keep tabs inside values, e.g. in a quoted string. Only tabs in the indentation are replaced with spaces * Keep the leading zero of octal values like `0755` that are followed by a comment or are items of a list. Before, they became `755` @@ -40,6 +41,8 @@ * The extension is only activated when a yaml file is opened or one of its commands is run, instead of on every start of VS Code ๐Ÿ“ฆ Dependencies * Move `@types/js-yaml` and `@vscode/test-electron` to `devDependencies`, so they are no longer shipped with the extension +๐Ÿ—๏ธ Internal +* The sources are split into `src/core` (yaml logic, no VS Code API) and `src/vscode` (commands, formatter, notifications). The core reports problems by throwing errors, the vscode layer shows them. `npm run code-analysis` enforces the split, and `npm run test:unit` runs the core's tests in plain node within seconds # 6.6.4 - Feb 9, 2026 ๐Ÿ“ฆ Dependencies From 5899f3354bd848c3534856253f1c167f625f7ca2 Mon Sep 17 00:00:00 2001 From: Pascal Reitermann Date: Fri, 20 Nov 2026 14:33:46 +0000 Subject: [PATCH 18/18] refactor(comments): move comment recognition into yaml-comment CommentProcessor had grown beyond 200 lines and mixed two jobs: recognizing comments in a single line and putting them back into the sorted text. isLineComment() and getInlineComment() are pure functions of a line and move to core/processor/yaml-comment.ts together with their tests. The magic anchor string of a comment in the last line becomes the named constant LAST_LINE. No behavior changes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Jdb5ivdJ5L75kyEDAbuvMZ --- src/core/processor/comment-processor.ts | 57 ++++--------------- src/core/processor/yaml-comment.ts | 45 +++++++++++++++ .../core/processor/comment-processor.test.ts | 43 -------------- .../unit/core/processor/yaml-comment.test.ts | 47 +++++++++++++++ 4 files changed, 102 insertions(+), 90 deletions(-) create mode 100644 src/core/processor/yaml-comment.ts create mode 100644 src/test/unit/core/processor/yaml-comment.test.ts diff --git a/src/core/processor/comment-processor.ts b/src/core/processor/comment-processor.ts index eb9ff05..0ec9117 100644 --- a/src/core/processor/comment-processor.ts +++ b/src/core/processor/comment-processor.ts @@ -1,3 +1,8 @@ +import { getInlineComment, isLineComment } from "./yaml-comment" + +/** Anchor of a comment in the last line, which has no following line to stay above. */ +export const LAST_LINE = "vscode-yaml-sort.lastLine" + export class CommentProcessor { store: string[][] = [] inlineStore: string[][] = [] @@ -23,7 +28,7 @@ export class CommentProcessor { */ findComments() { this.lines.forEach((line, index) => { - if (CommentProcessor.isLineComment(line)) { + if (isLineComment(line)) { this.addLineToComments(index) } else { this.addInlineCommentToStore(line) @@ -31,64 +36,22 @@ export class CommentProcessor { }) } - static isLineComment(line: string) { - return /^ *#/.test(line) - } - addLineToComments(index: number) { if (index < this.lines.length-1) { this.store.push([this.lines[index], this.lines[index+1]]) } else { - this.store.push([this.lines[index], "vscode-yaml-sort.lastLine"]) + this.store.push([this.lines[index], LAST_LINE]) } } addInlineCommentToStore(line: string) { - const comment = CommentProcessor.getInlineComment(line) + const comment = getInlineComment(line) if (comment !== undefined) { this.inlineStore.push([` ${comment}`, line.slice(0, line.length - comment.length).trimEnd()]) } } - /** - * Finds the comment at the end of a line, ignoring `#` inside quoted values. - * @param line A line of yaml. - * @returns The comment starting with `#`, or undefined if the line has none. - */ - static getInlineComment(line: string): string | undefined { - let quote: string | undefined - - for (let i = 0; i < line.length; i++) { - const char = line[i] - if (quote !== undefined) { - if (quote === "\"" && char === "\\") { - // skip the escaped character, e.g. \" - i++ - } else if (quote === "'" && char === "'" && line[i + 1] === "'") { - // '' is an escaped single quote - i++ - } else if (char === quote) { - quote = undefined - } - } else if ((char === "\"" || char === "'") && CommentProcessor.startsValue(line, i)) { - quote = char - } else if (char === "#" && i > 0 && (line[i - 1] === " " || line[i - 1] === "\t")) { - return line.slice(i) - } - } - - return undefined - } - - /** - * A quote only starts a quoted value at the beginning of a value, e.g. after `key: `, `- ` or `[`. - * Elsewhere it is part of a plain value, like the apostrophe in `key: it's`. - */ - static startsValue(line: string, index: number): boolean { - return index === 0 || /[\s[{,]/.test(line[index - 1]) - } - postprocess(): string { this.reverseComments() this.store.forEach(comment => { @@ -134,7 +97,7 @@ export class CommentProcessor { } applyComment(comment: string[]) { - if (comment[1] === "vscode-yaml-sort.lastLine") { + if (comment[1] === LAST_LINE) { this.append(comment[0]) } else { this.insert(comment) @@ -167,7 +130,7 @@ export class CommentProcessor { } getIndexOfString(text: string): number { - if (text === "vscode-yaml-sort.lastLine") { + if (text === LAST_LINE) { return this.text.length } else { return this.search(text) diff --git a/src/core/processor/yaml-comment.ts b/src/core/processor/yaml-comment.ts new file mode 100644 index 0000000..7757909 --- /dev/null +++ b/src/core/processor/yaml-comment.ts @@ -0,0 +1,45 @@ +/* + * Recognizes comments in single lines of yaml. + */ + +/** + * @param line A line of yaml. + * @returns True if the whole line is a comment. + */ +export const isLineComment = (line: string): boolean => /^ *#/.test(line) + +/** + * A quote only starts a quoted value at the beginning of a value, e.g. after `key: `, `- ` or `[`. + * Elsewhere it is part of a plain value, like the apostrophe in `key: it's`. + */ +const startsValue = (line: string, index: number): boolean => index === 0 || /[\s[{,]/.test(line[index - 1]) + +/** + * Finds the comment at the end of a line, ignoring `#` inside quoted values. + * @param line A line of yaml. + * @returns The comment starting with `#`, or undefined if the line has none. + */ +export const getInlineComment = (line: string): string | undefined => { + let quote: string | undefined + + for (let i = 0; i < line.length; i++) { + const char = line[i] + if (quote !== undefined) { + if (quote === "\"" && char === "\\") { + // skip the escaped character, e.g. \" + i++ + } else if (quote === "'" && char === "'" && line[i + 1] === "'") { + // '' is an escaped single quote + i++ + } else if (char === quote) { + quote = undefined + } + } else if ((char === "\"" || char === "'") && startsValue(line, i)) { + quote = char + } else if (char === "#" && i > 0 && (line[i - 1] === " " || line[i - 1] === "\t")) { + return line.slice(i) + } + } + + return undefined +} diff --git a/src/test/unit/core/processor/comment-processor.test.ts b/src/test/unit/core/processor/comment-processor.test.ts index 7bac4af..4fa219e 100644 --- a/src/test/unit/core/processor/comment-processor.test.ts +++ b/src/test/unit/core/processor/comment-processor.test.ts @@ -26,24 +26,6 @@ suite("Test CommentProcessor - findComments()", () => { }) }) -suite("Test CommentProcessor - isLineComment()", () => { - test("when input is '# comment' should return true", () => { - equal(CommentProcessor.isLineComment("# comment"), true) - }) - test("when input is ' # comment' should return true", () => { - equal(CommentProcessor.isLineComment(" # comment"), true) - }) - test("when input is 'text # comment' should return false", () => { - equal(CommentProcessor.isLineComment("text # comment"), false) - }) - test("when input is 'text' should return false", () => { - equal(CommentProcessor.isLineComment("text"), false) - }) - test("when input is an empty string should return false", () => { - equal(CommentProcessor.isLineComment(""), false) - }) -}) - suite("Test CommentProcessor - addLineToComments()", () => { test("should add comments to array 'comments'", () => { const text = @@ -523,31 +505,6 @@ suite("Test CommentProcessor - GitHub issue #222", () => { }) }) -suite("Test CommentProcessor - getInlineComment()", () => { - const detectionCases: { line: string, expectedComment: string | undefined }[] = [ - { line: "key: value # comment", expectedComment: "# comment" }, - { line: "key: \"value # quoted\"", expectedComment: undefined }, - { line: "key: 'value # quoted'", expectedComment: undefined }, - { line: "key: value#noComment", expectedComment: undefined }, - { line: "key: \"a\" # comment", expectedComment: "# comment" }, - { line: "# full line comment", expectedComment: undefined }, - // an apostrophe inside a plain value does not start a quoted value - { line: "key: it's fine # comment", expectedComment: "# comment" }, - { line: "key: don't # comment 'quoted'", expectedComment: "# comment 'quoted'" }, - // escaped quotes do not end a quoted value - { line: "key: \"a \\\" # b\" # comment", expectedComment: "# comment" }, - { line: "key: 'a '' # b' # comment", expectedComment: "# comment" }, - { line: "- 'a # b' # comment", expectedComment: "# comment" }, - { line: "key: [\"a # b\", 'c # d'] # comment", expectedComment: "# comment" } - ] - - detectionCases.forEach(({ line, expectedComment }) => { - test(`should only detect comments outside of quotes: ${line}`, () => { - equal(CommentProcessor.getInlineComment(line), expectedComment) - }) - }) -}) - suite("Test CommentProcessor - inline comments of values with apostrophes", () => { test("should keep the inline comment of a value containing an apostrophe", () => { const processor = new CommentProcessor("c: it's fine # note\nb: 1") diff --git a/src/test/unit/core/processor/yaml-comment.test.ts b/src/test/unit/core/processor/yaml-comment.test.ts new file mode 100644 index 0000000..4bc2b4a --- /dev/null +++ b/src/test/unit/core/processor/yaml-comment.test.ts @@ -0,0 +1,47 @@ +import { equal } from "assert" + +import { getInlineComment, isLineComment } from "../../../../core/processor/yaml-comment" + +suite("Test isLineComment()", () => { + test("when input is '# comment' should return true", () => { + equal(isLineComment("# comment"), true) + }) + test("when input is ' # comment' should return true", () => { + equal(isLineComment(" # comment"), true) + }) + test("when input is 'text # comment' should return false", () => { + equal(isLineComment("text # comment"), false) + }) + test("when input is 'text' should return false", () => { + equal(isLineComment("text"), false) + }) + test("when input is an empty string should return false", () => { + equal(isLineComment(""), false) + }) +}) + + +suite("Test getInlineComment()", () => { + const detectionCases: { line: string, expectedComment: string | undefined }[] = [ + { line: "key: value # comment", expectedComment: "# comment" }, + { line: "key: \"value # quoted\"", expectedComment: undefined }, + { line: "key: 'value # quoted'", expectedComment: undefined }, + { line: "key: value#noComment", expectedComment: undefined }, + { line: "key: \"a\" # comment", expectedComment: "# comment" }, + { line: "# full line comment", expectedComment: undefined }, + // an apostrophe inside a plain value does not start a quoted value + { line: "key: it's fine # comment", expectedComment: "# comment" }, + { line: "key: don't # comment 'quoted'", expectedComment: "# comment 'quoted'" }, + // escaped quotes do not end a quoted value + { line: "key: \"a \\\" # b\" # comment", expectedComment: "# comment" }, + { line: "key: 'a '' # b' # comment", expectedComment: "# comment" }, + { line: "- 'a # b' # comment", expectedComment: "# comment" }, + { line: "key: [\"a # b\", 'c # d'] # comment", expectedComment: "# comment" } + ] + + detectionCases.forEach(({ line, expectedComment }) => { + test(`should only detect comments outside of quotes: ${line}`, () => { + equal(getInlineComment(line), expectedComment) + }) + }) +})