Skip to content

Commit

Permalink
Fix empty completion list on empty file (#372)
Browse files Browse the repository at this point in the history
* #349 fix empty completion list on empty file

Signed-off-by: Yevhen Vydolob <[email protected]>

* Fix completion in the middle of file

Signed-off-by: Yevhen Vydolob <[email protected]>
  • Loading branch information
evidolob authored Dec 14, 2020
1 parent 85b0ac7 commit 3571b76
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 75 deletions.
9 changes: 9 additions & 0 deletions src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,15 @@ export class YAMLCompletion extends JSONCompletion {

const textLine = document.getText().substring(start, end);

// Check if document contains only white spaces and line delimiters
if (document.getText().trim().length === 0) {
return {
// add empty object to be compatible with JSON
newText: `{${document.getText()}}\n`,
newPosition: textDocumentPosition,
};
}

// Check if the string we are looking at is a node
if (textLine.indexOf(':') === -1) {
// We need to add the ":" to load the nodes
Expand Down
189 changes: 114 additions & 75 deletions test/autoCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1452,98 +1452,137 @@ suite('Auto Completion Tests', () => {
expect(completion.items[0].label).eq('fooBar');
expect(completion.items[0].insertText).eq('fooBar:\n name: $1\n aaa:\n - $2');
});
});

it('should complete string which contains number in default value', async () => {
languageService.addSchema(SCHEMA_ID, {
type: 'object',
properties: {
env: {
type: 'integer',
default: '1',
},
enum: {
type: 'string',
default: '1',
it('should complete string which contains number in default value', async () => {
languageService.addSchema(SCHEMA_ID, {
type: 'object',
properties: {
env: {
type: 'integer',
default: '1',
},
enum: {
type: 'string',
default: '1',
},
},
},
});

const content = 'enum';
const completion = await parseSetup(content, 3);
});

const enumItem = completion.items.find((i) => i.label === 'enum');
expect(enumItem).to.not.undefined;
expect(enumItem.textEdit.newText).equal('enum: ${1:"1"}');
const content = 'enum';
const completion = await parseSetup(content, 3);

const envItem = completion.items.find((i) => i.label === 'env');
expect(envItem).to.not.undefined;
expect(envItem.textEdit.newText).equal('env: ${1:1}');
});
const enumItem = completion.items.find((i) => i.label === 'enum');
expect(enumItem).to.not.undefined;
expect(enumItem.textEdit.newText).equal('enum: ${1:"1"}');

it('should complete string which contains number in examples values', async () => {
languageService.addSchema(SCHEMA_ID, {
type: 'object',
properties: {
fooBar: {
type: 'string',
examples: ['test', '1', 'true'],
},
},
const envItem = completion.items.find((i) => i.label === 'env');
expect(envItem).to.not.undefined;
expect(envItem.textEdit.newText).equal('env: ${1:1}');
});

const content = 'fooBar: \n';
const completion = await parseSetup(content, 8);
it('should complete string which contains number in examples values', async () => {
languageService.addSchema(SCHEMA_ID, {
type: 'object',
properties: {
fooBar: {
type: 'string',
examples: ['test', '1', 'true'],
},
},
});

const testItem = completion.items.find((i) => i.label === 'test');
expect(testItem).to.not.undefined;
expect(testItem.textEdit.newText).equal('test');
const content = 'fooBar: \n';
const completion = await parseSetup(content, 8);

const oneItem = completion.items.find((i) => i.label === '1');
expect(oneItem).to.not.undefined;
expect(oneItem.textEdit.newText).equal('"1"');
const testItem = completion.items.find((i) => i.label === 'test');
expect(testItem).to.not.undefined;
expect(testItem.textEdit.newText).equal('test');

const trueItem = completion.items.find((i) => i.label === 'true');
expect(trueItem).to.not.undefined;
expect(trueItem.textEdit.newText).equal('"true"');
});
const oneItem = completion.items.find((i) => i.label === '1');
expect(oneItem).to.not.undefined;
expect(oneItem.textEdit.newText).equal('"1"');

it('should provide completion for flow map', async () => {
languageService.addSchema(SCHEMA_ID, {
type: 'object',
properties: { A: { type: 'string', enum: ['a1', 'a2'] }, B: { type: 'string', enum: ['b1', 'b2'] } },
const trueItem = completion.items.find((i) => i.label === 'true');
expect(trueItem).to.not.undefined;
expect(trueItem.textEdit.newText).equal('"true"');
});

const content = '{A: , B: b1}';
const completion = await parseSetup(content, 4);
expect(completion.items).lengthOf(2);
expect(completion.items[0]).eql(
createExpectedCompletion('a1', 'a1', 0, 4, 0, 4, 12, InsertTextFormat.Snippet, { documentation: undefined })
);
expect(completion.items[1]).eql(
createExpectedCompletion('a2', 'a2', 0, 4, 0, 4, 12, InsertTextFormat.Snippet, { documentation: undefined })
);
});
it('should provide completion for flow map', async () => {
languageService.addSchema(SCHEMA_ID, {
type: 'object',
properties: { A: { type: 'string', enum: ['a1', 'a2'] }, B: { type: 'string', enum: ['b1', 'b2'] } },
});

it('should provide completion for "null" enum value', async () => {
languageService.addSchema(SCHEMA_ID, {
type: 'object',
properties: {
kind: {
enum: ['project', null],
const content = '{A: , B: b1}';
const completion = await parseSetup(content, 4);
expect(completion.items).lengthOf(2);
expect(completion.items[0]).eql(
createExpectedCompletion('a1', 'a1', 0, 4, 0, 4, 12, InsertTextFormat.Snippet, { documentation: undefined })
);
expect(completion.items[1]).eql(
createExpectedCompletion('a2', 'a2', 0, 4, 0, 4, 12, InsertTextFormat.Snippet, { documentation: undefined })
);
});

it('should provide completion for "null" enum value', async () => {
languageService.addSchema(SCHEMA_ID, {
type: 'object',
properties: {
kind: {
enum: ['project', null],
},
},
},
});

const content = 'kind: \n';
const completion = await parseSetup(content, 6);
expect(completion.items).lengthOf(2);
expect(completion.items[0]).eql(
createExpectedCompletion('project', 'project', 0, 6, 0, 6, 12, InsertTextFormat.Snippet, { documentation: undefined })
);
expect(completion.items[1]).eql(
createExpectedCompletion('null', 'null', 0, 6, 0, 6, 12, InsertTextFormat.Snippet, { documentation: undefined })
);
});

const content = 'kind: \n';
const completion = await parseSetup(content, 6);
expect(completion.items).lengthOf(2);
expect(completion.items[0]).eql(
createExpectedCompletion('project', 'project', 0, 6, 0, 6, 12, InsertTextFormat.Snippet, { documentation: undefined })
);
expect(completion.items[1]).eql(
createExpectedCompletion('null', 'null', 0, 6, 0, 6, 12, InsertTextFormat.Snippet, { documentation: undefined })
);
it('should provide completion for empty file', async () => {
languageService.addSchema(SCHEMA_ID, {
oneOf: [
{
type: 'object',
description: 'dummy schema',
},
{
properties: {
kind: {
type: 'string',
},
},
type: 'object',
additionalProperties: false,
},
{
properties: {
name: {
type: 'string',
},
},
type: 'object',
additionalProperties: false,
},
],
});

const content = ' \n\n\n';
const completion = await parseSetup(content, 3);
expect(completion.items).lengthOf(2);
expect(completion.items[0]).eql(
createExpectedCompletion('kind', 'kind: $1', 2, 0, 2, 0, 10, InsertTextFormat.Snippet, { documentation: '' })
);
expect(completion.items[1]).eql(
createExpectedCompletion('name', 'name: $1', 2, 0, 2, 0, 10, InsertTextFormat.Snippet, { documentation: '' })
);
});
});
});
});

0 comments on commit 3571b76

Please sign in to comment.