Commits

Anonymous committed c4df5fe

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

  • Participants
  • Parent commits 060b569

Comments (0)

Files changed (2)

File 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

File 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()