in tools/yapf/yapf/yapflib/reformatter.py [0:0]
def Reformat(uwlines, verify=False):
"""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.
indent_width = style.Get('INDENT_WIDTH')
for uwline in _SingleOrMergedLines(uwlines):
first_token = uwline.first
_FormatFirstToken(first_token, uwline.depth, prev_uwline, final_lines)
indent_amt = indent_width * uwline.depth
state = format_decision_state.FormatDecisionState(uwline, indent_amt)
state.MoveStateToNextToken()
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) and not any(tok.must_split
for tok in uwline.tokens):
# The unwrapped line fits on one line.
while state.next_token:
state.AddTokenToState(newline=False, dry_run=False)
else:
if not _AnalyzeSolutionSpace(state):
# 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)
state.MoveStateToNextToken()
_RetainHorizontalSpacing(uwline)
_RetainVerticalSpacing(uwline, prev_uwline)
_EmitLineUnformatted(state)
final_lines.append(uwline)
prev_uwline = uwline
return _FormatFinalLines(final_lines, verify)