From 3cd0608067bdb507fdc80fd217f98df8335cb4a9 Mon Sep 17 00:00:00 2001 From: Matt Wean Date: Wed, 13 Aug 2014 21:25:08 -0700 Subject: [PATCH] Ignore command if no match is found --- file_scanner.py | 18 +++++-- jump_along_indent.py | 7 ++- tests/test_jump_next_offset_indent.py | 51 +++++++++++++++++++ ...ent.py => test_jump_prev_offset_indent.py} | 19 +++++-- 4 files changed, 85 insertions(+), 10 deletions(-) create mode 100644 tests/test_jump_next_offset_indent.py rename tests/{test_jump_offset_indent.py => test_jump_prev_offset_indent.py} (69%) diff --git a/file_scanner.py b/file_scanner.py index 185d844..594e092 100644 --- a/file_scanner.py +++ b/file_scanner.py @@ -10,8 +10,13 @@ def scan(self, direction = 'forward', indent_offset = 0): self.indent_offset = indent_offset if direction == 'forward': indent_match = self.search(self.search_str(), self.next_point()) or 0 - block_match = self.find_last_line_of_block() - return max([indent_match, block_match]) + possible_matches = [indent_match] + + if indent_offset == 0: + block_match = self.find_last_line_of_block() + possible_matches.append(block_match) + + return max(possible_matches) else: if self.previous_point() < 0: end = 0 @@ -19,8 +24,13 @@ def scan(self, direction = 'forward', indent_offset = 0): end = self.previous_point() indent_match = self.reverse_search(self.search_str(), 0, end) - block_match = self.find_first_line_of_block(end) - return min([indent_match, block_match]) + possible_matches = [indent_match] + + if indent_offset == 0: + block_match = self.find_first_line_of_block(end) + possible_matches.append(block_match) + + return min(possible_matches) def adapt_indent(self, indent_str): tab_size = self.view.settings().get("tab_size") diff --git a/jump_along_indent.py b/jump_along_indent.py index 8140e3e..10e5962 100644 --- a/jump_along_indent.py +++ b/jump_along_indent.py @@ -50,8 +50,13 @@ def deselect(self): def target_point(self, matched_row=None): matched_row = matched_row or self.scanner.scan(self.direction, self.indent_offset) + selection_offset = self.indent_offset + + if matched_row == self.view_helper.initial_row(): + selection_offset = 0 + matched_point_bol = self.view.text_point(matched_row, 0) - return self.view.text_point(matched_row, self.view_helper.target_column(matched_point_bol, self.indent_offset)) + return self.view.text_point(matched_row, self.view_helper.target_column(matched_point_bol, selection_offset)) class JumpNextIndentCommand(JumpIndentCommand, sublime_plugin.TextCommand): diff --git a/tests/test_jump_next_offset_indent.py b/tests/test_jump_next_offset_indent.py new file mode 100644 index 0000000..384b0be --- /dev/null +++ b/tests/test_jump_next_offset_indent.py @@ -0,0 +1,51 @@ +from helper import TestHelper + +class TestJumpNextOffsetIndent(TestHelper): + def command(self): + return 'jump_next_indent' + + def test_positive_indent_offset(self): + lines = [ + 'Lorem ipsum dolor sit amet', + 'Lorem ipsum dolor sit amet', + '', + ' Lorem ipsum dolor sit amet' + ] + starting_selection = [0, 0] + ending_selection = [57, 57] + + self.check_command(lines, starting_selection, ending_selection, indent_offset = 1) + + def test_negative_indent_offset(self): + lines = [ + ' Lorem ipsum dolor sit amet', + '', + 'Lorem ipsum dolor sit amet', + ' Lorem ipsum dolor sit amet' + ] + starting_selection = [2, 2] + ending_selection = [30, 30] + + self.check_command(lines, starting_selection, ending_selection, indent_offset = -1) + + def test_block_skip(self): + lines = [ + ' Lorem ipsum dolor sit amet', + 'Lorem ipsum dolor sit amet', + 'Lorem ipsum dolor sit amet' + ] + starting_selection = [2, 2] + ending_selection = [29, 29] + + self.check_command(lines, starting_selection, ending_selection, indent_offset = -1) + + def test_ignore_if_no_match(self): + lines = [ + 'Lorem ipsum dolor sit amet', + 'Lorem ipsum dolor sit amet', + ' Lorem ipsum dolor sit amet' + ] + starting_selection = [0, 0] + ending_selection = [0, 0] + + self.check_command(lines, starting_selection, ending_selection, indent_offset = 1) diff --git a/tests/test_jump_offset_indent.py b/tests/test_jump_prev_offset_indent.py similarity index 69% rename from tests/test_jump_offset_indent.py rename to tests/test_jump_prev_offset_indent.py index 3afe2fd..f453363 100644 --- a/tests/test_jump_offset_indent.py +++ b/tests/test_jump_prev_offset_indent.py @@ -1,6 +1,6 @@ from helper import TestHelper -class TestJumpOffsetIndent(TestHelper): +class TestJumpPrevOffsetIndent(TestHelper): def command(self): return 'jump_prev_indent' @@ -28,15 +28,24 @@ def test_negative_indent_offset(self): self.check_command(lines, starting_selection, ending_selection, indent_offset = -1) - def test_block_skip(self): lines = [ - '', 'Lorem ipsum dolor sit amet', 'Lorem ipsum dolor sit amet', ' Lorem ipsum dolor sit amet' ] - starting_selection = [57, 57] - ending_selection = [28, 28] + starting_selection = [56, 56] + ending_selection = [27, 27] self.check_command(lines, starting_selection, ending_selection, indent_offset = -1) + + def test_ignore_if_no_match(self): + lines = [ + ' Lorem ipsum dolor sit amet', + 'Lorem ipsum dolor sit amet', + 'Lorem ipsum dolor sit amet' + ] + starting_selection = [58, 58] + ending_selection = [58, 58] + + self.check_command(lines, starting_selection, ending_selection, indent_offset = 1)