Anonymous avatar Anonymous committed c4df5fe

move refactoring works well if target file contains encoding cookie but no imports

Comments (0)

Files changed (2)

rope/refactor/move.py

 
         module_with_imports = self.import_tools.module_imports(pymodule)
         source = pymodule.source_code
+        lineno = 0
         if module_with_imports.imports:
-            start = pymodule.lines.get_line_end(
-                module_with_imports.imports[-1].end_line - 1)
-            result = source[:start + 1] + '\n\n'
+            lineno = module_with_imports.imports[-1].end_line - 1
         else:
-            result = ''
-            start = -1
-        result += moving + source[start + 1:]
+            while lineno < pymodule.lines.length() and \
+                  pymodule.lines.get_line(lineno + 1).lstrip().startswith('#'):
+                lineno += 1
+        if lineno > 0:
+            cut = pymodule.lines.get_line_end(lineno) + 1
+            result = source[:cut] + '\n\n' + moving + source[cut:]
+        else:
+            result = moving + source
 
         # Organizing imports
         source = result

ropetest/refactor/movetest.py

         dir = self.project.root.create_folder('dir')
         mover = move.create_move(self.project, dir)
 
+    def test_moving_to_a_module_with_encoding_cookie(self):
+        code1 = '# -*- coding: utf-8 -*-'
+        self.mod1.write(code1)
+        code2 = 'def f(): pass\n'
+        self.mod2.write(code2)
+        mover = move.create_move(self.project, self.mod2,
+                                 code2.index('f()') + 1)
+        self.project.do(mover.get_changes(self.mod1))
+        expected = '%s\n%s' % (code1, code2)
+        self.assertEquals(expected, self.mod1.read())
+
 
 if __name__ == '__main__':
     unittest.main()
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.