Skip to content

Commit fc942bc

Browse files
authored
Merge pull request #327 from pascalre/feat/coverage-and-settings
fix: restore coverage measurement, read settings per document, activate on demand
2 parents cf6e5bc + b4f085a commit fc942bc

19 files changed

Lines changed: 345 additions & 140 deletions

‎.nycrc‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
{
2-
"reporter": [
3-
"html",
4-
"text-lcov",
5-
"text-summary",
6-
"lcov"
2+
"include": [
3+
"out/**/*.js",
4+
"src/**/*.ts"
5+
],
6+
"exclude": [
7+
"out/test/**",
8+
"src/test/**"
9+
],
10+
"reporter": [
11+
"html",
12+
"text",
13+
"text-summary",
14+
"lcov"
715
]
8-
}
16+
}

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44
🐛 Bugfixes
5+
* The editor commands (sort, custom sort, format, validate) are only offered in the command palette for yaml files. The condition was declared at a place VS Code ignores
56
* (refs [#65](https://github.com/pascalre/vscode-yaml-sort/issues/65)) Formatting via "Format Document" or on save no longer shows "Yaml formatted successfully", once per document. The command `Format YAML` shows it once after the changes were applied
67
* Command `Recursively sort YAML files` now sorts files with several documents, works without an open editor and is only offered for folders in the explorer. A file that cannot be sorted is reported and left unchanged. Files are now also found on Windows paths
78
* Keep every document delimiter (`---`, including a comment behind it) in front of its own document. Before, several documents without a leading `---` got a duplicated `---` (an additional empty document), and with a leading `--- # comment` the comments moved to the next document or got lost
@@ -23,7 +24,10 @@
2324
* (closes [#221](https://github.com/pascalre/vscode-yaml-sort/issues/221)) Keep comments above their anchor while sorting, even when a deeper indented line contains the anchor as a substring
2425

2526
✨ Feature
27+
* Settings can now be set per workspace folder in a multi-root workspace and per language (e.g. `"[yaml]": { "vscode-yaml-sort.indent": 4 }`). Every command and the formatter read the settings that apply to the processed document
2628
* (closes [#204](https://github.com/pascalre/vscode-yaml-sort/issues/204)) Add setting `nullStyle` to define how null values are printed (`null`, `Null`, `NULL`, `~` or empty)
29+
⚡ Performance
30+
* The extension is only activated when a yaml file is opened or one of its commands is run, instead of on every start of VS Code
2731
📦 Dependencies
2832
* Move `@types/js-yaml` and `@vscode/test-electron` to `devDependencies`, so they are no longer shipped with the extension
2933

‎README.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ This extension contributes the following settings:
5252
| `useHelmProcessor` | When `true`, will activate HelmProcessor. | `true` |
5353
| `useOctalProcessor` | When `true`, will activate OctalProcessor. | `true` |
5454

55+
All settings except `notifySuccess` can also be set per workspace folder in a multi-root workspace and, except `extensions`, per language:
56+
57+
#### **`.vscode/settings.json`**
58+
```json
59+
{
60+
"[yaml]": {
61+
"vscode-yaml-sort.indent": 4
62+
}
63+
}
64+
```
65+
5566
# ❓ FAQ
5667
## How to sort on save?
5768
Register this extension as VS Code formatter. Also configure VS Code to format files on save. Caution: This setting will apply for all files. Changes will require a restart of VS Code. If you wish to also sort (not only format) the file on saving, set `sortOnSave` to `0`. Use `1`, `2` or `3` for custom sort.

‎package.json‎

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@
2828
"Formatters"
2929
],
3030
"activationEvents": [
31-
"onStartupFinished"
31+
"onLanguage:yaml",
32+
"onCommand:vscode-yaml-sort.customSortYaml_1",
33+
"onCommand:vscode-yaml-sort.customSortYaml_2",
34+
"onCommand:vscode-yaml-sort.customSortYaml_3",
35+
"onCommand:vscode-yaml-sort.formatYaml",
36+
"onCommand:vscode-yaml-sort.sortYaml",
37+
"onCommand:vscode-yaml-sort.sortYamlFilesInDirectory",
38+
"onCommand:vscode-yaml-sort.validateYaml"
3239
],
3340
"main": "./out/extension.js",
3441
"contributes": {
@@ -51,11 +58,13 @@
5158
"properties": {
5259
"vscode-yaml-sort.emptyLinesUntilLevel": {
5360
"type": "number",
61+
"scope": "language-overridable",
5462
"default": 0,
5563
"description": "When bigger than 0, will add a new line before each keyword on level n."
5664
},
5765
"vscode-yaml-sort.customSortKeywords_1": {
5866
"type": "array",
67+
"scope": "language-overridable",
5968
"items": {
6069
"type": "string"
6170
},
@@ -70,6 +79,7 @@
7079
},
7180
"vscode-yaml-sort.customSortKeywords_2": {
7281
"type": "array",
82+
"scope": "language-overridable",
7383
"items": {
7484
"type": "string"
7585
},
@@ -78,6 +88,7 @@
7888
},
7989
"vscode-yaml-sort.customSortKeywords_3": {
8090
"type": "array",
91+
"scope": "language-overridable",
8192
"items": {
8293
"type": "string"
8394
},
@@ -86,6 +97,7 @@
8697
},
8798
"vscode-yaml-sort.extensions": {
8899
"type": "array",
100+
"scope": "resource",
89101
"items": {
90102
"type": "string"
91103
},
@@ -97,26 +109,31 @@
97109
},
98110
"vscode-yaml-sort.forceQuotes": {
99111
"type": "boolean",
112+
"scope": "language-overridable",
100113
"default": false,
101114
"description": "When true, all non-key strings will be quoted even if they normally don't need to."
102115
},
103116
"vscode-yaml-sort.indent": {
104117
"type": "integer",
118+
"scope": "language-overridable",
105119
"default": 2,
106120
"description": "Indentation width in spaces."
107121
},
108122
"vscode-yaml-sort.lineWidth": {
109123
"type": "integer",
124+
"scope": "language-overridable",
110125
"default": 500,
111126
"description": "Maximum line width for YAML files."
112127
},
113128
"vscode-yaml-sort.locale": {
114129
"type": "string",
130+
"scope": "language-overridable",
115131
"default": "en",
116132
"description": "Language whose sort order should be used."
117133
},
118134
"vscode-yaml-sort.nullStyle": {
119135
"type": "string",
136+
"scope": "language-overridable",
120137
"default": "lowercase",
121138
"enum": [
122139
"lowercase",
@@ -136,11 +153,13 @@
136153
},
137154
"vscode-yaml-sort.noArrayIndent": {
138155
"type": "boolean",
156+
"scope": "language-overridable",
139157
"default": false,
140158
"description": "When true, will not add an indentation level to array elements."
141159
},
142160
"vscode-yaml-sort.noCompatMode": {
143161
"type": "boolean",
162+
"scope": "language-overridable",
144163
"default": false,
145164
"description": "When true, don't try to be compatible with older yaml versions. Currently: don't quote 'yes', 'no' and so on, as required for YAML 1.1"
146165
},
@@ -151,6 +170,7 @@
151170
},
152171
"vscode-yaml-sort.quotingType": {
153172
"type": "string",
173+
"scope": "language-overridable",
154174
"default": "'",
155175
"enum": [
156176
"'",
@@ -160,6 +180,7 @@
160180
},
161181
"vscode-yaml-sort.schema": {
162182
"type": "string",
183+
"scope": "language-overridable",
163184
"default": "DEFAULT_SCHEMA",
164185
"enum": [
165186
"HOMEASSISTANT_SCHEMA",
@@ -173,51 +194,61 @@
173194
},
174195
"vscode-yaml-sort.sortArrays": {
175196
"type": "boolean",
197+
"scope": "language-overridable",
176198
"default": false,
177199
"description": "When `true`, will sort arrays"
178200
},
179201
"vscode-yaml-sort.sortOrderReverse": {
180202
"type": "boolean",
203+
"scope": "language-overridable",
181204
"default": false,
182205
"description": "When `true`, will sort in reverse order"
183206
},
184207
"vscode-yaml-sort.sortOnSave": {
185208
"type": "number",
209+
"scope": "language-overridable",
186210
"default": 0,
187211
"description": "When `0`, will sort files when saving. When `1`, `2` or `3`, will use customSortKeywords. Set to negative value to disable sortOnSave."
188212
},
189213
"vscode-yaml-sort.useCustomSortRecursively": {
190214
"type": "boolean",
215+
"scope": "language-overridable",
191216
"default": false,
192217
"description": "When true, will use the custom sort keywords recursively on a file, when using custom sort"
193218
},
194219
"vscode-yaml-sort.useLeadingDashes": {
195220
"type": "boolean",
221+
"scope": "language-overridable",
196222
"default": true,
197223
"description": "Define if sorted YAML files should begin with leading dashes."
198224
},
199225
"vscode-yaml-sort.useArrayProcessor": {
200226
"type": "boolean",
227+
"scope": "language-overridable",
201228
"default": true,
202229
"description": "When `true`, will activate ArrayProcessor"
203230
},
204231
"vscode-yaml-sort.useBlockProcessor": {
205232
"type": "boolean",
233+
"scope": "language-overridable",
206234
"default": true,
207235
"description": "When `true`, will activate BlockProcessor"
208236
},
209237
"vscode-yaml-sort.useCommentProcessor": {
210238
"type": "boolean",
239+
"scope": "language-overridable",
211240
"default": true,
212241
"description": "When `true`, will activate CommentProcessor"
213242
},
214243
"vscode-yaml-sort.useHelmProcessor": {
215244
"type": "boolean",
245+
"scope": "language-overridable",
216246
"default": true,
217247
"description": "When `true`, will activate HelmProcessor"
218248
},
219249
"vscode-yaml-sort.useOctalProcessor": {
220250
"type": "boolean",
251+
"scope": "language-overridable",
221252
"default": true,
222253
"description": "When `true`, will activate OctalProcessor"
223254
}
@@ -226,44 +257,62 @@
226257
"commands": [
227258
{
228259
"command": "vscode-yaml-sort.customSortYaml_1",
229-
"title": "YAML Sort: Custom sort 1",
230-
"when": "editorLangId == yaml"
260+
"title": "YAML Sort: Custom sort 1"
231261
},
232262
{
233263
"command": "vscode-yaml-sort.customSortYaml_2",
234-
"title": "YAML Sort: Custom sort 2",
235-
"when": "editorLangId == yaml"
264+
"title": "YAML Sort: Custom sort 2"
236265
},
237266
{
238267
"command": "vscode-yaml-sort.customSortYaml_3",
239-
"title": "YAML Sort: Custom sort 3",
240-
"when": "editorLangId == yaml"
268+
"title": "YAML Sort: Custom sort 3"
241269
},
242270
{
243271
"command": "vscode-yaml-sort.formatYaml",
244-
"title": "YAML Sort: Format YAML",
245-
"when": "editorLangId == yaml"
272+
"title": "YAML Sort: Format YAML"
246273
},
247274
{
248275
"command": "vscode-yaml-sort.sortYaml",
249-
"title": "YAML Sort: Sort YAML",
250-
"when": "editorLangId == yaml"
276+
"title": "YAML Sort: Sort YAML"
251277
},
252278
{
253279
"command": "vscode-yaml-sort.sortYamlFilesInDirectory",
254280
"title": "YAML Sort: Recursively sort YAML files"
255281
},
256282
{
257283
"command": "vscode-yaml-sort.validateYaml",
258-
"title": "YAML Sort: Validate YAML",
259-
"when": "editorLangId == yaml"
284+
"title": "YAML Sort: Validate YAML"
260285
}
261286
],
262287
"menus": {
263288
"commandPalette": [
264289
{
265290
"command": "vscode-yaml-sort.sortYamlFilesInDirectory",
266291
"when": "false"
292+
},
293+
{
294+
"command": "vscode-yaml-sort.customSortYaml_1",
295+
"when": "editorLangId == yaml"
296+
},
297+
{
298+
"command": "vscode-yaml-sort.customSortYaml_2",
299+
"when": "editorLangId == yaml"
300+
},
301+
{
302+
"command": "vscode-yaml-sort.customSortYaml_3",
303+
"when": "editorLangId == yaml"
304+
},
305+
{
306+
"command": "vscode-yaml-sort.formatYaml",
307+
"when": "editorLangId == yaml"
308+
},
309+
{
310+
"command": "vscode-yaml-sort.sortYaml",
311+
"when": "editorLangId == yaml"
312+
},
313+
{
314+
"command": "vscode-yaml-sort.validateYaml",
315+
"when": "editorLangId == yaml"
267316
}
268317
],
269318
"explorer/context": [

‎src/adapter/vs-code-adapter.ts‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,12 @@ export enum Severity {
77
}
88

99
export class VsCodeAdapter {
10-
section = "vscode-yaml-sort"
1110
settings: Settings
1211

1312
constructor(settings = new Settings()) {
1413
this.settings = settings
1514
}
1615

17-
getProperty(property: string) {
18-
return workspace.getConfiguration().get(`${this.section}.${property}`)
19-
}
20-
2116
showMessage(severity: Severity, message: string) {
2217
if (severity === Severity.ERROR) {
2318
window.showErrorMessage(message)

‎src/controller/controller.ts‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,22 @@ import { YamlUtil } from "../util/yaml-util"
99

1010
export class Controller {
1111
editor: TextEditor
12-
yamlutil = new YamlUtil()
13-
jsyamladapter = new JsYamlAdapter()
14-
vscodeadapter = new VsCodeAdapter()
12+
settings: Settings
13+
yamlutil: YamlUtil
14+
jsyamladapter: JsYamlAdapter
15+
vscodeadapter: VsCodeAdapter
1516
errorutil = new ErrorUtil()
16-
settings = new Settings()
1717

1818
constructor() {
19-
if (window.activeTextEditor) {
20-
this.editor = window.activeTextEditor
21-
} else {
19+
if (!window.activeTextEditor) {
2220
throw new Error("No texteditor is active")
2321
}
22+
this.editor = window.activeTextEditor
23+
// one snapshot of the settings that apply to the edited document
24+
this.settings = Settings.forDocument(this.editor.document)
25+
this.yamlutil = new YamlUtil(this.settings)
26+
this.jsyamladapter = new JsYamlAdapter(this.settings)
27+
this.vscodeadapter = new VsCodeAdapter(this.settings)
2428
}
2529

2630
validateYamlWrapper(): boolean {

‎src/controller/document-transformer.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const transformDocuments = (stream: YamlStream, transform: ContentTransformation
7070
*/
7171
export const sortDocuments = (text: string, options: SortOptions, yaml: YamlOperations = new YamlUtil()): string | null => {
7272
validateTextRange(text)
73-
new JsYamlAdapter().validateYaml(text)
73+
new JsYamlAdapter(yaml.settings).validateYaml(text)
7474

7575
const sorted = transformDocuments(splitDocuments(text), (content) => {
7676
// an indented selection is sorted like a top-level document and indented again afterwards (see #53)

‎src/controller/file-sorter.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Uri } from "vscode"
44

55
import { YamlOperations, sortDocuments } from "./document-transformer"
66
import { Severity, VsCodeAdapter } from "../adapter/vs-code-adapter"
7+
import { Settings } from "../settings"
78
import { ErrorUtil } from "../util/error-util"
89
import { FileUtil } from "../util/file-util"
910
import { YamlUtil } from "../util/yaml-util"
@@ -38,9 +39,10 @@ export const sortYamlFilesInDirectory = (uri: Uri | undefined): void => {
3839
new VsCodeAdapter().showMessage(Severity.ERROR, "Please run this command from the context menu of a folder in the explorer.")
3940
return
4041
}
41-
for (const file of new FileUtil().getFiles(uri.fsPath)) {
42+
const settingsFor = (resource: Uri) => Settings.forDocument({ uri: resource, languageId: "yaml" })
43+
for (const file of new FileUtil(settingsFor(uri)).getFiles(uri.fsPath)) {
4244
try {
43-
sortFile(file)
45+
sortFile(file, new YamlUtil(settingsFor(Uri.file(file))))
4446
} catch (error) {
4547
errorutil.handleError(error)
4648
}

0 commit comments

Comments
 (0)