From ab4fe548f2b5a9619403a48f69428869a2f0df35 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Mon, 17 Feb 2020 09:55:11 +0100 Subject: [PATCH] Handle params better for extract variable --- jedi/api/refactoring.py | 9 +++++++++ test/refactor/extract_variable.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/jedi/api/refactoring.py b/jedi/api/refactoring.py index b5d71de8..7f8c5cb0 100644 --- a/jedi/api/refactoring.py +++ b/jedi/api/refactoring.py @@ -13,6 +13,7 @@ _INLINE_NEEDS_BRACKET = ( 'or_test and_test not_test comparison' ).split() _DEFINITION_SCOPES = ('suite', 'file_input') +_NON_EXCTRACABLE = ('param', ) class ChangedFile(object): @@ -219,6 +220,10 @@ def inline(grammar, names): def extract_variable(grammar, path, module_node, new_name, pos, until_pos): start_leaf = module_node.get_leaf_for_position(pos, include_prefixes=True) + if start_leaf.type == 'operator': + next_leaf = start_leaf.get_next_leaf() + if next_leaf is not None and next_leaf.start_pos == pos: + start_leaf = next_leaf if until_pos is None: node = start_leaf if node.type == 'operator': @@ -234,6 +239,10 @@ def extract_variable(grammar, path, module_node, new_name, pos, until_pos): end_leaf = end_leaf.get_previous_leaf() if end_leaf is None: raise RefactoringError('Cannot extract anything from that') + if any(node.type == 'name' and node.is_definition() for node in nodes): + raise RefactoringError('Cannot extract a definition of a name') + if any(node.type in _NON_EXCTRACABLE for node in nodes): + raise RefactoringError('Cannot extract a %s' % node.type) definition = _get_parent_definition(node) first_definition_leaf = definition.get_first_leaf() diff --git a/test/refactor/extract_variable.py b/test/refactor/extract_variable.py index 61cce967..78d00139 100644 --- a/test/refactor/extract_variable.py +++ b/test/refactor/extract_variable.py @@ -16,6 +16,13 @@ def test(): #? 25 text {'new_name': 'a'} a = 30 + b return test(100, (a, c) + 1) +# -------------------------------------------------- simple-3 +#? 13 text {'new_name': 'zzx.x'} +test(100, {1 |1: 2 + 3}) +# ++++++++++++++++++++++++++++++++++++++++++++++++++ +#? 13 text {'new_name': 'zzx.x'} +zzx.x = 1 |1 +test(100, {zzx.x: 2 + 3}) # -------------------------------------------------- multiline-1 def test(): #? 30 text {'new_name': 'x'} @@ -38,3 +45,24 @@ def test(): x = 30 + b return test(1, (x, c) + 1) +# -------------------------------------------------- for-param-error-1 +#? 10 error {'new_name': 'x'} +def test(p1): + return +# ++++++++++++++++++++++++++++++++++++++++++++++++++ +Cannot extract a definition of a name +# -------------------------------------------------- for-param-error-2 +#? 12 error {'new_name': 'x'} +def test(p1= 3): + return +# ++++++++++++++++++++++++++++++++++++++++++++++++++ +Cannot extract a param +# -------------------------------------------------- for-param-1 +#? 12 text {'new_name': 'x'} +def test(p1=20): + return +# ++++++++++++++++++++++++++++++++++++++++++++++++++ +#? 12 text {'new_name': 'x'} +x = 20 +def test(p1=x): + return