Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 86 additions & 60 deletions src/services/jsonCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -875,77 +875,103 @@ export class JSONCompletion {
let value;
let nValueProposals = 0;
if (propertySchema) {
if (Array.isArray(propertySchema.defaultSnippets)) {
if (propertySchema.defaultSnippets.length === 1) {
const body = propertySchema.defaultSnippets[0].body;
if (isDefined(body)) {
value = this.getInsertTextForSnippetValue(body, '');
}
}
nValueProposals += propertySchema.defaultSnippets.length;
const propertyValue = this.getInsertTextForPropertyValue(propertySchema);
value = propertyValue.value;
nValueProposals = propertyValue.nValueProposals;
if (!value && nValueProposals === 0) {
return propertyText;
}
if (propertySchema.enum) {
if (!value && propertySchema.enum.length === 1) {
value = this.getInsertTextForGuessedValue(propertySchema.enum[0], '');
}
if (!value || nValueProposals > 1) {
value = '$1';
}
return resultText + value + separatorAfter;
}

private getInsertTextForPropertyValue(propertySchema: JSONSchema): { value: string | undefined; nValueProposals: number } {
let value: string | undefined;
let nValueProposals = 0;

if (Array.isArray(propertySchema.defaultSnippets)) {
if (propertySchema.defaultSnippets.length === 1) {
const body = propertySchema.defaultSnippets[0].body;
if (isDefined(body)) {
value = this.getInsertTextForSnippetValue(body, '');
}
nValueProposals += propertySchema.enum.length;
}
if (isDefined(propertySchema.const)) {
if (!value) {
value = this.getInsertTextForGuessedValue(propertySchema.const, '');
}
nValueProposals++;
nValueProposals += propertySchema.defaultSnippets.length;
}
if (propertySchema.enum) {
if (!value && propertySchema.enum.length === 1) {
value = this.getInsertTextForGuessedValue(propertySchema.enum[0], '');
}
if (isDefined(propertySchema.default)) {
if (!value) {
value = this.getInsertTextForGuessedValue(propertySchema.default, '');
}
nValueProposals++;
nValueProposals += propertySchema.enum.length;
}
if (isDefined(propertySchema.const)) {
if (!value) {
value = this.getInsertTextForGuessedValue(propertySchema.const, '');
}
if (Array.isArray(propertySchema.examples) && propertySchema.examples.length) {
if (!value) {
value = this.getInsertTextForGuessedValue(propertySchema.examples[0], '');
}
nValueProposals += propertySchema.examples.length;
nValueProposals++;
}
if (isDefined(propertySchema.default)) {
if (!value) {
value = this.getInsertTextForGuessedValue(propertySchema.default, '');
}
if (nValueProposals === 0) {
let type = Array.isArray(propertySchema.type) ? propertySchema.type[0] : propertySchema.type;
if (!type) {
if (propertySchema.properties) {
type = 'object';
} else if (propertySchema.items) {
type = 'array';
nValueProposals++;
}
if (Array.isArray(propertySchema.examples) && propertySchema.examples.length) {
if (!value) {
value = this.getInsertTextForGuessedValue(propertySchema.examples[0], '');
}
nValueProposals += propertySchema.examples.length;
}
if (nValueProposals === 0) {
value = this.getInsertTextForSchemaType(propertySchema);
}
if (!value) {
const combinedSchemas = [propertySchema.allOf, propertySchema.anyOf, propertySchema.oneOf];
for (const schemaRefs of combinedSchemas) {
if (Array.isArray(schemaRefs)) {
for (const schemaRef of schemaRefs) {
if (typeof schemaRef === 'object') {
const propertyValue = this.getInsertTextForPropertyValue(schemaRef);
if (!value && propertyValue.value) {
value = propertyValue.value;
}
nValueProposals += propertyValue.nValueProposals;
}
}
}
switch (type) {
case 'boolean':
value = '$1';
break;
case 'string':
value = '"$1"';
break;
case 'object':
value = '{$1}';
break;
case 'array':
value = '[$1]';
break;
case 'number':
case 'integer':
value = '${1:0}';
break;
case 'null':
value = '${1:null}';
break;
default:
return propertyText;
}
}
}
if (!value || nValueProposals > 1) {
value = '$1';
return { value, nValueProposals };
}

private getInsertTextForSchemaType(propertySchema: JSONSchema): string | undefined {
let type = Array.isArray(propertySchema.type) ? propertySchema.type[0] : propertySchema.type;
if (!type) {
if (propertySchema.properties) {
type = 'object';
} else if (propertySchema.items) {
type = 'array';
}
}
return resultText + value + separatorAfter;
switch (type) {
case 'boolean':
return '$1';
case 'string':
return '"$1"';
case 'object':
return '{$1}';
case 'array':
return '[$1]';
case 'number':
case 'integer':
return '${1:0}';
case 'null':
return '${1:null}';
}
return undefined;
}

private getCurrentWord(document: TextDocument, offset: number) {
Expand Down
39 changes: 39 additions & 0 deletions src/test/completion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,45 @@ suite('JSON Completion', () => {
});
});

test('Complete property whose value schema uses oneOf', async function () {

const schema: JSONSchema = {
type: 'object',
properties: {
'enum': {
enum: ['a', 'b']
},
'object': {
type: 'object',
properties: {
'q': {
enum: ['c', 'd']
}
}
},
'oneOf': {
oneOf: [
{ enum: ['a', 'b'] },
{
type: 'object',
properties: {
'q': {
enum: ['c', 'd']
}
}
}
]
}
}
};
await testCompletionsFor('{ "on|" }', schema, {
count: 3,
items: [
{ label: 'oneOf', resultText: '{ "oneOf": $1 }' }
]
});
});

test('Complete with required anyOf', async function () {

const schema: JSONSchema = {
Expand Down