Skip to content

Commit

Permalink
Use yaml package for formatting
Browse files Browse the repository at this point in the history
This replaces Prettier as the LSP formatting solution with the `yaml`
package. The `yaml` package can format YAML just fine, and we already
have it as a dependency. Prettier is a big dependency.

If people want to use Prettier, other Prettier integrations are probably
better suited for them. For example, the YAML language server doesn’t
respect Prettier configuration files.

This is a proof of concept. I tried to map existing options to the new
implementation. A more ideal solution might be to change the formatting
options.

Refs #933
  • Loading branch information
remcohaszing committed Jul 24, 2024
1 parent f039273 commit db44f9b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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.4.5"
},
"devDependencies": {
"@microsoft/eslint-formatter-sarif": "3.0.0",
Expand All @@ -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",
Expand Down
1 change: 0 additions & 1 deletion src/languageserver/handlers/settingsHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down
32 changes: 19 additions & 13 deletions src/languageservice/services/yamlFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion test/formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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.4.5:
version "2.4.5"
resolved "https://registry.yarnpkg.com/yaml/-/yaml-2.4.5.tgz#60630b206dd6d84df97003d33fc1ddf6296cca5e"
integrity sha512-aBx2bnqDzVOyNKfsysjA2ms5ZlnjSAW2eG3/L5G/CSujfjLJTJsEw1bGw8kCf04KodQWk1pxlGnZ56CRxiawmg==

[email protected], yargs-parser@^20.2.2:
version "20.2.4"
Expand Down

0 comments on commit db44f9b

Please sign in to comment.