-
Notifications
You must be signed in to change notification settings - Fork 278
/
Copy pathyamlFormatter.ts
56 lines (46 loc) · 2.07 KB
/
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
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
/*---------------------------------------------------------------------------------------------
* 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.
*--------------------------------------------------------------------------------------------*/
'use strict';
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 { TextDocument } from 'vscode-languageserver-textdocument';
export class YAMLFormatter {
private formatterEnabled = true;
public configure(shouldFormat: LanguageSettings): void {
if (shouldFormat) {
this.formatterEnabled = shouldFormat.format;
}
}
public format(document: TextDocument, options: FormattingOptions & CustomFormatterOptions): TextEdit[] {
if (!this.formatterEnabled) {
return [];
}
try {
const text = document.getText();
const prettierOptions: Options = {
parser: 'yaml',
plugins: [parser],
// --- FormattingOptions ---
tabWidth: (options.tabWidth as number) || options.tabSize,
// --- 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,
commentSpacesFromContent: options.commentSpacesFromContent,
doNotIndent: options.doNotIndent,
};
const formatted = prettier.format(text, prettierOptions);
return [TextEdit.replace(Range.create(Position.create(0, 0), document.positionAt(text.length)), formatted)];
} catch (error) {
return [];
}
}
}