Repository navigation
fix(search): treat $$ as escaped $ in workspace search replace - #341892
冰水泡枸杞 (xingzihai) wants to merge 2 commits into
Conversation
…soft#299365) The workspace-search replace parser (parseReplaceString in src/vs/workbench/services/search/common/replace.ts) had no case for $$, so it was inserted literally and $$0 came out as $$0 instead of $0. The in-editor find/replace (src/vs/editor/contrib/find/browser/replacePattern.ts) already treats $$ as an escaped $, so the two had drifted apart. Handling $$ the same way (collapsing to a single $) makes workspace search consistent with the editor and with standard regex-replace semantics ($$0 -> $0, $$1 -> $1). Fixes microsoft#299365
There was a problem hiding this comment.
🟡 Changes recommended
Combining escaped dollars with capture references now produces incorrect replacements.
1 open finding
What changed in this PR
Updates workspace regex replacement to treat $$ as an escaped dollar, matching in-editor behavior and addressing #299365.
Changes:
- Adds dollar-escape handling to the replacement parser.
- Updates parser expectations and adds escape-only regression tests.
| File | Description |
|---|---|
| src/vs/workbench/services/search/test/common/replace.test.ts | Updates expectations and tests escaped dollars. |
| src/vs/workbench/services/search/common/replace.ts | Collapses $$ during replacement parsing. |
🧠 Review effort: Balanced
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| break; | ||
| case CharCode.DollarSign: | ||
| // $$ => $ (escape, consistent with in-editor find/replace) | ||
| replaceWithCharacter = '$'; |
|
冰水泡枸杞 (@xingzihai) please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
…soft#299365) parseReplaceString collapsed $$ to a single $; when the replace string also contained a real reference ($1/$0), getReplaceString passed the collapsed pattern to String.replace(), which re-interpreted the $. Searching (foo) and replacing $$1-$1 produced foo-foo instead of $1-foo, and $$$0 produced $& instead of $foo. Keep the $$ pair intact for String.replace() (which natively treats $$ as a literal $) when parameters are present, and only collapse $$ to $ for escape-only patterns (no parameters, never passed to String.replace()). Adds regression tests mixing escaped dollars with capture and whole-match references. Address Copilot review on PR microsoft#341892.

Summary
The workspace-search (Ctrl+Shift+F) regex replace treats
$$literally: searching for any pattern and replacing with$$0produces$$0in the results, instead of$0. The in-editor find/replace already treats$$as an escaped$, so the two surfaces had drifted apart.parseReplaceStringinsrc/vs/workbench/services/search/common/replace.tshas cases for$0,$`,$', and$1-$99, but none for$$, so it was never collapsed and got inserted verbatim.What does this PR do?
Adds a
$$ => $case toparseReplaceString, consistent with the in-editorreplacePattern.tsand with standard regex-replace semantics ($$0->$0,$$1->$1).Includes a regression test for the exact scenario from the issue, plus updated expectations in the existing parser test (which previously asserted the buggy
hello$$->hello$$behavior).Fixes #299365