def Reformat(uwlines, verify=True): """Reformat the unwrapped lines. Arguments: uwlines: (list of unwrapped_line.UnwrappedLine) Lines we want to format. verify: (bool) True if reformatted code should be verified for syntax. Returns: A string representing the reformatted code. """ final_lines = [] prev_uwline = None # The previous line. for uwline in _SingleOrMergedLines(uwlines): first_token = uwline.first _FormatFirstToken(first_token, uwline.depth, prev_uwline) indent_amt = style.Get('INDENT_WIDTH') * uwline.depth state = format_decision_state.FormatDecisionState(uwline, indent_amt) if not uwline.disable: if uwline.first.is_comment: uwline.first.node.value = uwline.first.node.value.rstrip() elif uwline.last.is_comment: uwline.last.node.value = uwline.last.node.value.rstrip() if prev_uwline and prev_uwline.disable: # Keep the vertical spacing between a disabled and enabled formatting # region. _RetainVerticalSpacing(prev_uwline, uwline) if _LineContainsI18n(uwline) or uwline.disable: _RetainHorizontalSpacing(uwline) _RetainVerticalSpacing(prev_uwline, uwline) _EmitLineUnformatted(state) elif _CanPlaceOnSingleLine(uwline) or _LineHasContinuationMarkers( uwline): # The unwrapped line fits on one line. Or the line contains continuation # markers, in which case we assume the programmer formatted the code this # way intentionally. while state.next_token: state.AddTokenToState(newline=False, dry_run=False) else: _AnalyzeSolutionSpace(state, dry_run=False) final_lines.append(uwline) prev_uwline = uwline formatted_code = [] for line in final_lines: formatted_line = [] for token in line.tokens: if token.name in pytree_utils.NONSEMANTIC_TOKENS: continue formatted_line.append(token.whitespace_prefix) formatted_line.append(token.value) formatted_code.append(''.join(formatted_line)) if verify: verifier.VerifyCode(formatted_code[-1]) return ''.join(formatted_code) + '\n'
def Reformat(uwlines): """Reformat the unwrapped lines. Arguments: uwlines: (list of unwrapped_line.UnwrappedLine) Lines we want to format. Returns: A string representing the reformatted code. """ final_lines = [] prev_last_uwline = None # The previous line. for uwline in _SingleOrMergedLines(uwlines): first_token = uwline.first _FormatFirstToken(first_token, uwline.depth, prev_last_uwline) indent_amt = style.Get('INDENT_WIDTH') * uwline.depth state = format_decision_state.FormatDecisionState(uwline, indent_amt) if _LineContainsI18n(uwline): _EmitLineUnformatted(state) elif _CanPlaceOnSingleLine(uwline): # The unwrapped line fits on one line. while state.next_token: state.AddTokenToState(newline=False, dry_run=False) else: _AnalyzeSolutionSpace(state, dry_run=False) final_lines.append(uwline) prev_last_uwline = uwline formatted_code = [] for line in final_lines: formatted_line = [] for token in line.tokens: if token.name in pytree_utils.NONSEMANTIC_TOKENS: continue formatted_line.append(token.whitespace_prefix) formatted_line.append(token.value) formatted_code.append(''.join(formatted_line)) verifier.VerifyCode(formatted_code[-1]) return ''.join(formatted_code) + '\n'
def _FormatFinalLines(final_lines, verify): """Compose the final output from the finalized lines.""" formatted_code = [] for line in final_lines: formatted_line = [] for tok in line.tokens: if not tok.is_pseudo: formatted_line.append(tok.formatted_whitespace_prefix) formatted_line.append(tok.value) elif (not tok.next_token.whitespace_prefix.startswith('\n') and not tok.next_token.whitespace_prefix.startswith(' ')): if (tok.previous_token.value == ':' or tok.next_token.value not in ',}])'): formatted_line.append(' ') formatted_code.append(''.join(formatted_line)) if verify: verifier.VerifyCode(formatted_code[-1]) return ''.join(formatted_code) + '\n'
def _FormatFinalLines(final_lines, verify): formatted_code = [] for line in final_lines: formatted_line = [] for tok in line.tokens: if not tok.is_pseudo_paren: formatted_line.append(tok.whitespace_prefix) formatted_line.append(tok.value) else: if (not tok.next_token.whitespace_prefix.startswith('\n') and not tok.next_token.whitespace_prefix.startswith(' ')): if (tok.previous_token.value == ':' or tok.next_token.value not in frozenset(',}])')): formatted_line.append(' ') formatted_code.append(''.join(formatted_line)) if verify: verifier.VerifyCode(formatted_code[-1]) return ''.join(formatted_code) + '\n'
def Reformat(uwlines, verify=True): """Reformat the unwrapped lines. Arguments: uwlines: (list of unwrapped_line.UnwrappedLine) Lines we want to format. verify: (bool) True if reformatted code should be verified for syntax. Returns: A string representing the reformatted code. """ final_lines = [] prev_uwline = None # The previous line. for uwline in _SingleOrMergedLines(uwlines): first_token = uwline.first _FormatFirstToken(first_token, uwline.depth, prev_uwline) indent_amt = style.Get('INDENT_WIDTH') * uwline.depth state = format_decision_state.FormatDecisionState(uwline, indent_amt) if not uwline.disable: if uwline.first.is_comment: uwline.first.node.value = uwline.first.node.value.rstrip() elif uwline.last.is_comment: uwline.last.node.value = uwline.last.node.value.rstrip() if prev_uwline and prev_uwline.disable: # Keep the vertical spacing between a disabled and enabled formatting # region. _RetainVerticalSpacingBetweenTokens(uwline.first, prev_uwline.last) if any(tok.is_comment for tok in uwline.tokens): _RetainVerticalSpacingBeforeComments(uwline) if (_LineContainsI18n(uwline) or uwline.disable or _LineHasContinuationMarkers(uwline)): _RetainHorizontalSpacing(uwline) _RetainVerticalSpacing(uwline, prev_uwline) _EmitLineUnformatted(state) elif _CanPlaceOnSingleLine(uwline): # The unwrapped line fits on one line. while state.next_token: state.AddTokenToState(newline=False, dry_run=False) else: if not _AnalyzeSolutionSpace(state, dry_run=False): # Failsafe mode. If there isn't a solution to the line, then just emit # it as is. state = format_decision_state.FormatDecisionState(uwline, indent_amt) _RetainHorizontalSpacing(uwline) _RetainVerticalSpacing(uwline, prev_uwline) _EmitLineUnformatted(state) final_lines.append(uwline) prev_uwline = uwline formatted_code = [] for line in final_lines: formatted_line = [] for tok in line.tokens: if not tok.is_pseudo_paren: formatted_line.append(tok.whitespace_prefix) formatted_line.append(tok.value) else: if (not tok.next_token.whitespace_prefix.startswith('\n') and not tok.next_token.whitespace_prefix.startswith(' ')): if (tok.previous_token.value == ':' or tok.next_token.value not in ',}])'): formatted_line.append(' ') formatted_code.append(''.join(formatted_line)) if verify: verifier.VerifyCode(formatted_code[-1]) return ''.join(formatted_code) + '\n'
def process_lines(lines): for line in lines: line = ''.join(value for value, _, _ in line) if verify: verifier.VerifyCode(line) yield line