Skip to content

Commit

Permalink
Ignore command if no match is found
Browse files Browse the repository at this point in the history
  • Loading branch information
mwean committed Aug 14, 2014
1 parent 71251ba commit 3cd0608
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 10 deletions.
18 changes: 14 additions & 4 deletions file_scanner.py
Expand Up @@ -10,17 +10,27 @@ 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
else:
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")
Expand Down
7 changes: 6 additions & 1 deletion jump_along_indent.py
Expand Up @@ -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):
Expand Down
51 changes: 51 additions & 0 deletions 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)
@@ -1,6 +1,6 @@
from helper import TestHelper

class TestJumpOffsetIndent(TestHelper):
class TestJumpPrevOffsetIndent(TestHelper):
def command(self):
return 'jump_prev_indent'

Expand Down Expand Up @@ -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)

0 comments on commit 3cd0608

Please sign in to comment.