Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add new params to completion snippet #388

Merged
Merged
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
5 changes: 5 additions & 0 deletions src/languageservice/jsonSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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),
JPinkney marked this conversation as resolved.
Show resolved Hide resolved
label,
documentation: super.fromMarkup(s.markdownDescription) || s.description,
insertText,
Expand Down
24 changes: 22 additions & 2 deletions test/defaultSnippets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ';
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand Down
23 changes: 23 additions & 0 deletions test/fixtures/defaultSnippets.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,29 @@
}
}
]
},
"anyOf_arrayObj": {
"anyOf": [
{
"type": "array",
"items": {
"type": "object"
}
},
{
"type": "null"
}
],
"defaultSnippets": [
{
"label": "- (array item)",
"type": "array",
"body": {
"key": ""
},
"suggestionKind": 9
}
]
}
}
}
2 changes: 1 addition & 1 deletion test/schemaValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down