src/vs/workbench/services/search/common/replace.ts

284 LOC · 272 covered · 12 uncovered · 73 ranges · 22 concepts · 20 introducers · 8 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the filesrc/vs/base/common/search.ts · 48 LOCcommon/search.tssearch.ts ×6 · 31 introduced LOCsearch.ts ×6replace.ts ×2 · 4 introduced LOCreplace.ts ×2replace.test|title=Replace Pattern test case operations and newline|occurrence=1 · 0 introduced LOCreplace.test|title=Repla…replace.ts ×1 · 3 introduced LOCreplace.ts ×1replace.ts ×1 · 3 introduced LOCreplace.ts ×1replace.ts ×8 · 45 introduced LOCreplace.ts ×8replace.ts ×9 · 33 introduced LOCreplace.ts ×9replace.test|title=Replace Pattern test get replace string if given text is a complete match|occurrence=1 · 0 introduced LOCreplace.test|title=Repla…replace.ts ×2 · 6 introduced LOCreplace.ts ×2replace.ts ×5 · 13 introduced LOCreplace.ts ×5replace.ts ×6 · 17 introduced LOCreplace.ts ×6replace.ts ×1 · 3 introduced LOCreplace.ts ×1replace.ts ×1 · 6 introduced LOCreplace.ts ×1replace.ts ×3 · 5 introduced LOCreplace.ts ×3replace.ts ×2 · 4 introduced LOCreplace.ts ×2replace.ts ×1 · 3 introduced LOCreplace.ts ×1replace.ts ×3 · 9 introduced LOCreplace.ts ×3replace.ts ×1 · 2 introduced LOCreplace.ts ×1replace.ts ×7 · 20 introduced LOCreplace.ts ×7replace.ts ×2 · 4 introduced LOCreplace.ts ×2replace.ts ×2 · 7 introduced LOCreplace.ts ×2replace.ts ×15 · 94 introduced LOCreplace.ts ×15replace.test|title=Replace Pattern test case operations - no false positive|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/services/search/test/common/replace.test|title=Replace Pattern test case operations - no false positive|occurrence=1replace.test|title=Repla…replace.test|title=Replace Pattern test case operations and newline|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/services/search/test/common/replace.test|title=Replace Pattern test case operations and newline|occurrence=1replace.test|title=Repla…replace.test|title=Replace Pattern test case operations|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/services/search/test/common/replace.test|title=Replace Pattern test case operations|occurrence=1replace.test|title=Repla…replace.test|title=Replace Pattern test create pattern by passing regExp|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/services/search/test/common/replace.test|title=Replace Pattern test create pattern by passing regExp|occurrence=1replace.test|title=Repla…replace.test|title=Replace Pattern test get replace string for no matches|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/services/search/test/common/replace.test|title=Replace Pattern test get replace string for no matches|occurrence=1replace.test|title=Repla…replace.test|title=Replace Pattern test get replace string if given text is a complete match|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/services/search/test/common/replace.test|title=Replace Pattern test get replace string if given text is a complete match|occurrence=1replace.test|title=Repla…replace.test|title=Replace Pattern test get replace string if match is sub-string of the text|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/services/search/test/common/replace.test|title=Replace Pattern test get replace string if match is sub-string of the text|occurrence=1replace.test|title=Repla…replace.test|title=Replace Pattern test parse replace string|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/services/search/test/common/replace.test|title=Replace Pattern test parse replace string|occurrence=1replace.test|title=Repla…Focused file · src/vs/workbench/services/search/common/replace.ts · 284 LOCcommon/replace.ts

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- replace.ts ×15
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import * as strings from '../../../../base/common/strings.js';
7 > import { IPatternInfo } from './search.js';
8 > import { CharCode } from '../../../../base/common/charCode.js';
9 > import { buildReplaceStringWithCasePreserved } from '../../../../base/common/search.js';
10 >
11 > export class ReplacePattern {
12 >
13 > private _replacePattern: string;
14 > private _hasParameters: boolean = false;
15 > private _regExp: RegExp;
16 > private _caseOpsRegExp: RegExp;
17 >
18 > constructor(replaceString: string, searchPatternInfo: IPatternInfo);
19 > constructor(replaceString: string, parseParameters: boolean, regEx: RegExp);
20 > constructor(replaceString: string, arg2: any, arg3?: any) {
21 > this._replacePattern = replaceString;
22 > let searchPatternInfo: IPatternInfo;
23 > let parseParameters: boolean;
24 > if (typeof arg2 === 'boolean') {
25 > parseParameters = arg2; replace.ts ×1
26 > this._regExp = arg3;
27 >
28 > } else { replace.ts ×15
29 > searchPatternInfo = arg2; replace.ts ×2
30 > parseParameters = !!searchPatternInfo.isRegExp;
31 > this._regExp = strings.createRegExp(searchPatternInfo.pattern, !!searchPatternInfo.isRegExp, { matchCase: searchPatternInfo.isCaseSensitive, wholeWord: searchPatternInfo.isWordMatch, multiline: searchPatternInfo.isMultiline, global: false, unicode: true });
32 > }
34 > if (parseParameters) {
35 > this.parseReplaceString(replaceString);
36 > }
37 >
38 > if (this._regExp.global) {
39 > this._regExp = strings.createRegExp(this._regExp.source, true, { matchCase: !this._regExp.ignoreCase, wholeWord: false, multiline: this._regExp.multiline, global: false }); replace.ts ×2
40 > }
42 > this._caseOpsRegExp = new RegExp(/([\s\S]*?)((?:\\[uUlL])+?|)(\$[0-9]+)([\s\S]*?)/g);
43 > }
44 >
45 > get hasParameters(): boolean {
46 > return this._hasParameters; replace.ts ×7
47 > }
49 > get pattern(): string {
50 > return this._replacePattern; replace.ts ×9
51 > }
53 > get regExp(): RegExp {
54 > return this._regExp; replace.ts ×2
55 > }
57 > /**
58 > * Returns the replace string for the first match in the given text.
59 > * If text has no matches then returns null.
60 > */
61 > getReplaceString(text: string, preserveCase?: boolean): string | null {
62 > this._regExp.lastIndex = 0; replace.ts ×2
63 > const match = this._regExp.exec(text);
64 > if (match) {
65 > if (this.hasParameters) { replace.ts ×5
66 > const replaceString = this.replaceWithCaseOperations(text, this._regExp, this.buildReplaceString(match, preserveCase));
67 > if (match[0] === text) {
68 > return replaceString;
69 > }
70 > return replaceString.substr(match.index, match[0].length - (text.length - replaceString.length)); replace.ts ×2
71 > }
72 > return this.buildReplaceString(match, preserveCase);
73 > }
75 > return null;
78 > /**
79 > * replaceWithCaseOperations applies case operations to relevant replacement strings and applies
80 > * the affected $N arguments. It then passes unaffected $N arguments through to string.replace().
81 > *
82 > * \u => upper-cases one character in a match.
83 > * \U => upper-cases ALL remaining characters in a match.
84 > * \l => lower-cases one character in a match.
85 > * \L => lower-cases ALL remaining characters in a match.
86 > */
87 > private replaceWithCaseOperations(text: string, regex: RegExp, replaceString: string): string {
88 > // Short-circuit the common path. replace.ts ×5
89 > if (!/\\[uUlL]/.test(replaceString)) {
90 > return text.replace(regex, replaceString); replace.ts ×2
91 > }
92 > // Store the values of the search parameters. replace.ts ×8
93 > const firstMatch = regex.exec(text);
94 > if (firstMatch === null) {
95 return text.replace(regex, replaceString);
96 }
98 > let patMatch: RegExpExecArray | null;
99 > let newReplaceString = '';
100 > let lastIndex = 0;
101 > let lastMatch = '';
102 > // For each annotated $N, perform text processing on the parameters and perform the substitution.
103 > while ((patMatch = this._caseOpsRegExp.exec(replaceString)) !== null) {
104 > lastIndex = patMatch.index;
105 > const fullMatch = patMatch[0];
106 > lastMatch = fullMatch;
107 > let caseOps = patMatch[2]; // \u, \l\u, etc.
108 > const money = patMatch[3]; // $1, $2, etc.
109 >
110 > if (!caseOps) {
111 > newReplaceString += fullMatch;
112 > continue;
113 > }
114 > const replacement = firstMatch[parseInt(money.slice(1))];
115 > if (!replacement) {
116 newReplaceString += fullMatch;
117 continue;
118 }
119 > const replacementLen = replacement.length; replace.ts ×8
120 >
121 > newReplaceString += patMatch[1]; // prefix
122 > caseOps = caseOps.replace(/\\/g, '');
123 > let i = 0;
124 > for (; i < caseOps.length; i++) {
125 > switch (caseOps[i]) {
126 > case 'U':
127 > newReplaceString += replacement.slice(i).toUpperCase(); replace.ts ×1
128 > i = replacementLen;
129 > break;
130 > case 'u': replace.ts ×8
131 > newReplaceString += replacement[i].toUpperCase(); replace.ts ×2
132 > break;
133 > case 'L': replace.ts ×8
134 > newReplaceString += replacement.slice(i).toLowerCase(); replace.ts ×1
135 > i = replacementLen;
136 > break;
137 > case 'l': replace.ts ×8
138 > newReplaceString += replacement[i].toLowerCase(); replace.ts ×2
139 > break;
140 > } replace.ts ×8
141 > }
142 > // Append any remaining replacement string content not covered by case operations.
143 > if (i < replacementLen) {
144 newReplaceString += replacement.slice(i);
145 }
147 > newReplaceString += patMatch[4]; // suffix
148 > }
149 >
150 > // Append any remaining trailing content after the final regex match.
151 > newReplaceString += replaceString.slice(lastIndex + lastMatch.length);
152 >
153 > return text.replace(regex, newReplaceString);
154 > } replace.ts ×5
156 > public buildReplaceString(matches: string[] | null, preserveCase?: boolean): string {
157 > if (preserveCase) { replace.ts ×5
158 > return buildReplaceStringWithCasePreserved(matches, this._replacePattern); search.ts ×6
159 > } else { replace.ts ×5
160 > return this._replacePattern;
161 > }
162 > }
164 > /**
165 > * \n => LF
166 > * \t => TAB
167 > * \\ => \
168 > * $0 => $& (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter)
169 > * everything else stays untouched
170 > */
171 > private parseReplaceString(replaceString: string): void {
172 > if (!replaceString || replaceString.length === 0) {
173 return;
174 }
176 > let substrFrom = 0, result = '';
177 > for (let i = 0, len = replaceString.length; i < len; i++) {
178 > const chCode = replaceString.charCodeAt(i);
179 >
180 > if (chCode === CharCode.Backslash) {
182 > // move to next char
183 > i++;
184 >
185 > if (i >= len) {
186 > // string ends with a \ replace.ts ×9
187 > break;
188 > }
190 > const nextChCode = replaceString.charCodeAt(i);
191 > let replaceWithCharacter: string | null = null;
192 >
193 > switch (nextChCode) {
194 > case CharCode.Backslash:
195 > // \\ => \ replace.ts ×9
196 > replaceWithCharacter = '\\';
197 > break;
198 > case CharCode.n: replace.ts ×6
199 > // \n => LF replace.ts ×9
200 > replaceWithCharacter = '\n';
201 > break;
202 > case CharCode.t: replace.ts ×6
203 > // \t => TAB replace.ts ×9
204 > replaceWithCharacter = '\t';
205 > break;
206 > } replace.ts ×6
207 >
208 > if (replaceWithCharacter) {
209 > result += replaceString.substring(substrFrom, i - 1) + replaceWithCharacter; replace.ts ×9
210 > substrFrom = i + 1;
211 > }
212 > } replace.ts ×6
214 > if (chCode === CharCode.DollarSign) {
216 > // move to next char
217 > i++;
218 >
219 > if (i >= len) {
220 // string ends with a $
221 break;
222 }
224 > const nextChCode = replaceString.charCodeAt(i);
225 > let replaceWithCharacter: string | null = null;
226 >
227 > switch (nextChCode) {
228 > case CharCode.Digit0:
229 > // $0 => $& replace.ts ×3
230 > replaceWithCharacter = '$&';
231 > this._hasParameters = true;
232 > break;
233 > case CharCode.BackTick: replace.ts ×7
234 > case CharCode.SingleQuote:
235 > this._hasParameters = true; replace.ts ×9
236 > break;
237 > default: { replace.ts ×7
238 > // check if it is a valid string parameter $n (0 <= n <= 99). $0 is already handled by now. replace.ts ×3
239 > if (!this.between(nextChCode, CharCode.Digit1, CharCode.Digit9)) {
240 > break; replace.ts ×9
241 > }
242 > if (i === replaceString.length - 1) { replace.ts ×3
243 > this._hasParameters = true; replace.ts ×1
244 > break;
245 > }
246 > let charCode = replaceString.charCodeAt(++i); replace.ts ×1
247 > if (!this.between(charCode, CharCode.Digit0, CharCode.Digit9)) {
248 > this._hasParameters = true;
249 > --i;
250 > break;
251 > }
252 > if (i === replaceString.length - 1) { replace.ts ×9
253 > this._hasParameters = true;
254 > break;
255 > }
256 > charCode = replaceString.charCodeAt(++i);
257 > if (!this.between(charCode, CharCode.Digit0, CharCode.Digit9)) {
258 > this._hasParameters = true;
259 > --i;
260 > break;
261 > }
262 > break;
263 > }
264 > } replace.ts ×7
265 >
266 > if (replaceWithCharacter) {
267 > result += replaceString.substring(substrFrom, i - 1) + replaceWithCharacter; replace.ts ×3
268 > substrFrom = i + 1;
269 > }
270 > } replace.ts ×7
271 > } replace.ts ×15
272 >
273 > if (substrFrom === 0) {
274 > // no replacement occurred replace.ts ×2
275 > return;
276 > }
278 > this._replacePattern = result + replaceString.substring(substrFrom);
279 > } replace.ts ×15
280 >
281 > private between(value: number, from: number, to: number): boolean {
282 > return from <= value && value <= to; replace.ts ×3
283 > }
284 > } replace.ts ×15