Skip to content

Commit 0e3ce42

Browse files
committed
fix: default value with special chars with anyOf
PR: github.com/redhat-developer/pull/963
1 parent b4642d2 commit 0e3ce42

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

src/languageservice/services/yamlCompletion.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,7 +1292,7 @@ export class YamlCompletion {
12921292
case 'anyOf': {
12931293
let value = propertySchema.default || propertySchema.const;
12941294
if (value) {
1295-
if (type === 'string') {
1295+
if (type === 'string' || typeof value === 'string') {
12961296
value = convertToStringValue(value);
12971297
}
12981298
insertText += `${indent}${key}: \${${insertIndex++}:${value}}\n`;
@@ -1406,7 +1406,7 @@ export class YamlCompletion {
14061406
case 'string': {
14071407
let snippetValue = JSON.stringify(value);
14081408
snippetValue = snippetValue.substr(1, snippetValue.length - 2); // remove quotes
1409-
snippetValue = this.getInsertTextForPlainText(snippetValue); // escape \ and }
1409+
snippetValue = getInsertTextForPlainText(snippetValue); // escape \ and }
14101410
if (type === 'string') {
14111411
snippetValue = convertToStringValue(snippetValue);
14121412
}
@@ -1419,10 +1419,6 @@ export class YamlCompletion {
14191419
return this.getInsertTextForValue(value, separatorAfter, type);
14201420
}
14211421

1422-
private getInsertTextForPlainText(text: string): string {
1423-
return text.replace(/[\\$}]/g, '\\$&'); // escape $, \ and }
1424-
}
1425-
14261422
// eslint-disable-next-line @typescript-eslint/no-explicit-any
14271423
private getInsertTextForValue(value: any, separatorAfter: string, type: string | string[]): string {
14281424
if (value === null) {
@@ -1435,13 +1431,13 @@ export class YamlCompletion {
14351431
}
14361432
case 'number':
14371433
case 'boolean':
1438-
return this.getInsertTextForPlainText(value + separatorAfter);
1434+
return getInsertTextForPlainText(value + separatorAfter);
14391435
}
14401436
type = Array.isArray(type) ? type[0] : type;
14411437
if (type === 'string') {
14421438
value = convertToStringValue(value);
14431439
}
1444-
return this.getInsertTextForPlainText(value + separatorAfter);
1440+
return getInsertTextForPlainText(value + separatorAfter);
14451441
}
14461442

14471443
private getInsertTemplateForValue(
@@ -1466,14 +1462,14 @@ export class YamlCompletion {
14661462
if (typeof element === 'object') {
14671463
valueTemplate = `${this.getInsertTemplateForValue(element, indent + this.indentation, navOrder, separatorAfter)}`;
14681464
} else {
1469-
valueTemplate = ` \${${navOrder.index++}:${this.getInsertTextForPlainText(element + separatorAfter)}}\n`;
1465+
valueTemplate = ` \${${navOrder.index++}:${getInsertTextForPlainText(element + separatorAfter)}}\n`;
14701466
}
14711467
insertText += `${valueTemplate}`;
14721468
}
14731469
}
14741470
return insertText;
14751471
}
1476-
return this.getInsertTextForPlainText(value + separatorAfter);
1472+
return getInsertTextForPlainText(value + separatorAfter);
14771473
}
14781474

14791475
private addSchemaValueCompletions(
@@ -1860,6 +1856,13 @@ export class YamlCompletion {
18601856
}
18611857
}
18621858

1859+
/**
1860+
* escape $, \ and }
1861+
*/
1862+
function getInsertTextForPlainText(text: string): string {
1863+
return text.replace(/[\\$}]/g, '\\$&'); //
1864+
}
1865+
18631866
const isNumberExp = /^\d+$/;
18641867
function convertToStringValue(param: unknown): string {
18651868
let value: string;
@@ -1872,6 +1875,8 @@ function convertToStringValue(param: unknown): string {
18721875
return value;
18731876
}
18741877

1878+
value = getInsertTextForPlainText(value); // escape $, \ and }
1879+
18751880
if (value === 'true' || value === 'false' || value === 'null' || isNumberExp.test(value)) {
18761881
return `"${value}"`;
18771882
}

test/autoCompletion.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,34 @@ describe('Auto Completion Tests', () => {
11381138
);
11391139
});
11401140

1141+
it('Autocompletion should escape $ in defaultValue in anyOf', async () => {
1142+
schemaProvider.addSchema(SCHEMA_ID, {
1143+
type: 'object',
1144+
properties: {
1145+
car: {
1146+
type: 'object',
1147+
required: ['engine'],
1148+
properties: {
1149+
engine: {
1150+
anyOf: [
1151+
{
1152+
type: 'object',
1153+
},
1154+
{
1155+
type: 'string',
1156+
},
1157+
],
1158+
default: 'type$1234',
1159+
},
1160+
},
1161+
},
1162+
},
1163+
});
1164+
const content = '';
1165+
const completion = await parseSetup(content, 0);
1166+
expect(completion.items.map((i) => i.insertText)).to.deep.equal(['car:\n engine: ${1:type\\$1234}']);
1167+
});
1168+
11411169
it('Autocompletion should escape colon when indicating map', async () => {
11421170
schemaProvider.addSchema(SCHEMA_ID, {
11431171
type: 'object',

0 commit comments

Comments
 (0)