Skip to content

Commit c62ade6

Browse files
remcohaszingdatho7561
authored andcommitted
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 577a456 commit c62ade6

File tree

5 files changed

+41
-22
lines changed

5 files changed

+41
-22
lines changed

package-lock.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
"@vscode/l10n": "^0.0.18",
3131
"ajv": "^8.17.1",
3232
"ajv-draft-04": "^1.0.0",
33-
"prettier": "^3.8.1",
3433
"request-light": "^0.5.7",
3534
"vscode-json-languageservice": "4.1.8",
3635
"vscode-languageserver": "^9.0.0",
@@ -56,6 +55,7 @@
5655
"mocha": "11.7.1",
5756
"mocha-lcov-reporter": "^1.3.0",
5857
"nyc": "^15.1.0",
58+
"prettier": "3.8.1",
5959
"rimraf": "^3.0.2",
6060
"sinon": "^9.0.3",
6161
"sinon-chai": "^3.5.0",

src/languageserver/handlers/settingsHandlers.ts

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

107107
if (settings.yaml.format) {
108108
this.yamlSettings.yamlFormatterSettings = {
109-
proseWrap: settings.yaml.format.proseWrap || 'preserve',
110109
printWidth: settings.yaml.format.printWidth || 80,
111110
};
112111

src/languageservice/services/yamlFormatter.ts

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@
44
* Licensed under the MIT License. See License.txt in the project root for license information.
55
*--------------------------------------------------------------------------------------------*/
66

7-
import { Range, Position, TextEdit, FormattingOptions } from 'vscode-languageserver-types';
8-
import { CustomFormatterOptions, LanguageSettings } from '../yamlLanguageService';
9-
import { Options } from 'prettier';
10-
import * as yamlPlugin from 'prettier/plugins/yaml';
11-
import * as estreePlugin from 'prettier/plugins/estree';
12-
import { format } from 'prettier/standalone';
137
import { TextDocument } from 'vscode-languageserver-textdocument';
8+
import { FormattingOptions, Position, Range, TextEdit } from 'vscode-languageserver-types';
9+
import { parseDocument, ToStringOptions } from 'yaml';
10+
import { YamlVersion } from '../parser/yamlParser07';
11+
import { CustomFormatterOptions, LanguageSettings } from '../yamlLanguageService';
12+
import { getCustomTags } from '../parser/custom-tag-provider';
1413

1514
export class YAMLFormatter {
1615
private formatterEnabled = true;
16+
private yamlVersion: YamlVersion = '1.2';
17+
private customTags: string[] = [];
1718

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

@@ -31,24 +34,28 @@ export class YAMLFormatter {
3134

3235
try {
3336
const text = document.getText();
37+
const doc = parseDocument(text, {
38+
version: this.yamlVersion,
39+
customTags: getCustomTags(this.customTags),
40+
});
3441

35-
const prettierOptions: Options = {
36-
parser: 'yaml',
37-
plugins: [yamlPlugin, estreePlugin],
38-
42+
const toStringOptions: ToStringOptions = {
3943
// --- FormattingOptions ---
40-
tabWidth: (options.tabWidth as number) || options.tabSize,
44+
indent: (options.tabWidth as number) || options.tabSize || 2,
4145

4246
// --- CustomFormatterOptions ---
4347
singleQuote: options.singleQuote,
44-
bracketSpacing: options.bracketSpacing,
45-
// 'preserve' is the default for Options.proseWrap. See also server.ts
46-
proseWrap: 'always' === options.proseWrap ? 'always' : 'never' === options.proseWrap ? 'never' : 'preserve',
47-
printWidth: options.printWidth,
48-
trailingComma: options.trailingComma === false ? 'none' : 'all',
48+
flowCollectionPadding: options.bracketSpacing,
49+
blockQuote: options.proseWrap === 'always' ? 'folded' : true,
50+
lineWidth: Math.max(options.printWidth || 0, 22),
51+
trailingComma: options.trailingComma === undefined ? true : options.trailingComma,
4952
};
5053

51-
const formatted = await format(text, prettierOptions);
54+
const formatted = doc.toString(toStringOptions);
55+
56+
if (formatted === text) {
57+
return [];
58+
}
5259

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

test/formatter.test.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,19 @@ describe('Formatter Tests', () => {
8686
printWidth: 20,
8787
proseWrap: 'always',
8888
});
89-
assert.equal(edits[0].newText, 'comments: >\n test test test\n test test test\n test test test\n test test test\n');
89+
assert.equal(edits[0].newText, 'comments: >\n test test test test\n test test test test\n test test test test\n');
90+
});
91+
92+
it('Formatting handles trailing commas (default)', async () => {
93+
const content = `{
94+
key: 'value',
95+
food: 'raisins',
96+
airport: 'YYZ',
97+
lightened_bulb: 'illuminating',
98+
}
99+
`;
100+
const edits = await parseSetup(content, { singleQuote: true, trailingComma: true });
101+
assert.equal(edits.length, 0);
90102
});
91103

92104
it('Formatting handles trailing commas (enabled)', async () => {
@@ -98,7 +110,7 @@ describe('Formatter Tests', () => {
98110
}
99111
`;
100112
const edits = await parseSetup(content, { singleQuote: true });
101-
assert.equal(edits[0].newText, content);
113+
assert.equal(edits.length, 0);
102114
});
103115

104116
it('Formatting handles trailing commas (disabled)', async () => {

0 commit comments

Comments
 (0)