Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use yaml package for formatting #983

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.5.0"
},
"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.5.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/yaml/-/yaml-2.5.0.tgz#c6165a721cf8000e91c36490a41d7be25176cf5d"
integrity sha512-2wWLbGbYDiSqqIKoPjar3MPgB94ErzCtrNE1FdqGuaO0pi2JGjmE8aW8TDZwzU7vuxcGRdL/4gPQwQ7hD5AMSw==

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