Skip to content
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"dependencies": {
"ajv": "^8.11.0",
"lodash": "4.17.21",
"prettier": "^3.0.0",
"prettier": "^3.5.0",
"request-light": "^0.5.7",
"vscode-json-languageservice": "4.1.8",
"vscode-languageserver": "^9.0.0",
Expand Down
4 changes: 4 additions & 0 deletions src/languageserver/handlers/settingsHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ export class SettingsHandler {
this.yamlSettings.yamlFormatterSettings.bracketSpacing = settings.yaml.format.bracketSpacing;
}

if (settings.yaml.format.trailingComma !== undefined) {
this.yamlSettings.yamlFormatterSettings.trailingComma = settings.yaml.format.trailingComma;
}

if (settings.yaml.format.enable !== undefined) {
this.yamlSettings.yamlFormatterSettings.enable = settings.yaml.format.enable;
}
Expand Down
6 changes: 4 additions & 2 deletions src/languageservice/services/yamlFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import { Range, Position, TextEdit, FormattingOptions } from 'vscode-languageserver-types';
import { CustomFormatterOptions, LanguageSettings } from '../yamlLanguageService';
import { Options } from 'prettier';
import * as parser from 'prettier/plugins/yaml';
import * as yamlPlugin from 'prettier/plugins/yaml';
import * as estreePlugin from 'prettier/plugins/estree';
import { format } from 'prettier/standalone';
import { TextDocument } from 'vscode-languageserver-textdocument';

Expand All @@ -33,7 +34,7 @@ export class YAMLFormatter {

const prettierOptions: Options = {
parser: 'yaml',
plugins: [parser],
plugins: [yamlPlugin, estreePlugin],

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

const formatted = await format(text, prettierOptions);
Expand Down
1 change: 1 addition & 0 deletions src/languageservice/yamlLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export interface SchemaConfiguration {
export interface CustomFormatterOptions {
singleQuote?: boolean;
bracketSpacing?: boolean;
trailingComma?: boolean;
proseWrap?: string;
printWidth?: number;
enable?: boolean;
Expand Down
1 change: 1 addition & 0 deletions src/yamlSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export class SettingsState {
bracketSpacing: true,
proseWrap: 'preserve',
printWidth: 80,
trailingComma: true,
enable: true,
} as CustomFormatterOptions;
yamlShouldHover = true;
Expand Down
42 changes: 39 additions & 3 deletions test/formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* Copyright (c) Red Hat. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { setupLanguageService, setupTextDocument } from './utils/testHelper';
import { ServiceSetup } from './utils/serviceSetup';
import * as assert from 'assert';
import { TextEdit } from 'vscode-languageserver-types';
import { SettingsState, TextDocumentTestManager } from '../src/yamlSettings';
import { LanguageHandlers } from '../src/languageserver/handlers/languageHandlers';
import { SettingsState, TextDocumentTestManager } from '../src/yamlSettings';
import { ServiceSetup } from './utils/serviceSetup';
import { setupLanguageService, setupTextDocument } from './utils/testHelper';

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

it('Formatting handles trailing commas (enabled)', async () => {
const content = `{
key: 'value',
food: 'raisins',
airport: 'YYZ',
lightened_bulb: 'illuminating',
}
`;
const edits = await parseSetup(content, { singleQuote: true });
assert.equal(edits[0].newText, content);
});

it('Formatting handles trailing commas (disabled)', async () => {
const content = `{
key: 'value',
food: 'raisins',
airport: 'YYZ',
lightened_bulb: 'illuminating',
}
`;
const edits = await parseSetup(content, {
singleQuote: true,
trailingComma: false,
});
assert.equal(
edits[0].newText,
`{
key: 'value',
food: 'raisins',
airport: 'YYZ',
lightened_bulb: 'illuminating'
}
`
);
});

it('Formatting uses tabSize', async () => {
const content = `map:
k1: v1
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2675,10 +2675,10 @@ prettier-linter-helpers@^1.0.0:
dependencies:
fast-diff "^1.1.2"

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

process-on-spawn@^1.0.0:
version "1.0.0"
Expand Down