diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bf4692..1c3e226 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 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 * (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/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/controller.ts b/src/controller/controller.ts index 1770979..42b5a14 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,47 +71,19 @@ 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 | null 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[] + return [] } - - 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) + 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 new file mode 100644 index 0000000..04e7a52 --- /dev/null +++ b/src/controller/document-transformer.ts @@ -0,0 +1,90 @@ +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, 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 | null => { + validateTextRange(text) + new JsYamlAdapter().validateYaml(text) + + const prepared = removeDirectives(text, yaml.getDelimiters(text, !options.isSelection)) + let newText = "" + for (const unsortedYaml of splitYaml(prepared.text)) { + let sortedYaml = yaml.sortYaml(unsortedYaml, options.customSort) + 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) +} + +/** + * 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/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/document-transformer.test.ts b/src/test/suite/controller/document-transformer.test.ts new file mode 100644 index 0000000..26eb126 --- /dev/null +++ b/src/test/suite/controller/document-transformer.test.ts @@ -0,0 +1,53 @@ +import { equal, throws } from "assert" + +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", () => { + 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 - 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") + }) +}) 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), []) + }) +})