提交 b7e6e6fa 编写于 作者: M Martin Aeschlimann

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

上级 0f6a10b4
......@@ -305,13 +305,22 @@ 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') {
} 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({
......@@ -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;
......
......@@ -104,7 +104,7 @@ export class ASTNode {
if ((<string[]>schema.type).indexOf(this.type) === -1) {
validationResult.warnings.push({
location: { start: this.start, end: this.end },
message: nls.localize('typeArrayMismatchWarning', 'Incorrect type. Expected one of {0}', schema.type.join())
message: nls.localize('typeArrayMismatchWarning', 'Incorrect type. Expected one of {0}', schema.type.join(', '))
});
}
}
......
......@@ -191,6 +191,34 @@ suite('JSON Completion', () => {
]).then(() => testDone(), (error) => testDone(error));
});
test('Complete value with schema: booleans, null', function(testDone) {
var schema: JsonSchema.IJSONSchema = {
type: 'object',
properties: {
'a': {
type: 'boolean'
},
'b': {
type: ['boolean', 'null']
},
}
};
Promise.all([
testSuggestionsFor('{ "a": /**/ }', '/**/', schema, result => {
assert.strictEqual(result.length, 2);
assertSuggestion(result, 'true');
assertSuggestion(result, 'false');
}),
testSuggestionsFor('{ "b": "/**/ }', '/**/', schema, result => {
assert.strictEqual(result.length, 3);
assertSuggestion(result, 'true');
assertSuggestion(result, 'false');
assertSuggestion(result, 'null');
})
]).then(() => testDone(), (error) => testDone(error));
});
test('Complete with nested schema', function(testDone) {
var content = '{/**/}';
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册