Skip to content

Commit 0093264

Browse files
committed
Fixes microsoft#3549: Add support for $0
1 parent 44bd2d1 commit 0093264

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

src/vs/editor/contrib/find/common/findModel.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,13 +362,16 @@ export class FindModelBoundToEditorModel {
362362
}
363363

364364
const BACKSLASH_CHAR_CODE = '\\'.charCodeAt(0);
365+
const DOLLAR_CHAR_CODE = '$'.charCodeAt(0);
366+
const ZERO_CHAR_CODE = '0'.charCodeAt(0);
365367
const n_CHAR_CODE = 'n'.charCodeAt(0);
366368
const t_CHAR_CODE = 't'.charCodeAt(0);
367369

368370
/**
369371
* \n => LF
370372
* \t => TAB
371373
* \\ => \
374+
* $0 => $& (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter)
372375
* everything else stays untouched
373376
*/
374377
export function parseReplaceString(input:string): string {
@@ -413,6 +416,32 @@ export function parseReplaceString(input:string): string {
413416
substrFrom = i + 1;
414417
}
415418
}
419+
420+
if (chCode === DOLLAR_CHAR_CODE) {
421+
422+
// move to next char
423+
i++;
424+
425+
if (i >= len) {
426+
// string ends with a $
427+
break;
428+
}
429+
430+
let nextChCode = input.charCodeAt(i);
431+
let replaceWithCharacter: string = null;
432+
433+
switch (nextChCode) {
434+
case ZERO_CHAR_CODE:
435+
// $0 => $&
436+
replaceWithCharacter = '$&';
437+
break;
438+
}
439+
440+
if (replaceWithCharacter) {
441+
result += input.substring(substrFrom, i - 1) + replaceWithCharacter;
442+
substrFrom = i + 1;
443+
}
444+
}
416445
}
417446

418447
if (substrFrom === 0) {

src/vs/editor/contrib/find/test/common/findModel.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ suite('FindModel', () => {
5050

5151
// \ with back reference => no treatment
5252
testParse('hello\\0', 'hello\\0');
53+
54+
55+
56+
// $1 => no treatment
57+
testParse('hello$1', 'hello$1');
58+
// $2 => no treatment
59+
testParse('hello$2', 'hello$2');
60+
// $12 => no treatment
61+
testParse('hello$12', 'hello$12');
62+
// $$ => no treatment
63+
testParse('hello$$', 'hello$$');
64+
// $$0 => no treatment
65+
testParse('hello$$0', 'hello$$0');
66+
67+
// $0 => $&
68+
testParse('hello$0', 'hello$&');
69+
testParse('hello$02', 'hello$&2');
5370
});
5471

5572
function findTest(testName:string, callback:(editor:ICommonCodeEditor, cursor:Cursor)=>void): void {

0 commit comments

Comments
 (0)