Skip to content

Commit

Permalink
WIP: Add possibility for custom sort keys per file (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalre committed Dec 8, 2023
1 parent 69419a1 commit eb54197
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@
],
"configuration": {
"properties": {
"vscode-yaml-sort.customSortByFileNameKeywords": {
"type": "map",
"default": [
["gitlab-ci.yaml", ["stages", "variables", "build"]],
["action.yaml", ["name", "on", "jobs"]]
],
"description": "Map of filenames and according sort order."
},
"vscode-yaml-sort.emptyLinesUntilLevel": {
"type": "number",
"default": 0,
Expand Down Expand Up @@ -215,6 +223,11 @@
"title": "YAML Sort: Custom sort 3",
"when": "editorLangId == yaml"
},
{
"command": "vscode-yaml-sort.customSortYamlByFileName",
"title": "YAML Sort: Custom sort by filename",
"when": "editorLangId == yaml"
},
{
"command": "vscode-yaml-sort.formatYaml",
"title": "YAML Sort: Format YAML",
Expand Down
3 changes: 3 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export function activate(context: ExtensionContext) {
}),
commands.registerCommand("vscode-yaml-sort.sortYamlFilesInDirectory", (uri: Uri) => {
new Controller().sortYamlFiles(uri)
}),
commands.registerCommand("vscode-yaml-sort.customSortYamlByFileName", () => {
new Controller().sortYamlWrapper(99)
})
)
for (const index of [1, 2, 3]) {
Expand Down
20 changes: 19 additions & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import path = require("path")

import { CLOUDFORMATION_SCHEMA } from "cloudformation-js-yaml-schema"
import { HOMEASSISTANT_SCHEMA } from "homeassistant-js-yaml-schema"
import { CORE_SCHEMA, DEFAULT_SCHEMA, FAILSAFE_SCHEMA, JSON_SCHEMA, Schema } from "js-yaml"
import { workspace } from "vscode"
import { window, workspace } from "vscode"

export class Settings {
filter = "vscode-yaml-sort"
Expand All @@ -22,14 +24,30 @@ export class Settings {
getCustomSortKeywords(index: number): string[] {
if ([1, 2, 3].includes(index))
return this.workspace.getConfiguration().get(`${this.filter}.customSortKeywords_${index}`) as string[]
if (index == 99)
return this.getCustomSortKeywordsByFileName()
return []
}

getCustomSortKeywordsByFileName(): string[] {
const file = window.activeTextEditor?.document.fileName as string
const filename = path.parse(file).base
const map = this.workspace.getConfiguration().get(`${this.filter}.customSortByFileNameKeywords`) as Map<string, string[]>
const sortorder = map.get(filename)
if (sortorder)
return sortorder

return []
}

getExtensions(): string[] {
return this.workspace.getConfiguration().get(`${this.filter}.extensions`) as string[]
}

getQuotingType(): "'" | "\"" {
return this.workspace.getConfiguration().get(`${this.filter}.quotingType`) as "'" | "\""
}

getSchema(): Schema {
const schema = this.workspace.getConfiguration().get(`${this.filter}.schema`) as string
return Settings.getJsYamlSchemaFromString(schema)
Expand Down

0 comments on commit eb54197

Please sign in to comment.