mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-06 22:14:27 +08:00
Some refactorings and final tests for extract variable
This commit is contained in:
@@ -16,8 +16,8 @@ _EXPRESSION_PARTS = (
|
|||||||
_EXTRACT_USE_PARENT = _EXPRESSION_PARTS + ['trailer']
|
_EXTRACT_USE_PARENT = _EXPRESSION_PARTS + ['trailer']
|
||||||
_DEFINITION_SCOPES = ('suite', 'file_input')
|
_DEFINITION_SCOPES = ('suite', 'file_input')
|
||||||
_VARIABLE_EXCTRACTABLE = _EXPRESSION_PARTS + \
|
_VARIABLE_EXCTRACTABLE = _EXPRESSION_PARTS + \
|
||||||
('keyword atom name number string testlist_star_expr test_list test '
|
('atom testlist_star_expr testlist test lambdef lambdef_nocond '
|
||||||
'lambdef lambdef_nocond').split()
|
'keyword name number string fstring').split()
|
||||||
|
|
||||||
|
|
||||||
class ChangedFile(object):
|
class ChangedFile(object):
|
||||||
@@ -261,12 +261,16 @@ def extract_variable(grammar, path, module_node, new_name, pos, until_pos):
|
|||||||
|
|
||||||
nodes = _remove_unwanted_expression_nodes(parent_node, pos, until_pos)
|
nodes = _remove_unwanted_expression_nodes(parent_node, pos, until_pos)
|
||||||
|
|
||||||
|
debug.dbg('Extracting nodes: %s', nodes)
|
||||||
|
|
||||||
if any(node.type == 'name' and node.is_definition() for node in nodes):
|
if any(node.type == 'name' and node.is_definition() for node in nodes):
|
||||||
raise RefactoringError('Cannot extract a name that defines something')
|
raise RefactoringError('Cannot extract a name that defines something')
|
||||||
debug.dbg('Extracting nodes: %s', nodes)
|
|
||||||
if nodes[0].type not in _VARIABLE_EXCTRACTABLE:
|
if nodes[0].type not in _VARIABLE_EXCTRACTABLE:
|
||||||
raise RefactoringError('Cannot extract a "%s"' % nodes[0].type)
|
raise RefactoringError('Cannot extract a "%s"' % nodes[0].type)
|
||||||
|
|
||||||
|
# Now try to replace the nodes found with a variable and move the code
|
||||||
|
# before the current statement.
|
||||||
definition = _get_parent_definition(nodes[0])
|
definition = _get_parent_definition(nodes[0])
|
||||||
first_definition_leaf = definition.get_first_leaf()
|
first_definition_leaf = definition.get_first_leaf()
|
||||||
|
|
||||||
|
|||||||
@@ -226,3 +226,26 @@ yy = not foo or bar
|
|||||||
#? 4 text {'new_name': 'x', 'until_column': 7}
|
#? 4 text {'new_name': 'x', 'until_column': 7}
|
||||||
x = not foo
|
x = not foo
|
||||||
yy = x or bar
|
yy = x or bar
|
||||||
|
# -------------------------------------------------- augassign
|
||||||
|
yy = ()
|
||||||
|
#? 6 text {'new_name': 'x', 'until_column': 10}
|
||||||
|
yy += 3, 4
|
||||||
|
# ++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||||
|
yy = ()
|
||||||
|
#? 6 text {'new_name': 'x', 'until_column': 10}
|
||||||
|
x = 3, 4
|
||||||
|
yy += x
|
||||||
|
# -------------------------------------------------- if-else
|
||||||
|
#? 9 text {'new_name': 'x', 'until_column': 22}
|
||||||
|
yy = foo(a if y else b)
|
||||||
|
# ++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||||
|
#? 9 text {'new_name': 'x', 'until_column': 22}
|
||||||
|
x = a if y else b
|
||||||
|
yy = foo(x)
|
||||||
|
# -------------------------------------------------- lambda
|
||||||
|
#? 8 text {'new_name': 'x', 'until_column': 17}
|
||||||
|
y = foo(lambda x: 3, 5)
|
||||||
|
# ++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||||
|
#? 8 text {'new_name': 'x', 'until_column': 17}
|
||||||
|
x = lambda x: 3
|
||||||
|
y = foo(x, 5)
|
||||||
|
|||||||
Reference in New Issue
Block a user