From c2dce7e1c8d027bc43c6ea921d970f4cc140001f Mon Sep 17 00:00:00 2001 From: Amrita kumari mishra Date: Mon, 30 Mar 2026 12:54:31 +0000 Subject: [PATCH] Fix: escape backslashes and dollar signs in code action snippets (#4314) --- src/extension.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/extension.ts b/src/extension.ts index 807fd31e9..b7c221562 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -346,7 +346,11 @@ export async function activate(context: ExtensionContext): Promise for (const edit of docChange.edits) { if ("snippet" in edit) { documentUris.push(Uri.parse(docChange.textDocument.uri).toString()); - const snippet = new SnippetTextEdit(client.protocol2CodeConverter.asRange((edit as any).range), new SnippetString((edit as any).snippet.value)); + const snippetValue = (edit as any).snippet.value; + const snippet = new SnippetTextEdit( + client.protocol2CodeConverter.asRange((edit as any).range), + new SnippetString(escapeSnippetLiterals(snippetValue)) + ); if (semver.gte(version, '1.98.0')) { snippet["keepWhitespace"] = true; } @@ -1267,3 +1271,9 @@ function registerRestartJavaLanguageServerCommand(context: ExtensionContext) { } })); } + +function escapeSnippetLiterals(value: string): string { + return value + .replace(/\\/g, '\\\\') // Escape backslashes + .replace(/\$(?!\{)/g, '\\$'); // Escape $ only if NOT followed by { +}