Anonymous avatar Anonymous committed 1713f94

change_signature: added autodef argument to ArgumentReorderer

Comments (0)

Files changed (2)

rope/refactor/change_signature.py

 import rope.base.exceptions
 from rope.base import pyobjects, taskhandle, evaluate, worder, codeanalyze, utils
 from rope.base.change import ChangeContents, ChangeSet
-from rope.refactor import occurrences, sourceutils, functionutils
+from rope.refactor import occurrences, functionutils
 
 
 class ChangeSignature(object):
 
 class ArgumentReorderer(_ArgumentChanger):
 
-    def __init__(self, new_order):
+    def __init__(self, new_order, autodef=None):
         """Construct an `ArgumentReorderer`
 
         Note that the `new_order` is a list containing the new
 
         """
         self.new_order = new_order
+        self.autodef = autodef
 
     def change_definition_info(self, definition_info):
         new_args = list(definition_info.args_with_defaults)
         for new_index, index in enumerate(self.new_order):
             new_args[new_index] = definition_info.args_with_defaults[index]
+        seen_default = False
+        for index, (arg, default) in enumerate(list(new_args)):
+            if default is not None:
+                seen_default = True
+            if seen_default and default is None and self.autodef is not None:
+                new_args[index] = (arg, self.autodef)
         definition_info.args_with_defaults = new_args
 
 

ropetest/refactor/change_signature_test.py

         self.assertEquals('import mod1\nmod1.a_func(1)\n', self.mod.read())
         self.assertEquals('def a_func():\n    pass\n', mod1.read())
 
+    def test_reordering_and_automatic_defaults(self):
+        code = 'def f(p1, p2=2):\n' \
+               '    pass\n' \
+               'f(1, 2)\n'
+        self.mod.write(code)
+        signature = ChangeSignature(self.project, self.mod,
+                                    code.index('f('))
+        reorder = change_signature.ArgumentReorderer([1, 0], autodef='1')
+        signature.get_changes([reorder]).do()
+        expected = 'def f(p2=2, p1=1):\n' \
+                   '    pass\n' \
+                   'f(2, 1)\n'
+        self.assertEquals(expected, self.mod.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.