[json] better support for null, and array of types

This commit is contained in:
Martin Aeschlimann
2016-02-10 12:09:34 +01:00
parent 0f6a10b4b0
commit b7e6e6fae7
3 changed files with 55 additions and 10 deletions

View File

@@ -73,7 +73,7 @@ export class JSONCompletion {
let addValue = true;
let currentKey = '';
let currentProperty: Parser.PropertyASTNode = null;
if (node) {
@@ -134,8 +134,8 @@ export class JSONCompletion {
} else {
// value proposals without schema
this.getSchemaLessValueSuggestions(doc, node, offset, document, collector);
}
}
if (!node) {
this.contributions.forEach((contribution) => {
let collectPromise = contribution.collectDefaultSuggestions(textDocumentPosition.uri, collector);
@@ -305,12 +305,21 @@ export class JSONCompletion {
collector.add({ kind: this.getSuggestionKind('boolean'), label: value ? 'true' : 'false', insertText: this.getTextForValue(value), documentation: '' });
}
private addNullSuggestion(collector: ISuggestionsCollector): void {
collector.add({ kind: this.getSuggestionKind('null'), label: 'null', insertText: 'null', documentation: '' });
}
private addEnumSuggestion(schema: JsonSchema.IJSONSchema, collector: ISuggestionsCollector): void {
if (Array.isArray(schema.enum)) {
schema.enum.forEach((enm) => collector.add({ kind: this.getSuggestionKind(schema.type), label: this.getLabelForValue(enm), insertText: this.getTextForValue(enm), documentation: '' }));
} else if (schema.type === 'boolean') {
this.addBooleanSuggestion(true, collector);
this.addBooleanSuggestion(false, collector);
} else {
if (this.isType(schema, 'boolean')) {
this.addBooleanSuggestion(true, collector);
this.addBooleanSuggestion(false, collector);
}
if (this.isType(schema, 'null')) {
this.addNullSuggestion(collector);
}
}
if (Array.isArray(schema.allOf)) {
schema.allOf.forEach((s) => this.addEnumSuggestion(s, collector));
@@ -323,6 +332,13 @@ export class JSONCompletion {
}
}
private isType(schema: JsonSchema.IJSONSchema, type: string) {
if (Array.isArray(schema.type)) {
return schema.type.indexOf(type) !== -1;
}
return schema.type === type;
}
private addDefaultSuggestion(schema: JsonSchema.IJSONSchema, collector: ISuggestionsCollector): void {
if (schema.default) {
collector.add({
@@ -409,7 +425,7 @@ export class JSONCompletion {
return result;
}
result += ': ';
if (propertySchema) {
let defaultVal = propertySchema.default;
if (typeof defaultVal !== 'undefined') {
@@ -417,7 +433,8 @@ export class JSONCompletion {
} else if (propertySchema.enum && propertySchema.enum.length > 0) {
result = result + this.getSnippetForValue(propertySchema.enum[0]);
} else {
switch (propertySchema.type) {
var type = Array.isArray(propertySchema.type) ? propertySchema.type[0] : propertySchema.type;
switch (type) {
case 'boolean':
result += '{{false}}';
break;