Skip to content

Commit 58b628b

Browse files
committed
Revert "Use yaml package for formatting"
This reverts commit 22d01b4. There were many bugs in the new formatter implementation, so we're reverting for now. In order to move forwards with this, we'll either need to adjust the behaviour of `toString` in the YAML parser through contributing patches, or write our own formatter. Fixes redhat-developer/vscode-yaml#1237 Fixes redhat-developer/vscode-yaml#1235 Fixes redhat-developer/vscode-yaml#1234 Fixes redhat-developer/vscode-yaml#1233 Fixes #1237
1 parent 35d8e53 commit 58b628b

5 files changed

Lines changed: 22 additions & 41 deletions

File tree

package-lock.json

Lines changed: 1 addition & 2 deletions
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
@@ -31,6 +31,7 @@
3131
"ajv": "^8.17.1",
3232
"ajv-draft-04": "^1.0.0",
3333
"ajv-i18n": "^4.2.0",
34+
"prettier": "^3.8.1",
3435
"request-light": "^0.5.7",
3536
"vscode-json-languageservice": "4.1.8",
3637
"vscode-languageserver": "^9.0.0",
@@ -56,7 +57,6 @@
5657
"mocha": "11.7.1",
5758
"mocha-lcov-reporter": "^1.3.0",
5859
"nyc": "^15.1.0",
59-
"prettier": "3.8.1",
6060
"rimraf": "^3.0.2",
6161
"sinon": "^9.0.3",
6262
"sinon-chai": "^3.5.0",

src/languageserver/handlers/settingsHandlers.ts

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

107107
if (settings.yaml.format) {
108108
this.yamlSettings.yamlFormatterSettings = {
109+
proseWrap: settings.yaml.format.proseWrap || 'preserve',
109110
printWidth: settings.yaml.format.printWidth || 80,
110111
};
111112

src/languageservice/services/yamlFormatter.ts

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

7-
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';
7+
import { Range, Position, TextEdit, FormattingOptions } from 'vscode-languageserver-types';
118
import { CustomFormatterOptions, LanguageSettings } from '../yamlLanguageService';
12-
import { getCustomTags } from '../parser/custom-tag-provider';
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';
13+
import { TextDocument } from 'vscode-languageserver-textdocument';
1314

1415
export class YAMLFormatter {
1516
private formatterEnabled = true;
16-
private yamlVersion: YamlVersion = '1.2';
17-
private customTags: string[] = [];
1817

1918
public configure(shouldFormat: LanguageSettings): void {
2019
if (shouldFormat) {
2120
this.formatterEnabled = shouldFormat.format;
22-
this.yamlVersion = shouldFormat.yamlVersion;
23-
this.customTags = shouldFormat.customTags;
2421
}
2522
}
2623

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

3532
try {
3633
const text = document.getText();
37-
const doc = parseDocument(text, {
38-
version: this.yamlVersion,
39-
customTags: getCustomTags(this.customTags),
40-
});
4134

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

4642
// --- CustomFormatterOptions ---
4743
singleQuote: options.singleQuote,
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,
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',
5249
};
5350

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

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

test/formatter.test.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,7 @@ describe('Formatter Tests', () => {
8686
printWidth: 20,
8787
proseWrap: 'always',
8888
});
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);
89+
assert.equal(edits[0].newText, 'comments: >\n test test test\n test test test\n test test test\n test test test\n');
10290
});
10391

10492
it('Formatting handles trailing commas (enabled)', async () => {
@@ -110,7 +98,7 @@ describe('Formatter Tests', () => {
11098
}
11199
`;
112100
const edits = await parseSetup(content, { singleQuote: true });
113-
assert.equal(edits.length, 0);
101+
assert.equal(edits[0].newText, content);
114102
});
115103

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

0 commit comments

Comments
 (0)