Skip to content

Commit 54dc5b8

Browse files
committed
Replace regex-based search replace matching with line-based sequence finding
- on windows, long search blocks fail to get found in original files └── apps └── editor └── src └── utils └── changes-integration └── search-replace-processor ├── apply-search-replace-to-content │ ├── apply-search-replace-to-content.spec.ts │ ├── apply-search-replace-to-content.ts │ ├── index.ts │ └── test-cases │ ├── basic-conflict │ │ ├── expected.txt │ │ ├── markers.txt │ │ └── original.txt │ ├── deletion │ │ ├── expected.txt │ │ ├── markers.txt │ │ └── original.txt │ ├── multiple-conflicts │ │ ├── expected.txt │ │ ├── markers.txt │ │ └── original.txt │ ├── rst-syntax │ │ ├── expected.txt │ │ ├── markers.txt │ │ └── original.txt │ ├── trailing-whitespace │ │ ├── expected.txt │ │ ├── markers.txt │ │ └── original.txt │ └── whitespace-issues │ ├── expected.txt │ ├── markers.txt │ └── original.txt ├── index.ts ├── parse-search-replace-segments.ts └── types.ts
1 parent 5ac87d6 commit 54dc5b8

2 files changed

Lines changed: 141 additions & 54 deletions

File tree

‎apps/editor/src/utils/changes-integration/search-replace-processor/apply-search-replace-to-content/apply-search-replace-to-content.ts‎

Lines changed: 140 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,127 @@
11
import { Segment } from '../types'
22

