Anonymous avatar Anonymous committed c7635f1

importutils: better import insertion line detection

Comments (0)

Files changed (3)

rope/contrib/autoimport.py

         module_imports = importutils.get_module_imports(
             self.project.pycore, pymodule)
         module_imports.add_import(importinfo)
-        offset = module_imports.get_changed_source().index(testmodname)
-        return code[:offset].count('\n') + 1
+        code = module_imports.get_changed_source()
+        offset = code.index(testmodname)
+        lineno = code.count('\n', 0, offset) + 1
+        return lineno

rope/refactor/importutils/module_imports.py

                                blank_lines=blanks))
 
     def _get_new_import_blanks(self):
-        if self.get_import_statements():
-            return 0
-        return 2
+        return 0
 
     def _get_new_import_lineno(self):
         imports = self.get_import_statements()
 
     def _first_import_line(self):
         nodes = self.pymodule.get_ast().body
-        first_child = 0
+        lineno = 0
         if self.pymodule.get_doc() is not None:
-            first_child = 1
-        if len(nodes) > first_child:
-            last_index = nodes[first_child].lineno
+            lineno = 1
+        if len(nodes) > lineno:
+            lineno = self.pymodule.logical_lines.logical_line_in(
+                nodes[lineno].lineno)[0]
         else:
-            last_index = self.pymodule.lines.length()
-        return last_index
+            lineno = self.pymodule.lines.length()
+        while lineno > 1:
+            line = self.pymodule.lines.get_line(lineno - 1)
+            if line.strip() == '' or line.rstrip().startswith('#'):
+                lineno -= 1
+            else:
+                break
+        return lineno
 
     def _compare_imports(self, stmt1, stmt2):
         str1 = stmt1.get_import_statement()
         nodes = self.pymodule.get_ast().body
         for index, node in enumerate(nodes):
             if isinstance(node, (ast.Import, ast.ImportFrom)):
-                end_line = self.lines.length() + 1
-                if index + 1 < len(nodes):
-                    end_line = nodes[index + 1].lineno
-                while self.lines.get_line(end_line - 1).strip() == '':
-                    end_line -= 1
+                lines = self.pymodule.logical_lines
+                end_line = lines.logical_line_in(node.lineno)[1] + 1
             if isinstance(node, ast.Import):
                 self.visit_import(node, end_line)
             if isinstance(node, ast.ImportFrom):

ropetest/contrib/autoimporttest.py

             'import mod1\n\nimport mod2\n')
         self.assertEquals(4, result)
 
+    def test_insertion_line_with_blank_lines(self):
+        result = self.importer.find_insertion_line(
+            'import mod1\n\n# comment\n')
+        self.assertEquals(2, result)
+
 
 class AutoImportObservingTest(unittest.TestCase):
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.