Handle params better for extract variable

This commit is contained in:
Dave Halter
2020-02-17 09:55:11 +01:00
parent d1f7400829
commit ab4fe548f2
2 changed files with 37 additions and 0 deletions

View File

@@ -13,6 +13,7 @@ _INLINE_NEEDS_BRACKET = (
'or_test and_test not_test comparison' 'or_test and_test not_test comparison'
).split() ).split()
_DEFINITION_SCOPES = ('suite', 'file_input') _DEFINITION_SCOPES = ('suite', 'file_input')
_NON_EXCTRACABLE = ('param', )
class ChangedFile(object): class ChangedFile(object):
@@ -219,6 +220,10 @@ def inline(grammar, names):
def extract_variable(grammar, path, module_node, new_name, pos, until_pos): def extract_variable(grammar, path, module_node, new_name, pos, until_pos):
start_leaf = module_node.get_leaf_for_position(pos, include_prefixes=True) 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: if until_pos is None:
node = start_leaf node = start_leaf
if node.type == 'operator': 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() end_leaf = end_leaf.get_previous_leaf()
if end_leaf is None: if end_leaf is None:
raise RefactoringError('Cannot extract anything from that') 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) definition = _get_parent_definition(node)
first_definition_leaf = definition.get_first_leaf() first_definition_leaf = definition.get_first_leaf()

View File

@@ -16,6 +16,13 @@ def test():
#? 25 text {'new_name': 'a'} #? 25 text {'new_name': 'a'}
a = 30 + b a = 30 + b
return test(100, (a, c) + 1) 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 # -------------------------------------------------- multiline-1
def test(): def test():
#? 30 text {'new_name': 'x'} #? 30 text {'new_name': 'x'}
@@ -38,3 +45,24 @@ def test():
x = 30 + b x = 30 + b
return test(1, (x, c) return test(1, (x, c)
+ 1) + 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