def testCanReplace(self): self.view.run_command("append", {"characters": "aaa bbb ccc"}) self.view.sel().clear() self.view.sel().add(R(5)) old = "'" for new, brackets in BRACKETS.items(): # with self.subTest(bracket=new): # Not supported in Python 3.3 old_a, old_b = BRACKETS[old] new_a, new_b = brackets self.view.sel().clear() self.view.sel().add(R(7)) self.view.run_command("insert", {"characters": old_b}) self.view.sel().clear() self.view.sel().add(R(4)) self.view.run_command("insert", {"characters": old_a}) self.assertEquals(self.view.substr(4), old_a) self.assertEquals(self.view.substr(8), old_b) self.view.run_command("_six_surround_delete", {"old": old}) self.assertEquals(self.view.substr(4), "b") self.assertEquals(self.view.substr(7), " ") old = new
def run(self, edit, old): old_a, old_b = BRACKETS[old] a = find_in_line(self.view, old_a, forward=False) if a < 0: return b = find_in_line(self.view, old_b) if b < 0: return self.view.erase(edit, R(b, b + 1)) self.view.erase(edit, R(a, a + 1))
def testReturnsPointIfFoundReversed(self): self.view.run_command("append", {"characters": "aaa\nxbb\nccc"}) self.view.sel().clear() self.view.sel().add(R(6)) rv = find_in_line(self.view, "x", forward=False) self.assertEquals(rv, 4)
def testReturnsNegativeNumberIfNotFoundReversed(self): self.view.run_command("append", {"characters": "aaa\nbbb\nccc"}) self.view.sel().clear() self.view.sel().add(R(5)) rv = find_in_line(self.view, "x", forward=False) self.assertTrue(rv < 0)
def testReturnsPointIfFound(self): self.view.run_command("append", {"characters": "aaa\nbbx\nccc"}) self.view.sel().clear() self.view.sel().add(R(5)) rv = find_in_line(self.view, "x") self.assertEquals(rv, 6)
def testCanExecute(self): self.view.run_command("append", {"characters": "aaa (bbb) ccc"}) self.view.sel().clear() self.view.sel().add(R(5)) self.assertEquals("(", self.view.substr(4)) self.assertEquals(")", self.view.substr(8)) self.command.old = "(" self.command.execute() self.assertEquals("b", self.view.substr(4)) self.assertEquals(" ", self.view.substr(7))
def run(self, edit, old, new): # The drudgery above is necessary only to reach this point, where we know # exactly what Sublime Text needs to do. old_a, old_b = BRACKETS[old] new_a, new_b = BRACKETS[new] a = find_in_line(self.view, old_a, forward=False) if a < 0: # TODO: Signal the state that it should abort. # Caller can't catch this exception from the command; just stop. # raise AbortCommandError return b = find_in_line(self.view, old_b) if b < 0: # TODO: Signal the state that it should abort. # Caller can't catch this exception from the command; just stop. # raise AbortCommandError return self.view.replace(edit, R(a, a + 1), new_a) self.view.replace(edit, R(b, b + 1), new_b)
def testDoesNotExecuteForIdenticalBracket(self): self.view.run_command("append", {"characters": "aaa (bbb) ccc"}) self.view.sel().clear() self.view.sel().add(R(5)) self.assertEquals("(", self.view.substr(4)) self.assertEquals(")", self.view.substr(8)) self.command.old = "(" self.command.new = ")" self.command.execute() self.assertEquals("(", self.view.substr(4)) self.assertEquals(")", self.view.substr(8))
def testCanUndoInOneStep(self): self.view.run_command("append", {"characters": "aaa 'bbb' ccc"}) self.view.sel().clear() self.view.sel().add(R(5)) self.assertEquals(self.view.substr(4), "'") self.assertEquals(self.view.substr(8), "'") self.view.run_command("_six_surround_change", {"old": "'", "new": '"'}) self.assertEquals(self.view.substr(4), '"') self.assertEquals(self.view.substr(8), '"') self.view.run_command("undo") self.assertEquals(self.view.substr(4), "'") self.assertEquals(self.view.substr(8), "'")
from Vintageous.vi.text_objects import find_next_lone_bracket from sublime import Region as R test = namedtuple('simple_test', 'content start brackets expected msg') TESTS = ( test(content='aaa', start=1, brackets=('\\{', '\\}'), expected=None, msg='should return none'), test(content='a{a}a', start=1, brackets=('\\{', '\\}'), expected=R(1, 2), msg='should find bracket at caret position'), test(content='{aa}a', start=1, brackets=('\\{', '\\}'), expected=R(0, 1), msg='should find bracket at BOF'), test(content='bbb{aa}a', start=2, brackets=('\\{', '\\}'), expected=None, msg='should not find brackets after caret'), test(content='a{bc', start=3, brackets=('\\{', '\\}'), expected=R(1, 2),
from sublime import Region as R from Vintageous.tests import set_text from Vintageous.tests import add_sel from Vintageous.tests import ViewTest from Vintageous.vi.text_objects import find_prev_lone_bracket from Vintageous.vi.text_objects import find_next_lone_bracket test = namedtuple('simple_test', 'content start brackets expected msg') TESTS = ( test(content='aaa', start=1, brackets=('\\{', '\\}'), expected=None, msg='should return none'), test(content='a{a}a', start=1, brackets=('\\{', '\\}'), expected=R(1, 2), msg='should find bracket at caret position'), test(content='{aa}a', start=1, brackets=('\\{', '\\}'), expected=R(0, 1), msg='should find bracket at BOF'), test(content='bbb{aa}a', start=2, brackets=('\\{', '\\}'), expected=None, msg='should not find brackets after caret'), test(content='a{bc', start=3, brackets=('\\{', '\\}'), expected=R(1, 2), msg='should find unbalanced bracket before caret'), test(content='foo {bar {foo} bar}', start=16, brackets=('\\{', '\\}'), expected=R(4, 5), msg='should find outer bracket from RHS'), test(content='foo {bar {foo} bar}', start=7, brackets=('\\{', '\\}'), expected=R(4, 5), msg='should find outer bracket from LHS'), test(content='foo {bar {foo} bar}', start=13, brackets=('\\{', '\\}'), expected=R(9, 10), msg='should find inner bracket'), test(content='foo {bar {foo} bar', start=16, brackets=('\\{', '\\}'), expected=R(4, 5), msg='should find outer if unbalanced outer'), test(content='foo {bar {foo} bar', start=12, brackets=('\\{', '\\}'), expected=R(9, 10), msg='should find inner if unbalanced outer'), test(content='foo {bar {foo} bar', start=4, brackets=('\\{', '\\}'), expected=R(4, 5), msg='should find bracket at caret position'), test(content='a\\{bc', start=2, brackets=('\\{', '\\}'), expected=None, msg='should not find escaped bracket at caret position'), test(content='a\\{bc', start=3, brackets=('\\{', '\\}'), expected=None, msg='should not find escaped bracket'), )