-
Notifications
You must be signed in to change notification settings - Fork 8
/
yamlFormatter.ts
26 lines (20 loc) · 920 Bytes
/
yamlFormatter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
'use strict';
import jsyaml = require('js-yaml')
import { EOL } from 'os';
import { TextDocument, Range, Position, FormattingOptions, TextEdit } from 'vscode-languageserver-types';
export function format(document: TextDocument, options: FormattingOptions): TextEdit[] {
const text = document.getText()
const documents = []
jsyaml.loadAll(text, doc => documents.push(doc))
const dumpOptions = { indent: options.tabSize, noCompatMode: true };
let newText;
if (documents.length == 1) {
const yaml = documents[0]
newText = jsyaml.safeDump(yaml, dumpOptions)
}
else {
const formatted = documents.map(d => jsyaml.safeDump(d, dumpOptions))
newText = '%YAML 1.2' + EOL + '---' + EOL + formatted.join('...' + EOL + '---' + EOL) + '...' + EOL
}
return [TextEdit.replace(Range.create(Position.create(0, 0), document.positionAt(text.length)), newText)]
}