Skip to content

Commit

Permalink
Merge pull request #68 from Kronuz/blocks-fix
Browse files Browse the repository at this point in the history
Fix BaseBlockCommand to work with block at the boundaries of the view
  • Loading branch information
mgaitan committed Feb 27, 2014
2 parents 1d8407a + 079849d commit 87c3494
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
import sublime_plugin


class BaseBlockCommand(sublime_plugin.TextCommand):
class LineIndexError(Exception):
pass


class BaseBlockCommand(sublime_plugin.TextCommand):
def _get_row_text(self, row):

if row < 0 or row > self.view.rowcol(self.view.size())[0]:
raise RuntimeError('Cannot find table bounds.')
raise LineIndexError('Cannot find table bounds.')

point = self.view.text_point(row, 0)
region = self.view.line(point)
Expand All @@ -28,24 +31,25 @@ def get_block_bounds(self):
try:
while self._get_row_text(upper - 1).strip():
upper -= 1

except Exception as e:
print(e)
except LineIndexError:
pass
else:
upper += 1

try:
while self._get_row_text(lower + 1).strip():
lower += 1
except Exception as e:
print(e)
except LineIndexError:
pass
else:
lower -= 1

block_region = Region(self.view.text_point(upper - 1, 0),
self.view.text_point(lower + 2, 0))
lines = [self.view.substr(region) for region in self.view.lines(block_region)]
indent = re.match('^(\s*).*$', self._get_row_text(upper - 1)).group(1)
try:
row_text = self._get_row_text(upper - 1)
except LineIndexError:
row_text = ''
indent = re.match('^(\s*).*$', row_text).group(1)
return block_region, lines, indent

0 comments on commit 87c3494

Please sign in to comment.