Skip to content

Commit db44f9b

Browse files
committed
Use yaml package for formatting
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
1 parent f039273 commit db44f9b

File tree

5 files changed

+26
-23
lines changed

5 files changed

+26
-23
lines changed

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@
2929
"type": "git",
3030
"url": "https://github.com/redhat-developer/yaml-language-server.git"
3131
},
32-
"optionalDependencies": {
33-
"prettier": "2.8.7"
34-
},
3532
"dependencies": {
3633
"ajv": "^8.11.0",
3734
"lodash": "4.17.21",
@@ -42,7 +39,7 @@
4239
"vscode-languageserver-types": "^3.16.0",
4340
"vscode-nls": "^5.0.0",
4441
"vscode-uri": "^3.0.2",
45-
"yaml": "2.2.2"
42+
"yaml": "2.4.5"
4643
},
4744
"devDependencies": {
4845
"@microsoft/eslint-formatter-sarif": "3.0.0",
@@ -65,6 +62,7 @@
6562
"mocha": "9.2.2",
6663
"mocha-lcov-reporter": "^1.3.0",
6764
"nyc": "^15.1.0",
65+
"prettier": "2.8.7",
6866
"rimraf": "^3.0.2",
6967
"sinon": "^9.0.3",
7068
"sinon-chai": "^3.5.0",

src/languageserver/handlers/settingsHandlers.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ export class SettingsHandler {
9696

9797
if (settings.yaml.format) {
9898
this.yamlSettings.yamlFormatterSettings = {
99-
proseWrap: settings.yaml.format.proseWrap || 'preserve',
10099
printWidth: settings.yaml.format.printWidth || 80,
101100
};
102101

src/languageservice/services/yamlFormatter.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@
66

77
import { Range, Position, TextEdit, FormattingOptions } from 'vscode-languageserver-types';
88
import { CustomFormatterOptions, LanguageSettings } from '../yamlLanguageService';
9-
import * as prettier from 'prettier';
10-
import { Options } from 'prettier';
11-
import * as parser from 'prettier/parser-yaml';
9+
import { parseDocument, ToStringOptions } from 'yaml';
1210
import { TextDocument } from 'vscode-languageserver-textdocument';
11+
import { YamlVersion } from '../parser/yamlParser07';
1312

1413
export class YAMLFormatter {
1514
private formatterEnabled = true;
15+
private yamlVersion: YamlVersion = '1.2';
16+
private customTags: string[] = [];
1617

1718
public configure(shouldFormat: LanguageSettings): void {
1819
if (shouldFormat) {
1920
this.formatterEnabled = shouldFormat.format;
21+
this.yamlVersion = shouldFormat.yamlVersion;
22+
this.customTags = shouldFormat.customTags;
2023
}
2124
}
2225

@@ -27,23 +30,26 @@ export class YAMLFormatter {
2730

2831
try {
2932
const text = document.getText();
33+
const doc = parseDocument(text, {
34+
version: this.yamlVersion,
35+
});
3036

31-
const prettierOptions: Options = {
32-
parser: 'yaml',
33-
plugins: [parser],
34-
37+
const toStringOptions: ToStringOptions = {
3538
// --- FormattingOptions ---
36-
tabWidth: (options.tabWidth as number) || options.tabSize,
39+
indent: (options.tabWidth as number) || options.tabSize || 2,
3740

3841
// --- CustomFormatterOptions ---
3942
singleQuote: options.singleQuote,
40-
bracketSpacing: options.bracketSpacing,
41-
// 'preserve' is the default for Options.proseWrap. See also server.ts
42-
proseWrap: 'always' === options.proseWrap ? 'always' : 'never' === options.proseWrap ? 'never' : 'preserve',
43-
printWidth: options.printWidth,
43+
flowCollectionPadding: options.bracketSpacing,
44+
blockQuote: options.proseWrap === 'always' ? 'folded' : true,
45+
lineWidth: Math.max(options.printWidth || 0, 22),
4446
};
4547

46-
const formatted = prettier.format(text, prettierOptions);
48+
const formatted = doc.toString(toStringOptions);
49+
50+
if (formatted === text) {
51+
return [];
52+
}
4753

4854
return [TextEdit.replace(Range.create(Position.create(0, 0), document.positionAt(text.length)), formatted)];
4955
} catch (error) {

test/formatter.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('Formatter Tests', () => {
5656
printWidth: 20,
5757
proseWrap: 'always',
5858
});
59-
assert.equal(edits[0].newText, 'comments: >\n test test test\n test test test\n test test test\n test test test\n');
59+
assert.equal(edits[0].newText, 'comments: >\n test test test test\n test test test test\n test test test test\n');
6060
});
6161

6262
it('Formatting uses tabSize', () => {

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3222,10 +3222,10 @@ yallist@^4.0.0:
32223222
resolved "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz"
32233223
integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==
32243224

3225-
yaml@2.2.2:
3226-
version "2.2.2"
3227-
resolved "https://registry.yarnpkg.com/yaml/-/yaml-2.2.2.tgz#ec551ef37326e6d42872dad1970300f8eb83a073"
3228-
integrity sha512-CBKFWExMn46Foo4cldiChEzn7S7SRV+wqiluAb6xmueD/fGyRHIhX8m14vVGgeFWjN540nKCNVj6P21eQjgTuA==
3225+
yaml@2.4.5:
3226+
version "2.4.5"
3227+
resolved "https://registry.yarnpkg.com/yaml/-/yaml-2.4.5.tgz#60630b206dd6d84df97003d33fc1ddf6296cca5e"
3228+
integrity sha512-aBx2bnqDzVOyNKfsysjA2ms5ZlnjSAW2eG3/L5G/CSujfjLJTJsEw1bGw8kCf04KodQWk1pxlGnZ56CRxiawmg==
32293229

32303230
yargs-parser@20.2.4, yargs-parser@^20.2.2:
32313231
version "20.2.4"

0 commit comments

Comments
 (0)