diff --git a/package.json b/package.json index 62be0e23..90dd6a0c 100644 --- a/package.json +++ b/package.json @@ -29,9 +29,6 @@ "type": "git", "url": "https://github.com/redhat-developer/yaml-language-server.git" }, - "optionalDependencies": { - "prettier": "2.8.7" - }, "dependencies": { "ajv": "^8.11.0", "lodash": "4.17.21", @@ -42,7 +39,7 @@ "vscode-languageserver-types": "^3.16.0", "vscode-nls": "^5.0.0", "vscode-uri": "^3.0.2", - "yaml": "2.2.2" + "yaml": "2.5.0" }, "devDependencies": { "@microsoft/eslint-formatter-sarif": "3.0.0", @@ -65,6 +62,7 @@ "mocha": "9.2.2", "mocha-lcov-reporter": "^1.3.0", "nyc": "^15.1.0", + "prettier": "2.8.7", "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 e2351ea7..cfa6cf4d 100644 --- a/src/languageserver/handlers/settingsHandlers.ts +++ b/src/languageserver/handlers/settingsHandlers.ts @@ -96,7 +96,6 @@ 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 fff67444..675bc45b 100644 --- a/src/languageservice/services/yamlFormatter.ts +++ b/src/languageservice/services/yamlFormatter.ts @@ -6,17 +6,20 @@ import { Range, Position, TextEdit, FormattingOptions } from 'vscode-languageserver-types'; import { CustomFormatterOptions, LanguageSettings } from '../yamlLanguageService'; -import * as prettier from 'prettier'; -import { Options } from 'prettier'; -import * as parser from 'prettier/parser-yaml'; +import { parseDocument, ToStringOptions } from 'yaml'; import { TextDocument } from 'vscode-languageserver-textdocument'; +import { YamlVersion } from '../parser/yamlParser07'; 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; } } @@ -27,23 +30,26 @@ export class YAMLFormatter { try { const text = document.getText(); + const doc = parseDocument(text, { + version: this.yamlVersion, + }); - const prettierOptions: Options = { - parser: 'yaml', - plugins: [parser], - + const toStringOptions: ToStringOptions = { // --- FormattingOptions --- - tabWidth: (options.tabWidth as number) || options.tabSize, + indent: (options.tabWidth as number) || options.tabSize || 2, // --- CustomFormatterOptions --- singleQuote: options.singleQuote, - 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, + flowCollectionPadding: options.bracketSpacing, + blockQuote: options.proseWrap === 'always' ? 'folded' : true, + lineWidth: Math.max(options.printWidth || 0, 22), }; - const formatted = prettier.format(text, prettierOptions); + const formatted = doc.toString(toStringOptions); + + if (formatted === text) { + return []; + } 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 ed970a00..1dd2202e 100644 --- a/test/formatter.test.ts +++ b/test/formatter.test.ts @@ -56,7 +56,7 @@ describe('Formatter Tests', () => { printWidth: 20, proseWrap: 'always', }); - assert.equal(edits[0].newText, 'comments: >\n test test test\n test test test\n test test test\n test test test\n'); + assert.equal(edits[0].newText, 'comments: >\n test test test test\n test test test test\n test test test test\n'); }); it('Formatting uses tabSize', () => { diff --git a/yarn.lock b/yarn.lock index fd794ffd..f1f45b2a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3222,10 +3222,10 @@ yallist@^4.0.0: resolved "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz" integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A== -yaml@2.2.2: - version "2.2.2" - resolved "https://registry.yarnpkg.com/yaml/-/yaml-2.2.2.tgz#ec551ef37326e6d42872dad1970300f8eb83a073" - integrity sha512-CBKFWExMn46Foo4cldiChEzn7S7SRV+wqiluAb6xmueD/fGyRHIhX8m14vVGgeFWjN540nKCNVj6P21eQjgTuA== +yaml@2.5.0: + version "2.5.0" + resolved "https://registry.yarnpkg.com/yaml/-/yaml-2.5.0.tgz#c6165a721cf8000e91c36490a41d7be25176cf5d" + integrity sha512-2wWLbGbYDiSqqIKoPjar3MPgB94ErzCtrNE1FdqGuaO0pi2JGjmE8aW8TDZwzU7vuxcGRdL/4gPQwQ7hD5AMSw== yargs-parser@20.2.4, yargs-parser@^20.2.2: version "20.2.4"