Skip to content

Commit e274e48

Browse files
authored
Merge pull request #228 from pascalre/feat/reverse-order
Feat/reverse order
2 parents 554c020 + 83ac2da commit e274e48

7 files changed

Lines changed: 38 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Changelog
2+
# 6.6.0 - Oct 28, 2024
3+
✨ Feature
4+
* (refs [#225](https://github.com/pascalre/vscode-yaml-sort/issues/225)) Support sorting in reverse order
5+
26
# 6.5.18 - Oct 28, 2024
37
📦 Dependencies
48
* Update dependencies to latest versions

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ This extension contributes the following settings:
4141
| `schema` | Schema to use. Possible values are `HOMEASSISTANT_SCHEMA`, `CLOUDFORMATION_SCHEMA`, `CORE_SCHEMA`, `DEFAULT_SCHEMA`, `FAILSAFE_SCHEMA`, `JSON_SCHEMA`. | `DEFAULT_SCHEMA` |
4242
| `sortArrays` | When `true`, will sort arrays | `false` |
4343
| `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` set to `true`. | `0` |
44+
| `sortOrderReverse` | When `true`, will sort in reverse order | `false` |
4445
| `useAsFormatter` | When `true`, will enable default YAML formatter (requires restart). | `false` |
4546
| `useCustomSortRecursively` | When `true`, will use the custom sort keywords recursively on a file, when using custom sort. | `false` |
4647
| `useLeadingDashes` | When `true`, sorted YAML files begin with leading dashes. | `true` |

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vscode-yaml-sort",
33
"displayName": "YAML Sort",
44
"description": "This VS Code extension exposes the possibility to sort, format and validate yaml files.",
5-
"version": "6.5.18",
5+
"version": "6.6.0",
66
"engines": {
77
"vscode": "^1.49.0"
88
},
@@ -157,6 +157,11 @@
157157
"default": false,
158158
"description": "When `true`, will sort arrays"
159159
},
160+
"vscode-yaml-sort.sortOrderReverse": {
161+
"type": "boolean",
162+
"default": false,
163+
"description": "When `true`, will sort in reverse order"
164+
},
160165
"vscode-yaml-sort.sortOnSave": {
161166
"type": "number",
162167
"default": 0,

src/settings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export class Settings {
5050
schema = this.getSchema()
5151
sortArrays = this.getBoolean("sortArrays")
5252
sortOnSave = this.getNumber("sortOnSave")
53+
sortOrderReverse = this.getBoolean("sortOrderReverse")
5354
useCustomSortRecursively = this.getBoolean("useCustomSortRecursively")
5455
useLeadingDashes = this.getBoolean("useLeadingDashes")
5556
useArrayProcessor = this.getBoolean("useArrayProcessor")

src/test/suite/util/sort-util.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,24 @@ suite("Test Sort - customSort()", () => {
3333
test("when `custom` is `1` and keywords are `['kind', 'data']` and a is `kind` and b is `data` should return -1", () => {
3434
equal(sort.customSort("kind", "data"), -1)
3535
})
36-
test("when `custom` is `1` and keywords are `['kind', 'data']` and a is `kind` and b is `kind` should return 0", () => {
36+
test("when `custom` is `1` and keywords are `['kind', 'kind']` and a is `kind` and b is `kind` should return 0", () => {
3737
equal(sort.customSort("kind", "kind"), 0)
3838
})
39+
40+
sort.custom = 0
41+
sort.settings.sortOrderReverse = false
42+
test("when `sortOrderReverse` is false and keywords are `['kind', 'data']` should return 1", () => {
43+
equal(sort.customSort("data", "kind"), 1)
44+
equal(sort.customSort("kind", "data"), -1)
45+
equal(sort.customSort("kind", "kind"), 0)
46+
})
47+
sort.settings.sortOrderReverse = true
48+
test("when `sortOrderReverse` is true and keywords are `['abc', 'bcd']` should return -1", () => {
49+
equal(sort.customSort("abc", "bcd"), -1)
50+
equal(sort.customSort("b", "a"), 1)
51+
equal(sort.customSort("a", "a"), 0)
52+
})
53+
sort.custom = 1
54+
sort.settings.sortOrderReverse = false
55+
3956
})

src/util/sort-util.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export class SortUtil {
1010
}
1111

1212
customSort(a: string, b: string): number {
13+
const sortOrderReverse = this.settings.sortOrderReverse
1314
const sortOrder = this.settings.getCustomSortKeywords(this.custom)
1415
const indexA = sortOrder.indexOf(a)
1516
const indexB = sortOrder.indexOf(b)
@@ -23,16 +24,21 @@ export class SortUtil {
2324
if (indexA === -1 && indexB !== -1) {
2425
return 1
2526
}
27+
if (sortOrderReverse) {
28+
return this.localeSort(b, a)
29+
}
2630
return this.localeSort(a, b)
2731
}
2832

2933
static compare(a: number, b: number) {
3034
if (a > b) {
3135
return 1
3236
}
37+
3338
if (a < b) {
3439
return -1
3540
}
41+
3642
return 0
3743
}
3844

0 commit comments

Comments
 (0)