Example #1
0
def FormatCode(unformatted_source,
               filename='<unknown>',
               style_config=None,
               lines=None,
               print_diff=False,
               verify=False):
    """Format a string of Python code.

  This provides an alternative entry point to YAPF.

  Arguments:
    unformatted_source: (unicode) The code to format.
    filename: (unicode) The name of the file being reformatted.
    remaining arguments: see comment at the top of this module.

  Returns:
    Tuple of (reformatted_source, changed). reformatted_source conforms to the
    desired formatting style. changed is True if the source changed.
  """
    _CheckPythonVersion()
    style.SetGlobalStyle(style.CreateStyleFromConfig(style_config))
    if not unformatted_source.endswith('\n'):
        unformatted_source += '\n'

    try:
        tree = pytree_utils.ParseCodeToTree(unformatted_source)
    except parse.ParseError as e:
        raise parse.ParseError(filename + ': ' + e.message)

    # Run passes on the tree, modifying it in place.
    comment_splicer.SpliceComments(tree)
    continuation_splicer.SpliceContinuations(tree)
    subtype_assigner.AssignSubtypes(tree)
    split_penalty.ComputeSplitPenalties(tree)
    blank_line_calculator.CalculateBlankLines(tree)

    uwlines = pytree_unwrapper.UnwrapPyTree(tree)
    _RecalTokenColumn(uwlines, unformatted_source)
    for uwl in uwlines:
        uwl.CalculateFormattingInformation()

    lines = _LineRangesToSet(lines)
    _MarkLinesToFormat(uwlines, lines)
    reformatted_source = reformatter.Reformat(_SplitSemicolons(uwlines),
                                              verify, lines)

    if unformatted_source == reformatted_source:
        return '' if print_diff else reformatted_source, False

    code_diff = _GetUnifiedDiff(unformatted_source,
                                reformatted_source,
                                filename=filename)

    if print_diff:
        return code_diff, code_diff.strip() != ''  # pylint: disable=g-explicit-bool-comparison

    return reformatted_source, True
Example #2
0
 def parse_tokens(self, tokens, debug=False):
     """Parse a series of tokens and return the syntax tree."""
     # XXX Move the prefix computation into a wrapper around tokenize.
     p = parse.Parser(self.grammar, self.convert)
     p.setup()
     lineno = 1
     column = 0
     type = value = start = end = line_text = None
     prefix = ""
     for quintuple in tokens:
         type, value, start, end, line_text = quintuple
         if start != (lineno, column):
             assert (lineno, column) <= start, ((lineno, column), start)
             s_lineno, s_column = start
             if lineno < s_lineno:
                 prefix += "\n" * (s_lineno - lineno)
                 lineno = s_lineno
                 column = 0
             if column < s_column:
                 prefix += line_text[column:s_column]
                 column = s_column
         if type in (tokenize.COMMENT, tokenize.NL):
             prefix += value
             lineno, column = end
             if value.endswith("\n"):
                 lineno += 1
                 column = 0
             continue
         if type == token.OP:
             type = grammar.opmap[value]
         if debug:
             self.logger.debug("%s %r (prefix=%r)",
                               token.tok_name[type], value, prefix)
         if p.addtoken(type, value, (prefix, start)):
             if debug:
                 self.logger.debug("Stop.")
             break
         prefix = ""
         lineno, column = end
         if value.endswith("\n"):
             lineno += 1
             column = 0
     else:
         # We never broke out -- EOF is too soon (how can this happen???)
         raise parse.ParseError("incomplete input",
                                type, value, (prefix, start))
     return p.rootnode