From 055788f0287674947cd6a9aae2809118a3fc433a Mon Sep 17 00:00:00 2001 From: Pascal Reitermann Date: Thu, 19 Nov 2026 12:57:32 +0000 Subject: [PATCH 1/3] refactor(controller): extract multi-document handling into document-transformer sortYamlWrapper() and formatYamlWrapper() duplicated the handling of yaml directives, delimiters and the loop over all documents, and mixed it with editor access. Move it into sortDocuments() and formatDocuments(), which work on plain text and accept the yaml operations as a dependency, so they can be tested without a VS Code instance. No behavior change. Characterization tests cover the moved code. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Jdb5is9YsMnteaxHryN26M --- src/controller/controller.ts | 69 ++------------ src/controller/document-transformer.ts | 89 +++++++++++++++++++ .../controller/document-transformer.test.ts | 29 ++++++ 3 files changed, 127 insertions(+), 60 deletions(-) create mode 100644 src/controller/document-transformer.ts create mode 100644 src/test/suite/controller/document-transformer.test.ts diff --git a/src/controller/controller.ts b/src/controller/controller.ts index 1770979..82162fe 100644 --- a/src/controller/controller.ts +++ b/src/controller/controller.ts @@ -1,12 +1,12 @@ import { TextEdit, TextEditor, Uri, 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 { prependWhitespacesOnEachLine, removeLeadingLineBreakOfFirstElement } from "../lib" import { Settings } from "../settings" import { ErrorUtil } from "../util/error-util" import { FileUtil } from "../util/file-util" -import { YamlUtil, splitYaml, validateTextRange } from "../util/yaml-util" +import { YamlUtil } from "../util/yaml-util" export class Controller { editor: TextEditor @@ -57,29 +57,10 @@ export class Controller { } formatYamlWrapper(): TextEdit[] { - let doc = VsCodeAdapter.getActiveDocument(this.editor) - let delimiters = this.yamlutil.getDelimiters(doc, true) - // remove yaml metadata tags - const matchMetadata = /^%.*\n/gm - // set metadata tags, if there is no metadata tag it should be an emtpy array - if (matchMetadata.test(doc)) { - delimiters.shift() - delimiters = removeLeadingLineBreakOfFirstElement(delimiters) + const newText = formatDocuments(VsCodeAdapter.getActiveDocument(this.editor), this.yamlutil) + if (newText === null) { + return [] } - doc = doc.replace(matchMetadata, "") - doc = doc.replace(/^\n/, "") - - const yamls = splitYaml(doc) - let newText = "" - for (const unformattedYaml of yamls) { - const formattedYaml = this.yamlutil.formatYaml(unformattedYaml, false) - if (formattedYaml) { - newText += delimiters.shift() + formattedYaml - } else { - return [] - } - } - newText = this.yamlutil.setLeadingDashes(newText) return this.applyEdits(newText) } @@ -90,45 +71,13 @@ export class Controller { } sortYamlWrapper(customSort = 0): TextEdit[] { - const textRange = VsCodeAdapter.getRange(this.editor) - let text = VsCodeAdapter.getText(this.editor, textRange) - + const text = VsCodeAdapter.getText(this.editor, VsCodeAdapter.getRange(this.editor)) + let newText: string try { - validateTextRange(text) - this.jsyamladapter.validateYaml(text) + newText = sortDocuments(text, { customSort, isSelection: !this.editor.selection.isEmpty }, this.yamlutil) } catch (error) { this.errorutil.handleError(error) - return [] as TextEdit[] - } - - let delimiters = this.yamlutil.getDelimiters(text, this.editor.selection.isEmpty) - // remove yaml metadata tags - const matchMetadata = /^%.*\n/gm - // set metadata tags, if there is no metadata tag it should be an emtpy array - if (matchMetadata.test(text)) { - delimiters.shift() - delimiters = removeLeadingLineBreakOfFirstElement(delimiters) - } - text = text.replace(matchMetadata, "") - text = text.replace(/^\n/, "") - - // sort yaml - let newText = "" - splitYaml(text).forEach((unsortedYaml) => { - let sortedYaml = this.yamlutil.sortYaml(unsortedYaml, customSort) - if (sortedYaml) { - if (!this.editor.selection.isEmpty) { - // get number of leading whitespaces, these whitespaces will be used for indentation - const indentation = YamlUtil.getNumberOfLeadingSpaces(unsortedYaml) - sortedYaml = prependWhitespacesOnEachLine(sortedYaml, indentation) - } - newText += delimiters.shift() + sortedYaml - } else { - return [] as TextEdit[] - } - }) - if (this.editor.selection.isEmpty) { - newText = this.yamlutil.setLeadingDashes(newText) + return [] } this.vscodeadapter.showMessage(Severity.INFO, "Keys resorted successfully") return this.applyEdits(newText) diff --git a/src/controller/document-transformer.ts b/src/controller/document-transformer.ts new file mode 100644 index 0000000..c6f9bd6 --- /dev/null +++ b/src/controller/document-transformer.ts @@ -0,0 +1,89 @@ +import { JsYamlAdapter } from "../adapter/js-yaml-adapter" +import { prependWhitespacesOnEachLine, removeLeadingLineBreakOfFirstElement } from "../lib" +import { YamlUtil, splitYaml, validateTextRange } from "../util/yaml-util" + +/** + * The parts of YamlUtil a transformation depends on. Kept narrow so tests can pass a stub. + */ +export type YamlOperations = Pick + +export interface SortOptions { + /** 0 for the default sort, 1 to 3 for the custom sort keywords. */ + customSort: number + /** True when the text is a selection instead of the whole document. */ + isSelection: boolean +} + +interface PreparedText { + text: string + delimiters: string[] +} + +const DIRECTIVE = /^%.*\n/m +const DIRECTIVES = /^%.*\n/gm + +/** + * Removes yaml directives (e.g. `%YAML 1.1`) and collects the document delimiters. + * @param text Text containing one or more yaml documents. + * @param delimiters Delimiters as returned by YamlUtil.getDelimiters(). + * @returns The text without directives and the delimiters to put in front of each document. + */ +const removeDirectives = (text: string, delimiters: RegExpMatchArray): PreparedText => { + let adjustedDelimiters = delimiters + if (DIRECTIVE.test(text)) { + adjustedDelimiters.shift() + adjustedDelimiters = removeLeadingLineBreakOfFirstElement(adjustedDelimiters) + } + const withoutDirectives = text.replace(DIRECTIVES, "").replace(/^\n/, "") + return { text: withoutDirectives, delimiters: adjustedDelimiters } +} + +/** + * 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. + * @throws Error if the text is not valid yaml or the selection ends invalid. + */ +export const sortDocuments = (text: string, options: SortOptions, yaml: YamlOperations = new YamlUtil()): string => { + validateTextRange(text) + new JsYamlAdapter().validateYaml(text) + + const prepared = removeDirectives(text, yaml.getDelimiters(text, !options.isSelection)) + let newText = "" + splitYaml(prepared.text).forEach((unsortedYaml) => { + let sortedYaml = yaml.sortYaml(unsortedYaml, options.customSort) + if (sortedYaml) { + if (options.isSelection) { + // the leading whitespaces of the selection are used for indentation + const indentation = YamlUtil.getNumberOfLeadingSpaces(unsortedYaml) + sortedYaml = prependWhitespacesOnEachLine(sortedYaml, indentation) + } + newText += prepared.delimiters.shift() + sortedYaml + } else { + return [] + } + }) + return options.isSelection ? newText : yaml.setLeadingDashes(newText) +} + +/** + * 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). + */ +export const formatDocuments = (text: string, yaml: YamlOperations = new YamlUtil()): string | null => { + const prepared = removeDirectives(text, yaml.getDelimiters(text, true)) + let newText = "" + for (const unformattedYaml of splitYaml(prepared.text)) { + const formattedYaml = yaml.formatYaml(unformattedYaml, false) + if (!formattedYaml) { + return null + } + newText += prepared.delimiters.shift() + formattedYaml + } + return yaml.setLeadingDashes(newText) +} diff --git a/src/test/suite/controller/document-transformer.test.ts b/src/test/suite/controller/document-transformer.test.ts new file mode 100644 index 0000000..fc65db7 --- /dev/null +++ b/src/test/suite/controller/document-transformer.test.ts @@ -0,0 +1,29 @@ +import { equal, throws } from "assert" + +import { formatDocuments, sortDocuments } from "../../../controller/document-transformer" + +suite("Test DocumentTransformer - sortDocuments()", () => { + test("should sort every document and keep the delimiters", () => { + const text = "---\nb: 1\na: 2\n---\nd: 1\nc: 2" + + equal(sortDocuments(text, { customSort: 0, isSelection: false }), "---\na: 2\nb: 1\n---\nc: 2\nd: 1") + }) + + test("should not add leading dashes to a selection", () => { + equal(sortDocuments("b: 1\na: 2", { customSort: 0, isSelection: true }), "a: 2\nb: 1") + }) + + test("should throw on invalid yaml", () => { + throws(() => sortDocuments("a: [", { customSort: 0, isSelection: false })) + }) + + test("should throw when a selection ends with a colon", () => { + throws(() => sortDocuments("key:", { customSort: 0, isSelection: true }), /YAML selection is invalid/) + }) +}) + +suite("Test DocumentTransformer - formatDocuments()", () => { + test("should format every document and keep the delimiters", () => { + equal(formatDocuments("---\nkey1: value\n---\nkey2: value"), "---\nkey1: value\n---\nkey2: value") + }) +}) From 8a31b23a2b33cf2ea24f10dfbe07b3084d449002 Mon Sep 17 00:00:00 2001 From: Pascal Reitermann Date: Thu, 19 Nov 2026 12:58:10 +0000 Subject: [PATCH 2/3] fix(controller): abort sorting when a yaml document cannot be sorted The loop over all documents used forEach with `return []`, which only left the callback. A document that could not be sorted was dropped from the result, the delimiters shifted, and "Keys resorted successfully" was shown anyway. For a selection that could not be sorted, e.g. an indented section (refs #53), the result was an empty string, so the selection was deleted. sortDocuments() now returns null as soon as one document fails, and the controller applies no edits in that case. The success message is shown after the edits were applied. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Jdb5is9YsMnteaxHryN26M --- CHANGELOG.md | 1 + src/controller/controller.ts | 8 +++-- src/controller/document-transformer.ts | 29 ++++++++++--------- .../controller/document-transformer.test.ts | 26 ++++++++++++++++- 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bf4692..212bfb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased 🐛 Bugfixes +* Abort sorting when one of several yaml documents cannot be sorted. Previously the failing document was silently dropped, and a selection that could not be sorted (e.g. an indented section, refs [#53](https://github.com/pascalre/vscode-yaml-sort/issues/53)) was replaced with nothing. The success message is now only shown after the edits were applied * Keep octal values such as `0755` when the same digits appear earlier in the document. Previously the first occurrence was substituted instead of the matched value, so `z: 0755` became `z: 755` * Keep every helm, array and block value at its key when a document contains more than ten of them. Previously the substitute `vscode-yaml-sort.helm.1` also matched the prefix of `vscode-yaml-sort.helm.10`, which swapped or lost values * (refs [#186](https://github.com/pascalre/vscode-yaml-sort/issues/186)) Retain newline at end of file when using command `Recursively sort YAML files` diff --git a/src/controller/controller.ts b/src/controller/controller.ts index 82162fe..42b5a14 100644 --- a/src/controller/controller.ts +++ b/src/controller/controller.ts @@ -72,14 +72,18 @@ export class Controller { sortYamlWrapper(customSort = 0): TextEdit[] { const text = VsCodeAdapter.getText(this.editor, VsCodeAdapter.getRange(this.editor)) - let newText: string + let newText: string | null 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 this.applyEdits(newText) + return edits } } diff --git a/src/controller/document-transformer.ts b/src/controller/document-transformer.ts index c6f9bd6..04e7a52 100644 --- a/src/controller/document-transformer.ts +++ b/src/controller/document-transformer.ts @@ -43,28 +43,29 @@ const removeDirectives = (text: string, delimiters: RegExpMatchArray): PreparedT * @param text Text containing one or more yaml documents. * @param options Sort options. * @param yaml Yaml operations, injectable for tests. - * @returns The sorted text. + * @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. */ -export const sortDocuments = (text: string, options: SortOptions, yaml: YamlOperations = new YamlUtil()): string => { +export const sortDocuments = (text: string, options: SortOptions, yaml: YamlOperations = new YamlUtil()): string | null => { validateTextRange(text) new JsYamlAdapter().validateYaml(text) const prepared = removeDirectives(text, yaml.getDelimiters(text, !options.isSelection)) let newText = "" - splitYaml(prepared.text).forEach((unsortedYaml) => { + for (const unsortedYaml of splitYaml(prepared.text)) { let sortedYaml = yaml.sortYaml(unsortedYaml, options.customSort) - if (sortedYaml) { - if (options.isSelection) { - // the leading whitespaces of the selection are used for indentation - const indentation = YamlUtil.getNumberOfLeadingSpaces(unsortedYaml) - sortedYaml = prependWhitespacesOnEachLine(sortedYaml, indentation) - } - newText += prepared.delimiters.shift() + sortedYaml - } else { - return [] + if (!sortedYaml) { + // never return a partial result: it would drop documents or empty the selection + return null } - }) + if (options.isSelection) { + // the leading whitespaces of the selection are used for indentation + const indentation = YamlUtil.getNumberOfLeadingSpaces(unsortedYaml) + sortedYaml = prependWhitespacesOnEachLine(sortedYaml, indentation) + } + newText += (prepared.delimiters.shift() ?? "") + sortedYaml + } return options.isSelection ? newText : yaml.setLeadingDashes(newText) } @@ -83,7 +84,7 @@ export const formatDocuments = (text: string, yaml: YamlOperations = new YamlUti if (!formattedYaml) { return null } - newText += prepared.delimiters.shift() + formattedYaml + newText += (prepared.delimiters.shift() ?? "") + formattedYaml } return yaml.setLeadingDashes(newText) } diff --git a/src/test/suite/controller/document-transformer.test.ts b/src/test/suite/controller/document-transformer.test.ts index fc65db7..26eb126 100644 --- a/src/test/suite/controller/document-transformer.test.ts +++ b/src/test/suite/controller/document-transformer.test.ts @@ -1,6 +1,17 @@ import { equal, throws } from "assert" -import { formatDocuments, sortDocuments } from "../../../controller/document-transformer" +import { YamlOperations, formatDocuments, sortDocuments } from "../../../controller/document-transformer" +import { YamlUtil } from "../../../util/yaml-util" + +/** + * 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) => (text.includes(marker) ? null : original(text, customSort)) + return yamlutil +} suite("Test DocumentTransformer - sortDocuments()", () => { test("should sort every document and keep the delimiters", () => { @@ -22,6 +33,19 @@ suite("Test DocumentTransformer - sortDocuments()", () => { }) }) +suite("Test DocumentTransformer - sortDocuments() when a document cannot be sorted", () => { + test("should return null 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) + }) + + test("should return null instead of 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) + }) +}) + suite("Test DocumentTransformer - formatDocuments()", () => { test("should format every document and keep the delimiters", () => { equal(formatDocuments("---\nkey1: value\n---\nkey2: value"), "---\nkey1: value\n---\nkey2: value") From 1005353c877c418618ccd19a3a3eaf18b40e87b4 Mon Sep 17 00:00:00 2001 From: Pascal Reitermann Date: Thu, 19 Nov 2026 12:59:30 +0000 Subject: [PATCH 3/3] fix(formatter): return edits to VS Code instead of applying them twice The document formatting provider called the sort/format commands, which applied their edits via workspace.applyEdit() and also returned them. VS Code then applied the returned edits a second time, so formatting needed two undo steps (closes #180). The provider also ignored the document it was given and used the active editor and its selection. Move the provider into YamlFormattingProvider: it transforms the given document as a whole and only returns one edit with a validated full range. Register it through context.subscriptions so it is disposed. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Jdb5is9YsMnteaxHryN26M --- CHANGELOG.md | 1 + src/adapter/vs-code-adapter.ts | 6 +-- src/controller/formatting-provider.ts | 38 ++++++++++++++ src/extension.ts | 19 ++----- .../controller/formatting-provider.test.ts | 50 +++++++++++++++++++ 5 files changed, 95 insertions(+), 19 deletions(-) create mode 100644 src/controller/formatting-provider.ts create mode 100644 src/test/suite/controller/formatting-provider.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 212bfb2..1c3e226 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased 🐛 Bugfixes +* (closes [#180](https://github.com/pascalre/vscode-yaml-sort/issues/180)) "Format Document" and format on save no longer apply their changes twice. The formatter now only returns its edits to VS Code and always processes the whole document it was asked to format, instead of the active editor or its selection * Abort sorting when one of several yaml documents cannot be sorted. Previously the failing document was silently dropped, and a selection that could not be sorted (e.g. an indented section, refs [#53](https://github.com/pascalre/vscode-yaml-sort/issues/53)) was replaced with nothing. The success message is now only shown after the edits were applied * Keep octal values such as `0755` when the same digits appear earlier in the document. Previously the first occurrence was substituted instead of the matched value, so `z: 0755` became `z: 755` * Keep every helm, array and block value at its key when a document contains more than ten of them. Previously the substitute `vscode-yaml-sort.helm.1` also matched the prefix of `vscode-yaml-sort.helm.10`, which swapped or lost values diff --git a/src/adapter/vs-code-adapter.ts b/src/adapter/vs-code-adapter.ts index 060ecfc..f45d02c 100644 --- a/src/adapter/vs-code-adapter.ts +++ b/src/adapter/vs-code-adapter.ts @@ -1,4 +1,4 @@ -import { DocumentFormattingEditProvider, Position, Range, Selection, TextEdit, TextEditor, WorkspaceEdit, languages, window, workspace } from "vscode" +import { Disposable, DocumentFormattingEditProvider, Position, Range, Selection, TextEdit, TextEditor, WorkspaceEdit, languages, window, workspace } from "vscode" import { Settings } from "../settings" @@ -32,8 +32,8 @@ export class VsCodeAdapter { } } - static registerFormatter(formatter: DocumentFormattingEditProvider) { - languages.registerDocumentFormattingEditProvider("yaml", formatter) + static registerFormatter(formatter: DocumentFormattingEditProvider): Disposable { + return languages.registerDocumentFormattingEditProvider("yaml", formatter) } static getText(textEditor: TextEditor, range: Range) { diff --git a/src/controller/formatting-provider.ts b/src/controller/formatting-provider.ts new file mode 100644 index 0000000..e4c885a --- /dev/null +++ b/src/controller/formatting-provider.ts @@ -0,0 +1,38 @@ +import { DocumentFormattingEditProvider, Range, TextDocument, TextEdit } from "vscode" + +import { formatDocuments, sortDocuments } from "./document-transformer" +import { Settings } from "../settings" +import { ErrorUtil } from "../util/error-util" + +/** + * Formats (or, depending on `sortOnSave`, sorts) a whole yaml document. + * + * VS Code applies the returned edits itself, e.g. for "Format Document" or format on save, + * so this provider must never apply them on its own (see #180). + */ +export class YamlFormattingProvider implements DocumentFormattingEditProvider { + errorutil = new ErrorUtil() + + /** + * @param document The document VS Code asks to format. It may differ from the active editor. + * @returns One edit replacing the whole document, or no edit if the document cannot be processed. + */ + provideDocumentFormattingEdits(document: TextDocument): TextEdit[] { + const settings = new Settings() + const text = document.getText() + let newText: string | null + try { + newText = settings.doSortOnSave() + ? sortDocuments(text, { customSort: settings.sortOnSave, isSelection: false }) + : formatDocuments(text) + } catch (error) { + 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)] + } +} diff --git a/src/extension.ts b/src/extension.ts index a492295..9e5a271 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,25 +1,12 @@ -import { DocumentFormattingEditProvider, ExtensionContext, TextEdit, Uri, commands } from "vscode" +import { ExtensionContext, Uri, commands } from "vscode" import { VsCodeAdapter } from "./adapter/vs-code-adapter" import { Controller } from "./controller/controller" -import { Settings } from "./settings" +import { YamlFormattingProvider } from "./controller/formatting-provider" export function activate(context: ExtensionContext) { - - const formatter: DocumentFormattingEditProvider = { - provideDocumentFormattingEdits(): TextEdit[] { - const settings = new Settings() - if (settings.doSortOnSave()) { - return new Controller().sortYamlWrapper(settings.sortOnSave) - } else { - return new Controller().formatYamlWrapper() - } - } - } - - VsCodeAdapter.registerFormatter(formatter) - context.subscriptions.push( + VsCodeAdapter.registerFormatter(new YamlFormattingProvider()), commands.registerCommand("vscode-yaml-sort.sortYaml", () => { new Controller().sortYamlWrapper() }), diff --git a/src/test/suite/controller/formatting-provider.test.ts b/src/test/suite/controller/formatting-provider.test.ts new file mode 100644 index 0000000..2c877ca --- /dev/null +++ b/src/test/suite/controller/formatting-provider.test.ts @@ -0,0 +1,50 @@ +import { deepEqual, equal } from "assert" + +import { window, workspace } from "vscode" + +import { YamlFormattingProvider } from "../../../controller/formatting-provider" + +suite("Test YamlFormattingProvider - provideDocumentFormattingEdits()", () => { + const provider = new YamlFormattingProvider() + + test("should return one edit replacing the whole document with the sorted text", async () => { + const document = await workspace.openTextDocument({ language: "yaml", content: "b: 1\na: 2\n" }) + + const edits = provider.provideDocumentFormattingEdits(document) + + equal(edits.length, 1) + equal(edits[0].newText, "---\na: 2\nb: 1") + deepEqual(edits[0].range, document.validateRange(edits[0].range)) + equal(edits[0].range.start.line, 0) + equal(edits[0].range.end.line, document.lineCount - 1) + }) + + test("should not apply the edits itself, VS Code does that (#180)", async () => { + const content = "b: 1\na: 2" + const document = await workspace.openTextDocument({ language: "yaml", content }) + // the old formatter applied its edits to the active editor, so make the document active + await window.showTextDocument(document, { preview: false }) + + provider.provideDocumentFormattingEdits(document) + // a self-applied edit would show up asynchronously, so give it a chance to happen + await new Promise((resolve) => setTimeout(resolve, 500)) + + equal(document.getText(), content) + }) + + test("should process the given document, not the active editor", async () => { + const active = await workspace.openTextDocument({ language: "yaml", content: "active: 1" }) + await window.showTextDocument(active, { preview: false }) + const other = await workspace.openTextDocument({ language: "yaml", content: "d: 1\nc: 2" }) + + const edits = provider.provideDocumentFormattingEdits(other) + + equal(edits[0].newText, "---\nc: 2\nd: 1") + }) + + test("should return no edits for invalid yaml", async () => { + const document = await workspace.openTextDocument({ language: "yaml", content: "a: [" }) + + deepEqual(provider.provideDocumentFormattingEdits(document), []) + }) +})