Skip to content

Commit

Permalink
Fix #35: emptyLinesUntilLevel doesn't work with keywords containing d…
Browse files Browse the repository at this point in the history
…ashes
  • Loading branch information
pascalre committed Aug 29, 2021
1 parent 86f1cde commit 1ff916b
Show file tree
Hide file tree
Showing 7 changed files with 434 additions and 398 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,4 +1,8 @@
# Changelog
## 5.0.1 - August 29, 2021
* (refs [#54](https://github.com/pascalre/vscode-yaml-sort/issues/35)) emptyLinesUntilLevel doesn't work with keywords containing dashes
* Update dependencies to latest versions

## 5.0.0 - July 10, 2021
* (refs [#34](https://github.com/pascalre/vscode-yaml-sort/issues/34)) Make formatter configurable
* Default YAML formatter is disabled by default. Use `vscode-yaml-sort.useAsFormatter` to activate it.
Expand Down
787 changes: 402 additions & 385 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
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.0.0",
"version": "5.0.1",
"engines": {
"vscode": "^1.49.0"
},
Expand Down Expand Up @@ -202,8 +202,8 @@
"coverage": "nyc npm run test"
},
"devDependencies": {
"@types/mocha": "^8.2.0",
"@types/node": "^15.0.1",
"@types/mocha": "^9.0.0",
"@types/node": "^16.7.6",
"@types/vscode": "^1.49.0",
"@typescript-eslint/eslint-plugin": "^4.15.0",
"@typescript-eslint/parser": "^4.15.0",
Expand Down
6 changes: 3 additions & 3 deletions src/extension.ts
Expand Up @@ -269,7 +269,7 @@ export function sortYaml(
}

return sortedYaml
} catch (e) {
} catch (e: any) {
vscode.window.showErrorMessage("Keys could not be resorted: " + e.message)
return null
}
Expand All @@ -296,7 +296,7 @@ export function validateYaml(text: string, schema: yamlParser.Schema): boolean {
})
vscode.window.showInformationMessage("YAML is valid.")
return true
} catch (e) {
} catch (e: any) {
vscode.window.showErrorMessage("YAML is invalid: " + e.message)
return false
}
Expand Down Expand Up @@ -353,7 +353,7 @@ export function formatYaml(
}
vscode.window.showInformationMessage("Yaml formatted successfully")
return doc
} catch (e) {
} catch (e: any) {
vscode.window.showErrorMessage("Yaml could not be formatted: " + e.message)
return null
}
Expand Down
12 changes: 6 additions & 6 deletions src/lib.ts
Expand Up @@ -183,19 +183,19 @@ export function addNewLineBeforeRootKeywords(text: string): string{
* @returns {string} text with new lines
*/
export function addNewLineBeforeKeywordsUpToLevelN(n: number, indent: number, text: string): string {
let i = 0;
let level = 0;
let result = text;

while (i < n) {
if (i == 0) {
while (level < n) {
if (level == 0) {
result = result.replace(/\n[^\s]*:/g, "\n$&")
} else {
let spaces = " ".repeat(indent)
spaces = spaces.repeat(i)
const regex = new RegExp("\n" + spaces + "\\w*:", "g")
spaces = spaces.repeat(level)
const regex = new RegExp("\n" + spaces + "[\\w-]*:", "g")
result = result.replace(regex, "\n$&")
}
i++;
level++;
}

return result;
Expand Down
1 change: 0 additions & 1 deletion src/test/suite/extension.test.ts
Expand Up @@ -21,7 +21,6 @@ import {
sortYamlWrapper
} from "../../extension"
import { CLOUDFORMATION_SCHEMA } from "cloudformation-js-yaml-schema"
import exp = require("constants")

const locale = "en"

Expand Down
16 changes: 16 additions & 0 deletions src/test/suite/lib.test.ts
Expand Up @@ -279,6 +279,22 @@ spec: value
key: value
spec: value
`
assert.strictEqual(addNewLineBeforeKeywordsUpToLevelN(2, 2, actual), expected)
})

test("should recognize keywords containing the char -", () => {
const actual = `data:
key:
key: value
sp-ec: value
`
const expected = `data:
key:
key: value
sp-ec: value
`
assert.strictEqual(addNewLineBeforeKeywordsUpToLevelN(2, 2, actual), expected)
})
Expand Down

0 comments on commit 1ff916b

Please sign in to comment.