Skip to content

Commit

Permalink
Remove blank lines along with import statements
Browse files Browse the repository at this point in the history
  • Loading branch information
soupytwist authored and mcepl committed Jan 29, 2016
1 parent ed88f7b commit 9650636
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
12 changes: 9 additions & 3 deletions rope/refactor/importutils/module_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,19 @@ def _get_location(self, stmt):
def _remove_imports(self, imports):
lines = self.pymodule.source_code.splitlines(True)
after_removing = []
first_import_line = self._first_import_line()
last_index = 0
for stmt in imports:
start, end = stmt.get_old_location()
after_removing.extend(lines[last_index:start - 1])
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
after_removing.extend(lines[last_index:start - 1 - blank_lines])
last_index = end - 1
for i in range(start, end):
after_removing.append('')
after_removing.extend(lines[last_index:])
return after_removing

Expand Down
11 changes: 11 additions & 0 deletions ropetest/refactor/importutilstest.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,17 @@ def test_sorting_imports_moving_to_top_and_module_docs2(self):
'import mod\n\n\ndef f():\n print(mod)\n',
self.import_tools.sort_imports(pymod))

def test_get_changed_source_preserves_blank_lines(self):
self.mod.write(
'__author__ = "author"\n\nimport aaa\n\nimport bbb\n\n'
'def f():\n print(mod)\n')
pymod = self.project.get_module('mod')
module_with_imports = self.import_tools.module_imports(pymod)
self.assertEquals(
'import aaa\n\nimport bbb\n\n__author__ = "author"\n\n'
'def f():\n print(mod)\n',
module_with_imports.get_changed_source())

def test_sorting_future_imports(self):
self.mod.write('import os\nfrom __future__ import devision\n')
pymod = self.project.get_module('mod')
Expand Down

0 comments on commit 9650636

Please sign in to comment.