From 73c244a3efe09ec4250def78068c54af3acaed58 Mon Sep 17 00:00:00 2001 From: jpinkney Date: Tue, 15 May 2018 15:36:18 -0400 Subject: [PATCH] Added custom tags to autocompletion --- .../services/yamlCompletion.ts | 30 +++++++++++++++++++ src/languageservice/yamlLanguageService.ts | 4 ++- src/server.ts | 3 +- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/languageservice/services/yamlCompletion.ts b/src/languageservice/services/yamlCompletion.ts index 797b26c5..85e90311 100644 --- a/src/languageservice/services/yamlCompletion.ts +++ b/src/languageservice/services/yamlCompletion.ts @@ -25,11 +25,19 @@ export class YAMLCompletion { private schemaService: SchemaService.IJSONSchemaService; private contributions: JSONWorkerContribution[]; private promise: PromiseConstructor; + private customTags: Array; constructor(schemaService: SchemaService.IJSONSchemaService, contributions: JSONWorkerContribution[] = [], promiseConstructor?: PromiseConstructor) { this.schemaService = schemaService; this.contributions = contributions; this.promise = promiseConstructor || Promise; + this.customTags = []; + } + + public configure(customTags: Array){ + if(customTags && customTags.length > 0){ + this.customTags = customTags; + } } public doResolve(item: CompletionItem): Thenable { @@ -185,6 +193,9 @@ export class YAMLCompletion { if (this.contributions.length > 0) { this.getContributedValueCompletions(currentDoc, node, offset, document, collector, collectionPromises); } + if (this.customTags.length > 0) { + this.getCustomTagValueCompletions(collector); + } return this.promise.all(collectionPromises).then(() => { return result; @@ -322,6 +333,15 @@ export class YAMLCompletion { } } + private getCustomTagValueCompletions(collector: CompletionsCollector) { + this.customTags.forEach((customTagItem) => { + let tagItemSplit = customTagItem.split(" "); + if(tagItemSplit && tagItemSplit[0]){ + this.addCustomTagValueCompletion(collector, " ", tagItemSplit[0]); + } + }); + } + private addSchemaValueCompletions(schema: JSONSchema, collector: CompletionsCollector, types: { [type: string]: boolean }, separatorAfter: string): void { this.addDefaultValueCompletions(schema, collector, separatorAfter); this.addEnumValueCompletions(schema, collector, separatorAfter); @@ -408,6 +428,16 @@ export class YAMLCompletion { }); } + private addCustomTagValueCompletion(collector: CompletionsCollector, separatorAfter: string, label: string): void { + collector.add({ + kind: this.getSuggestionKind('string'), + label: label, + insertText: label + separatorAfter, + insertTextFormat: InsertTextFormat.Snippet, + documentation: '' + }); + } + private getLabelForValue(value: any): string { let label = typeof value === "string" ? value : JSON.stringify(value); if (label.length > 57) { diff --git a/src/languageservice/yamlLanguageService.ts b/src/languageservice/yamlLanguageService.ts index 9ad4b286..28a91cb8 100644 --- a/src/languageservice/yamlLanguageService.ts +++ b/src/languageservice/yamlLanguageService.ts @@ -17,7 +17,8 @@ import { YAMLDocument, Diagnostic } from 'vscode-yaml-languageservice'; export interface LanguageSettings { validate?: boolean; //Setting for whether we want to validate the schema isKubernetes?: boolean; //If true then its validating against kubernetes - schemas?: any[]; //List of schemas + schemas?: any[]; //List of schemas, + customTags?: Array; //Array of Custom Tags } export interface PromiseConstructor { @@ -119,6 +120,7 @@ export function getLanguageService(schemaRequestService, workspaceContext, contr }); } yamlValidation.configure(settings); + completer.configure(settings["customTags"]); }, doComplete: completer.doComplete.bind(completer), doResolve: completer.doResolve.bind(completer), diff --git a/src/server.ts b/src/server.ts index 1074be77..024c427f 100755 --- a/src/server.ts +++ b/src/server.ts @@ -297,7 +297,8 @@ connection.onNotification(SchemaAssociationNotification.type, associations => { function updateConfiguration() { let languageSettings: LanguageSettings = { validate: yamlShouldValidate, - schemas: [] + schemas: [], + customTags: customTags }; if (schemaAssociations) { for (var pattern in schemaAssociations) {