Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 1 | ==== |
| 2 | YAPF |
| 3 | ==== |
| 4 | |
Bill Wendling | fb8ab38 | 2015-03-18 20:24:14 -0700 | [diff] [blame] | 5 | .. image:: https://travis-ci.org/google/yapf.svg?branch=master |
| 6 | :target: https://travis-ci.org/google/yapf |
| 7 | :alt: Build status |
| 8 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 9 | Introduction |
| 10 | ============ |
| 11 | |
| 12 | Most of the current formatters for Python -- e.g., autopep8, and pep8ify -- are |
| 13 | made to remove lint errors from code. This has some obvious limitations. For |
| 14 | instance, code that conforms to the PEP 8 guidelines may not be reformatted. |
| 15 | But it doesn't mean that the code looks good. |
| 16 | |
| 17 | YAPF takes a different approach. It's based off of 'clang-format', developed by |
| 18 | Daniel Jasper. In essence, the algorithm takes the code and reformats it to the |
| 19 | best formatting that conforms to the style guide, even if the original code |
| 20 | didn't violate the style guide. |
| 21 | |
| 22 | The ultimate goal is that the code YAPF produces is as good as the code that a |
| 23 | programmer would write if they were following the style guide. |
| 24 | |
Bill Wendling | 52e0411 | 2015-03-18 20:42:26 -0700 | [diff] [blame^] | 25 | .. note:: |
| 26 | |
| 27 | YAPF is not an official Google product (experimental or otherwise), it is |
| 28 | just code that happens to be owned by Google. |
| 29 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 30 | .. contents:: |
| 31 | |
| 32 | Installation |
| 33 | ============ |
| 34 | |
| 35 | From source directory:: |
| 36 | |
| 37 | $ sudo python ./setup.py install |
| 38 | |
| 39 | |
| 40 | Usage |
| 41 | ===== |
| 42 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 43 | Options:: |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 44 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 45 | usage: __main__.py [-h] [-d | -i] [-l START-END | -r] ... |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 46 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 47 | Formatter for Python code. |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 48 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 49 | positional arguments: |
| 50 | files |
| 51 | |
| 52 | optional arguments: |
| 53 | -h, --help show this help message and exit |
| 54 | -d, --diff print the diff for the fixed source |
| 55 | -i, --in-place make changes to files in place |
| 56 | -l START-END, --lines START-END |
| 57 | range of lines to reformat, one-based |
| 58 | -r, --recursive run recursively over directories |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 59 | |
| 60 | |
| 61 | Why Not Improve Existing Tools? |
| 62 | =============================== |
| 63 | |
| 64 | We wanted to use clang-format's reformatting algorithm. It's very powerful and |
| 65 | designed to come up with the best formatting possible. Existing tools were |
| 66 | created with different goals in mind, and would require extensive modifications |
| 67 | to convert to using clang-format's algorithm. |
| 68 | |
| 69 | |
| 70 | Can I Use YAPF In My Program? |
| 71 | ============================= |
| 72 | |
| 73 | Please do! YAPF was designed to be used as a library as well as a command line |
| 74 | tool. This means that a tool or IDE plugin is free to use YAPF. |
| 75 | |
| 76 | |
| 77 | Gory Details |
| 78 | ============ |
| 79 | |
| 80 | Algorithm Design |
| 81 | ---------------- |
| 82 | |
| 83 | The main data structure in YAPF is the UnwrappedLine object. It holds a list of |
| 84 | FormatTokens, that we would want to place on a single line if there were no |
| 85 | column limit. An exception being a comment in the middle of an expression |
| 86 | statement will force the line to be formatted on more than one line. The |
| 87 | formatter works on one UnwrappedLine object at a time. |
| 88 | |
| 89 | An UnwrappedLine typically won't affect the formatting of lines before or after |
| 90 | it. There is a part of the algorithm that may join two or more UnwrappedLines |
| 91 | into one line. For instance, an if-then statement with a short body can be |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 92 | placed on a single line:: |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 93 | |
| 94 | if a == 42: continue |
| 95 | |
| 96 | YAPF's formatting algorithm creates a weighted tree that acts as the solution |
| 97 | space for the algorithm. Each node in the tree represents the result of a |
| 98 | formatting decision --- i.e., whether to split or not to split before a token. |
| 99 | Each formatting decision has a cost associated with it. Therefore, the cost is |
| 100 | realized on the edge between two nodes. (In reality, the weighted tree doesn't |
| 101 | have separate edge objects, so the cost resides on the nodes themselves.) |
| 102 | |
| 103 | For example, take the following Python code snippet. For the sake of this |
| 104 | example, assume that line (1) violates the column limit restriction and needs to |
| 105 | be reformatted. |
| 106 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 107 | .. code-block:: python |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 108 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 109 | def xxxxxxxxxxx(aaaaaaaaaaaa, bbbbbbbbb, cccccccc, dddddddd, eeeeee): # 1 |
| 110 | pass # 2 |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 111 | |
| 112 | For line (1), the algorithm will build a tree where each node (a |
| 113 | FormattingDecisionState object) is the state of the line at that token given the |
| 114 | decision to split before the token or not. Note: the FormatDecisionState objects |
| 115 | are copied by value so each node in the graph is unique and a change in one |
| 116 | doesn't affect other nodes. |
| 117 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 118 | Heuristics are used to determine the costs of splitting or not splitting. |
| 119 | Because a node holds the state of the tree up to a token's insertion, it can |
| 120 | easily determine if a splitting decision will violate one of the style |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 121 | requirements. For instance, the heuristic is able to apply an extra penalty to |
| 122 | the edge when not splitting between the previous token and the one being added. |
| 123 | |
| 124 | There are some instances where we will never want to split the line, because |
| 125 | doing so will always be detrimental (i.e., it will require a backslash-newline, |
| 126 | which is very rarely desirable). For line (1), we will never want to split the |
| 127 | first three tokens: 'def', 'xxxxxxxxxxx', and '('. Nor will we want to split |
| 128 | between the ')' and the ':' at the end. These regions are said to be |
| 129 | "unbreakable." This is reflected in the tree by there not being a 'split' |
| 130 | decision (left hand branch) within the unbreakable region. |
| 131 | |
| 132 | Now that we have the tree, we determine what the "best" formatting is by finding |
| 133 | the path through the tree with the lowest cost. |
| 134 | |
| 135 | And that's it! |