diff --git a/package-lock.json b/package-lock.json index 33fbd62d..ba74c1d2 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 a8e9b300..06255ecf 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 b1adc891..581b3f0d 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 8380a8cb..d2eb5753 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 5f460652..ceb537c9 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 () => {