Skip to content

Commit

Permalink
Merge pull request #126 from redhat-developer/fixFormatter
Browse files Browse the repository at this point in the history
Fix formatter
  • Loading branch information
JPinkney authored Mar 20, 2019
2 parents 43c6d8c + 767e655 commit 817a4a3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
27 changes: 22 additions & 5 deletions src/languageservice/services/yamlFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)];
}
8 changes: 6 additions & 2 deletions src/languageservice/yamlLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { 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<String>; //Array of Custom Tags
Expand Down Expand Up @@ -97,6 +98,7 @@ export interface CustomFormatterOptions {
bracketSpacing?: boolean;
proseWrap?: string;
printWidth?: number;
enable?: boolean;
}

export interface LanguageService {
Expand All @@ -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) => {
Expand All @@ -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);
Expand All @@ -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)
}
}
18 changes: 13 additions & 5 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -223,12 +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.bracketSpacing !== undefined) {
yamlFormatterSettings.bracketSpacing = settings.yaml.format.bracketSpacing;
}
if (settings.yaml.format.enable !== undefined) {
yamlFormatterSettings.enable = settings.yaml.format.enable;
}
}
}
Expand Down Expand Up @@ -319,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];
Expand Down
7 changes: 4 additions & 3 deletions test/formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 });
Expand Down

0 comments on commit 817a4a3

Please sign in to comment.