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
20 changes: 14 additions & 6 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
{
"reporter": [
"html",
"text-lcov",
"text-summary",
"lcov"
"include": [
"out/**/*.js",
"src/**/*.ts"
],
"exclude": [
"out/test/**",
"src/test/**"
],
"reporter": [
"html",
"text",
"text-summary",
"lcov"
]
}
}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased
🐛 Bugfixes
* 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
* 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
Expand All @@ -23,7 +24,10 @@
* (closes [#221](https://github.com/pascalre/vscode-yaml-sort/issues/221)) Keep comments above their anchor while sorting, even when a deeper indented line contains the anchor as a substring

✨ Feature
* Settings can now be set per workspace folder in a multi-root workspace and per language (e.g. `"[yaml]": { "vscode-yaml-sort.indent": 4 }`). Every command and the formatter read the settings that apply to the processed document
* (closes [#204](https://github.com/pascalre/vscode-yaml-sort/issues/204)) Add setting `nullStyle` to define how null values are printed (`null`, `Null`, `NULL`, `~` or empty)
⚡ Performance
* 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

Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ This extension contributes the following settings:
| `useHelmProcessor` | When `true`, will activate HelmProcessor. | `true` |
| `useOctalProcessor` | When `true`, will activate OctalProcessor. | `true` |

All settings except `notifySuccess` can also be set per workspace folder in a multi-root workspace and, except `extensions`, per language:

#### **`.vscode/settings.json`**
```json
{
"[yaml]": {
"vscode-yaml-sort.indent": 4
}
}
```

# ❓ FAQ
## How to sort on save?
Register this extension as VS Code formatter. Also configure VS Code to format files on save. Caution: This setting will apply for all files. Changes will require a restart of VS Code. If you wish to also sort (not only format) the file on saving, set `sortOnSave` to `0`. Use `1`, `2` or `3` for custom sort.
Expand Down
75 changes: 62 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@
"Formatters"
],
"activationEvents": [
"onStartupFinished"
"onLanguage:yaml",
"onCommand:vscode-yaml-sort.customSortYaml_1",
"onCommand:vscode-yaml-sort.customSortYaml_2",
"onCommand:vscode-yaml-sort.customSortYaml_3",
"onCommand:vscode-yaml-sort.formatYaml",
"onCommand:vscode-yaml-sort.sortYaml",
"onCommand:vscode-yaml-sort.sortYamlFilesInDirectory",
"onCommand:vscode-yaml-sort.validateYaml"
],
"main": "./out/extension.js",
"contributes": {
Expand All @@ -51,11 +58,13 @@
"properties": {
"vscode-yaml-sort.emptyLinesUntilLevel": {
"type": "number",
"scope": "language-overridable",
"default": 0,
"description": "When bigger than 0, will add a new line before each keyword on level n."
},
"vscode-yaml-sort.customSortKeywords_1": {
"type": "array",
"scope": "language-overridable",
"items": {
"type": "string"
},
Expand All @@ -70,6 +79,7 @@
},
"vscode-yaml-sort.customSortKeywords_2": {
"type": "array",
"scope": "language-overridable",
"items": {
"type": "string"
},
Expand All @@ -78,6 +88,7 @@
},
"vscode-yaml-sort.customSortKeywords_3": {
"type": "array",
"scope": "language-overridable",
"items": {
"type": "string"
},
Expand All @@ -86,6 +97,7 @@
},
"vscode-yaml-sort.extensions": {
"type": "array",
"scope": "resource",
"items": {
"type": "string"
},
Expand All @@ -97,26 +109,31 @@
},
"vscode-yaml-sort.forceQuotes": {
"type": "boolean",
"scope": "language-overridable",
"default": false,
"description": "When true, all non-key strings will be quoted even if they normally don't need to."
},
"vscode-yaml-sort.indent": {
"type": "integer",
"scope": "language-overridable",
"default": 2,
"description": "Indentation width in spaces."
},
"vscode-yaml-sort.lineWidth": {
"type": "integer",
"scope": "language-overridable",
"default": 500,
"description": "Maximum line width for YAML files."
},
"vscode-yaml-sort.locale": {
"type": "string",
"scope": "language-overridable",
"default": "en",
"description": "Language whose sort order should be used."
},
"vscode-yaml-sort.nullStyle": {
"type": "string",
"scope": "language-overridable",
"default": "lowercase",
"enum": [
"lowercase",
Expand All @@ -136,11 +153,13 @@
},
"vscode-yaml-sort.noArrayIndent": {
"type": "boolean",
"scope": "language-overridable",
"default": false,
"description": "When true, will not add an indentation level to array elements."
},
"vscode-yaml-sort.noCompatMode": {
"type": "boolean",
"scope": "language-overridable",
"default": false,
"description": "When true, don't try to be compatible with older yaml versions. Currently: don't quote 'yes', 'no' and so on, as required for YAML 1.1"
},
Expand All @@ -151,6 +170,7 @@
},
"vscode-yaml-sort.quotingType": {
"type": "string",
"scope": "language-overridable",
"default": "'",
"enum": [
"'",
Expand All @@ -160,6 +180,7 @@
},
"vscode-yaml-sort.schema": {
"type": "string",
"scope": "language-overridable",
"default": "DEFAULT_SCHEMA",
"enum": [
"HOMEASSISTANT_SCHEMA",
Expand All @@ -173,51 +194,61 @@
},
"vscode-yaml-sort.sortArrays": {
"type": "boolean",
"scope": "language-overridable",
"default": false,
"description": "When `true`, will sort arrays"
},
"vscode-yaml-sort.sortOrderReverse": {
"type": "boolean",
"scope": "language-overridable",
"default": false,
"description": "When `true`, will sort in reverse order"
},
"vscode-yaml-sort.sortOnSave": {
"type": "number",
"scope": "language-overridable",
"default": 0,
"description": "When `0`, will sort files when saving. When `1`, `2` or `3`, will use customSortKeywords. Set to negative value to disable sortOnSave."
},
"vscode-yaml-sort.useCustomSortRecursively": {
"type": "boolean",
"scope": "language-overridable",
"default": false,
"description": "When true, will use the custom sort keywords recursively on a file, when using custom sort"
},
"vscode-yaml-sort.useLeadingDashes": {
"type": "boolean",
"scope": "language-overridable",
"default": true,
"description": "Define if sorted YAML files should begin with leading dashes."
},
"vscode-yaml-sort.useArrayProcessor": {
"type": "boolean",
"scope": "language-overridable",
"default": true,
"description": "When `true`, will activate ArrayProcessor"
},
"vscode-yaml-sort.useBlockProcessor": {
"type": "boolean",
"scope": "language-overridable",
"default": true,
"description": "When `true`, will activate BlockProcessor"
},
"vscode-yaml-sort.useCommentProcessor": {
"type": "boolean",
"scope": "language-overridable",
"default": true,
"description": "When `true`, will activate CommentProcessor"
},
"vscode-yaml-sort.useHelmProcessor": {
"type": "boolean",
"scope": "language-overridable",
"default": true,
"description": "When `true`, will activate HelmProcessor"
},
"vscode-yaml-sort.useOctalProcessor": {
"type": "boolean",
"scope": "language-overridable",
"default": true,
"description": "When `true`, will activate OctalProcessor"
}
Expand All @@ -226,44 +257,62 @@
"commands": [
{
"command": "vscode-yaml-sort.customSortYaml_1",
"title": "YAML Sort: Custom sort 1",
"when": "editorLangId == yaml"
"title": "YAML Sort: Custom sort 1"
},
{
"command": "vscode-yaml-sort.customSortYaml_2",
"title": "YAML Sort: Custom sort 2",
"when": "editorLangId == yaml"
"title": "YAML Sort: Custom sort 2"
},
{
"command": "vscode-yaml-sort.customSortYaml_3",
"title": "YAML Sort: Custom sort 3",
"when": "editorLangId == yaml"
"title": "YAML Sort: Custom sort 3"
},
{
"command": "vscode-yaml-sort.formatYaml",
"title": "YAML Sort: Format YAML",
"when": "editorLangId == yaml"
"title": "YAML Sort: Format YAML"
},
{
"command": "vscode-yaml-sort.sortYaml",
"title": "YAML Sort: Sort YAML",
"when": "editorLangId == yaml"
"title": "YAML Sort: Sort YAML"
},
{
"command": "vscode-yaml-sort.sortYamlFilesInDirectory",
"title": "YAML Sort: Recursively sort YAML files"
},
{
"command": "vscode-yaml-sort.validateYaml",
"title": "YAML Sort: Validate YAML",
"when": "editorLangId == yaml"
"title": "YAML Sort: Validate YAML"
}
],
"menus": {
"commandPalette": [
{
"command": "vscode-yaml-sort.sortYamlFilesInDirectory",
"when": "false"
},
{
"command": "vscode-yaml-sort.customSortYaml_1",
"when": "editorLangId == yaml"
},
{
"command": "vscode-yaml-sort.customSortYaml_2",
"when": "editorLangId == yaml"
},
{
"command": "vscode-yaml-sort.customSortYaml_3",
"when": "editorLangId == yaml"
},
{
"command": "vscode-yaml-sort.formatYaml",
"when": "editorLangId == yaml"
},
{
"command": "vscode-yaml-sort.sortYaml",
"when": "editorLangId == yaml"
},
{
"command": "vscode-yaml-sort.validateYaml",
"when": "editorLangId == yaml"
}
],
"explorer/context": [
Expand Down
5 changes: 0 additions & 5 deletions src/adapter/vs-code-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@ export enum Severity {
}

export class VsCodeAdapter {
section = "vscode-yaml-sort"
settings: Settings

constructor(settings = new Settings()) {
this.settings = settings
}

getProperty(property: string) {
return workspace.getConfiguration().get(`${this.section}.${property}`)
}

showMessage(severity: Severity, message: string) {
if (severity === Severity.ERROR) {
window.showErrorMessage(message)
Expand Down
18 changes: 11 additions & 7 deletions src/controller/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@ import { YamlUtil } from "../util/yaml-util"

export class Controller {
editor: TextEditor
yamlutil = new YamlUtil()
jsyamladapter = new JsYamlAdapter()
vscodeadapter = new VsCodeAdapter()
settings: Settings
yamlutil: YamlUtil
jsyamladapter: JsYamlAdapter
vscodeadapter: VsCodeAdapter
errorutil = new ErrorUtil()
settings = new Settings()

constructor() {
if (window.activeTextEditor) {
this.editor = window.activeTextEditor
} else {
if (!window.activeTextEditor) {
throw new Error("No texteditor is active")
}
this.editor = window.activeTextEditor
// one snapshot of the settings that apply to the edited document
this.settings = Settings.forDocument(this.editor.document)
this.yamlutil = new YamlUtil(this.settings)
this.jsyamladapter = new JsYamlAdapter(this.settings)
this.vscodeadapter = new VsCodeAdapter(this.settings)
}

validateYamlWrapper(): boolean {
Expand Down
2 changes: 1 addition & 1 deletion src/controller/document-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const transformDocuments = (stream: YamlStream, transform: ContentTransformation
*/
export const sortDocuments = (text: string, options: SortOptions, yaml: YamlOperations = new YamlUtil()): string | null => {
validateTextRange(text)
new JsYamlAdapter().validateYaml(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)
Expand Down
6 changes: 4 additions & 2 deletions src/controller/file-sorter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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"
Expand Down Expand Up @@ -38,9 +39,10 @@ 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
}
for (const file of new FileUtil().getFiles(uri.fsPath)) {
const settingsFor = (resource: Uri) => Settings.forDocument({ uri: resource, languageId: "yaml" })
for (const file of new FileUtil(settingsFor(uri)).getFiles(uri.fsPath)) {
try {
sortFile(file)
sortFile(file, new YamlUtil(settingsFor(Uri.file(file))))
} catch (error) {
errorutil.handleError(error)
}
Expand Down
Loading
Loading