Skip to content

Commit 67717e6

Browse files
committed
Limit conflict marker context to 5 lines and improve cursor positioning logic
1 parent a0c6855 commit 67717e6

3 files changed

Lines changed: 19 additions & 13 deletions

File tree

‎apps/editor/src/commands/apply-chat-response-command/utils/edit-formats/conflict-markers/apply-conflict-markers-to-content/apply-conflict-markers-to-content.ts‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,21 @@ export const apply_conflict_markers_to_content = (params: {
1313
const segments = parse_conflict_segments(params.markers_content)
1414
let cursor = 0
1515

16+
const MAX_CONTEXT_LINES = 5
17+
1618
for (let i = 0; i < segments.length; i++) {
1719
const segment = segments[i]
1820
if (segment.type !== 'conflict') continue
1921

2022
const prev = i > 0 ? segments[i - 1] : undefined
2123
const next = i < segments.length - 1 ? segments[i + 1] : undefined
2224

23-
const context_before = prev && prev.type == 'common' ? prev.lines : []
24-
const context_after = next && next.type == 'common' ? next.lines : []
25+
const context_before =
26+
prev && prev.type == 'common' ? prev.lines.slice(-MAX_CONTEXT_LINES) : []
27+
const context_after =
28+
next && next.type == 'common'
29+
? next.lines.slice(0, MAX_CONTEXT_LINES)
30+
: []
2531

2632
const clean_lines = (lines: string[]) =>
2733
lines.filter((l) => l.trim() !== '')
@@ -30,7 +36,7 @@ export const apply_conflict_markers_to_content = (params: {
3036
const clean_original = clean_lines(segment.original_lines)
3137
const clean_context_after = clean_lines(context_after)
3238

33-
const SEPARATOR = '(?:\\r?\\n[ \\t]*)+'
39+
const SEPARATOR = '(?:\\r?\\n[ \\t]*){1,5}'
3440

3541
const build_block_pattern = (lines: string[]) => {
3642
return lines
@@ -101,14 +107,12 @@ export const apply_conflict_markers_to_content = (params: {
101107
replacement_text +
102108
current_content.slice(match_index + matched_text.length)
103109

104-
// Advance cursor while preserving context for the next possible conflict
105-
let advance_length = replacement_text.length
106-
if (clean_context_after.length > 0 && matched_after) {
107-
const offset = replacement_text.lastIndexOf(matched_after)
108-
advance_length = offset > 0 ? offset - line_ending.length : 0
109-
}
110-
111-
cursor = Math.max(0, match_index + advance_length)
110+
const content_before_after_context = matched_before
111+
? matched_before + line_ending + updated_text
112+
: updated_text
113+
cursor = matched_after
114+
? match_index + content_before_after_context.length
115+
: match_index + replacement_text.length
112116
}
113117

114118
return current_content

‎apps/editor/src/commands/apply-chat-response-command/utils/edit-formats/truncations/test-cases/basic/expected.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ line 1
22
line 2
33
line 3
44
line 4
5-
line 5
5+
line 5
6+
line 6
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
line 1
22
// ...
3-
line 5
3+
line 5
4+
line 6

0 commit comments

Comments
 (0)