From 6940bef5ea070aa055b4c105f7f08da6b523df9d Mon Sep 17 00:00:00 2001 From: jpinkney Date: Tue, 13 Jun 2017 16:00:54 -0400 Subject: [PATCH] Refactored autoCompleter file --- client/src/extension.ts | 2 +- .../languageService/services/autoCompleter.ts | 82 ++++++++----------- .../services/yamlCompletion.ts | 2 +- 3 files changed, 34 insertions(+), 52 deletions(-) diff --git a/client/src/extension.ts b/client/src/extension.ts index d406fd37..000c9d2f 100755 --- a/client/src/extension.ts +++ b/client/src/extension.ts @@ -8,7 +8,7 @@ import { LanguageClient, LanguageClientOptions, SettingMonitor, ServerOptions, T export function activate(context: ExtensionContext) { // The server is implemented in node - let serverModule = context.asAbsolutePath(path.join('server', 'server.js')); + let serverModule = context.asAbsolutePath(path.join('server/src', 'server.js')); // The debug options for the server let debugOptions = { execArgv: ["--nolazy", "--debug=6009"] }; diff --git a/server/src/languageService/services/autoCompleter.ts b/server/src/languageService/services/autoCompleter.ts index 17dcecb6..b68beda8 100644 --- a/server/src/languageService/services/autoCompleter.ts +++ b/server/src/languageService/services/autoCompleter.ts @@ -7,70 +7,63 @@ let AutoComplete = require('triesearch'); 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 = []; + this.kuberSchema = new SchemaToMappingTransformer(schema).getSchema(); } - public search(searchItem: String): Array{ - let results = this.autoCompleter.search(searchItem).map(x => ({ - label: x.value.toString() + public search(searchItem: String, data: Array): Array{ + let auto = new AutoComplete(); + auto.initialize(data); + return auto.search(searchItem).map(searchResult => ({ + label: searchResult.value.toString() })); - return results; } public searchAll() { - let results = Object.keys(this.kuberSchema); - return this.arrToCompletionList(results); + let allSchemaKeys = Object.keys(this.kuberSchema); + return this.arrToCompletionList(allSchemaKeys); } - public initData(data:Array): void { - this.purge(); - this.autoCompleter.initialize(data); - } - - private purge(): void{ - this.autoCompleter.words = 0; - this.autoCompleter.prefixes = 0; - this.autoCompleter.value = ""; - this.autoCompleter.children = []; - } - - public generateResults(node){ - let genVal = ""; + public generateRegularAutocompletion(node){ + let nodeToSearch = ""; if(node.kind === Kind.MAPPING && node.value === null){ - genVal = this.getParentVal(node); + nodeToSearch = this.getParentVal(node); }else{ - genVal = node.key.value; + nodeToSearch = node.key.value; } - if(genVal === ""){ - this.initData(Object.keys(this.kuberSchema)); - return this.search(node.key.value); + if(nodeToSearch === ""){ + return this.search(node.key.value, Object.keys(this.kuberSchema)); }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); + let nodeChildrenArray = this.kuberSchema[nodeToSearch].map(node => node.children); + let flattenNodeChildrenArray = nodeChildrenArray.reduce((cur, newVal) => cur.concat(newVal)); + let uniqueChildrenArray = flattenNodeChildrenArray.filter((value, index, self) => self.indexOf(value) === index); + if(nodeToSearch !== node.key.value){ + return this.search(node.key.value, uniqueChildrenArray); }else{ - return this.arrToCompletionList(results); + return this.arrToCompletionList(uniqueChildrenArray ); } - - } } + public generateScalarAutocompletion(nodeValue: String){ + let defaultScalarValues = this.kuberSchema[nodeValue.toString()].map(node => node.default); + let defaultScalarValuesUnique = defaultScalarValues.filter((value, index, self) => self.indexOf(value) === index && value !== undefined); + return this.arrToCompletionList(defaultScalarValuesUnique); + } + + private arrToCompletionList(arr){ + return arr.map(x => ({ + label: x.toString() + })); + } + private getParentVal(node: YAMLNode){ let parentNodeKey = node.parent; while(parentNodeKey != null && parentNodeKey.key === undefined){ @@ -88,15 +81,4 @@ export class AutoCompleter { return parentNodeKey.key.value; } - 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 this.arrToCompletionList(results); - } - - private arrToCompletionList(arr){ - return arr.map(x => ({ - label: x.toString() - })); - } - } \ No newline at end of file diff --git a/server/src/languageService/services/yamlCompletion.ts b/server/src/languageService/services/yamlCompletion.ts index 1a87d7e0..6ffe62a5 100644 --- a/server/src/languageService/services/yamlCompletion.ts +++ b/server/src/languageService/services/yamlCompletion.ts @@ -41,7 +41,7 @@ export class YamlCompletion { }else{ - return autoComplete.generateResults(node); + return autoComplete.generateRegularAutocompletion(node); }