Skip to content

Commit

Permalink
#408 support $ref in additionalItems
Browse files Browse the repository at this point in the history
  • Loading branch information
evidolob committed Mar 31, 2021
1 parent c0bee52 commit 6ab70bd
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1016,14 +1016,18 @@ export class YAMLCompletion extends JSONCompletion {
document.getText().substr(lineOffset[linePos + 1] || document.getText().length);

// For when missing semi colon case
} else {
} else if (trimmedText.indexOf('[') === -1) {
// Add a semicolon to the end of the current line so we can validate the node
newText =
document.getText().substring(0, start + textLine.length) +
':\r\n' +
document.getText().substr(lineOffset[linePos + 1] || document.getText().length);
}

if (newText.length === 0) {
newText = document.getText();
}

return {
newText: newText,
newPosition: textDocumentPosition,
Expand Down
1 change: 1 addition & 0 deletions src/languageservice/services/yamlSchemaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ export class YAMLSchemaService extends JSONSchemaService {

collectEntries(
<JSONSchema>next.items,
next.additionalItems,
<JSONSchema>next.additionalProperties,
next.not,
next.contains,
Expand Down
64 changes: 64 additions & 0 deletions test/autoCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1840,6 +1840,70 @@ describe('Auto Completion Tests', () => {
createExpectedCompletion('kind', 'kind', 0, 0, 0, 2, 10, InsertTextFormat.Snippet, { documentation: '' })
);
});

it('should follow $ref in additionalItems', async () => {
languageService.addSchema(SCHEMA_ID, {
type: 'object',
properties: {
test: {
$ref: '#/definitions/Recur',
},
},
definitions: {
Recur: {
type: 'array',
items: [
{
type: 'string',
enum: ['and'],
},
],
additionalItems: {
$ref: '#/definitions/Recur',
},
},
},
});

const content = 'test:\n - and\n - - ';
const completion = await parseSetup(content, 19);
expect(completion.items).lengthOf(1);
expect(completion.items[0]).eql(
createExpectedCompletion('and', 'and', 2, 4, 2, 5, 12, InsertTextFormat.Snippet, { documentation: undefined })
);
});

it('should follow $ref in additionalItems for flow style array', async () => {
languageService.addSchema(SCHEMA_ID, {
type: 'object',
properties: {
test: {
$ref: '#/definitions/Recur',
},
},
definitions: {
Recur: {
type: 'array',
items: [
{
type: 'string',
enum: ['and'],
},
],
additionalItems: {
$ref: '#/definitions/Recur',
},
},
},
});

const content = 'test:\n - and\n - []';
const completion = await parseSetup(content, 18);
expect(completion.items).lengthOf(1);
expect(completion.items[0]).eql(
createExpectedCompletion('and', 'and', 2, 4, 2, 4, 12, InsertTextFormat.Snippet, { documentation: undefined })
);
});
});
describe('Array completion', () => {
it('Simple array object completion with "-" without any item', async () => {
Expand Down

0 comments on commit 6ab70bd

Please sign in to comment.