diff --git a/server/src/languageService/services/autoCompleter.ts b/server/src/languageService/services/autoCompleter.ts index 031320ad..b68beda8 100644 --- a/server/src/languageService/services/autoCompleter.ts +++ b/server/src/languageService/services/autoCompleter.ts @@ -1,49 +1,67 @@ import {SchemaToMappingTransformer} from "../schemaToMappingTransformer" -import {TextDocument} from 'vscode-languageserver-types'; +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'); export class AutoCompleter { - private autoCompleter; - private schema: JSONSchema; private kuberSchema; constructor(schema:JSONSchema){ - this.schema = schema; - this.autoCompleter = new AutoComplete(); - this.kuberSchema = new SchemaToMappingTransformer(this.schema).getSchema(); + this.kuberSchema = new SchemaToMappingTransformer(schema).getSchema(); } - public search(searchItem: String): Array{ - return this.autoCompleter.search(searchItem).map(x => x.value); + public search(searchItem: String, data: Array): Array{ + let auto = new AutoComplete(); + auto.initialize(data); + return auto.search(searchItem).map(searchResult => ({ + label: searchResult.value.toString() + })); } - public searchAll(): Array{ - return Object.keys(this.kuberSchema); + public searchAll() { + let allSchemaKeys = Object.keys(this.kuberSchema); + return this.arrToCompletionList(allSchemaKeys); } - public initData(data:Array): void { - this.purge(); - this.autoCompleter.initialize(data); + public generateRegularAutocompletion(node){ + let nodeToSearch = ""; + + if(node.kind === Kind.MAPPING && node.value === null){ + nodeToSearch = this.getParentVal(node); + }else{ + nodeToSearch = node.key.value; + } + + if(nodeToSearch === ""){ + return this.search(node.key.value, Object.keys(this.kuberSchema)); + }else{ + + 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(uniqueChildrenArray ); + } + + } + } - public purge(): void{ - this.autoCompleter.words = 0; - this.autoCompleter.prefixes = 0; - this.autoCompleter.value = ""; - this.autoCompleter.children = []; + 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); } - 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); - }else{ - this.initData(this.searchAll()); - } + private arrToCompletionList(arr){ + return arr.map(x => ({ + label: x.toString() + })); } private getParentVal(node: YAMLNode){ @@ -63,6 +81,4 @@ export class AutoCompleter { return parentNodeKey.key.value; } - - } \ No newline at end of file diff --git a/server/src/languageService/services/yamlCompletion.ts b/server/src/languageService/services/yamlCompletion.ts index fb79f929..6ffe62a5 100644 --- a/server/src/languageService/services/yamlCompletion.ts +++ b/server/src/languageService/services/yamlCompletion.ts @@ -25,27 +25,29 @@ export class YamlCompletion { let offset = document.offsetAt(position); let node = findNode(doc, offset); - if(node !== undefined && node.kind === Kind.SCALAR){ - return []; - } - - if(node === undefined || node.parent === null){ - //Its a root node - autoComplete.searchAll().map(x => result.items.push({ - label: x.toString() - })); + if(node === undefined || node.kind === Kind.MAP){ + + return autoComplete.searchAll(); + }else{ - autoComplete.generateResults(node); - autoComplete.search(node.key.value).map(x => result.items.push({ - label: x.toString() - })); - } + 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 result; + return autoComplete.generateRegularAutocompletion(node); + + } + + } }); - } - + } }