Skip to content

Commit

Permalink
Removed the constant coverting to CompletionList
Browse files Browse the repository at this point in the history
  • Loading branch information
JPinkney committed Jun 12, 2017
1 parent d0e10d2 commit 4a24571
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 24 deletions.
46 changes: 35 additions & 11 deletions server/src/languageService/services/autoCompleter.ts
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -10,45 +10,65 @@ 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<String>{
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<String>): void {
this.purge();
this.autoCompleter.initialize(data);
}

public purge(): void{
private purge(): void{
this.autoCompleter.words = 0;
this.autoCompleter.prefixes = 0;
this.autoCompleter.value = "";
this.autoCompleter.children = [];
}

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){
Expand All @@ -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()
}));
}
Expand Down
31 changes: 18 additions & 13 deletions server/src/languageService/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,27 @@ export class YamlCompletion {
let offset = document.offsetAt(position);
let node = findNode(<YAMLNode>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);

}

}
});
}

Expand Down
1 change: 1 addition & 0 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ connection.onCompletionResolve((item: CompletionItem): CompletionItem => {
return item;
});


let t: Thenable<string>;

/*
Expand Down

0 comments on commit 4a24571

Please sign in to comment.