Skip to content

Commit a5092e3

Browse files
committed
Added formatter
1 parent 74f640a commit a5092e3

File tree

3 files changed

+11
-35
lines changed

3 files changed

+11
-35
lines changed

package-lock.json

+6-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"dependencies": {
2525
"js-yaml": "^3.12.0",
2626
"jsonc-parser": "^1.0.3",
27+
"prettier": "^1.14.3",
2728
"request-light": "^0.2.3",
2829
"vscode-json-languageservice": "3.0.12",
2930
"vscode-languageserver": "^4.0.0",

src/languageservice/services/yamlFormatter.ts

+4-34
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,13 @@
55
*--------------------------------------------------------------------------------------------*/
66
'use strict';
77

8-
import * as jsyaml from 'js-yaml';
9-
import * as Yaml from 'yaml-ast-parser'
10-
import { EOL } from 'os';
118
import { TextDocument, Range, Position, FormattingOptions, TextEdit } from 'vscode-languageserver-types';
9+
const prettier = require("prettier");
1210

1311
export function format(document: TextDocument, options: FormattingOptions, customTags: Array<String>): TextEdit[] {
1412
const text = document.getText();
1513

16-
let schemaWithAdditionalTags = jsyaml.Schema.create(customTags.map((tag) => {
17-
const typeInfo = tag.split(' ');
18-
return new jsyaml.Type(typeInfo[0], { kind: typeInfo[1] || 'scalar' });
19-
}));
14+
const formatted = prettier.format(text, { parser: "yaml"});
2015

21-
//We need compiledTypeMap to be available from schemaWithAdditionalTags before we add the new custom properties
22-
customTags.map((tag) => {
23-
const typeInfo = tag.split(' ');
24-
schemaWithAdditionalTags.compiledTypeMap[typeInfo[0]] = new jsyaml.Type(typeInfo[0], { kind: typeInfo[1] || 'scalar' });
25-
});
26-
27-
let additionalOptions: Yaml.LoadOptions = {
28-
schema: schemaWithAdditionalTags
29-
}
30-
31-
const documents = []
32-
jsyaml.loadAll(text, doc => documents.push(doc), additionalOptions)
33-
34-
const dumpOptions = { indent: options.tabSize, noCompatMode: true };
35-
36-
let newText;
37-
if (documents.length == 1) {
38-
const yaml = documents[0]
39-
newText = jsyaml.safeDump(yaml, dumpOptions)
40-
}
41-
else {
42-
const formatted = documents.map(d => jsyaml.safeDump(d, dumpOptions))
43-
newText = '%YAML 1.2' + EOL + '---' + EOL + formatted.join('...' + EOL + '---' + EOL) + '...' + EOL
44-
}
45-
46-
return [TextEdit.replace(Range.create(Position.create(0, 0), document.positionAt(text.length)), newText)]
47-
}
16+
return [TextEdit.replace(Range.create(Position.create(0, 0), document.positionAt(text.length)), formatted)];
17+
}

0 commit comments

Comments
 (0)