forked from redhat-developer/yaml-language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyamlFormatter.ts
More file actions
65 lines (55 loc) · 2.34 KB
/
yamlFormatter.ts
File metadata and controls
65 lines (55 loc) · 2.34 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*---------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Copyright (c) Adam Voss. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { TextDocument } from 'vscode-languageserver-textdocument';
import { FormattingOptions, Position, Range, TextEdit } from 'vscode-languageserver-types';
import { parseDocument, ToStringOptions } from 'yaml';
import { YamlVersion } from '../parser/yamlParser07';
import { CustomFormatterOptions, LanguageSettings } from '../yamlLanguageService';
import { getCustomTags } from '../parser/custom-tag-provider';
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;
}
}
public async format(
document: TextDocument,
options: Partial<FormattingOptions> & CustomFormatterOptions = {}
): Promise<TextEdit[]> {
if (!this.formatterEnabled) {
return [];
}
try {
const text = document.getText();
const doc = parseDocument(text, {
version: this.yamlVersion,
customTags: getCustomTags(this.customTags),
});
const toStringOptions: ToStringOptions = {
// --- FormattingOptions ---
indent: (options.tabWidth as number) || options.tabSize || 2,
// --- CustomFormatterOptions ---
singleQuote: options.singleQuote,
flowCollectionPadding: options.bracketSpacing,
blockQuote: options.proseWrap === 'always' ? 'folded' : true,
lineWidth: Math.max(options.printWidth || 0, 22),
trailingComma: options.trailingComma === undefined ? true : options.trailingComma,
};
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) {
return [];
}
}
}