Skip to content

Commit

Permalink
Added custom tags to autocompletion
Browse files Browse the repository at this point in the history
  • Loading branch information
JPinkney committed May 15, 2018
1 parent 5f5d212 commit 73c244a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
30 changes: 30 additions & 0 deletions src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,19 @@ export class YAMLCompletion {
private schemaService: SchemaService.IJSONSchemaService;
private contributions: JSONWorkerContribution[];
private promise: PromiseConstructor;
private customTags: Array<String>;

constructor(schemaService: SchemaService.IJSONSchemaService, contributions: JSONWorkerContribution[] = [], promiseConstructor?: PromiseConstructor) {
this.schemaService = schemaService;
this.contributions = contributions;
this.promise = promiseConstructor || Promise;
this.customTags = [];
}

public configure(customTags: Array<String>){
if(customTags && customTags.length > 0){
this.customTags = customTags;
}
}

public doResolve(item: CompletionItem): Thenable<CompletionItem> {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion src/languageservice/yamlLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>; //Array of Custom Tags
}

export interface PromiseConstructor {
Expand Down Expand Up @@ -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),
Expand Down
3 changes: 2 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 73c244a

Please sign in to comment.