Commits

emax committed 8fd7dac

fixed bug with decorated function in RopeOrganizeImport

Organize import doesn't work properly if function args has same name as the decorator name

Comments (0)

Files changed (2)

rope/base/pyscopes.py

               (new_scope.get_kind() == 'Module' or
                self._get_scope_indents(new_scope) <= line_indents):
             current_scope = new_scope
+            
+            if current_scope.get_start() == lineno and \
+               current_scope.get_kind() == 'Function' and \
+               self._is_decorator(lineno):
+                return current_scope.parent
+
             if current_scope.get_start() == lineno and \
                current_scope.get_kind() != 'Module':
                 return current_scope
                 else:
                     break
         return current_scope
-
+    
+    def _is_decorator(self, lineno):
+        line = self.pymodule.lines.get_line(lineno)
+        return line.lstrip().startswith("@")
+ 
     def _is_empty_line(self, lineno):
         line = self.lines.get_line(lineno)
         return line.strip() == '' or line.lstrip().startswith('#')

ropetest/refactor/importutilstest.py

         pymod = self.pycore.resource_to_pyobject(self.mod1)
         self.assertEquals('', self.import_tools.organize_imports(pymod))
 
+    def test_organizing_imports_for_decorated_functions(self):
+        self.mod1.write(u"from mod import mydecorator\nclass Test(object):\n    @mydecorator\n    def myfunc(myvar):        pass")
+        pymod1 = self.pycore.resource_to_pyobject(self.mod1)
+        self.assertEquals(self.import_tools.organize_imports(pymod1), \
+                u"from mod import mydecorator\n\n\nclass Test(object):\n    @mydecorator\n    def myfunc(myvar):        pass")
+
+    def test_organizing_imports_for_decorated_functions1(self):
+        self.mod1.write(u"from mod import mydecorator\nclass Test(object):\n    @mydecorator\n    def myfunc(mydecorator):        pass")
+        pymod1 = self.pycore.resource_to_pyobject(self.mod1)
+        self.assertEquals(self.import_tools.organize_imports(pymod1), \
+                u"from mod import mydecorator\n\n\nclass Test(object):\n    @mydecorator\n    def myfunc(mydecorator):        pass")
+
+    def test_organizing_imports_for_decorated_functions3(self):
+        self.mod1.write(u"from mod import mydecorator, mydecorator2\nclass Test(object):\n    @mydecorator\n    @mydecorator2\n    def myfunc(mydecorator):        pass")
+        pymod1 = self.pycore.resource_to_pyobject(self.mod1)
+        self.assertEquals(self.import_tools.organize_imports(pymod1), \
+                    u"from mod import mydecorator, mydecorator2\n\n\nclass Test(object):\n    @mydecorator\n    @mydecorator2\n    def myfunc(mydecorator):        pass")
+
+
     def test_removing_self_imports(self):
         self.mod.write('import mod\nmod.a_var = 1\nprint(mod.a_var)\n')
         pymod = self.pycore.resource_to_pyobject(self.mod)