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: merge simple property completion #685

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
94 changes: 73 additions & 21 deletions src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,35 +195,48 @@ export class YamlCompletion {
if (!isString(label)) {
label = String(label);
}
const existing = proposed[label];
if (!existing || isForParentCompletion) {
label = label.replace(/[\n]/g, '↵');
if (label.length > 60) {
const shortendedLabel = label.substr(0, 57).trim() + '...';
if (!proposed[shortendedLabel]) {
label = shortendedLabel;
}
}

// trim $1 from end of completion
if (completionItem.insertText.endsWith('$1') && !isForParentCompletion) {
completionItem.insertText = completionItem.insertText.substr(0, completionItem.insertText.length - 2);
}
if (overwriteRange && overwriteRange.start.line === overwriteRange.end.line) {
completionItem.textEdit = TextEdit.replace(overwriteRange, completionItem.insertText);
label = label.replace(/[\n]/g, '↵');
if (label.length > 60) {
const shortendedLabel = label.substr(0, 57).trim() + '...';
if (!proposed[shortendedLabel]) {
label = shortendedLabel;
}
}

// trim $1 from end of completion
if (completionItem.insertText.endsWith('$1') && !isForParentCompletion) {
completionItem.insertText = completionItem.insertText.substr(0, completionItem.insertText.length - 2);
}
if (overwriteRange && overwriteRange.start.line === overwriteRange.end.line) {
completionItem.textEdit = TextEdit.replace(overwriteRange, completionItem.insertText);
}

completionItem.label = label;
completionItem.label = label;

if (isForParentCompletion) {
addSuggestionForParent(completionItem);
}
if (isForParentCompletion) {
addSuggestionForParent(completionItem);
return;
}

if (!existing) {
const existing = proposed[label];
const isInsertTextDifferent =
existing?.label !== existingProposeItem && existing?.insertText !== completionItem.insertText;
if (!existing) {
proposed[label] = completionItem;
result.items.push(completionItem);
} else if (isInsertTextDifferent) {
// try to merge simple insert values
const mergedText = this.mergeSimpleInsertTexts(label, existing.insertText, completionItem.insertText);
if (mergedText) {
this.updateCompletionText(existing, mergedText);
} else {
// add to result when it wasn't able to merge (even if the item is already there but with a different value)
proposed[label] = completionItem;
result.items.push(completionItem);
}
} else if (!existing.documentation && completionItem.documentation) {
}
if (existing && !existing.documentation && completionItem.documentation) {
existing.documentation = completionItem.documentation;
}
},
Expand Down Expand Up @@ -461,6 +474,45 @@ export class YamlCompletion {
return result;
}

updateCompletionText(completionItem: CompletionItem, text: string): void {
completionItem.insertText = text;
if (completionItem.textEdit) {
completionItem.textEdit.newText = text;
}
}

mergeSimpleInsertTexts(label: string, existingText: string, addingText: string): string | undefined {
const containsNewLineAfterColon = (value: string): boolean => {
return value.includes('\n');
};
if (containsNewLineAfterColon(existingText) || containsNewLineAfterColon(addingText)) {
return undefined;
}
const existingValues = this.getValuesFromInsertText(existingText);
const addingValues = this.getValuesFromInsertText(addingText);

const newValues = Array.prototype.concat(existingValues, addingValues);
if (!newValues.length) {
return undefined;
} else if (newValues.length === 1) {
return `${label}: \${1:${newValues[0]}}`;
} else {
return `${label}: \${1|${newValues.join(',')}|}`;
}
}

getValuesFromInsertText(insertText: string): string[] {
const value = insertText.substring(insertText.indexOf(':') + 1).trim();
if (!value) {
return [];
}
const valueMath = value.match(/^\${1[|:]([^|]*)+\|?}$/); // ${1|one,two,three|} or ${1:one}
if (valueMath) {
return valueMath[1].split(',');
}
return [value];
}

private finalizeParentCompletion(result: CompletionList): void {
const reindexText = (insertTexts: string[]): string[] => {
//modify added props to have unique $x
Expand Down
14 changes: 10 additions & 4 deletions test/autoCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2635,9 +2635,9 @@ describe('Auto Completion Tests', () => {
const content = '';
const result = await parseSetup(content, content.length);

expect(result.items.length).equal(4);
expect(result.items.length).equal(5);
expect(result.items[0]).to.deep.equal(
createExpectedCompletion('type', 'type: typeObj1', 0, 0, 0, 0, 10, 2, { documentation: '' })
createExpectedCompletion('type', 'type: ${1|typeObj1,typeObj2|}', 0, 0, 0, 0, 10, 2, { documentation: '' })
);
expect(result.items[1]).to.deep.equal(
createExpectedCompletion('Object1', 'type: typeObj1\noptions:\n label: ', 0, 0, 0, 0, 7, 2, {
Expand All @@ -2660,6 +2660,9 @@ describe('Auto Completion Tests', () => {
sortText: '_obj2',
})
);
expect(result.items[4]).to.deep.equal(
createExpectedCompletion('options', 'options:\n description: ', 0, 0, 0, 0, 10, 2, { documentation: '' })
);
});

it('Should suggest complete object skeleton - array', async () => {
Expand All @@ -2681,9 +2684,9 @@ describe('Auto Completion Tests', () => {
const content = '- ';
const result = await parseSetup(content, content.length);

expect(result.items.length).equal(4);
expect(result.items.length).equal(5);
expect(result.items[0]).to.deep.equal(
createExpectedCompletion('type', 'type: typeObj1', 0, 2, 0, 2, 10, 2, { documentation: '' })
createExpectedCompletion('type', 'type: ${1|typeObj1,typeObj2|}', 0, 2, 0, 2, 10, 2, { documentation: '' })
);
expect(result.items[1]).to.deep.equal(
createExpectedCompletion('Object1', 'type: typeObj1\n options:\n label: ', 0, 2, 0, 2, 7, 2, {
Expand All @@ -2706,6 +2709,9 @@ describe('Auto Completion Tests', () => {
sortText: '_obj2',
})
);
expect(result.items[4]).to.deep.equal(
createExpectedCompletion('options', 'options:\n description: ', 0, 2, 0, 2, 10, 2, { documentation: '' })
);
});
it('Should not agregate suggested text from different schemas', async () => {
const schema = {
Expand Down
88 changes: 88 additions & 0 deletions test/autoCompletionFix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { SCHEMA_ID, setupLanguageService, setupSchemaIDTextDocument } from './ut
import { expect } from 'chai';
import { createExpectedCompletion } from './utils/verifyError';
import * as path from 'path';
import { JSONSchema } from './../src/languageservice/jsonSchema';

describe('Auto Completion Fix Tests', () => {
let languageSettingsSetup: ServiceSetup;
Expand Down Expand Up @@ -383,4 +384,91 @@ objB:
);
});
});

describe('merge properties from anyOf objects', () => {
it('should merge different simple values', async () => {
const schema: JSONSchema = {
anyOf: [
{
properties: {
simplePropWithSimpleValue: { type: 'string', const: 'const value' },
},
},
{
properties: {
simplePropWithSimpleValue: { type: 'boolean', default: false },
},
},
{
properties: {
simplePropWithSimpleValue: { type: 'null', default: null },
},
},
{
properties: {
simplePropWithSimpleValue: { type: 'string' },
},
},
],
};
languageService.addSchema(SCHEMA_ID, schema);
const content = '';
const completion = await parseSetup(content, 0, 1);

expect(completion.items.length).equal(1);
expect(completion.items[0].insertText).to.be.equal('simplePropWithSimpleValue: ${1|const value,false,null|}');
});

it('should autocomplete as single item with same value', async () => {
const schema: JSONSchema = {
anyOf: [
{
properties: {
simplePropWithSameValue: { type: 'string', const: 'const value 1' },
obj1: { properties: { prop1: { type: 'string' } } },
},
},
{
properties: {
simplePropWithSameValue: { type: 'string', const: 'const value 1' },
obj1: { properties: { prop1: { type: 'string' } } },
},
},
],
};
languageService.addSchema(SCHEMA_ID, schema);
const content = '';
const completion = await parseSetup(content, 0, 1);

expect(completion.items.length).equal(2);
expect(completion.items[0].insertText).to.be.equal('simplePropWithSameValue: const value 1');
expect(completion.items[1].insertText).to.be.equal('obj1:\n ');
});

it('should not merge objects', async () => {
const schema: JSONSchema = {
anyOf: [
{
properties: {
obj1: { properties: { prop1: { type: 'string' } }, required: ['prop1'] },
},
},
{
properties: {
obj1: { properties: { prop2: { type: 'string', const: 'value' } }, required: ['prop2'] },
},
},
],
};
languageService.addSchema(SCHEMA_ID, schema);
const content = '';
const completion = await parseSetup(content, 0, 1);

expect(completion.items.length).equal(2);
expect(completion.items[0].label).to.be.equal('obj1');
expect(completion.items[0].insertText).to.be.equal('obj1:\n prop1: ');
expect(completion.items[1].label).to.be.equal('obj1');
expect(completion.items[1].insertText).to.be.equal('obj1:\n prop2: ${1:value}');
});
});
});