Commits

Anonymous committed 28bfd7e

functionutils: moved code analysis to Worder

  • Participants
  • Parent commits f62bbba

Comments (0)

Files changed (3)

File rope/base/worder.py

 import bisect
-import re
 
 import rope.base.simplify
 
     def get_assignment_type(self, offset):
         return self.code_finder.get_assignment_type(offset)
 
+    def get_function_and_args_in_header(self, offset):
+        return self.code_finder.get_function_and_args_in_header(offset)
+
+    def find_function_offset(self, offset):
+        return self.code_finder.find_function_offset(offset)
+
 
 class _RealFinder(object):
 
         # XXX: only handling (x, y) = value
         return offset < equals_offset and \
                self.code[start:parens_start].strip() == ''
+
+    def get_function_and_args_in_header(self, offset):
+        offset = self.find_function_offset(offset)
+        offset = self._get_line_end(offset)
+        colon = self.code.rindex(':', 0, offset)
+        rparens = self.code.rindex(')', 0, colon)
+        lparens = self._find_parens_start(rparens)
+        end = self._find_last_non_space_char(lparens - 1)
+        start = self._find_primary_start(end)
+        return self.raw[start:colon]
+
+    def find_function_offset(self, offset):
+        while True:
+            offset = self.code.index('def ', offset)
+            if offset == 0 or not self._is_id_char(offset - 1):
+                break
+            offset += 1
+        def_ = offset + 4
+        return self._find_first_non_space_char(def_)

File rope/refactor/functionutils.py

 import rope.base.exceptions
 import rope.base.pyobjects
-from rope.base import codeanalyze, worder
+from rope.base import worder
 
 
 class DefinitionInfo(object):
     @staticmethod
     def read(pyfunction):
         pymodule = pyfunction.get_module()
-        source = pymodule.source_code
-        lines = pymodule.lines
+        word_finder = worder.Worder(pymodule.source_code)
         lineno = pyfunction.get_ast().lineno
-        if pyfunction.decorators:
-            lineno = max(lineno, getattr(pyfunction.decorators[-1],
-                                         'lineno', lineno))
-            # XXX: problems when 'def ' happens inside a string
-            if 'def ' not in lines.get_line(lineno):
-                lineno += 1
-        start_line, end_line = pymodule.logical_lines.logical_line_in(lineno)
-        start = lines.get_line_start(start_line)
-        end = lines.get_line_end(end_line)
-        start = pymodule.source_code.find('def ', start) + 4
-        # XXX: problems for one-liners with a colon in a str
-        end = pymodule.source_code.rfind(':', start, end)
-        return DefinitionInfo._read(pyfunction, pymodule.source_code[start:end])
+        start = pymodule.lines.get_line_start(lineno)
+        call = word_finder.get_function_and_args_in_header(start)
+        return DefinitionInfo._read(pyfunction, call)
 
 
 class CallInfo(object):

File rope/refactor/inline.py

     def _function_location(self):
         pymodule, lineno = self.pyname.get_definition_location()
         resource = pymodule.get_resource()
-        start = pymodule.logical_lines.logical_line_in(lineno)[0]
-        start_offset = pymodule.lines.get_line_start(start)
-        def_ = pymodule.source_code.index('def', start_offset)
-        offset = def_ + 4
-        while pymodule.source_code[offset].isspace():
-            offset += 1
+        start = pymodule.lines.get_line_start(lineno)
+        word_finder = worder.Worder(pymodule.source_code)
+        offset = word_finder.find_function_offset(start)
         return resource, offset
 
     def get_changes(self, **kwds):