Skip to content

Commit

Permalink
fix: special chars in property
Browse files Browse the repository at this point in the history
  • Loading branch information
p-spacek committed Oct 24, 2024
1 parent 10fcfed commit 33ef09a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1669,7 +1669,10 @@ export class YamlCompletion {
* escape $, \ and }
*/
function getInsertTextForPlainText(text: string): string {
return text.replace(/[\\$}]/g, '\\$&'); //
return text.replace(/(\\?)([\\$}])/g, (match, escapeChar, specialChar) => {
// If it's already escaped (has a backslash before it), return it as is
return escapeChar ? match : `\\${specialChar}`;
});
}

const isNumberExp = /^\d+$/;
Expand Down
15 changes: 15 additions & 0 deletions test/autoCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,21 @@ describe('Auto Completion Tests', () => {
expect(completion.items.map((i) => i.insertText)).to.deep.equal(['car:\n engine: ${1:type\\$1234}']);
});

it('Autocompletion should escape $ in property', async () => {
schemaProvider.addSchema(SCHEMA_ID, {
type: 'object',
properties: {
$prop$1: {
type: 'string',
},
},
required: ['$prop$1'],
});
const content = '';
const completion = await parseSetup(content, 0);
expect(completion.items.map((i) => i.insertText)).includes('\\$prop\\$1: ');
});

it('Autocompletion should escape colon when indicating map', async () => {
schemaProvider.addSchema(SCHEMA_ID, {
type: 'object',
Expand Down
2 changes: 1 addition & 1 deletion test/schemaValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,7 @@ obj:
4,
18,
DiagnosticSeverity.Error,
'yaml-schema: Package',
'yaml-schema: Composer Package',
'https://raw.githubusercontent.com/composer/composer/master/res/composer-schema.json'
)
);
Expand Down

0 comments on commit 33ef09a

Please sign in to comment.