From 4ebdba8386aa0314535082ffe580e69cb3a2049b Mon Sep 17 00:00:00 2001 From: Misode Date: Fri, 27 Dec 2024 15:39:24 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20double-escaping=20completi?= =?UTF-8?q?on=20items=20in=20strings=20(#1686)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/processor/completer/Completer.ts | 7 +++++++ packages/core/src/processor/completer/builtin.ts | 5 ++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/core/src/processor/completer/Completer.ts b/packages/core/src/processor/completer/Completer.ts index a07028697..d156bb9e7 100644 --- a/packages/core/src/processor/completer/Completer.ts +++ b/packages/core/src/processor/completer/Completer.ts @@ -92,6 +92,13 @@ export namespace CompletionItem { export function escape(textToInsert: string): string { return textToInsert.replace(/([\\$}])/g, '\\$1') } + + /** + * Un-escape `$`, `\`, and `}` in `textToInsert` + */ + export function unescape(textToInsert: string): string { + return textToInsert.replace(/\\([\\$}])/g, '$1') + } } export class InsertTextBuilder { diff --git a/packages/core/src/processor/completer/builtin.ts b/packages/core/src/processor/completer/builtin.ts index bed9fa9de..cee08a53a 100644 --- a/packages/core/src/processor/completer/builtin.ts +++ b/packages/core/src/processor/completer/builtin.ts @@ -283,7 +283,10 @@ export function escapeString(value: string, quote?: Quote) { if (!quote) { return value } - return value.replaceAll('\\', '\\\\').replaceAll(quote, '\\"') + // Un-escape and then re-escape completion + value = CompletionItem.unescape(value) + value = value.replaceAll('\\', '\\\\').replaceAll(quote, '\\"') + return CompletionItem.escape(value) } export const symbol: Completer = (node, ctx) => {