Skip to content

Commit 0600e00

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 0600e00

File tree

5 files changed

+37
-16
lines changed

5 files changed

+37
-16
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: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,20 @@ import { Options } from 'prettier';
1010
import * as yamlPlugin from 'prettier/plugins/yaml';
1111
import * as estreePlugin from 'prettier/plugins/estree';
1212
import { format } from 'prettier/standalone';
13+
import { parseDocument, ToStringOptions } from 'yaml';
1314
import { TextDocument } from 'vscode-languageserver-textdocument';
15+
import { YamlVersion } from '../parser/yamlParser07';
1416

1517
export class YAMLFormatter {
1618
private formatterEnabled = true;
19+
private yamlVersion: YamlVersion = '1.2';
20+
private customTags: string[] = [];
1721

1822
public configure(shouldFormat: LanguageSettings): void {
1923
if (shouldFormat) {
2024
this.formatterEnabled = shouldFormat.format;
25+
this.yamlVersion = shouldFormat.yamlVersion;
26+
this.customTags = shouldFormat.customTags;
2127
}
2228
}
2329

@@ -31,24 +37,27 @@ export class YAMLFormatter {
3137

3238
try {
3339
const text = document.getText();
40+
const doc = parseDocument(text, {
41+
version: this.yamlVersion,
42+
});
3443

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

4248
// --- CustomFormatterOptions ---
4349
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',
50+
flowCollectionPadding: options.bracketSpacing,
51+
blockQuote: options.proseWrap === 'always' ? 'folded' : true,
52+
lineWidth: Math.max(options.printWidth || 0, 22),
53+
trailingComma: options.trailingComma === undefined ? true : options.trailingComma,
4954
};
5055

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

5362
return [TextEdit.replace(Range.create(Position.create(0, 0), document.positionAt(text.length)), formatted)];
5463
} 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)