Skip to content

Commit 6c66fdf

Browse files
committed
Add option to remove trailing comma
This option is available in prettier so implementing it is trivial. Needs the 'estree' prettier plugin, which is responsible for formatting JSON. As a result, this might increase the size of the language server. eg. in the following, when 'yaml.format.trailingComma` is set to `false`, the last comma should be removed: ```yaml { key: 'value', food: 'raisins', airport: 'YYZ', lightened_bulb: 'illuminating', } ``` See redhat-developer/vscode-yaml#1112 Signed-off-by: David Thompson <davthomp@redhat.com>
1 parent 27f3391 commit 6c66fdf

File tree

7 files changed

+54
-10
lines changed

7 files changed

+54
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"dependencies": {
2929
"ajv": "^8.11.0",
3030
"lodash": "4.17.21",
31-
"prettier": "^3.0.0",
31+
"prettier": "^3.5.0",
3232
"request-light": "^0.5.7",
3333
"vscode-json-languageservice": "4.1.8",
3434
"vscode-languageserver": "^9.0.0",

src/languageserver/handlers/settingsHandlers.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ export class SettingsHandler {
107107
this.yamlSettings.yamlFormatterSettings.bracketSpacing = settings.yaml.format.bracketSpacing;
108108
}
109109

110+
if (settings.yaml.format.trailingComma !== undefined) {
111+
this.yamlSettings.yamlFormatterSettings.trailingComma = settings.yaml.format.trailingComma;
112+
}
113+
110114
if (settings.yaml.format.enable !== undefined) {
111115
this.yamlSettings.yamlFormatterSettings.enable = settings.yaml.format.enable;
112116
}

src/languageservice/services/yamlFormatter.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import { Range, Position, TextEdit, FormattingOptions } from 'vscode-languageserver-types';
88
import { CustomFormatterOptions, LanguageSettings } from '../yamlLanguageService';
99
import { Options } from 'prettier';
10-
import * as parser from 'prettier/plugins/yaml';
10+
import * as yamlPlugin from 'prettier/plugins/yaml';
11+
import * as estreePlugin from 'prettier/plugins/estree';
1112
import { format } from 'prettier/standalone';
1213
import { TextDocument } from 'vscode-languageserver-textdocument';
1314

@@ -33,7 +34,7 @@ export class YAMLFormatter {
3334

3435
const prettierOptions: Options = {
3536
parser: 'yaml',
36-
plugins: [parser],
37+
plugins: [yamlPlugin, estreePlugin],
3738

3839
// --- FormattingOptions ---
3940
tabWidth: (options.tabWidth as number) || options.tabSize,
@@ -44,6 +45,7 @@ export class YAMLFormatter {
4445
// 'preserve' is the default for Options.proseWrap. See also server.ts
4546
proseWrap: 'always' === options.proseWrap ? 'always' : 'never' === options.proseWrap ? 'never' : 'preserve',
4647
printWidth: options.printWidth,
48+
trailingComma: options.trailingComma === false ? 'none' : 'all',
4749
};
4850

4951
const formatted = await format(text, prettierOptions);

src/languageservice/yamlLanguageService.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ export interface SchemaConfiguration {
151151
export interface CustomFormatterOptions {
152152
singleQuote?: boolean;
153153
bracketSpacing?: boolean;
154+
trailingComma?: boolean;
154155
proseWrap?: string;
155156
printWidth?: number;
156157
enable?: boolean;

src/yamlSettings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export class SettingsState {
6969
bracketSpacing: true,
7070
proseWrap: 'preserve',
7171
printWidth: 80,
72+
trailingComma: true,
7273
enable: true,
7374
} as CustomFormatterOptions;
7475
yamlShouldHover = true;

test/formatter.test.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
* Copyright (c) Red Hat. All rights reserved.
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
5-
import { setupLanguageService, setupTextDocument } from './utils/testHelper';
6-
import { ServiceSetup } from './utils/serviceSetup';
75
import * as assert from 'assert';
86
import { TextEdit } from 'vscode-languageserver-types';
9-
import { SettingsState, TextDocumentTestManager } from '../src/yamlSettings';
107
import { LanguageHandlers } from '../src/languageserver/handlers/languageHandlers';
8+
import { SettingsState, TextDocumentTestManager } from '../src/yamlSettings';
9+
import { ServiceSetup } from './utils/serviceSetup';
10+
import { setupLanguageService, setupTextDocument } from './utils/testHelper';
1111

1212
describe('Formatter Tests', () => {
1313
let languageHandler: LanguageHandlers;
@@ -60,6 +60,42 @@ describe('Formatter Tests', () => {
6060
assert.equal(edits[0].newText, 'comments: >\n test test test\n test test test\n test test test\n test test test\n');
6161
});
6262

63+
it('Formatting handles trailing commas (enabled)', async () => {
64+
const content = `{
65+
key: 'value',
66+
food: 'raisins',
67+
airport: 'YYZ',
68+
lightened_bulb: 'illuminating',
69+
}
70+
`;
71+
const edits = await parseSetup(content, { singleQuote: true });
72+
assert.equal(edits[0].newText, content);
73+
});
74+
75+
it('Formatting handles trailing commas (disabled)', async () => {
76+
const content = `{
77+
key: 'value',
78+
food: 'raisins',
79+
airport: 'YYZ',
80+
lightened_bulb: 'illuminating',
81+
}
82+
`;
83+
const edits = await parseSetup(content, {
84+
singleQuote: true,
85+
trailingComma: false,
86+
});
87+
assert.equal(
88+
edits[0].newText,
89+
`{
90+
key: 'value',
91+
food: 'raisins',
92+
airport: 'YYZ',
93+
lightened_bulb: 'illuminating'
94+
}
95+
`
96+
);
97+
});
98+
6399
it('Formatting uses tabSize', async () => {
64100
const content = `map:
65101
k1: v1

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2675,10 +2675,10 @@ prettier-linter-helpers@^1.0.0:
26752675
dependencies:
26762676
fast-diff "^1.1.2"
26772677

2678-
prettier@^3.0.0:
2679-
version "3.3.1"
2680-
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.3.1.tgz#e68935518dd90bb7ec4821ba970e68f8de16e1ac"
2681-
integrity sha512-7CAwy5dRsxs8PHXT3twixW9/OEll8MLE0VRPCJyl7CkS6VHGPSlsVaWTiASPTyGyYRyApxlaWTzwUxVNrhcwDg==
2678+
prettier@^3.5.0:
2679+
version "3.5.3"
2680+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.5.3.tgz#4fc2ce0d657e7a02e602549f053b239cb7dfe1b5"
2681+
integrity sha512-QQtaxnoDJeAkDvDKWCLiwIXkTgRhwYDEQCghU9Z6q03iyek/rxRh/2lC3HB7P8sWT2xC/y5JDctPLBIGzHKbhw==
26822682

26832683
process-on-spawn@^1.0.0:
26842684
version "1.0.0"

0 commit comments

Comments
 (0)