diff --git a/CHANGELOG.md b/CHANGELOG.md index 915f9a7..75071d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ # Changelog +# 6.6.0 - Oct 28, 2024 +✨ Feature +* (refs [#225](https://github.com/pascalre/vscode-yaml-sort/issues/225)) Support sorting in reverse order + # 6.5.18 - Oct 28, 2024 📦 Dependencies * Update dependencies to latest versions diff --git a/README.md b/README.md index 9b328e8..dc4d296 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ This extension contributes the following settings: | `schema` | Schema to use. Possible values are `HOMEASSISTANT_SCHEMA`, `CLOUDFORMATION_SCHEMA`, `CORE_SCHEMA`, `DEFAULT_SCHEMA`, `FAILSAFE_SCHEMA`, `JSON_SCHEMA`. | `DEFAULT_SCHEMA` | | `sortArrays` | When `true`, will sort arrays | `false` | | `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` | +| `sortOrderReverse` | When `true`, will sort in reverse order | `false` | | `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` | diff --git a/package-lock.json b/package-lock.json index 7bea107..bf4921e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "vscode-yaml-sort", - "version": "6.5.18", + "version": "6.6.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "vscode-yaml-sort", - "version": "6.5.18", + "version": "6.6.0", "license": "MIT", "dependencies": { "@types/js-yaml": "^4.0.6", diff --git a/package.json b/package.json index 42874d2..70fd335 100644 --- a/package.json +++ b/package.json @@ -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": "6.5.18", + "version": "6.6.0", "engines": { "vscode": "^1.49.0" }, @@ -157,6 +157,11 @@ "default": false, "description": "When `true`, will sort arrays" }, + "vscode-yaml-sort.sortOrderReverse": { + "type": "boolean", + "default": false, + "description": "When `true`, will sort in reverse order" + }, "vscode-yaml-sort.sortOnSave": { "type": "number", "default": 0, diff --git a/src/settings.ts b/src/settings.ts index 469555a..35723c2 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -50,6 +50,7 @@ export class Settings { schema = this.getSchema() sortArrays = this.getBoolean("sortArrays") sortOnSave = this.getNumber("sortOnSave") + sortOrderReverse = this.getBoolean("sortOrderReverse") useCustomSortRecursively = this.getBoolean("useCustomSortRecursively") useLeadingDashes = this.getBoolean("useLeadingDashes") useArrayProcessor = this.getBoolean("useArrayProcessor") diff --git a/src/test/suite/util/sort-util.test.ts b/src/test/suite/util/sort-util.test.ts index 8475982..35181cc 100644 --- a/src/test/suite/util/sort-util.test.ts +++ b/src/test/suite/util/sort-util.test.ts @@ -33,7 +33,24 @@ suite("Test Sort - customSort()", () => { test("when `custom` is `1` and keywords are `['kind', 'data']` and a is `kind` and b is `data` should return -1", () => { equal(sort.customSort("kind", "data"), -1) }) - test("when `custom` is `1` and keywords are `['kind', 'data']` and a is `kind` and b is `kind` should return 0", () => { + test("when `custom` is `1` and keywords are `['kind', 'kind']` and a is `kind` and b is `kind` should return 0", () => { equal(sort.customSort("kind", "kind"), 0) }) + + sort.custom = 0 + sort.settings.sortOrderReverse = false + test("when `sortOrderReverse` is false and keywords are `['kind', 'data']` should return 1", () => { + equal(sort.customSort("data", "kind"), 1) + equal(sort.customSort("kind", "data"), -1) + equal(sort.customSort("kind", "kind"), 0) + }) + sort.settings.sortOrderReverse = true + test("when `sortOrderReverse` is true and keywords are `['abc', 'bcd']` should return -1", () => { + equal(sort.customSort("abc", "bcd"), -1) + equal(sort.customSort("b", "a"), 1) + equal(sort.customSort("a", "a"), 0) + }) + sort.custom = 1 + sort.settings.sortOrderReverse = false + }) \ No newline at end of file diff --git a/src/util/sort-util.ts b/src/util/sort-util.ts index 18d059f..fd5d19a 100644 --- a/src/util/sort-util.ts +++ b/src/util/sort-util.ts @@ -10,6 +10,7 @@ export class SortUtil { } customSort(a: string, b: string): number { + const sortOrderReverse = this.settings.sortOrderReverse const sortOrder = this.settings.getCustomSortKeywords(this.custom) const indexA = sortOrder.indexOf(a) const indexB = sortOrder.indexOf(b) @@ -23,6 +24,9 @@ export class SortUtil { if (indexA === -1 && indexB !== -1) { return 1 } + if (sortOrderReverse) { + return this.localeSort(b, a) + } return this.localeSort(a, b) } @@ -30,9 +34,11 @@ export class SortUtil { if (a > b) { return 1 } + if (a < b) { return -1 } + return 0 }