From 4a24571303f22d57c1d802cb2b1524215c33b665 Mon Sep 17 00:00:00 2001 From: jpinkney Date: Mon, 12 Jun 2017 16:49:02 -0400 Subject: [PATCH] Removed the constant coverting to CompletionList --- .../languageService/services/autoCompleter.ts | 46 ++++++++++++++----- .../services/yamlCompletion.ts | 31 +++++++------ server/src/server.ts | 1 + 3 files changed, 54 insertions(+), 24 deletions(-) diff --git a/server/src/languageService/services/autoCompleter.ts b/server/src/languageService/services/autoCompleter.ts index 00b47962..17dcecb6 100644 --- a/server/src/languageService/services/autoCompleter.ts +++ b/server/src/languageService/services/autoCompleter.ts @@ -1,7 +1,7 @@ import {SchemaToMappingTransformer} from "../schemaToMappingTransformer" import {TextDocument, CompletionList} from 'vscode-languageserver-types'; import {JSONSchema} from "../jsonSchema"; -import {YAMLDocument, YAMLNode} from 'yaml-ast-parser'; +import {YAMLDocument, YAMLNode, Kind} from 'yaml-ast-parser'; let AutoComplete = require('triesearch'); @@ -10,23 +10,25 @@ export class AutoCompleter { private autoCompleter; private schema: JSONSchema; private kuberSchema; + private currentWords; constructor(schema:JSONSchema){ this.schema = schema; this.autoCompleter = new AutoComplete(); this.kuberSchema = new SchemaToMappingTransformer(this.schema).getSchema(); + this.currentWords = []; } public search(searchItem: String): Array{ - return this.autoCompleter.search(searchItem).map(x => ({ + let results = this.autoCompleter.search(searchItem).map(x => ({ label: x.value.toString() })); + return results; } public searchAll() { - return Object.keys(this.kuberSchema).map(x => ({ - label: x.toString() - })); + let results = Object.keys(this.kuberSchema); + return this.arrToCompletionList(results); } public initData(data:Array): void { @@ -34,7 +36,7 @@ export class AutoCompleter { this.autoCompleter.initialize(data); } - public purge(): void{ + private purge(): void{ this.autoCompleter.words = 0; this.autoCompleter.prefixes = 0; this.autoCompleter.value = ""; @@ -42,13 +44,31 @@ export class AutoCompleter { } public generateResults(node){ - let getParentNodeValue = this.getParentVal(node); - if(getParentNodeValue !== ""){ - let results = this.kuberSchema[getParentNodeValue].map(x => x.children).reduce((a, b) => a.concat(b)).filter((value, index, self) => self.indexOf(value) === index); - this.initData(results); + let genVal = ""; + + if(node.kind === Kind.MAPPING && node.value === null){ + genVal = this.getParentVal(node); }else{ + genVal = node.key.value; + } + + if(genVal === ""){ this.initData(Object.keys(this.kuberSchema)); + return this.search(node.key.value); + }else{ + + let results = this.kuberSchema[genVal].map(x => x.children).reduce((a, b) => a.concat(b)).filter((value, index, self) => self.indexOf(value) === index); + if(genVal !== node.key.value){ + this.initData(results); + return this.search(node.key.value); + }else{ + return this.arrToCompletionList(results); + } + + + } + } private getParentVal(node: YAMLNode){ @@ -70,7 +90,11 @@ export class AutoCompleter { public generateScalarAutocompletion(nodeValue: String){ let results = this.kuberSchema[nodeValue.toString()].map(x => x.default).filter((value, index, self) => self.indexOf(value) === index && value !== undefined); - return results.map(x => ({ + return this.arrToCompletionList(results); + } + + private arrToCompletionList(arr){ + return arr.map(x => ({ label: x.toString() })); } diff --git a/server/src/languageService/services/yamlCompletion.ts b/server/src/languageService/services/yamlCompletion.ts index 900fcd4f..1a87d7e0 100644 --- a/server/src/languageService/services/yamlCompletion.ts +++ b/server/src/languageService/services/yamlCompletion.ts @@ -25,22 +25,27 @@ export class YamlCompletion { let offset = document.offsetAt(position); let node = findNode(doc, offset); - if(node !== undefined && node.kind === Kind.SCALAR){ - return autoComplete.generateScalarAutocompletion(node.parent.key.value); - } - - if(node != undefined && node.value !== null && node.value !== undefined && node.value.kind === Kind.SCALAR){ - return autoComplete.generateScalarAutocompletion(node.key.value); - } - - if(node === undefined || node.parent === null){ - //Its a root node + if(node === undefined || node.kind === Kind.MAP){ + return autoComplete.searchAll(); + }else{ - autoComplete.generateResults(node); - return autoComplete.search(node.key.value); - } + + if(node.kind === Kind.SCALAR){ + return autoComplete.generateScalarAutocompletion(node.parent.key.value); + + }else if(node.value != null && node.kind === Kind.MAPPING && node.value.kind === Kind.SCALAR){ + + return autoComplete.generateScalarAutocompletion(node.key.value); + + }else{ + + return autoComplete.generateResults(node); + + } + + } }); } diff --git a/server/src/server.ts b/server/src/server.ts index 0e745ac8..10874f6f 100755 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -231,6 +231,7 @@ connection.onCompletionResolve((item: CompletionItem): CompletionItem => { return item; }); + let t: Thenable; /*