Commits

zjes committed 6771411

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

Comments (0)

Files changed (3)

rope/base/builtins.py

     def get_param_names(self, special_args=True):
         return self.argnames
 
+    @utils.saveit
+    def get_attributes(self):
+        result = _object_attributes(self.builtin.__class__, self)
+        return result
+
 
 class BuiltinUnknown(_BuiltinElement, pyobjects.PyObject):
 
         if inspect.isclass(child):
             pyobject = BuiltinClass(child, {}, parent=parent)
         elif inspect.isroutine(child):
-            pyobject = BuiltinFunction(builtin=child, parent=parent)
+            if inspect.ismethoddescriptor(child) and "__weakref__" in dir(obj):
+                try:
+                    bobj = child.__get__(obj.__weakref__.__objclass__())
+                except:
+                    bobj = child
+                pyobject = BuiltinFunction(builtin=bobj, parent=parent)
+            else:
+                pyobject = BuiltinFunction(builtin=child, parent=parent)
         else:
             pyobject = BuiltinUnknown(builtin=child)
         attributes[name] = BuiltinName(pyobject)

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
         pymodule = self.pycore.get_string_module(source, dest)

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