def _check_for_non_extractables(nodes): for n in nodes: try: children = n.children except AttributeError: if n.value == 'return': raise RefactoringError( 'Can only extract return statements if they are at the end.') if n.value == 'yield': raise RefactoringError('Cannot extract yield statements.') else: _check_for_non_extractables(children)
def apply(self): if self._from_path is None: raise RefactoringError( 'Cannot apply a refactoring on a Script with path=None') with open(self._from_path, 'w', newline='') as f: f.write(self.get_new_code())
def extract_variable(inference_state, path, module_node, name, pos, until_pos): nodes = _find_nodes(module_node, pos, until_pos) debug.dbg('Extracting nodes: %s', nodes) is_expression, message = _is_expression_with_error(nodes) if not is_expression: raise RefactoringError(message) generated_code = name + ' = ' + _expression_nodes_to_string(nodes) file_to_node_changes = {path: _replace(nodes, name, generated_code, pos)} return Refactoring(inference_state, file_to_node_changes)
def _find_nodes(module_node, pos, until_pos): """ Looks up a module and tries to find the appropriate amount of nodes that are in there. """ start_node = module_node.get_leaf_for_position(pos, include_prefixes=True) if until_pos is None: if start_node.type == 'operator': next_leaf = start_node.get_next_leaf() if next_leaf is not None and next_leaf.start_pos == pos: start_node = next_leaf if _is_not_extractable_syntax(start_node): start_node = start_node.parent if start_node.parent.type == 'trailer': start_node = start_node.parent.parent while start_node.parent.type in EXPRESSION_PARTS: start_node = start_node.parent nodes = [start_node] else: # Get the next leaf if we are at the end of a leaf if start_node.end_pos == pos: next_leaf = start_node.get_next_leaf() if next_leaf is not None: start_node = next_leaf # Some syntax is not exactable, just use its parent if _is_not_extractable_syntax(start_node): start_node = start_node.parent # Find the end end_leaf = module_node.get_leaf_for_position(until_pos, include_prefixes=True) if end_leaf.start_pos > until_pos: end_leaf = end_leaf.get_previous_leaf() if end_leaf is None: raise RefactoringError('Cannot extract anything from that') parent_node = start_node while parent_node.end_pos < end_leaf.end_pos: parent_node = parent_node.parent nodes = _remove_unwanted_expression_nodes(parent_node, pos, until_pos) # If the user marks just a return statement, we return the expression # instead of the whole statement, because the user obviously wants to # extract that part. if len(nodes) == 1 and start_node.type in ('return_stmt', 'yield_expr'): return [nodes[0].children[1]] return nodes
def rename(inference_state, definitions, new_name): file_renames = set() file_tree_name_map = {} if not definitions: raise RefactoringError("There is no name under the cursor") for d in definitions: tree_name = d._name.tree_name if d.type == 'module' and tree_name is None: file_renames.add(_calculate_rename(d.module_path, new_name)) else: # This private access is ok in a way. It's not public to # protect Jedi users from seeing it. if tree_name is not None: fmap = file_tree_name_map.setdefault(d.module_path, {}) fmap[tree_name] = tree_name.prefix + new_name return Refactoring(inference_state, file_tree_name_map, file_renames)
def inline(inference_state, names): if not names: raise RefactoringError("There is no name under the cursor") if any(n.api_type == 'module' for n in names): raise RefactoringError("Cannot inline imports or modules") if any(n.tree_name is None for n in names): raise RefactoringError("Cannot inline builtins/extensions") definitions = [n for n in names if n.tree_name.is_definition()] if len(definitions) == 0: raise RefactoringError("No definition found to inline") if len(definitions) > 1: raise RefactoringError( "Cannot inline a name with multiple definitions") tree_name = definitions[0].tree_name expr_stmt = tree_name.get_definition() if expr_stmt.type != 'expr_stmt': type_ = dict( funcdef='function', classdef='class', ).get(expr_stmt.type, expr_stmt.type) raise RefactoringError("Cannot inline a %s" % type_) if len(expr_stmt.get_defined_names(include_setitem=True)) > 1: raise RefactoringError( "Cannot inline a statement with multiple definitions") first_child = expr_stmt.children[1] if first_child.type == 'annassign' and len(first_child.children) == 4: first_child = first_child.children[2] if first_child != '=': if first_child.type == 'annassign': raise RefactoringError( 'Cannot inline a statement that is defined by an annotation') else: raise RefactoringError('Cannot inline a statement with "%s"' % first_child.get_code(include_prefix=False)) rhs = expr_stmt.get_rhs() replace_code = rhs.get_code(include_prefix=False) references = [n for n in names if not n.tree_name.is_definition()] file_to_node_changes = {} for name in references: tree_name = name.tree_name path = name.get_root_context().py__file__() s = replace_code if rhs.type == 'testlist_star_expr' \ or tree_name.parent.type in EXPRESSION_PARTS \ or tree_name.parent.type == 'trailer' \ and tree_name.parent.get_next_sibling() is not None: s = '(' + replace_code + ')' of_path = file_to_node_changes.setdefault(path, {}) n = tree_name prefix = n.prefix par = n.parent if par.type == 'trailer' and par.children[0] == '.': prefix = par.parent.children[0].prefix n = par for some_node in par.parent.children[:par.parent.children.index(par )]: of_path[some_node] = '' of_path[n] = prefix + s path = definitions[0].get_root_context().py__file__() changes = file_to_node_changes.setdefault(path, {}) changes[expr_stmt] = _remove_indent_of_prefix( expr_stmt.get_first_leaf().prefix) next_leaf = expr_stmt.get_next_leaf() # Most of the time we have to remove the newline at the end of the # statement, but if there's a comment we might not need to. if next_leaf.prefix.strip(' \t') == '' \ and (next_leaf.type == 'newline' or next_leaf == ';'): changes[next_leaf] = '' return Refactoring(inference_state, file_to_node_changes)