Skip to content

Commit

Permalink
Merge pull request redhat-developer#37 from JPinkney/issue-34
Browse files Browse the repository at this point in the history
Fixing issue 34
  • Loading branch information
JPinkney authored Jun 13, 2017
2 parents ccaeb0b + c231b2a commit eb810c4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 46 deletions.
74 changes: 45 additions & 29 deletions server/src/languageService/services/autoCompleter.ts
Original file line number Diff line number Diff line change
@@ -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<String>{
return this.autoCompleter.search(searchItem).map(x => x.value);
public search(searchItem: String, data: Array<String>): Array<String>{
let auto = new AutoComplete();
auto.initialize(data);
return auto.search(searchItem).map(searchResult => ({
label: searchResult.value.toString()
}));
}

public searchAll(): Array<String>{
return Object.keys(this.kuberSchema);
public searchAll() {
let allSchemaKeys = Object.keys(this.kuberSchema);
return this.arrToCompletionList(allSchemaKeys);
}

public initData(data:Array<String>): 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){
Expand All @@ -63,6 +81,4 @@ export class AutoCompleter {
return parentNodeKey.key.value;
}



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

}

}
});
}

}


}
Expand Down

0 comments on commit eb810c4

Please sign in to comment.