5
5
*--------------------------------------------------------------------------------------------*/
6
6
'use strict' ;
7
7
8
- import * as jsyaml from 'js-yaml' ;
9
- import * as Yaml from 'yaml-ast-parser'
10
- import { EOL } from 'os' ;
11
8
import { TextDocument , Range , Position , FormattingOptions , TextEdit } from 'vscode-languageserver-types' ;
9
+ const prettier = require ( "prettier" ) ;
12
10
13
11
export function format ( document : TextDocument , options : FormattingOptions , customTags : Array < String > ) : TextEdit [ ] {
14
12
const text = document . getText ( ) ;
15
13
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" } ) ;
20
15
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