Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased
🐛 Bugfixes
* (refs [#65](https://github.com/pascalre/vscode-yaml-sort/issues/65)) Formatting via "Format Document" or on save no longer shows "Yaml formatted successfully", once per document. The command `Format YAML` shows it once after the changes were applied
* Command `Recursively sort YAML files` now sorts files with several documents, works without an open editor and is only offered for folders in the explorer. A file that cannot be sorted is reported and left unchanged. Files are now also found on Windows paths
* Keep every document delimiter (`---`, including a comment behind it) in front of its own document. Before, several documents without a leading `---` got a duplicated `---` (an additional empty document), and with a leading `--- # comment` the comments moved to the next document or got lost
* Keep yaml directives like `%YAML 1.2` when sorting or formatting, and accept them when validating. Before, files with directives could not be processed
* Keep empty documents (`---` directly followed by `---`) instead of aborting
* Setting `sortArrays` now sorts arrays with mixed item types deterministically: numbers (numerically), booleans and strings first, mappings and nested lists keep their order at the end. Before, such arrays were left in an arbitrary order
* Setting `sortOrderReverse` now also applies to the default sort and to the keys following the keywords of a custom sort. Before, it only worked with `useCustomSortRecursively`
* (closes [#53](https://github.com/pascalre/vscode-yaml-sort/issues/53)) Sort an indented selection, e.g. a part of a list or the children of a nested key, without having to unindent it first. The selection keeps its indentation
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This extension contributes the following commands:
| `Custom Sort 2` | Same as `Custom Sort 1` |
| `Custom Sort 3` | Same as `Custom Sort 1` |
| `Format Document` | Formats a yaml document without sorting it. Also possible using the shortcut (⇧⌥F on Mac). |
| `Recursively sort YAML files` | Sorts all `.yaml` and `.yml` files in a directory and all its subdirectories. A newline at the end of a file will be retained. |
| `Recursively sort YAML files` | Sorts all `.yaml` and `.yml` files in a directory and all its subdirectories, including files with several documents. Run it from the context menu of a folder in the explorer. A newline at the end of a file will be retained. |
| `Sort YAML` | Sorts a given YAML. You can either sort the whole YAML document or sort only a selection of the text. |
| `Validate YAML` | Validates a given YAML. |

Expand Down
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,17 @@
}
],
"menus": {
"commandPalette": [
{
"command": "vscode-yaml-sort.sortYamlFilesInDirectory",
"when": "false"
}
],
"explorer/context": [
{
"command": "vscode-yaml-sort.sortYamlFilesInDirectory",
"title": "Recursively sort YAML files"
"title": "Recursively sort YAML files",
"when": "explorerResourceIsFolder"
}
]
}
Expand Down
7 changes: 4 additions & 3 deletions src/adapter/js-yaml-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { dump, load } from "js-yaml"

import { Settings } from "../settings"
import { SortUtil } from "../util/sort-util"
import { removeTrailingCharacters, splitYaml } from "../util/yaml-text"
import { removeTrailingCharacters, splitDocuments } from "../util/yaml-text"

/**
* @param value Any loaded yaml value.
Expand All @@ -24,8 +24,9 @@ export class JsYamlAdapter {
* @returns true if yaml is valid
*/
validateYaml(text: string): boolean {
splitYaml(text).forEach((yaml) => {
load(yaml, { schema: this.settings.getSchema() })
// directives are not part of a document's content, so they are not loaded (see splitDocuments)
splitDocuments(text).documents.forEach((document) => {
load(document.content, { schema: this.settings.getSchema() })
})
return true
}
Expand Down
28 changes: 5 additions & 23 deletions src/controller/controller.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { TextEdit, TextEditor, Uri, window } from "vscode"
import { TextEdit, TextEditor, window } from "vscode"

import { formatDocuments, sortDocuments } from "./document-transformer"
import { JsYamlAdapter } from "../adapter/js-yaml-adapter"
import { Severity, VsCodeAdapter } from "../adapter/vs-code-adapter"
import { Settings } from "../settings"
import { ErrorUtil } from "../util/error-util"
import { FileUtil } from "../util/file-util"
import { YamlUtil } from "../util/yaml-util"

export class Controller {
editor: TextEditor
fileutil = new FileUtil()
yamlutil = new YamlUtil()
jsyamladapter = new JsYamlAdapter()
vscodeadapter = new VsCodeAdapter()
Expand All @@ -37,31 +35,15 @@ export class Controller {
return false
}

/**
* Sorts all yaml files in a directory
* @param {Uri} uri Base URI
*/
sortYamlFiles(uri: Uri) {
const files = this.fileutil.getFiles(uri.fsPath)
files.forEach((file: string) => {
this.sortFile(file)
})
}

sortFile(file: string) {
try {
this.fileutil.sortFile(file)
} catch (error) {
this.errorutil.handleError(error)
}
}

formatYamlWrapper(): TextEdit[] {
const newText = formatDocuments(VsCodeAdapter.getActiveDocument(this.editor), this.yamlutil)
if (newText === null) {
return []
}
return this.applyEdits(newText)
const edits = this.applyEdits(newText)
// only the command reports success, the formatter stays silent (see #65)
this.vscodeadapter.showMessage(Severity.INFO, "Yaml formatted successfully")
return edits
}

applyEdits(text: string) {
Expand Down
92 changes: 52 additions & 40 deletions src/controller/document-transformer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { JsYamlAdapter } from "../adapter/js-yaml-adapter"
import { prependWhitespacesOnEachLine, removeLeadingLineBreakOfFirstElement, removeLeadingSpacesOnEachLine } from "../lib"
import { splitYaml, validateTextRange } from "../util/yaml-text"
import { prependWhitespacesOnEachLine, removeLeadingSpacesOnEachLine } from "../lib"
import { YamlDocument, YamlStream, splitDocuments, validateTextRange } from "../util/yaml-text"
import { YamlUtil } from "../util/yaml-util"

/**
* The parts of YamlUtil a transformation depends on. Kept narrow so tests can pass a stub.
*/
export type YamlOperations = Pick<YamlUtil, "formatYaml" | "getDelimiters" | "setLeadingDashes" | "sortYaml">
export type YamlOperations = Pick<YamlUtil, "formatYaml" | "settings" | "sortYaml">

export interface SortOptions {
/** 0 for the default sort, 1 to 3 for the custom sort keywords. */
Expand All @@ -15,28 +15,48 @@
isSelection: boolean
}

interface PreparedText {
text: string
delimiters: string[]
}
/**
* Transforms the content of a single, non-empty document. Returns null if that fails.
*/
type ContentTransformation = (content: string) => string | null

const DIRECTIVE = /^%.*\n/m
const DIRECTIVES = /^%.*\n/gm
/**
* Puts the transformed documents back together.
* @param stream The directives and the documents, with their transformed content.
* @param leadingDashes Whether the first document should start with a delimiter. Directives always keep theirs.
* @returns The joined text.
*/
const joinDocuments = (stream: YamlStream, leadingDashes: boolean | undefined): string => {
const rendered = stream.documents.map((document, index) => {
let delimiter = document.delimiter
if (index === 0 && leadingDashes !== undefined && stream.directives === "") {
// undefined keeps the first delimiter as it is, e.g. for a selection
delimiter = leadingDashes ? (delimiter ?? "---") : undefined
}
return [delimiter, document.content].filter((line) => line).join("\n")

Check warning on line 36 in src/controller/document-transformer.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

arrow function is equivalent to `Boolean`. Use `Boolean` directly.

See more on https://sonarcloud.io/project/issues?id=pascalre_vscode-yaml-sort&issues=AaH1aEPUu47YQoh2NUe8&open=AaH1aEPUu47YQoh2NUe8&pullRequest=326
})
return stream.directives + rendered.join("\n")
}

/**
* Removes yaml directives (e.g. `%YAML 1.1`) and collects the document delimiters.
* @param text Text containing one or more yaml documents.
* @param delimiters Delimiters as returned by YamlUtil.getDelimiters().
* @returns The text without directives and the delimiters to put in front of each document.
* Applies a transformation to every non-empty document. Empty documents are kept as they are.
* @returns The transformed stream, or null if one document could not be transformed.
*/
const removeDirectives = (text: string, delimiters: RegExpMatchArray): PreparedText => {
let adjustedDelimiters = delimiters
if (DIRECTIVE.test(text)) {
adjustedDelimiters.shift()
adjustedDelimiters = removeLeadingLineBreakOfFirstElement(adjustedDelimiters)
const transformDocuments = (stream: YamlStream, transform: ContentTransformation): YamlStream | null => {
const documents: YamlDocument[] = []
for (const document of stream.documents) {
if (document.content.trim() === "") {
documents.push({ delimiter: document.delimiter, content: "" })
continue
}
const content = transform(document.content)
if (!content) {
// never return a partial result: it would drop documents or empty the selection
return null
}
documents.push({ delimiter: document.delimiter, content })
}
const withoutDirectives = text.replace(DIRECTIVES, "").replace(/^\n/, "")
return { text: withoutDirectives, delimiters: adjustedDelimiters }
return { directives: stream.directives, documents }
}

/**
Expand All @@ -52,19 +72,16 @@
validateTextRange(text)
new JsYamlAdapter().validateYaml(text)

const prepared = removeDirectives(text, yaml.getDelimiters(text, !options.isSelection))
let newText = ""
for (const unsortedYaml of splitYaml(prepared.text)) {
const sorted = transformDocuments(splitDocuments(text), (content) => {
// an indented selection is sorted like a top-level document and indented again afterwards (see #53)
const indentation = options.isSelection ? YamlUtil.getNumberOfLeadingSpaces(unsortedYaml) : 0
const sortedYaml = yaml.sortYaml(removeLeadingSpacesOnEachLine(unsortedYaml, indentation), options.customSort)
if (!sortedYaml) {
// never return a partial result: it would drop documents or empty the selection
return null
}
newText += (prepared.delimiters.shift() ?? "") + prependWhitespacesOnEachLine(sortedYaml, indentation)
const indentation = options.isSelection ? YamlUtil.getNumberOfLeadingSpaces(content) : 0
const sortedContent = yaml.sortYaml(removeLeadingSpacesOnEachLine(content, indentation), options.customSort)
return sortedContent ? prependWhitespacesOnEachLine(sortedContent, indentation) : null
})
if (!sorted) {
return null
}
return options.isSelection ? newText : yaml.setLeadingDashes(newText)
return joinDocuments(sorted, options.isSelection ? undefined : yaml.settings.useLeadingDashes)
}

/**
Expand All @@ -75,14 +92,9 @@
* (the error has already been reported to the user).
*/
export const formatDocuments = (text: string, yaml: YamlOperations = new YamlUtil()): string | null => {
const prepared = removeDirectives(text, yaml.getDelimiters(text, true))
let newText = ""
for (const unformattedYaml of splitYaml(prepared.text)) {
const formattedYaml = yaml.formatYaml(unformattedYaml, false)
if (!formattedYaml) {
return null
}
newText += (prepared.delimiters.shift() ?? "") + formattedYaml
const formatted = transformDocuments(splitDocuments(text), (content) => yaml.formatYaml(content, false))
if (!formatted) {
return null
}
return yaml.setLeadingDashes(newText)
return joinDocuments(formatted, yaml.settings.useLeadingDashes)
}
48 changes: 48 additions & 0 deletions src/controller/file-sorter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { readFileSync, writeFileSync } from "fs"

Check warning on line 1 in src/controller/file-sorter.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `node:fs` over `fs`.

See more on https://sonarcloud.io/project/issues?id=pascalre_vscode-yaml-sort&issues=AaH1aERKu47YQoh2NUe9&open=AaH1aERKu47YQoh2NUe9&pullRequest=326

import { Uri } from "vscode"

import { YamlOperations, sortDocuments } from "./document-transformer"
import { Severity, VsCodeAdapter } from "../adapter/vs-code-adapter"
import { ErrorUtil } from "../util/error-util"
import { FileUtil } from "../util/file-util"
import { YamlUtil } from "../util/yaml-util"

/**
* Sorts all yaml documents of a file and writes the result back.
* A trailing line break of the original file is retained (see #186).
* @param file Path of the file.
* @param yaml Yaml operations, injectable for tests.
* @throws Error if the file cannot be read, is not valid yaml or cannot be sorted.
*/
export const sortFile = (file: string, yaml: YamlOperations = new YamlUtil()): void => {
const text = readFileSync(file, "utf-8")
let sorted = sortDocuments(text, { customSort: 0, isSelection: false }, yaml)
if (sorted === null) {
throw new Error(`File ${file} could not be sorted`)
}
if (text.endsWith("\n") && !sorted.endsWith("\n")) {
sorted += "\n"
}
writeFileSync(file, sorted)
}

/**
* Command handler: sorts every yaml file in a directory and its subdirectories.
* A file that cannot be sorted is reported and left unchanged; the other files are still sorted.
* @param uri The directory selected in the explorer. Undefined when the command is run from the command palette.
*/
export const sortYamlFilesInDirectory = (uri: Uri | undefined): void => {
const errorutil = new ErrorUtil()
if (!uri) {
new VsCodeAdapter().showMessage(Severity.ERROR, "Please run this command from the context menu of a folder in the explorer.")
return
}
for (const file of new FileUtil().getFiles(uri.fsPath)) {
try {
sortFile(file)
} catch (error) {
errorutil.handleError(error)
}
}
}
5 changes: 3 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ExtensionContext, Uri, commands } from "vscode"

import { VsCodeAdapter } from "./adapter/vs-code-adapter"
import { Controller } from "./controller/controller"
import { sortYamlFilesInDirectory } from "./controller/file-sorter"
import { YamlFormattingProvider } from "./controller/formatting-provider"

export function activate(context: ExtensionContext) {
Expand All @@ -16,8 +17,8 @@ export function activate(context: ExtensionContext) {
commands.registerCommand("vscode-yaml-sort.formatYaml", () => {
new Controller().formatYamlWrapper()
}),
commands.registerCommand("vscode-yaml-sort.sortYamlFilesInDirectory", (uri: Uri) => {
new Controller().sortYamlFiles(uri)
commands.registerCommand("vscode-yaml-sort.sortYamlFilesInDirectory", (uri?: Uri) => {
sortYamlFilesInDirectory(uri)
})
)
for (const index of [1, 2, 3]) {
Expand Down
15 changes: 0 additions & 15 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,6 @@ export function prependWhitespacesOnEachLine(text: string, count: number): strin
return text.replace(/^(?!---)/mg, spaces)
}

/**
* Removes the leading line break of the first element of an array.
* @param {RegExpMatchArray} delimiters Array for processing.
* @returns {RegExpMatchArray}
*/
export function removeLeadingLineBreakOfFirstElement(delimiters: RegExpMatchArray): RegExpMatchArray {
let firstDelimiter = delimiters.shift()
if (firstDelimiter) {
firstDelimiter = firstDelimiter.replace(/^\n/, "")
delimiters.unshift(firstDelimiter)
}
return delimiters
}


/**
* Removes up to a given count of leading spaces from every single line in a text.
* The counterpart of prependWhitespacesOnEachLine().
Expand Down
Loading
Loading