Skip to content

Commit

Permalink
Extract common function for counting blank lines
Browse files Browse the repository at this point in the history
  • Loading branch information
soupytwist authored and mcepl committed Jan 29, 2016
1 parent 9650636 commit bd89775
Showing 1 changed file with 20 additions and 35 deletions.
55 changes: 20 additions & 35 deletions rope/refactor/importutils/module_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,16 @@ def _remove_imports(self, imports):
start, end = stmt.get_old_location()
blank_lines = 0
if start != first_import_line:
for lineno in range(start - 2, last_index - 1, -1):
if lines[lineno].strip() == '':
blank_lines += 1
else:
break
blank_lines = _count_blank_lines(lines.__getitem__, start - 2,
last_index - 1, -1)
after_removing.extend(lines[last_index:start - 1 - blank_lines])
last_index = end - 1
after_removing.extend(lines[last_index:])
return after_removing

def _first_non_blank_line(self, lines, lineno):
result = lineno
for line in lines[lineno:]:
if line.strip() == '':
result += 1
else:
break
return result
return lineno + _count_blank_lines(lines.__getitem__, lineno,
len(lines))

def add_import(self, import_info):
visitor = actions.AddingVisitor(self.project, [import_info])
Expand Down Expand Up @@ -236,13 +228,9 @@ def _first_import_line(self):
nodes[lineno].lineno)[0]
else:
lineno = self.pymodule.lines.length()
while lineno > 1:
line = self.pymodule.lines.get_line(lineno - 1)
if line.strip() == '':
lineno -= 1
else:
break
return lineno

return lineno - _count_blank_lines(self.pymodule.lines.get_line,
lineno - 1, 1, -1)

def _get_import_name(self, import_stmt):
import_info = import_stmt.import_info
Expand Down Expand Up @@ -292,6 +280,16 @@ def remove_pyname(self, pyname):
import_stmt.accept(visitor)


def _count_blank_lines(get_line, start, end, step=1):
count = 0
for idx in range(start, end, step):
if get_line(idx).strip() == '':
count += 1
else:
break
return count


class _OneTimeSelector(object):

def __init__(self, names):
Expand Down Expand Up @@ -439,24 +437,11 @@ def visit_import(self, node, end_line):
self.imports.append(import_statement)

def _count_empty_lines_before(self, lineno):
result = 0
for current in range(lineno - 1, 0, -1):
line = self.lines.get_line(current)
if line.strip() == '':
result += 1
else:
break
return result
return _count_blank_lines(self.lines.get_line, lineno - 1, 0, -1)

def _count_empty_lines_after(self, lineno):
result = 0
for current in range(lineno + 1, self.lines.length()):
line = self.lines.get_line(current)
if line.strip() == '':
result += 1
else:
break
return result
return _count_blank_lines(self.lines.get_line, lineno + 1,
self.lines.length())

def get_separating_line_count(self):
if not self.imports:
Expand Down

0 comments on commit bd89775

Please sign in to comment.