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
@@ -1,4 +1,9 @@
# Changelog
## 6.0.0 - October 4, 2022
* (refs [#85](https://github.com/pascalre/vscode-yaml-sort/issues/85)) Is custom sort on save planned?
* New default for `sortOnSave` allows to specify custom sort for auto-saving
* Update dependencies to latest versions

## 5.5.0 - September 29, 2022
* (refs [#65](https://github.com/pascalre/vscode-yaml-sort/issues/65)) No need to send notification when used as a formatter
* New configuration `notifySuccess`
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ This extension contributes the following settings:
| `notifySuccess` | When `true`, will notify on successfully performed tasks. | `true` |
| `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` |
| `sortOnSave` | When `0`, will sort files when saving document. When `1`, `2` or `3`, will use customSortKeywords. Set to negative value to disable sortOnSave. Only works in combination with `editor.formatOnSave` and `vscode-yaml-sort.useAsFormatter` both set to `true`. | `0` |
| `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`.
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.

#### **`.vscode/settings.json`**
```json
{
"editor.formatOnSave": true,
"vscode-yaml-sort.sortOnSave": true,
"vscode-yaml-sort.sortOnSave": 0,
"vscode-yaml-sort.useAsFormatter": true
}
```
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 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.5.0",
"version": "6.0.0",
"engines": {
"vscode": "^1.49.0"
},
Expand Down Expand Up @@ -150,9 +150,9 @@
"description": "Schema to use"
},
"vscode-yaml-sort.sortOnSave": {
"type": "boolean",
"default": true,
"description": "When true, will sort files when saving"
"type": "number",
"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.useAsFormatter": {
"type": "boolean",
Expand Down Expand Up @@ -228,7 +228,7 @@
},
"devDependencies": {
"@types/mocha": "10.0.0",
"@types/node": "^18.7.23",
"@types/node": "18.8.0",
"@typescript-eslint/eslint-plugin": "5.38.1",
"@typescript-eslint/parser": "5.38.1",
"coveralls": "3.1.1",
Expand All @@ -252,4 +252,4 @@
"homeassistant-js-yaml-schema": "^1.1.0",
"js-yaml": "^4.1.0"
}
}
}
14 changes: 7 additions & 7 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export function activate(context: vscode.ExtensionContext) {

const formatter: vscode.DocumentFormattingEditProvider = {
provideDocumentFormattingEdits(): vscode.TextEdit[] {
const sortOnSave = vscode.workspace.getConfiguration().get('vscode-yaml-sort.sortOnSave') as boolean;
if (sortOnSave) {/* istanbul ignore next */
return sortYamlWrapper()
const sortOnSave = vscode.workspace.getConfiguration().get('vscode-yaml-sort.sortOnSave') as number;
if (sortOnSave >= 0 && sortOnSave <= 3) {/* istanbul ignore next */
return sortYamlWrapper(sortOnSave)
} else {/* istanbul ignore next */
return formatYamlWrapper()
}
Expand Down Expand Up @@ -230,7 +230,7 @@ export function sortYamlWrapper(customSort = 0): vscode.TextEdit[] {

if (validYaml) {
const edits = vscode.TextEdit.replace(rangeToBeReplaced, newText)
if (notifySuccess) {
if (notifySuccess) {
vscode.window.showInformationMessage("Keys resorted successfully")
}
applyEdits(activeEditor, [edits])
Expand Down Expand Up @@ -495,7 +495,7 @@ export function findComments(text: string): Map<string, string> {
i++
}
comment = comment.replace(/\n$/, "")
if (comment != "" ) {
if (comment != "") {
if (i < lines.length) {
comments.set(comment, lines[i])
} else {
Expand All @@ -512,7 +512,7 @@ export function applyComments(text: string, comments: Map<string, string>): stri
text += "\n" + comment
} else {
let index = text.search(line)
if (index == -1 ) {
if (index == -1) {
console.log("Comment not found. Searching for lines with other indentation...")
const trimmedLine = line.trim()
index = text.search(trimmedLine)
Expand All @@ -525,7 +525,7 @@ export function applyComments(text: string, comments: Map<string, string>): stri
textAfterComment += text.slice(text.search(trimmedLine))
text = textBeforeComment + "\n" + comment.split("\n")[0] + textAfterComment
}
} else {
} else {
text = text.slice(0, index) + comment + "\n" + text.slice(text.search(line))
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/files/customSort.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
spec: foo
data: bar
Loading