mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-07 22:44:27 +08:00
Fix extract issues when self is involved
This commit is contained in:
@@ -385,6 +385,7 @@ def extract_function(inference_state, path, module_context, name, pos, until_pos
|
|||||||
nodes = _find_nodes(module_context.tree_node, pos, until_pos)
|
nodes = _find_nodes(module_context.tree_node, pos, until_pos)
|
||||||
return_variables = []
|
return_variables = []
|
||||||
context = module_context.create_context(nodes[0])
|
context = module_context.create_context(nodes[0])
|
||||||
|
is_bound_method = context.is_bound_method()
|
||||||
params = list(_find_non_global_names(context, nodes))
|
params = list(_find_non_global_names(context, nodes))
|
||||||
|
|
||||||
dct = {}
|
dct = {}
|
||||||
@@ -393,7 +394,7 @@ def extract_function(inference_state, path, module_context, name, pos, until_pos
|
|||||||
if context.is_module():
|
if context.is_module():
|
||||||
insert_before_leaf = None # Leaf will be determined later
|
insert_before_leaf = None # Leaf will be determined later
|
||||||
else:
|
else:
|
||||||
node = _get_code_insertion_node(context)
|
node = _get_code_insertion_node(context.tree_node, is_bound_method)
|
||||||
insert_before_leaf = node.get_first_leaf()
|
insert_before_leaf = node.get_first_leaf()
|
||||||
if is_expression:
|
if is_expression:
|
||||||
code_block = 'return ' + _expression_nodes_to_string(nodes) + '\n'
|
code_block = 'return ' + _expression_nodes_to_string(nodes) + '\n'
|
||||||
@@ -403,15 +404,26 @@ def extract_function(inference_state, path, module_context, name, pos, until_pos
|
|||||||
code_block += '\nreturn ' + output_var_str + '\n'
|
code_block += '\nreturn ' + output_var_str + '\n'
|
||||||
|
|
||||||
decorator = ''
|
decorator = ''
|
||||||
if context.is_bound_method():
|
self_param = None
|
||||||
|
if is_bound_method:
|
||||||
|
if not function_is_staticmethod(context.tree_node):
|
||||||
|
function_param_names = context.get_value().get_param_names()
|
||||||
|
if len(function_param_names):
|
||||||
|
self_param = function_param_names[0].string_name
|
||||||
|
|
||||||
if function_is_classmethod(context.tree_node):
|
if function_is_classmethod(context.tree_node):
|
||||||
decorator = '@classmethod\n'
|
decorator = '@classmethod\n'
|
||||||
else:
|
else:
|
||||||
code_block += '\n'
|
code_block += '\n'
|
||||||
|
|
||||||
function_call = '%s(%s)' % (name, ', '.join(params))
|
function_code = '%sdef %s(%s):\n%s' % (
|
||||||
function_code = '%sdef %s:\n%s' % (decorator, function_call, indent_block(code_block))
|
decorator, name, ', '.join(params), indent_block(code_block)
|
||||||
|
)
|
||||||
|
|
||||||
|
function_call = '%s(%s)' % (
|
||||||
|
('' if self_param is None else self_param + '.') + name,
|
||||||
|
', '.join(p for p in params if p != self_param)
|
||||||
|
)
|
||||||
if is_expression:
|
if is_expression:
|
||||||
replacement = function_call
|
replacement = function_call
|
||||||
else:
|
else:
|
||||||
@@ -436,9 +448,8 @@ def _find_non_global_names(context, nodes):
|
|||||||
yield x
|
yield x
|
||||||
|
|
||||||
|
|
||||||
def _get_code_insertion_node(context):
|
def _get_code_insertion_node(node, is_bound_method):
|
||||||
node = context.tree_node
|
if not is_bound_method or function_is_staticmethod(node):
|
||||||
if not context.is_bound_method() or function_is_staticmethod(node):
|
|
||||||
while node.parent.type != 'file_input':
|
while node.parent.type != 'file_input':
|
||||||
node = node.parent
|
node = node.parent
|
||||||
|
|
||||||
|
|||||||
@@ -48,19 +48,19 @@ def f(x):
|
|||||||
class X:
|
class X:
|
||||||
def z(self): pass
|
def z(self): pass
|
||||||
|
|
||||||
def f(x):
|
def f(x, b):
|
||||||
#? 11 text {'new_name': 'ab'}
|
#? 11 text {'new_name': 'ab'}
|
||||||
return x + 1 * 2
|
return x + b * 2
|
||||||
# ++++++++++++++++++++++++++++++++++++++++++++++++++
|
# ++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||||
class X:
|
class X:
|
||||||
def z(self): pass
|
def z(self): pass
|
||||||
|
|
||||||
def ab(x):
|
def ab(x, b):
|
||||||
return x + 1 * 2
|
return x + b * 2
|
||||||
|
|
||||||
def f(x):
|
def f(x, b):
|
||||||
#? 11 text {'new_name': 'ab'}
|
#? 11 text {'new_name': 'ab'}
|
||||||
return ab(x)
|
return x.ab(b)
|
||||||
# -------------------------------------------------- in-classmethod-1
|
# -------------------------------------------------- in-classmethod-1
|
||||||
class X:
|
class X:
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|||||||
Reference in New Issue
Block a user