diff --git a/src/extension.ts b/src/extension.ts index 7cbed65d6..12a95a352 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1303,8 +1303,8 @@ function registerRestartJavaLanguageServerCommand(context: ExtensionContext) { })); } -function escapeSnippetLiterals(value: string): string { +export function escapeSnippetLiterals(value: string): string { return value - .replace(/\\/g, '\\\\') // Escape backslashes + .replace(/\\(?!,)/g, '\\\\') // Escape backslashes, but preserve escaped commas .replace(/\$(?!\{)/g, '\\$'); // Escape $ only if NOT followed by { } diff --git a/test/standard-mode-suite/extension.test.ts b/test/standard-mode-suite/extension.test.ts index 3b23a654a..aee1a1d02 100644 --- a/test/standard-mode-suite/extension.test.ts +++ b/test/standard-mode-suite/extension.test.ts @@ -7,6 +7,7 @@ import { Commands } from '../../src/commands'; import * as java from '../../src/javaServerStarter'; import * as plugin from '../../src/plugin'; import * as requirements from '../../src/requirements'; +import { escapeSnippetLiterals } from '../../src/extension'; suite('Java Language Extension - Standard', () => { @@ -233,4 +234,10 @@ suite('Java Language Extension - Standard', () => { assert(java.hasDebugFlag(['foo', '--debug=1234'])); assert(java.hasDebugFlag(['foo', '--debug-brk=1234'])); }); + + test('should preserve escaped commas in code action snippets', () => { + const snippet = '${1|HashMap,Map|} ${2:x} = new HashMap();'; + + assert.equal(escapeSnippetLiterals(snippet), snippet); + }); });