diff --git a/src/languageservice/jsonSchema.ts b/src/languageservice/jsonSchema.ts index 4385fb2e..e8b6f908 100644 --- a/src/languageservice/jsonSchema.ts +++ b/src/languageservice/jsonSchema.ts @@ -2,6 +2,9 @@ * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ + +import { CompletionItemKind } from 'vscode-json-languageservice'; + export type JSONSchemaRef = JSONSchema | boolean; export interface JSONSchema { @@ -65,6 +68,8 @@ export interface JSONSchema { label?: string; description?: string; markdownDescription?: string; + type?: string; + suggestionKind?: CompletionItemKind; // eslint-disable-next-line @typescript-eslint/no-explicit-any body?: any; bodyText?: string; diff --git a/src/languageservice/services/yamlCompletion.ts b/src/languageservice/services/yamlCompletion.ts index e54861e1..ff5b4ca4 100644 --- a/src/languageservice/services/yamlCompletion.ts +++ b/src/languageservice/services/yamlCompletion.ts @@ -524,7 +524,7 @@ export class YAMLCompletion extends JSONCompletion { let insertText: string; let filterText: string; if (isDefined(value)) { - const type = schema.type; + const type = s.type || schema.type; if (arrayDepth === 0 && type === 'array') { // We know that a - isn't present yet so we need to add one const fixedObj = {}; @@ -554,7 +554,7 @@ export class YAMLCompletion extends JSONCompletion { filterText = insertText.replace(/[\n]/g, ''); // remove new lines } collector.add({ - kind: this.getSuggestionKind(type), + kind: s.suggestionKind || this.getSuggestionKind(type), label, documentation: super.fromMarkup(s.markdownDescription) || s.description, insertText, diff --git a/test/defaultSnippets.test.ts b/test/defaultSnippets.test.ts index ea8345ce..a85051a4 100644 --- a/test/defaultSnippets.test.ts +++ b/test/defaultSnippets.test.ts @@ -96,6 +96,26 @@ describe('Default Snippet Tests', () => { }) .then(done, done); }); + it('Snippet in anyOf array schema should autocomplete correctly with "-" symbol', (done) => { + const content = 'anyOf_arrayObj:\n '; + const completion = parseSetup(content, content.length); + completion + .then(function (result) { + assert.equal(result.items.length, 1); + assert.equal(result.items[0].insertText, '- key: '); + }) + .then(done, done); + }); + it('Snippet custom suggestionKind', (done) => { + const content = 'anyOf_arrayObj:\n '; + const completion = parseSetup(content, content.length); + completion + .then(function (result) { + assert.strictEqual(result.items.length, 1); + assert.strictEqual(result.items[0].kind, 9); + }) + .then(done, done); + }); it('Snippet in object schema should autocomplete on next line ', (done) => { const content = 'object:\n '; @@ -190,7 +210,7 @@ describe('Default Snippet Tests', () => { const completion = parseSetup(content, 3); completion .then(function (result) { - assert.equal(result.items.length, 10); // This is just checking the total number of snippets in the defaultSnippets.json + assert.equal(result.items.length, 11); // This is just checking the total number of snippets in the defaultSnippets.json assert.equal(result.items[4].label, 'longSnippet'); // eslint-disable-next-line assert.equal( @@ -206,7 +226,7 @@ describe('Default Snippet Tests', () => { const completion = parseSetup(content, 11); completion .then(function (result) { - assert.equal(result.items.length, 10); // This is just checking the total number of snippets in the defaultSnippets.json + assert.equal(result.items.length, 11); // This is just checking the total number of snippets in the defaultSnippets.json assert.equal(result.items[5].label, 'arrayArraySnippet'); assert.equal( result.items[5].insertText, diff --git a/test/fixtures/defaultSnippets.json b/test/fixtures/defaultSnippets.json index 46b5b477..4eabfffa 100644 --- a/test/fixtures/defaultSnippets.json +++ b/test/fixtures/defaultSnippets.json @@ -143,6 +143,29 @@ } } ] + }, + "anyOf_arrayObj": { + "anyOf": [ + { + "type": "array", + "items": { + "type": "object" + } + }, + { + "type": "null" + } + ], + "defaultSnippets": [ + { + "label": "- (array item)", + "type": "array", + "body": { + "key": "" + }, + "suggestionKind": 9 + } + ] } } } diff --git a/test/schemaValidation.test.ts b/test/schemaValidation.test.ts index 33663a6f..a111107c 100644 --- a/test/schemaValidation.test.ts +++ b/test/schemaValidation.test.ts @@ -985,7 +985,7 @@ describe('Validation Tests', () => { languageService.configure(languageSettingsSetup.withKubernetes().languageSettings); yamlSettings.specificValidatorPaths = ['*.yml', '*.yaml']; const result = await parseSetup(content, 'file://~/Desktop/vscode-yaml/test.yml'); - expect(result[0]).deep.equal( + expect(result[2]).deep.equal( createDiagnosticWithData( ArrayTypeError, 4,