From a5092e3d33a2e208bfea7941076518dedd2aba7b Mon Sep 17 00:00:00 2001 From: jpinkney Date: Mon, 24 Sep 2018 16:40:08 -0400 Subject: [PATCH] Added formatter --- package-lock.json | 7 +++- package.json | 1 + src/languageservice/services/yamlFormatter.ts | 38 ++----------------- 3 files changed, 11 insertions(+), 35 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1484d0a5..7921b5b6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "yaml-language-server", - "version": "0.0.18", + "version": "0.0.19", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -3767,6 +3767,11 @@ "integrity": "sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns=", "dev": true }, + "prettier": { + "version": "1.14.3", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.14.3.tgz", + "integrity": "sha512-qZDVnCrnpsRJJq5nSsiHCE3BYMED2OtsI+cmzIzF1QIfqm5ALf8tEJcO27zV1gKNKRPdhjO0dNWnrzssDQ1tFg==" + }, "punycode": { "version": "1.4.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-1.4.1.tgz", diff --git a/package.json b/package.json index 6cbdfab7..c86b0166 100755 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "dependencies": { "js-yaml": "^3.12.0", "jsonc-parser": "^1.0.3", + "prettier": "^1.14.3", "request-light": "^0.2.3", "vscode-json-languageservice": "3.0.12", "vscode-languageserver": "^4.0.0", diff --git a/src/languageservice/services/yamlFormatter.ts b/src/languageservice/services/yamlFormatter.ts index e3b0569a..148f5402 100644 --- a/src/languageservice/services/yamlFormatter.ts +++ b/src/languageservice/services/yamlFormatter.ts @@ -5,43 +5,13 @@ *--------------------------------------------------------------------------------------------*/ 'use strict'; -import * as jsyaml from 'js-yaml'; -import * as Yaml from 'yaml-ast-parser' -import { EOL } from 'os'; import { TextDocument, Range, Position, FormattingOptions, TextEdit } from 'vscode-languageserver-types'; +const prettier = require("prettier"); export function format(document: TextDocument, options: FormattingOptions, customTags: Array): TextEdit[] { const text = document.getText(); - let schemaWithAdditionalTags = jsyaml.Schema.create(customTags.map((tag) => { - const typeInfo = tag.split(' '); - return new jsyaml.Type(typeInfo[0], { kind: typeInfo[1] || 'scalar' }); - })); + const formatted = prettier.format(text, { parser: "yaml"}); - //We need compiledTypeMap to be available from schemaWithAdditionalTags before we add the new custom properties - customTags.map((tag) => { - const typeInfo = tag.split(' '); - schemaWithAdditionalTags.compiledTypeMap[typeInfo[0]] = new jsyaml.Type(typeInfo[0], { kind: typeInfo[1] || 'scalar' }); - }); - - let additionalOptions: Yaml.LoadOptions = { - schema: schemaWithAdditionalTags - } - - const documents = [] - jsyaml.loadAll(text, doc => documents.push(doc), additionalOptions) - - 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)] -} \ No newline at end of file + return [TextEdit.replace(Range.create(Position.create(0, 0), document.positionAt(text.length)), formatted)]; +}