Skip to content

Commit 5ac87d6

Browse files
committed
Preserve original line endings during search and replace processing
1 parent 3626a1b commit 5ac87d6

1 file changed

Lines changed: 5 additions & 6 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: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ export const apply_search_replace_to_content = (params: {
88
original_content: string
99
segments: Segment[]
1010
}): string => {
11-
const uses_crlf = params.original_content.includes('\r\n')
12-
const line_ending = '\n'
13-
let current_content = params.original_content.replace(/\r\n/g, '\n')
11+
const line_ending = params.original_content.includes('\r\n') ? '\r\n' : '\n'
12+
let current_content = params.original_content
1413
const segments = params.segments
1514
let cursor = 0
1615

@@ -38,7 +37,7 @@ export const apply_search_replace_to_content = (params: {
3837
const clean_context_after = clean_lines(context_after)
3938

4039
// Allow invisible characters alongside tabs and spaces
41-
const SEPARATOR = '(?:[ \\t\\r\\u200B-\\u200F\\uFEFF]*\\n){1,5}'
40+
const SEPARATOR = '(?:[ \\t\\u200B-\\u200F\\uFEFF]*\\r?\\n){1,5}'
4241

4342
const build_block_pattern = (lines: string[]) => {
4443
return lines
@@ -49,7 +48,7 @@ export const apply_search_replace_to_content = (params: {
4948
const flexible = [...cleaned]
5049
.map(escape_reg_exp)
5150
.join('[\\u200B-\\u200F\\uFEFF]*')
52-
return `[ \\t\\r\\u200B-\\u200F\\uFEFF]*${flexible}`
51+
return `[ \\t\\u200B-\\u200F\\uFEFF]*${flexible}`
5352
})
5453
.join(SEPARATOR)
5554
}
@@ -123,5 +122,5 @@ export const apply_search_replace_to_content = (params: {
123122
: match_index + replacement_text.length
124123
}
125124

126-
return uses_crlf ? current_content.replace(/\n/g, '\r\n') : current_content
125+
return current_content
127126
}

0 commit comments

Comments
 (0)