Ejemplo n.º 1
0
 def _extract_function(self, line, column, new_name, until_line=None, until_column=None):
     if until_line is None and until_column is None:
         until_pos = None
     else:
         if until_line is None:
             until_line = line
         if until_column is None:
             until_column = len(self._code_lines[until_line - 1])
         until_pos = until_line, until_column
     return extract_function(
         self._inference_state, self.path, self._get_module_context(),
         new_name, (line, column), until_pos
     )
Ejemplo n.º 2
0
    def extract_function(self,
                         line,
                         column,
                         *,
                         new_name,
                         until_line=None,
                         until_column=None):
        """
        Moves an expression to a new function.

        For example if you have the cursor on ``foo`` and provide a
        ``new_name`` called ``bar``::

            global_var = 3

            def x():
                foo = 3.1
                x = int(foo + 1 + global_var)

        the code above will become::

            global_var = 3

            def bar(foo):
                return int(foo + 1 + global_var)

            def x():
                foo = 3.1
                x = bar(foo)

        :param new_name: The expression under the cursor will be replaced with
            a function with this name.
        :param int until_line: The the selection range ends at this line, when
            omitted, Jedi will be clever and try to define the range itself.
        :param int until_column: The the selection range ends at this column, when
            omitted, Jedi will be clever and try to define the range itself.
        :raises: :exc:`.RefactoringError`
        :rtype: :class:`.Refactoring`
        """
        if until_line is None and until_column is None:
            until_pos = None
        else:
            if until_line is None:
                until_line = line
            if until_column is None:
                until_column = len(self._code_lines[until_line - 1])
            until_pos = until_line, until_column
        return extract_function(self._inference_state, self.path,
                                self._get_module_context(), new_name,
                                (line, column), until_pos)