diff --git a/jedi/api/refactoring.py b/jedi/api/refactoring.py index cd51d261..f39aeb6d 100644 --- a/jedi/api/refactoring.py +++ b/jedi/api/refactoring.py @@ -384,12 +384,12 @@ def extract_function(inference_state, path, module_context, name, pos, until_pos is_expression = True nodes = _find_nodes(module_context.tree_node, pos, until_pos) return_variables = [] - params = _find_non_global_names(nodes) + context = module_context.create_context(nodes[0]) + params = list(_find_non_global_names(context, nodes)) dct = {} # Find variables # Is a class method / method - context = module_context.create_context(nodes[0]) if context.is_module(): insert_before_leaf = None # Leaf will be determined later else: @@ -424,8 +424,16 @@ def extract_function(inference_state, path, module_context, name, pos, until_pos return Refactoring(inference_state.grammar, file_to_node_changes) -def _find_non_global_names(nodes): - return [] +def _find_non_global_names(context, nodes): + for node in nodes: + try: + children = node.children + except AttributeError: + if node.type == 'name': + yield node.value + else: + for x in _find_non_global_names(context, children): # Python 2... + yield x def _get_code_insertion_node(context): diff --git a/test/refactor/extract_function.py b/test/refactor/extract_function.py index d3e0127f..3941fabe 100644 --- a/test/refactor/extract_function.py +++ b/test/refactor/extract_function.py @@ -3,11 +3,11 @@ test(100, (30 + b, c) + 1) # ++++++++++++++++++++++++++++++++++++++++++++++++++ #? 11 text {'new_name': 'a'} -def a(): +def a(b): return 30 + b -test(100, (a(), c) + 1) +test(100, (a(b), c) + 1) # -------------------------------------------------- in-module-2 #? 0 text {'new_name': 'ab'} 100 + 1 * 2 @@ -23,27 +23,27 @@ def f(x): #? 11 text {'new_name': 'ab'} return x + 1 * 2 # ++++++++++++++++++++++++++++++++++++++++++++++++++ -def ab(): +def ab(x): return x + 1 * 2 def f(x): #? 11 text {'new_name': 'ab'} - return ab() + return ab(x) # -------------------------------------------------- in-function-with-dec @classmethod def f(x): #? 11 text {'new_name': 'ab'} return x + 1 * 2 # ++++++++++++++++++++++++++++++++++++++++++++++++++ -def ab(): +def ab(x): return x + 1 * 2 @classmethod def f(x): #? 11 text {'new_name': 'ab'} - return ab() + return ab(x) # -------------------------------------------------- in-method-1 class X: def z(self): pass @@ -55,12 +55,12 @@ class X: class X: def z(self): pass - def ab(): + def ab(x): return x + 1 * 2 def f(x): #? 11 text {'new_name': 'ab'} - return ab() + return ab(x) # -------------------------------------------------- in-classmethod-1 class X: @classmethod @@ -98,11 +98,11 @@ class Ya(): #? 11 text {'new_name': 'f'} c = a + 2 # ++++++++++++++++++++++++++++++++++++++++++++++++++ -def f(): +def f(a): return a + 2 class Ya(): a = 3 #? 11 text {'new_name': 'f'} - c = f() + c = f(a)