Skip to content

Commit

Permalink
Fix #38: How to sort on save?
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalre committed Apr 2, 2022
1 parent d72c5ea commit 1e10f6e
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 166 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Changelog
## 5.3.0 - April 02, 2022
* (refs [#38](https://github.com/pascalre/vscode-yaml-sort/issues/38)) How to sort on save?
* New configuration `sortOnSave`

## 5.2.2 - March 31, 2022
* Fix [CVE-2021-44906](https://nvd.nist.gov/vuln/detail/CVE-2021-44906)

Expand Down
65 changes: 39 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,56 @@ YAML Sort extends VS Code to sort, format and validate YAML files.
# Commands
This extension contributes the following commands:

| Command | Description |
|------------------------------------------------|-------------------------------------------------------------------------------------------------------|
| Command | Description |
|------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------|
| `Custom Sort 1` | This command will sort a given YAML with custom order. If some of the keys of `customSortKeywords_1` will be found at the top level of the YAML, these will be put at the beginning of the YAML file (in the given order). You can use this e. g. to sort Kubernetes configmaps. |
| `Custom Sort 2` | Same as `Custom Sort 1` |
| `Custom Sort 3` | Same as `Custom Sort 1` |
| `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 (e. g. `SHIFT` + `OPTION` + `F` on Mac). |
| `Recursively sort YAML files` | Sorts all `.yaml` and `.yml` files in a directory and all its subdirectories. |
| `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. |
| `Recursively sort YAML files` | Sorts all `.yaml` and `.yml` files in a directory and all its subdirectories. |
| `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. |

# Configuration
This extension contributes the following settings:

| Setting | Description | Default |
|----------------------------|-------------------------------------------------------------------------------------------------------------------------------------- | ---------------- |
| `emptyLinesUntilLevel` | When bigger than `0`, will add a new line before each keyword on level n. | `0` |
| `customSortKeywords_1` | List of keywords for `Custom Sort 1` | `["apiVersion", "kind", "metadata", "spec", "data"]`|
| `customSortKeywords_2` | List of keywords for `Custom Sort 2` | - |
| `customSortKeywords_3` | List of keywords for `Custom Sort 3` | - |
| `forceQuotes` | When `true`, all non-key strings will be quoted even if they normally don't need to. | `false` |
| `indent` | Indentation width in spaces | `2` |
| `lineWidth` | Maximum line width for YAML files | `500` |
| `locale` | Language whose sort order should be used | `en` |
| `noArrayIndent` | When `true`, will not add an indentation level to array elements. | `false` |
| `noCompatMode` | if `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 | `false` |
| `quotingType` | Strings will be quoted using this quoting style. If you specify single quotes, double quotes will still be used for non-printable characters. | `'` |
| `schema` | Schema to use. Possible values are `HOMEASSISTANT_SCHEMA`, `CLOUDFORMATION_SCHEMA`, `CORE_SCHEMA`, `DEFAULT_SCHEMA`, `FAILSAFE_SCHEMA`, `JSON_SCHEMA`. | `DEFAULT_SCHEMA` |
| `useAsFormatter` | When `true`, will enable default YAML formatter (requires restart). | `false` |
| `useCustomSortRecursively` | When `true`, will use the custom sort keywords recursively on a file, when using custom sort. | `false` |
| `useLeadingDashes` | When `true`, sorted YAML files begin with leading dashes. | `true` |
| Setting | Description | Default |
|----------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------| ---------------- |
| `emptyLinesUntilLevel` | When bigger than `0`, will add a new line before each keyword on level n. | `0` |
| `customSortKeywords_1` | List of keywords for `Custom Sort 1` | `["apiVersion", "kind", "metadata", "spec", "data"]`|
| `customSortKeywords_2` | List of keywords for `Custom Sort 2` | - |
| `customSortKeywords_3` | List of keywords for `Custom Sort 3` | - |
| `forceQuotes` | When `true`, all non-key strings will be quoted even if they normally don't need to. | `false` |
| `indent` | Indentation width in spaces | `2` |
| `lineWidth` | Maximum line width for YAML files | `500` |
| `locale` | Language whose sort order should be used | `en` |
| `noArrayIndent` | When `true`, will not add an indentation level to array elements. | `false` |
| `noCompatMode` | 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 | `false` |
| `quotingType` | Strings will be quoted using this quoting style. If you specify single quotes, double quotes will still be used for non-printable characters. | `'` |
| `schema` | Schema to use. Possible values are `HOMEASSISTANT_SCHEMA`, `CLOUDFORMATION_SCHEMA`, `CORE_SCHEMA`, `DEFAULT_SCHEMA`, `FAILSAFE_SCHEMA`, `JSON_SCHEMA`. | `DEFAULT_SCHEMA` |
| `sortOnSave` | When `true`, will sort file when saving document. Only works in combination with `editor.formatOnSave` and `vscode-yaml-sort.useAsFormatter` both set to `true`. | `true` |
| `useAsFormatter` | When `true`, will enable default YAML formatter (requires restart). | `false` |
| `useCustomSortRecursively` | When `true`, will use the custom sort keywords recursively on a file, when using custom sort. | `false` |
| `useLeadingDashes` | When `true`, sorted YAML files begin with leading dashes. | `true` |

# 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 `true`.

#### **`.vscode/settings.json`**
```json
{
"editor.formatOnSave": true,
"vscode-yaml-sort.sortOnSave": true,
"vscode-yaml-sort.useAsFormatter": true
}
```

# Support

If you like YAML Sort, please feel free to [rate it](https://marketplace.visualstudio.com/items?itemName=PascalReitermann93.vscode-yaml-sort&ssr=false#review-details) on the marketplace.

# Issues
If you miss something or found a bug, please let me know and [open an issue](https://github.com/pascalre/vscode-yaml-sort/issues/new) on this project on GitHub. I am very happy about every issue which makes this project better. Do not hesitate to open a pull request with your changes.
If you miss something or found a bug, please let me know and [open an issue](https://github.com/pascalre/vscode-yaml-sort/issues/new) on this project on GitHub. Do not hesitate to open a pull request with your changes.

Check [open issues](https://github.com/pascalre/vscode-yaml-sort/issues) on GitHub.

Expand Down
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-yaml-sort",
"displayName": "YAML Sort",
"description": "This VS Code extension exposes the possibility to sort, format and validate yaml files.",
"version": "5.2.2",
"version": "5.3.0",
"engines": {
"vscode": "^1.49.0"
},
Expand Down Expand Up @@ -120,7 +120,7 @@
"vscode-yaml-sort.noCompatMode": {
"type": "boolean",
"default": false,
"description": "If 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"
"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"
},
"vscode-yaml-sort.quotingType": {
"type": "string",
Expand All @@ -144,6 +144,11 @@
],
"description": "Schema to use"
},
"vscode-yaml-sort.sortOnSave": {
"type": "boolean",
"default": true,
"description": "When true, will sort files when saving"
},
"vscode-yaml-sort.useAsFormatter": {
"type": "boolean",
"default": false,
Expand Down Expand Up @@ -242,4 +247,4 @@
"homeassistant-js-yaml-schema": "^1.0.2",
"js-yaml": "^4.1.0"
}
}
}

0 comments on commit 1e10f6e

Please sign in to comment.