Skip to content

Commit 89132fb

Browse files
committed
Allow multiple edits at the same offset
1 parent b2920f5 commit 89132fb

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/TextEdit.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static function applyEdits(array $edits, string $text) : string {
3737
for ($i = \count($edits) - 1; $i >= 0; $i--) {
3838
$edit = $edits[$i];
3939

40-
if (!$prevEditStart > $edit->start) {
40+
if ($prevEditStart < $edit->start) {
4141
throw new \OutOfBoundsException(sprintf('Supplied TextEdit[] "%s" must not overlap', $edit->content));
4242
}
4343

tests/api/TextEditTest.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,27 @@ public function testApplyMultipleEdits() {
7575
$this->assertEquals($expected, TextEdit::applyEdits($edits, $content));
7676
}
7777

78+
public function testApplyMultipleEditsAtSameOffset() {
79+
$content = self::INPUT_TEXT;
80+
81+
$expected = <<< 'PHP'
82+
helloawesome<?php
83+
84+
function a () { }
85+
86+
function b () { }
87+
PHP
88+
;
89+
$edits = [
90+
new TextEdit(0, 0, "hello"),
91+
new TextEdit(0, 0, "awesome"),
92+
new TextEdit(0, 0, "")
93+
];
94+
95+
$this->assertEquals($expected, TextEdit::applyEdits($edits, $content));
96+
}
97+
98+
7899
public function testApplyingEmptyTextEditArray() {
79100
$content = self::INPUT_TEXT;
80101

@@ -88,7 +109,7 @@ public function testOutOfOrderTextEdits() {
88109
new TextEdit(0, 10, 10),
89110
new TextEdit(0, 4 ,3)
90111
];
91-
$this->expectException(AssertionError::class);
112+
$this->expectException(OutOfBoundsException::class);
92113
TextEdit::applyEdits($edits, $content);
93114
}
94115

@@ -98,7 +119,7 @@ public function testOverlappingTextEdits() {
98119
new TextEdit(0, 4, 10),
99120
new TextEdit(0, 10, 10)
100121
];
101-
$this->expectException(AssertionError::class);
122+
$this->expectException(OutOfBoundsException::class);
102123
TextEdit::applyEdits($edits, $content);
103124
}
104125

0 commit comments

Comments
 (0)