Skip to content

Commit 7bdc6e4

Browse files
committed
search: fix bad crlf normalization for negated character classes
Fixes microsoft#122223
1 parent faed865 commit 7bdc6e4

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/vs/workbench/services/search/node/ripgrepTextSearchEngine.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -576,9 +576,15 @@ export function fixRegexNewline(pattern: string): string {
576576
} else if (context.some(isLookBehind)) {
577577
// no-op in a lookbehind, see #100569
578578
} else if (parent.type === 'CharacterClass') {
579-
// in a bracket expr, [a-z\n] -> (?:[a-z]|\r?\n)
580-
const otherContent = pattern.slice(parent.start + 1, char.start) + pattern.slice(char.end, parent.end - 1);
581-
replace(parent.start, parent.end, otherContent === '' ? '\\r?\\n' : `(?:[${otherContent}]|\\r?\\n)`);
579+
if (parent.negate) {
580+
// negative bracket expr, [^a-z\n] -> (?![a-z]|\r?\n)
581+
const otherContent = pattern.slice(parent.start + 2, char.start) + pattern.slice(char.end, parent.end - 1);
582+
replace(parent.start, parent.end, '(?!\\r?\\n' + (otherContent ? `|[${otherContent}]` : '') + ')');
583+
} else {
584+
// positive bracket expr, [a-z\n] -> (?:[a-z]|\r?\n)
585+
const otherContent = pattern.slice(parent.start + 1, char.start) + pattern.slice(char.end, parent.end - 1);
586+
replace(parent.start, parent.end, otherContent === '' ? '\\r?\\n' : `(?:[${otherContent}]|\\r?\\n)`);
587+
}
582588
} else if (parent.type === 'Quantifier') {
583589
replace(char.start, char.end, '(?:\\r?\\n)');
584590
}

src/vs/workbench/services/search/test/node/ripgrepTextSearchEngine.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ suite('RipgrepTextSearchEngine', () => {
3838
['f[\\n-a]', 'f[\\n-a]'],
3939
['(?<=\\n)\\w', '(?<=\\n)\\w'],
4040
['fo\\n+o', 'fo(?:\\r?\\n)+o'],
41+
['fo[^\\n]o', 'fo(?!\\r?\\n)o'],
42+
['fo[^\\na-z]o', 'fo(?!\\r?\\n|[a-z])o'],
4143
];
4244

4345
for (const [input, expected] of ttable) {

0 commit comments

Comments
 (0)