def suitify(parent): """ Turn the stuff after the first colon in parent's children into a suite, if it wasn't already """ for node in parent.children: if node.type == syms.suite: # already in the prefered format, do nothing return # One-liners have no suite node, we have to fake one up for i, node in enumerate(parent.children): if node.type == token.COLON: break else: raise ValueError(u"No class suite and no ':'!") # Move everything into a suite node suite = Node(syms.suite, [ Newline(), Leaf(token.INDENT, indentation(node) + indentation_step(node)) ]) one_node = parent.children[i + 1] one_node.remove() one_node.prefix = u'' suite.append_child(one_node) parent.append_child(suite)
def suitify(parent): u""" Turn the stuff after the first colon in parent's children into a suite, if it wasn't already """ for node in parent.children: if node.type == syms.suite: # already in the prefered format, do nothing return # One-liners have no suite node, we have to fake one up for i, node in enumerate(parent.children): if node.type == token.COLON: break else: raise ValueError(u"No class suite and no ':'!") # Move everything into a suite node suite = Node(syms.suite, [Newline(), Leaf(token.INDENT, indentation(node) + indentation_step(node))]) one_node = parent.children[i+1] one_node.remove() one_node.prefix = u'' suite.append_child(one_node) parent.append_child(suite)
def fix_leaves(self, node_to_split): parent_depth = find_indentation(node_to_split) new_indent = "%s%s" % (' ' * 4, parent_depth) # For now, just indent additional lines by 4 more spaces child_leaves = [] combined_prefix = "" prev_leaf = None for index, leaf in enumerate(node_to_split.leaves()): if index and leaf.prefix.count('#'): if not combined_prefix: combined_prefix = "%s#" % new_indent combined_prefix += leaf.prefix.split('#')[-1] # We want to strip all newlines so we can properly insert newlines # where they should be if leaf.type != token.NEWLINE: if leaf.prefix.count('\n') and index: # If the line contains a newline, we need to strip all # whitespace since there were leading indent spaces if (prev_leaf and prev_leaf.type in [token.DOT, token.LPAR] or leaf.type in [token.RPAR]): leaf.prefix = "" else: leaf.prefix = " " # Append any trailing inline comments to the combined # prefix child_leaves.append(leaf) prev_leaf = leaf # Like TextWrapper, but for nodes. We split on MAX_CHARS - 1 since we # may need to insert a leading parenth. It's not great, but it would be # hard to do properly. split_leaves = wrap_leaves(child_leaves, width=MAX_CHARS - 1, subsequent_indent=new_indent) new_node = Node(node_to_split.type, []) # We want to keep track of if we are breaking inside a parenth open_count = 0 need_parens = False for line_index, curr_line_nodes in enumerate(split_leaves): for node_index, curr_line_node in enumerate(curr_line_nodes): if line_index and not node_index: # If first node in non-first line, reset prefix since there # may have been spaces previously curr_line_node.prefix = new_indent new_node.append_child(curr_line_node) if curr_line_node.type in OPENING_TOKENS: open_count += 1 if curr_line_node.type in CLOSING_TOKENS: open_count -= 1 if line_index != len(split_leaves) - 1: # Don't add newline at the end since it it part of the next # sibling new_node.append_child(Leaf(token.NEWLINE, '\n')) # Checks if we ended a line without being surrounded by parens if open_count <= 0: need_parens = True if need_parens: # Parenthesize the parent if we're not inside parenths, braces, # brackets, since we inserted newlines between leaves parenth_before_equals = Leaf(token.EQUAL, "=") in split_leaves[0] self.parenthesize_parent(new_node, parenth_before_equals) node_to_split.replace(new_node) return combined_prefix
def fix_leaves(self, node_to_split): if IS_26: node_to_split = add_leaves_method(node_to_split) parent_depth = find_indentation(node_to_split) new_indent = "%s%s" % (' ' * 4, parent_depth) # For now, just indent additional lines by 4 more spaces child_leaves = [] combined_prefix = "" prev_leaf = None for index, leaf in enumerate(node_to_split.leaves()): if index and leaf.prefix.count('#'): if not combined_prefix: combined_prefix = "%s#" % new_indent combined_prefix += leaf.prefix.split('#')[-1] # We want to strip all newlines so we can properly insert newlines # where they should be if leaf.type != token.NEWLINE: if leaf.prefix.count('\n') and index: # If the line contains a newline, we need to strip all # whitespace since there were leading indent spaces if (prev_leaf and prev_leaf.type in [token.DOT, token.LPAR] or leaf.type in [token.RPAR]): leaf.prefix = "" else: leaf.prefix = " " # Append any trailing inline comments to the combined # prefix child_leaves.append(leaf) prev_leaf = leaf # Like TextWrapper, but for nodes. We split on MAX_CHARS - 1 since we # may need to insert a leading parenth. It's not great, but it would be # hard to do properly. split_leaves = wrap_leaves(child_leaves, width=MAX_CHARS - 1, subsequent_indent=new_indent) new_node = Node(node_to_split.type, []) # We want to keep track of if we are breaking inside a parenth open_count = 0 need_parens = False for line_index, curr_line_nodes in enumerate(split_leaves): for node_index, curr_line_node in enumerate(curr_line_nodes): if line_index and not node_index: # If first node in non-first line, reset prefix since there # may have been spaces previously curr_line_node.prefix = new_indent new_node.append_child(curr_line_node) if curr_line_node.type in OPENING_TOKENS: open_count += 1 if curr_line_node.type in CLOSING_TOKENS: open_count -= 1 if line_index != len(split_leaves) - 1: # Don't add newline at the end since it it part of the next # sibling new_node.append_child(Leaf(token.NEWLINE, '\n')) # Checks if we ended a line without being surrounded by parens if open_count <= 0: need_parens = True if need_parens: # Parenthesize the parent if we're not inside parenths, braces, # brackets, since we inserted newlines between leaves parenth_before_equals = Leaf(token.EQUAL, "=") in split_leaves[0] self.parenthesize_parent(new_node, parenth_before_equals) node_to_split.replace(new_node) return combined_prefix