Пример #1
0
def Reformat(uwlines, verify=False, lines=None):
  """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.
    lines: (set of int) The lines which can be modified or None if there is no
      line range restriction.

  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.
        _RetainRequiredVerticalSpacingBetweenTokens(uwline.first,
                                                    prev_uwline.last, lines)
      if any(tok.is_comment for tok in uwline.tokens):
        _RetainVerticalSpacingBeforeComments(uwline)

    if (_LineContainsI18n(uwline) or uwline.disable or
        _LineHasContinuationMarkers(uwline)):
      _RetainHorizontalSpacing(uwline)
      _RetainRequiredVerticalSpacing(uwline, prev_uwline, lines)
      _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)
        _RetainRequiredVerticalSpacing(uwline, prev_uwline, None)
        _EmitLineUnformatted(state)

    final_lines.append(uwline)
    prev_uwline = uwline
  return _FormatFinalLines(final_lines, verify)
Пример #2
0
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'
Пример #3
0
    def testSimpleFunctionDefWithNoSplitting(self):
        code = textwrap.dedent(r"""
      def f(a, b):
        pass
      """)
        print(code)
        uwlines = yapf_test_helper.ParseAndUnwrap(code)
        uwline = unwrapped_line.UnwrappedLine(0, _FilterLine(uwlines[0]))
        uwline.CalculateFormattingInformation()

        # Add: 'f'
        state = format_decision_state.FormatDecisionState(uwline, 0)
        state.MoveStateToNextToken()
        self.assertEqual('f', state.next_token.value)
        self.assertFalse(state.CanSplit(False))

        # Add: '('
        state.AddTokenToState(False, True)
        self.assertEqual('(', state.next_token.value)
        self.assertFalse(state.CanSplit(False))
        self.assertFalse(state.MustSplit())

        # Add: 'a'
        state.AddTokenToState(False, True)
        self.assertEqual('a', state.next_token.value)
        self.assertTrue(state.CanSplit(False))
        self.assertFalse(state.MustSplit())

        # Add: ','
        state.AddTokenToState(False, True)
        self.assertEqual(',', state.next_token.value)
        self.assertFalse(state.CanSplit(False))
        self.assertFalse(state.MustSplit())

        # Add: 'b'
        state.AddTokenToState(False, True)
        self.assertEqual('b', state.next_token.value)
        self.assertTrue(state.CanSplit(False))
        self.assertFalse(state.MustSplit())

        # Add: ')'
        state.AddTokenToState(False, True)
        self.assertEqual(')', state.next_token.value)
        self.assertTrue(state.CanSplit(False))
        self.assertFalse(state.MustSplit())

        # Add: ':'
        state.AddTokenToState(False, True)
        self.assertEqual(':', state.next_token.value)
        self.assertFalse(state.CanSplit(False))
        self.assertFalse(state.MustSplit())

        clone = state.Clone()
        self.assertEqual(repr(state), repr(clone))
Пример #4
0
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'
Пример #5
0
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'
Пример #6
0
def Reformat(uwlines, filename='<unknown>', verify=False, lines=None):
    """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.
    lines: (set of int) The lines which can be modified or None if there is no
      line range restriction.
    filename: name (full path) of the source file used for code style fixing

  Returns:
    A string representing the reformatted code.
  """
    final_lines = []
    prev_uwline = None  # The previous line.
    indent_width = style.Get('INDENT_WIDTH')
    keep_line_splits = style.Get('COLUMN_LIMIT') == 0

    # special checks for a format of a header that can produce warnings
    fix_shebang_comment_header(uwlines, style)
    format_doc_strings(uwlines, style)
    messages = warns.check_all_recommendations(uwlines, style, filename)

    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.
                _RetainRequiredVerticalSpacingBetweenTokens(
                    uwline.first, prev_uwline.last, lines)
            if any(tok.is_comment for tok in uwline.tokens):
                _RetainVerticalSpacingBeforeComments(uwline)

        if uwline.disable or _LineHasContinuationMarkers(uwline):
            _RetainHorizontalSpacing(uwline)
            _RetainRequiredVerticalSpacing(uwline, prev_uwline, lines)
            _EmitLineUnformatted(state)

        elif keep_line_splits:
            _EmitLineUnformatted(state)

        elif (_LineContainsPylintDisableLineTooLong(uwline)
              or _LineContainsI18n(uwline)):
            # Don't modify vertical spacing, but fix any horizontal spacing issues.
            _RetainRequiredVerticalSpacing(uwline, prev_uwline, lines)
            _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)
                _RetainRequiredVerticalSpacing(uwline, prev_uwline, None)
                _EmitLineUnformatted(state)

        final_lines.append(uwline)
        prev_uwline = uwline

    _AlignTrailingComments(final_lines)
    formatted_lines = _FormatFinalLines(final_lines)

    _UpdateWarnLocations(formatted_lines, messages)
    messages.show()

    return _ToText(formatted_lines, verify)