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

fix: suggest hyphen in array #901

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
9 changes: 2 additions & 7 deletions src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -910,13 +910,8 @@ export class YamlCompletion {
if (index < s.schema.items.length) {
this.addSchemaValueCompletions(s.schema.items[index], separatorAfter, collector, types, 'value');
}
} else if (
typeof s.schema.items === 'object' &&
(s.schema.items.type === 'object' || isAnyOfAllOfOneOfType(s.schema.items))
) {
this.addSchemaValueCompletions(s.schema.items, separatorAfter, collector, types, 'value', true);
} else {
this.addSchemaValueCompletions(s.schema.items, separatorAfter, collector, types, 'value');
this.addSchemaValueCompletions(s.schema.items, separatorAfter, collector, types, 'value', true);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unify the code for both scenarios. If a schema contains items it's an array. No need to check if type === 'object'

}
}
}
Expand Down Expand Up @@ -958,7 +953,7 @@ export class YamlCompletion {
);
collector.add({
kind: this.getSuggestionKind(schema.type),
label: '- (array item) ' + (schemaType || index),
label: '- (array item) ' + ((schemaType || index) ?? ''),
documentation: documentation,
insertText: insertText,
insertTextFormat: InsertTextFormat.Snippet,
Expand Down
36 changes: 35 additions & 1 deletion test/autoCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1488,6 +1488,39 @@ describe('Auto Completion Tests', () => {
.then(done, done);
});

it('Array of enum autocomplete on 2nd position without `-`', (done) => {
schemaProvider.addSchema(SCHEMA_ID, {
type: 'object',
properties: {
references: {
type: 'array',
items: {
enum: ['Test'],
},
},
},
});
const content = 'references:\n - Test\n |\n|';
const completion = parseSetup(content);
completion
.then(function (result) {
assert.deepEqual(
result.items.map((i) => ({ label: i.label, insertText: i.insertText })),
[
{
insertText: 'Test',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there should be '- Test' insert text, but it's another problem

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you file that as an issue?

Copy link
Contributor Author

@p-spacek p-spacek Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed it in this PR the end...

label: 'Test',
},
{
insertText: '- $1\n',
label: '- (array item) ',
},
]
);
})
.then(done, done);
});

it('Array of objects autocomplete with 4 space indentation check', async () => {
const languageSettingsSetup = new ServiceSetup().withCompletion().withIndentation(' ');
languageService.configure(languageSettingsSetup.languageSettings);
Expand Down Expand Up @@ -1926,7 +1959,8 @@ describe('Auto Completion Tests', () => {
assert.equal(result.items.length, 3, `Expecting 3 items in completion but found ${result.items.length}`);

const resultDoc2 = await parseSetup(content, content.length);
assert.equal(resultDoc2.items.length, 0, `Expecting no items in completion but found ${resultDoc2.items.length}`);
assert.equal(resultDoc2.items.length, 1, `Expecting 1 item in completion but found ${resultDoc2.items.length}`);
assert.equal(resultDoc2.items[0].label, '- (array item) ');
});

it('should handle absolute path', async () => {
Expand Down
13 changes: 10 additions & 3 deletions test/defaultSnippets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,16 @@ describe('Default Snippet Tests', () => {
const completion = parseSetup(content, content.length);
completion
.then(function (result) {
assert.equal(result.items.length, 1);
assert.equal(result.items[0].insertText, '- item1: $1\n item2: $2');
assert.equal(result.items[0].label, 'My array item');
assert.deepEqual(
result.items.map((i) => ({ insertText: i.insertText, label: i.label })),
[
{ insertText: '- item1: $1\n item2: $2', label: 'My array item' },
{
insertText: '- $1\n',
label: '- (array item) ',
},
]
);
})
.then(done, done);
});
Expand Down