Skip to content

Commit c4a1cd6

Browse files
amritamishra01datho7561
authored andcommitted
Fix: escape backslashes and dollar signs in code action snippets (#4314)
1 parent c893eb9 commit c4a1cd6

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/extension.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,11 @@ export async function activate(context: ExtensionContext): Promise<ExtensionAPI>
346346
for (const edit of docChange.edits) {
347347
if ("snippet" in edit) {
348348
documentUris.push(Uri.parse(docChange.textDocument.uri).toString());
349-
const snippet = new SnippetTextEdit(client.protocol2CodeConverter.asRange((edit as any).range), new SnippetString((edit as any).snippet.value));
349+
const snippetValue = (edit as any).snippet.value;
350+
const snippet = new SnippetTextEdit(
351+
client.protocol2CodeConverter.asRange((edit as any).range),
352+
new SnippetString(escapeSnippetLiterals(snippetValue))
353+
);
350354
if (semver.gte(version, '1.98.0')) {
351355
snippet["keepWhitespace"] = true;
352356
}
@@ -1267,3 +1271,9 @@ function registerRestartJavaLanguageServerCommand(context: ExtensionContext) {
12671271
}
12681272
}));
12691273
}
1274+
1275+
function escapeSnippetLiterals(value: string): string {
1276+
return value
1277+
.replace(/\\/g, '\\\\') // Escape backslashes
1278+
.replace(/\$(?!\{)/g, '\\$'); // Escape $ only if NOT followed by {
1279+
}

0 commit comments

Comments
 (0)