Skip to content

Commit

Permalink
Merge 7874e9a into bde125b
Browse files Browse the repository at this point in the history
  • Loading branch information
rwols committed Aug 26, 2019
2 parents bde125b + 7874e9a commit 3384b9e
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 11 deletions.
2 changes: 1 addition & 1 deletion plugin/core/edit.py
Expand Up @@ -35,7 +35,7 @@ def sort_by_application_order(changes: 'Iterable[TextEdit]') -> 'List[TextEdit]'

def get_start_position(pair: 'Tuple[int, TextEdit]'):
index, change = pair
return change[0][0], change[0][1], index
return change[0][0], change[0][1], -index

# The spec reads:
# > However, it is possible that multiple edits have the same start position: multiple
Expand Down
6 changes: 3 additions & 3 deletions plugin/core/test_edit.py
Expand Up @@ -64,8 +64,8 @@ def test_sorts_backwards(self):
((0, 0), (0, 0), 'a'),
((0, 2), (0, 2), 'c')
]
# expect 'c' (higher start), 'a' now reverse order before 'b'
# expect 'c' (higher start), but 'b' still before 'a' because the array order is preserved.
sorted = sort_by_application_order(edits)
self.assertEqual(sorted[0][2], 'c')
self.assertEqual(sorted[1][2], 'a')
self.assertEqual(sorted[2][2], 'b')
self.assertEqual(sorted[1][2], 'b')
self.assertEqual(sorted[2][2], 'a')
90 changes: 83 additions & 7 deletions tests/test_edit.py
@@ -1,4 +1,5 @@
from unittesting import DeferrableTestCase
from unittest import skip

import sublime

Expand Down Expand Up @@ -43,18 +44,93 @@ def test_apply_and_preserve_order(self):
# Note that (1, 2) comes before (0, 1) in the text.
file_changes = [
((1, 2), (1, 2), '4'), # insert after the g
((1, 2), (1, 2), '5'),
((1, 2), (1, 3), '6'), # replace the h
((0, 1), (0, 1), '1'), # insert after a
((0, 1), (0, 1), '2'),
((0, 1), (0, 1), '3'),
((1, 2), (1, 2), '5'), # then insert after the g again
((1, 2), (1, 3), '6'), # replace the just-inserted "5" with a "6"
((0, 1), (0, 1), '1'), # insert "1" after "a"
((0, 1), (0, 1), '2'), # insert "2" after "a"
((0, 1), (0, 1), '3'), # insert "3" after "a"
]
expected = (
'a123bcde\n'
'fg456ij\n'
'a321bcde\n'
'fg64hij\n'
)
self.run_test(original, expected, file_changes)

def test_remove_line_and_then_insert_at_that_line_not_at_end(self):
original = (
'a\n'
'b\n'
'c'
)
file_changes = [
((1, 0), (2, 0), ''),
((2, 0), (2, 0), 'x\n')
]
expected = (
'a\n'
'x\n'
'c'
)
self.run_test(original, expected, file_changes)

@skip("point is out-of-bounds")
def test_remove_line_and_then_insert_at_that_line_at_end(self):
original = (
'a\n'
'b\n'
'c'
)
file_changes = [
((2, 0), (3, 0), ''), # note out-of-bounds end position
((3, 0), (3, 0), 'c\n') # note out-of-bounds end position
]
expected = (
'a\n',
'b\n',
'c\n'
)
# The chain of events is like this:
# 1) first we end up with ('a\n', 'b\n', 'cc\n')
# 2) then we end up with ('a\n', 'b\n', '')
self.run_test(original, expected, file_changes)

# Quoting the spec:
# However, it is possible that multiple edits have the same start position: multiple inserts, or any number of
# inserts followed by a single remove or replace edit. If multiple inserts have the same position, the order in
# the array defines the order in which the inserted strings appear in the resulting text.

def test_insertions_at_same_location(self):
original = ('')
file_changes = [
((0, 0), (0, 0), 'c'),
((0, 0), (0, 0), 'b'),
((0, 0), (0, 0), 'a')
]
expected = ('abc')
self.run_test(original, expected, file_changes)

def test_insertions_followed_by_single_remove(self):
original = ('def')
file_changes = [
((0, 0), (0, 0), 'c'),
((0, 0), (0, 0), 'b'),
((0, 0), (0, 0), 'a'),
((0, 0), (0, 3), '')
]
expected = ('def')
self.run_test(original, expected, file_changes)

def test_insertions_followed_by_single_replace(self):
original = ('def')
file_changes = [
((0, 0), (0, 0), 'c'),
((0, 0), (0, 0), 'b'),
((0, 0), (0, 0), 'a'),
((0, 0), (0, 4), 'hello')
]
expected = ('helloef')
self.run_test(original, expected, file_changes)

def run_test(self, original: str, expected: str, file_changes):
self.view.run_command('insert', {"characters": original})
self.view.run_command(
Expand Down

0 comments on commit 3384b9e

Please sign in to comment.