From 12cb58ae0c6f569ffd103308490653c6027d0027 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Wed, 29 Apr 2026 13:53:41 -0400 Subject: [PATCH] Revert "Use yaml package for formatting" This reverts commit 22d01b401da90192846d4d3938360f288f687ee8. There were many bugs in the new formatter implementation, so we're reverting for now. In order to move forwards with this, we'll either need to adjust the behaviour of `toString` in the YAML parser through contributing patches, or write our own formatter. Fixes https://github.com/redhat-developer/vscode-yaml/issues/1237 Fixes https://github.com/redhat-developer/vscode-yaml/issues/1235 Fixes https://github.com/redhat-developer/vscode-yaml/issues/1234 Fixes https://github.com/redhat-developer/vscode-yaml/issues/1233 Fixes https://github.com/redhat-developer/yaml-language-server/issues/1237 --- package-lock.json | 3 +- package.json | 2 +- .../handlers/settingsHandlers.ts | 1 + src/languageservice/services/yamlFormatter.ts | 41 ++++++++----------- test/formatter.test.ts | 16 +------- 5 files changed, 22 insertions(+), 41 deletions(-) diff --git a/package-lock.json b/package-lock.json index 33fbd62dd..ba74c1d23 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "ajv": "^8.17.1", "ajv-draft-04": "^1.0.0", "ajv-i18n": "^4.2.0", + "prettier": "^3.8.1", "request-light": "^0.5.7", "vscode-json-languageservice": "4.1.8", "vscode-languageserver": "^9.0.0", @@ -41,7 +42,6 @@ "mocha": "11.7.1", "mocha-lcov-reporter": "^1.3.0", "nyc": "^15.1.0", - "prettier": "3.8.1", "rimraf": "^3.0.2", "sinon": "^9.0.3", "sinon-chai": "^3.5.0", @@ -5104,7 +5104,6 @@ "version": "3.8.1", "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.8.1.tgz", "integrity": "sha512-UOnG6LftzbdaHZcKoPFtOcCKztrQ57WkHDeRD9t/PTQtmT0NHSeWWepj6pS0z/N7+08BHFDQVUrfmfMRcZwbMg==", - "dev": true, "license": "MIT", "bin": { "prettier": "bin/prettier.cjs" diff --git a/package.json b/package.json index a8e9b3005..06255ecf4 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "ajv": "^8.17.1", "ajv-draft-04": "^1.0.0", "ajv-i18n": "^4.2.0", + "prettier": "^3.8.1", "request-light": "^0.5.7", "vscode-json-languageservice": "4.1.8", "vscode-languageserver": "^9.0.0", @@ -56,7 +57,6 @@ "mocha": "11.7.1", "mocha-lcov-reporter": "^1.3.0", "nyc": "^15.1.0", - "prettier": "3.8.1", "rimraf": "^3.0.2", "sinon": "^9.0.3", "sinon-chai": "^3.5.0", diff --git a/src/languageserver/handlers/settingsHandlers.ts b/src/languageserver/handlers/settingsHandlers.ts index b1adc8917..581b3f0de 100644 --- a/src/languageserver/handlers/settingsHandlers.ts +++ b/src/languageserver/handlers/settingsHandlers.ts @@ -106,6 +106,7 @@ export class SettingsHandler { if (settings.yaml.format) { this.yamlSettings.yamlFormatterSettings = { + proseWrap: settings.yaml.format.proseWrap || 'preserve', printWidth: settings.yaml.format.printWidth || 80, }; diff --git a/src/languageservice/services/yamlFormatter.ts b/src/languageservice/services/yamlFormatter.ts index 8380a8cba..d2eb57537 100644 --- a/src/languageservice/services/yamlFormatter.ts +++ b/src/languageservice/services/yamlFormatter.ts @@ -4,23 +4,20 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { TextDocument } from 'vscode-languageserver-textdocument'; -import { FormattingOptions, Position, Range, TextEdit } from 'vscode-languageserver-types'; -import { parseDocument, ToStringOptions } from 'yaml'; -import { YamlVersion } from '../parser/yamlParser07'; +import { Range, Position, TextEdit, FormattingOptions } from 'vscode-languageserver-types'; import { CustomFormatterOptions, LanguageSettings } from '../yamlLanguageService'; -import { getCustomTags } from '../parser/custom-tag-provider'; +import { Options } from 'prettier'; +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'; export class YAMLFormatter { private formatterEnabled = true; - private yamlVersion: YamlVersion = '1.2'; - private customTags: string[] = []; public configure(shouldFormat: LanguageSettings): void { if (shouldFormat) { this.formatterEnabled = shouldFormat.format; - this.yamlVersion = shouldFormat.yamlVersion; - this.customTags = shouldFormat.customTags; } } @@ -34,28 +31,24 @@ export class YAMLFormatter { try { const text = document.getText(); - const doc = parseDocument(text, { - version: this.yamlVersion, - customTags: getCustomTags(this.customTags), - }); - const toStringOptions: ToStringOptions = { + const prettierOptions: Options = { + parser: 'yaml', + plugins: [yamlPlugin, estreePlugin], + // --- FormattingOptions --- - indent: (options.tabWidth as number) || options.tabSize || 2, + tabWidth: (options.tabWidth as number) || options.tabSize, // --- CustomFormatterOptions --- singleQuote: options.singleQuote, - flowCollectionPadding: options.bracketSpacing, - blockQuote: options.proseWrap === 'always' ? 'folded' : true, - lineWidth: Math.max(options.printWidth || 0, 22), - trailingComma: options.trailingComma === undefined ? true : options.trailingComma, + bracketSpacing: options.bracketSpacing, + // '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 = doc.toString(toStringOptions); - - if (formatted === text) { - return []; - } + const formatted = await format(text, prettierOptions); return [TextEdit.replace(Range.create(Position.create(0, 0), document.positionAt(text.length)), formatted)]; } catch (error) { diff --git a/test/formatter.test.ts b/test/formatter.test.ts index 5f4606523..ceb537c90 100644 --- a/test/formatter.test.ts +++ b/test/formatter.test.ts @@ -86,19 +86,7 @@ describe('Formatter Tests', () => { printWidth: 20, proseWrap: 'always', }); - assert.equal(edits[0].newText, 'comments: >\n test test test test\n test test test test\n test test test test\n'); - }); - - it('Formatting handles trailing commas (default)', async () => { - const content = `{ - key: 'value', - food: 'raisins', - airport: 'YYZ', - lightened_bulb: 'illuminating', -} -`; - const edits = await parseSetup(content, { singleQuote: true, trailingComma: true }); - assert.equal(edits.length, 0); + 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 () => { @@ -110,7 +98,7 @@ describe('Formatter Tests', () => { } `; const edits = await parseSetup(content, { singleQuote: true }); - assert.equal(edits.length, 0); + assert.equal(edits[0].newText, content); }); it('Formatting handles trailing commas (disabled)', async () => {