Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for sorting arrays (#51) #121

Merged
merged 3 commits into from
Feb 11, 2023
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,5 +1,10 @@
# Changelog

## 6.4.0 - February 11, 2023
* (refs [#51](https://github.com/pascalre/vscode-yaml-sort/issues/51)) Add support for sorting arrays
* Add setting `sortArrays`
* Update dependencies to latest versions

## 6.3.0 - February 6, 2023
* (refs [#27](https://github.com/pascalre/vscode-yaml-sort/issues/36)) Add support for octal values
* Update dependencies to latest versions
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ 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` |
| `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` |
| `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` |
Expand Down
36 changes: 18 additions & 18 deletions package-lock.json

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

13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@
],
"description": "Schema to use"
},
"vscode-yaml-sort.sortArrays": {
"type": "boolean",
"default": false,
"description": "When `true`, will sort arrays"
},
"vscode-yaml-sort.sortOnSave": {
"type": "number",
"default": 0,
Expand Down Expand Up @@ -247,18 +252,18 @@
},
"devDependencies": {
"@types/mocha": "10.0.1",
"@types/node": "^18.11.19",
"@types/node": "^18.13.0",
"@types/sinon": "^10.0.13",
"@typescript-eslint/eslint-plugin": "^5.48.2",
"@typescript-eslint/parser": "^5.48.2",
"coveralls": "3.1.1",
"dependency-cruiser": "^12.5.0",
"eslint": "^8.32.0",
"dependency-cruiser": "^12.7.1",
"eslint": "^8.34.0",
"jsdoc": "4.0.0",
"mocha": "^10.2.0",
"nyc": "15.1.0",
"sinon": "^15.0.1",
"sonarqube-scanner": "^3.0.0",
"sonarqube-scanner": "^3.0.1",
"ts-node": "10.9.1",
"typescript": "^4.9.5",
"typescript-require": "0.3.0"
Expand Down
1 change: 1 addition & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class Settings {
notifySuccess = this.getBoolean("notifySuccess")
quotingType = this.getQuotingType()
schema = this.getSchema()
sortArrays = this.getBoolean("sortArrays")
sortOnSave = this.getNumber("sortOnSave")
useCustomSortRecursively = this.getBoolean("useCustomSortRecursively")
useLeadingDashes = this.getBoolean("useLeadingDashes")
Expand Down
46 changes: 46 additions & 0 deletions src/test/suite/util/yaml-util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,52 @@ import jsyaml = require("js-yaml")
import { Settings } from "../../../settings"
import { YamlUtil } from "../../../util/yaml-util"

suite("Test sortArrays", () => {
const yamlutil = new YamlUtil()
const text =
'array:\n' +
' - python\n' +
' - perl'

test("when sortArrays is set to false, should not sort arrays", () => {
const actual = yamlutil.sortYaml(text)
equal(actual, text)
})

test("when sortArrays is set to true, should sort arrays", () => {
yamlutil.settings.sortArrays = true
const actual = yamlutil.sortYaml('foo')
const expected = 'foo'
equal(actual, expected)
})

test("when sortArrays is set to true, should sort arrays", () => {
yamlutil.settings.sortArrays = true
const actual = yamlutil.sortYaml(text)
const expected =
'array:\n' +
' - perl\n' +
' - python'
equal(actual, expected)
})

test("when sortArrays is set to true, should sort nested arrays", () => {
yamlutil.settings.sortArrays = true
const text =
'foo:\n' +
' array:\n' +
' - python\n' +
' - perl'
const actual = yamlutil.sortYaml(text)
const expected =
'foo:\n' +
' array:\n' +
' - perl\n' +
' - python'
equal(actual, expected)
})
})

suite("Test dumpYaml", () => {

test("when useCustomSortRecursively is set to `true` should recursively use customSort", () => {
Expand Down
11 changes: 7 additions & 4 deletions src/util/yaml-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Severity, VsCodeAdapter } from "../adapter/vs-code-adapter"
import { ProcessorController } from "../controller/processor-controller"
import { ErrorUtil } from "./error-util"

const sortNestedArrays = (obj: unknown) => {
const sortArrays = (obj: unknown) => {
if (!obj || typeof obj !== 'object') {
return
} else if (Array.isArray(obj)) {
Expand All @@ -17,7 +17,7 @@ const sortNestedArrays = (obj: unknown) => {
if (Array.isArray(object)) {
Object.entries(object).sort()
}
sortNestedArrays(object)
sortArrays(object)
}
})
}
Expand Down Expand Up @@ -60,9 +60,12 @@ export class YamlUtil {

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const doc = this.jsyamladapter.load(unsortedYamlWithoutTabs) as any
sortNestedArrays(doc)
let sortedYaml = ""

if (this.settings.sortArrays) {
sortArrays(doc)
}

let sortedYaml = ""
if (customSort > 0 && !this.settings.useCustomSortRecursively) {
const keywords = this.settings.getCustomSortKeywords(customSort)

Expand Down