'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 ) { if ( shouldFormat ) { this . formatterEnabled = shouldFormat . format ; } } public format ( document , options ) { if ( ! this . formatterEnabled ) { return [ ] ; } try { const text = document . getText ( ) ; const prettierOptions = { parser : 'yaml' , plugins : [ parser ] , tabWidth : ( options . tabWidth as number ) || options . tabSize , singleQuote : options . singleQuote , bracketSpacing : options . bracketSpacing , proseWrap : 'always' === options . proseWrap ? 'always' : 'never' === options . proseWrap ? 'never' : 'preserve' , printWidth : options . printWidth , } ; const formatted = prettier . format ( text , prettierOptions ) ; return [ TextEdit . replace ( Range . create ( Position . create ( 0 , 0 ) , document . positionAt ( text . length ) ) , formatted ) ] ; } catch ( error ) { return [ ] ; } } }