From 6b20b8198f956a02ca1ac3154bc85b59199c8e86 Mon Sep 17 00:00:00 2001 From: jpinkney Date: Wed, 20 Feb 2019 11:12:20 -0500 Subject: [PATCH 1/3] Fix formatter --- src/languageservice/services/yamlFormatter.ts | 27 +++++++++++++++---- src/languageservice/yamlLanguageService.ts | 8 ++++-- src/server.ts | 6 ++++- test/formatter.test.ts | 7 ++--- 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/languageservice/services/yamlFormatter.ts b/src/languageservice/services/yamlFormatter.ts index a6f73ec1..1ab769fb 100644 --- a/src/languageservice/services/yamlFormatter.ts +++ b/src/languageservice/services/yamlFormatter.ts @@ -6,13 +6,30 @@ 'use strict'; import { TextDocument, Range, Position, TextEdit } from 'vscode-languageserver-types'; -import { CustomFormatterOptions } from '../yamlLanguageService'; +import { CustomFormatterOptions, LanguageSettings } from '../yamlLanguageService'; import * as prettier from 'prettier'; -export function format(document: TextDocument, options: CustomFormatterOptions): TextEdit[] { - const text = document.getText(); +export class YAMLFormatter { - const formatted = prettier.format(text, Object.assign(options, { parser: "yaml" as prettier.BuiltInParserName })); + private formatterEnabled: boolean = true; + + public configure(shouldFormat: LanguageSettings) { + if (shouldFormat) { + this.formatterEnabled = shouldFormat.format; + } + } + + public format(document: TextDocument, options: CustomFormatterOptions): TextEdit[] { + + if (!this.formatterEnabled) { + return []; + } + + const text = document.getText(); + + const formatted = prettier.format(text, Object.assign(options, { parser: "yaml" as prettier.BuiltInParserName })); + + return [TextEdit.replace(Range.create(Position.create(0, 0), document.positionAt(text.length)), formatted)]; + } - return [TextEdit.replace(Range.create(Position.create(0, 0), document.positionAt(text.length)), formatted)]; } diff --git a/src/languageservice/yamlLanguageService.ts b/src/languageservice/yamlLanguageService.ts index 0deb993a..73f58fd7 100644 --- a/src/languageservice/yamlLanguageService.ts +++ b/src/languageservice/yamlLanguageService.ts @@ -11,12 +11,13 @@ import { YAMLDocumentSymbols } from './services/documentSymbols'; import { YAMLCompletion } from "./services/yamlCompletion"; import { YAMLHover } from "./services/yamlHover"; import { YAMLValidation } from "./services/yamlValidation"; -import { format } from './services/yamlFormatter'; +import { format, YAMLFormatter } from './services/yamlFormatter'; export interface LanguageSettings { validate?: boolean; //Setting for whether we want to validate the schema hover?: boolean; //Setting for whether we want to have hover results completion?: boolean; //Setting for whether we want to have completion results + format?: boolean; //Setting for whether we want to have the formatter or not isKubernetes?: boolean; //If true then its validating against kubernetes schemas?: any[]; //List of schemas, customTags?: Array; //Array of Custom Tags @@ -97,6 +98,7 @@ export interface CustomFormatterOptions { bracketSpacing?: boolean; proseWrap?: string; printWidth?: number; + enable?: boolean; } export interface LanguageService { @@ -120,6 +122,7 @@ export function getLanguageService(schemaRequestService, workspaceContext, contr let hover = new YAMLHover(schemaService, contributions, promise); let yamlDocumentSymbols = new YAMLDocumentSymbols(); let yamlValidation = new YAMLValidation(schemaService, promise); + let formatter = new YAMLFormatter(); return { configure: (settings) => { @@ -133,6 +136,7 @@ export function getLanguageService(schemaRequestService, workspaceContext, contr hover.configure(settings); let customTagsSetting = settings && settings["customTags"] ? settings["customTags"] : []; completer.configure(settings, customTagsSetting); + formatter.configure(settings); }, registerCustomSchemaProvider: (schemaProvider: CustomSchemaProvider) => { schemaService.registerCustomSchemaProvider(schemaProvider); @@ -143,6 +147,6 @@ export function getLanguageService(schemaRequestService, workspaceContext, contr doHover: hover.doHover.bind(hover), findDocumentSymbols: yamlDocumentSymbols.findDocumentSymbols.bind(yamlDocumentSymbols), resetSchema: (uri: string) => schemaService.onResourceChange(uri), - doFormat: format + doFormat: formatter.format.bind(formatter) } } diff --git a/src/server.ts b/src/server.ts index f72264d6..d84d8cb9 100755 --- a/src/server.ts +++ b/src/server.ts @@ -199,7 +199,8 @@ let yamlFormatterSettings = { singleQuote: false, bracketSpacing: true, proseWrap: "preserve", - printWidth: 80 + printWidth: 80, + enable: true } as CustomFormatterOptions; let yamlShouldHover = true; let yamlShouldCompletion = true; @@ -230,6 +231,9 @@ connection.onDidChangeConfiguration((change) => { if (settings.yaml.format.bracketSpacing === false) { yamlFormatterSettings.bracketSpacing = false; } + if (settings.yaml.format.enable) { + yamlFormatterSettings.enable = settings.yaml.format.enable || true; + } } } schemaConfigurationSettings = []; diff --git a/test/formatter.test.ts b/test/formatter.test.ts index 2f39c4fd..2f454cc3 100644 --- a/test/formatter.test.ts +++ b/test/formatter.test.ts @@ -10,7 +10,7 @@ import { CompletionItem, CompletionItemKind, RequestType } from 'vscode-languageserver'; import { xhr, XHRResponse, configure as configureHttpRequests, getErrorStatusDescription } from 'request-light'; -import { getLanguageService } from '../src/languageservice/yamlLanguageService' +import { getLanguageService, LanguageSettings } from '../src/languageservice/yamlLanguageService' import Strings = require('../src/languageservice/utils/strings'); import URI from '../src/languageservice/utils/uri'; import * as URL from 'url'; @@ -25,10 +25,11 @@ let languageService = getLanguageService(schemaRequestService, workspaceContext, let schemaService = new JSONSchemaService(schemaRequestService, workspaceContext); let uri = 'http://json.schemastore.org/bowerrc'; -let languageSettings = { +let languageSettings: LanguageSettings = { schemas: [], validate: true, - customTags: [] + customTags: [], + format: true }; let fileMatch = ["*.yml", "*.yaml"]; languageSettings.schemas.push({ uri, fileMatch: fileMatch }); From f8c2cb1ee99dc2ebd54f47b5212732e0fa82b39c Mon Sep 17 00:00:00 2001 From: jpinkney Date: Wed, 20 Feb 2019 11:52:58 -0500 Subject: [PATCH 2/3] Fixup --- src/languageservice/yamlLanguageService.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/languageservice/yamlLanguageService.ts b/src/languageservice/yamlLanguageService.ts index 73f58fd7..f282f53f 100644 --- a/src/languageservice/yamlLanguageService.ts +++ b/src/languageservice/yamlLanguageService.ts @@ -11,7 +11,7 @@ import { YAMLDocumentSymbols } from './services/documentSymbols'; import { YAMLCompletion } from "./services/yamlCompletion"; import { YAMLHover } from "./services/yamlHover"; import { YAMLValidation } from "./services/yamlValidation"; -import { format, YAMLFormatter } from './services/yamlFormatter'; +import { YAMLFormatter } from './services/yamlFormatter'; export interface LanguageSettings { validate?: boolean; //Setting for whether we want to validate the schema From 767e65575090ac4e4e87c41ec80e6bf231b07bc9 Mon Sep 17 00:00:00 2001 From: jpinkney Date: Wed, 20 Mar 2019 10:31:23 -0400 Subject: [PATCH 3/3] Fixup --- src/server.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/server.ts b/src/server.ts index d84d8cb9..e0ff0976 100755 --- a/src/server.ts +++ b/src/server.ts @@ -224,15 +224,17 @@ connection.onDidChangeConfiguration((change) => { } if (settings.yaml.format) { yamlFormatterSettings = { - singleQuote: settings.yaml.format.singleQuote || false, proseWrap: settings.yaml.format.proseWrap || "preserve", printWidth: settings.yaml.format.printWidth || 80 }; - if (settings.yaml.format.bracketSpacing === false) { - yamlFormatterSettings.bracketSpacing = false; + if (settings.yaml.format.singleQuote !== undefined) { + yamlFormatterSettings.singleQuote = settings.yaml.format.singleQuote; } - if (settings.yaml.format.enable) { - yamlFormatterSettings.enable = settings.yaml.format.enable || true; + if (settings.yaml.format.bracketSpacing !== undefined) { + yamlFormatterSettings.bracketSpacing = settings.yaml.format.bracketSpacing; + } + if (settings.yaml.format.enable !== undefined) { + yamlFormatterSettings.enable = settings.yaml.format.enable; } } } @@ -323,8 +325,10 @@ function updateConfiguration() { hover: yamlShouldHover, completion: yamlShouldCompletion, schemas: [], - customTags: customTags + customTags: customTags, + format: yamlFormatterSettings.enable }; + if (schemaAssociations) { for (var pattern in schemaAssociations) { let association = schemaAssociations[pattern];