3-
const escape_reg_exp = (string: string) => {
4-
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
3+
const INVISIBLE_CHARS_REGEX = /[\u200B-\u200F\uFEFF]/g
4+
const IGNORABLE_CHAR_REGEX = /[\s\u200B-\u200F\uFEFF]/
5+
6+
type Normalizer = (line: string) => string
7+
8+
type IndexedLine = {
9+
// Absolute offset of the line start (including leading whitespace)
10+
start: number
11+
// Absolute offset right after the last visible character of the line
12+
content_end: number
13+
normalized: string
14+
}
15+
16+
const strip_invisible = (text: string) =>
17+
text.replace(INVISIBLE_CHARS_REGEX, '')
18+
19+
const normalize_strict: Normalizer = (line) => strip_invisible(line).trim()
20+
21+
const normalize_loose: Normalizer = (line) =>
22+
strip_invisible(line).replace(/\s+/g, ' ').trim()
23+
24+
const index_non_blank_lines = (params: {
25+
content: string
26+
from: number
27+
normalize: Normalizer
28+
}): IndexedLine[] => {
29+
const { content, from, normalize } = params
30+
const result: IndexedLine[] = []
31+
let line_start = from
32+
33+
while (line_start <= content.length) {
34+
let line_end = content.indexOf('\n', line_start)
35+
if (line_end == -1) line_end = content.length
36+
37+
const normalized = normalize(content.slice(line_start, line_end))
38+
if (normalized !== '') {
39+
let content_end = line_end
40+
while (
41+
content_end > line_start &&
42+
IGNORABLE_CHAR_REGEX.test(content[content_end - 1])
43+
) {
44+
content_end--
45+
}
46+
result.push({ start: line_start, content_end, normalized })
47+
}
48+
49+
line_start = line_end + 1
50+
}
51+
52+
return result
53+
}
54+
55+
const find_sequence = (lines: IndexedLine[], pattern: string[]): number => {
56+
outer: for (let i = 0; i + pattern.length <= lines.length; i++) {
57+
for (let k = 0; k < pattern.length; k++) {
58+
if (lines[i + k].normalized !== pattern[k]) continue outer
59+
}
60+
return i
61+
}
62+
return -1
63+
}
64+
65+
type BlockMatch = {
66+
match_start: number
67+
match_end: number
68+
matched_before: string
69+
matched_after: string
70+
}
71+
72+
const find_block = (params: {
73+
content: string
74+
from: number
75+
context_before: string[]
76+
original: string[]
77+
context_after: string[]
78+
normalize: Normalizer
79+
}): BlockMatch | null => {
80+
const prepare = (lines: string[]) =>
81+
lines.map(params.normalize).filter((l) => l !== '')
82+
83+
const before = prepare(params.context_before)
84+
const original = prepare(params.original)
85+
const after = prepare(params.context_after)
86+
const pattern = [...before, ...original, ...after]
87+
88+
if (pattern.length == 0) {
89+
return {
90+
match_start: params.from,
91+
match_end: params.from,
92+
matched_before: '',
93+
matched_after: ''
94+
}
95+
}
96+
97+
const lines = index_non_blank_lines({
98+
content: params.content,
99+
from: params.from,
100+
normalize: params.normalize
101+
})
102+
const i = find_sequence(lines, pattern)
103+
if (i == -1) return null
104+
105+
const match_start = lines[i].start
106+
const match_end = lines[i + pattern.length - 1].content_end
107+
108+
const matched_before =
109+
before.length > 0
110+
? params.content.slice(
111+
match_start,
112+
lines[i + before.length - 1].content_end
113+
)
114+
: ''
115+
116+
const matched_after =
117+
after.length > 0
118+
? params.content.slice(
119+
lines[i + before.length + original.length].start,
120+
match_end
121+
)
122+
: ''
123+
124+
return { match_start, match_end, matched_before, matched_after }
5125
}
6126

7127
export const apply_search_replace_to_content = (params: {
@@ -29,47 +149,19 @@ export const apply_search_replace_to_content = (params: {
29149
? next.lines.slice(0, MAX_CONTEXT_LINES)
30150
: []
31151

32-
const clean_lines = (lines: string[]) =>
33-
lines.filter((l) => l.replace(/[\u200B-\u200F\uFEFF]/g, '').trim() !== '')
34-
35-
const clean_context_before = clean_lines(context_before)
36-
const clean_original = clean_lines(segment.original_lines)
37-
const clean_context_after = clean_lines(context_after)
38-
39-
// Allow invisible characters alongside tabs and spaces
40-
const SEPARATOR = '(?:[ \\t\\u200B-\\u200F\\uFEFF]*\\r?\\n){1,5}'
41-
42-
const build_block_pattern = (lines: string[]) => {
43-
return lines
44-
.map((line) => {
45-
const cleaned = line.replace(/[\u200B-\u200F\uFEFF]/g, '').trim()
46-
// Spread operator iterates code points correctly (safeguards emojis/surrogate pairs).
47-
// Allow optional zero-width characters anywhere inside the matched line.
48-
const flexible = [...cleaned]
49-
.map(escape_reg_exp)
50-
.join('[\\u200B-\\u200F\\uFEFF]*')
51-
return `[ \\t\\u200B-\\u200F\\uFEFF]*${flexible}`
52-
})
53-
.join(SEPARATOR)
54-
}
55-
56-
const regex_parts: string[] = []
57-
58-
if (clean_context_before.length > 0) {
59-
regex_parts.push(`(${build_block_pattern(clean_context_before)})`)
60-
}
61-
62-
if (clean_original.length > 0) {
63-
regex_parts.push(`(${build_block_pattern(clean_original)})`)
152+
// Line-based matching (no giant regex) - avoids stack overflows and
153+
// "regular expression too large" errors for long blocks, notably on
154+
// Windows where the thread stack is small.
155+
const find_params = {
156+
content: current_content,
157+
from: cursor,
158+
context_before,
159+
original: segment.original_lines,
160+
context_after
64161
}
65-
66-
if (clean_context_after.length > 0) {
67-
regex_parts.push(`(${build_block_pattern(clean_context_after)})`)
68-
}
69-
70-
const regex = new RegExp(regex_parts.join(SEPARATOR))
71-
const content_slice = current_content.slice(cursor)
72-
const match = regex.exec(content_slice)
162+
const match =
163+
find_block({ ...find_params, normalize: normalize_strict }) ??
164+
find_block({ ...find_params, normalize: normalize_loose })
73165

74166
if (!match) {
75167
const all_search_lines = [
@@ -86,18 +178,13 @@ export const apply_search_replace_to_content = (params: {
86178
)
87179
}
88180

89-
const match_index = cursor + match.index
90-
const matched_text = match[0]
91-
92-
let group_index = 1
93-
const matched_before =
94-
clean_context_before.length > 0 ? match[group_index++] : ''
95-
if (clean_original.length > 0) group_index++
96-
const matched_after =
97-
clean_context_after.length > 0 ? match[group_index++] : ''
181+
const match_index = match.match_start
182+
const matched_length = match.match_end - match.match_start
183+
const matched_before = match.matched_before
184+
const matched_after = match.matched_after
98185

99186
const updated_text = segment.updated_lines
100-
.map((line) => line.replace(/[\u200B-\u200F\uFEFF]/g, ''))
187+
.map((line) => strip_invisible(line))
101188
.join(line_ending)
102189

103190
const replacement_parts: string[] = []
@@ -112,7 +199,7 @@ export const apply_search_replace_to_content = (params: {
112199
current_content =
113200
current_content.slice(0, match_index) +
114201
replacement_text +
115-
current_content.slice(match_index + matched_text.length)
202+
current_content.slice(match_index + matched_length)
116203

117204
const content_before_after_context = matched_before
118205
? matched_before + line_ending + updated_text

‎apps/editor/src/utils/changes-integration/search-replace-processor/parse-search-replace-segments.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export type Segment =
77
}
88

99
export const parse_search_replace_segments = (content: string): Segment[] => {
10-
const lines = content.replace(/\r\n/g, '\n').split('\n')
10+
const lines = content.replace(/\r/g, '').split('\n')
1111
const segments: Segment[] = []
1212
let current_lines: string[] = []
1313

0 commit comments

Comments
 (0)