Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 1 | ==== |
| 2 | YAPF |
| 3 | ==== |
| 4 | |
Bill Wendling | 19c44d0 | 2015-04-07 23:48:05 -0700 | [diff] [blame] | 5 | .. image:: https://badge.fury.io/py/yapf.svg |
| 6 | :target: http://badge.fury.io/py/yapf |
| 7 | :alt: PyPI version |
| 8 | |
Bill Wendling | fb8ab38 | 2015-03-18 20:24:14 -0700 | [diff] [blame] | 9 | .. image:: https://travis-ci.org/google/yapf.svg?branch=master |
| 10 | :target: https://travis-ci.org/google/yapf |
| 11 | :alt: Build status |
| 12 | |
Bill Wendling | 14ac881 | 2015-04-05 02:47:32 -0700 | [diff] [blame] | 13 | .. image:: https://coveralls.io/repos/google/yapf/badge.svg?branch=master |
| 14 | :target: https://coveralls.io/r/google/yapf?branch=master |
| 15 | :alt: Coverage status |
| 16 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 17 | Introduction |
| 18 | ============ |
| 19 | |
Bill Wendling | 5632e67 | 2015-03-29 17:06:07 -0700 | [diff] [blame] | 20 | Most of the current formatters for Python --- e.g., autopep8, and pep8ify --- |
| 21 | are made to remove lint errors from code. This has some obvious limitations. |
| 22 | For instance, code that conforms to the PEP 8 guidelines may not be |
| 23 | reformatted. But it doesn't mean that the code looks good. |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 24 | |
| 25 | YAPF takes a different approach. It's based off of 'clang-format', developed by |
| 26 | Daniel Jasper. In essence, the algorithm takes the code and reformats it to the |
| 27 | best formatting that conforms to the style guide, even if the original code |
Peter Bengtsson | 1c60ad7 | 2015-03-24 20:05:39 -0700 | [diff] [blame] | 28 | didn't violate the style guide. The idea is also similar to the 'gofmt' tool for |
Eli Bendersky | 07072f8 | 2015-03-23 06:41:14 -0700 | [diff] [blame] | 29 | the Go programming language: end all holy wars about formatting - if the whole |
| 30 | code base of a project is simply piped through YAPF whenever modifications are |
| 31 | made, the style remains consistent throughout the project and there's no point |
| 32 | arguing about style in every code review. |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 33 | |
| 34 | The ultimate goal is that the code YAPF produces is as good as the code that a |
Bill Wendling | 8fb9c48 | 2015-03-29 17:32:07 -0700 | [diff] [blame] | 35 | programmer would write if they were following the style guide. It takes away |
| 36 | some of the drudgery of maintaining your code. |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 37 | |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 38 | .. footer:: |
Bill Wendling | 52e0411 | 2015-03-18 20:42:26 -0700 | [diff] [blame] | 39 | |
| 40 | YAPF is not an official Google product (experimental or otherwise), it is |
| 41 | just code that happens to be owned by Google. |
| 42 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 43 | .. contents:: |
| 44 | |
| 45 | Installation |
| 46 | ============ |
| 47 | |
Eli Bendersky | e0e83c1 | 2015-04-06 20:23:30 -0700 | [diff] [blame] | 48 | To install YAPF from PyPI:: |
Eli Bendersky | 8a36536 | 2015-03-25 18:42:22 -0700 | [diff] [blame] | 49 | |
Eli Bendersky | e0e83c1 | 2015-04-06 20:23:30 -0700 | [diff] [blame] | 50 | $ pip install yapf |
| 51 | |
| 52 | YAPF is still considered in "alpha" stage, and the released version may change |
| 53 | often; therefore, the best way to keep up-to-date with the latest development |
| 54 | is to clone this repository. |
| 55 | |
| 56 | Note that if you intend to use YAPF as a command-line tool rather than as a |
| 57 | library, installation is not necessary. YAPF supports being run as a directory |
| 58 | by the Python interpreter. If you cloned/unzipped YAPF into ``DIR``, it's |
| 59 | possible to run:: |
Eli Bendersky | 07072f8 | 2015-03-23 06:41:14 -0700 | [diff] [blame] | 60 | |
Eli Bendersky | b3678b3 | 2015-03-25 14:16:11 -0700 | [diff] [blame] | 61 | $ PYTHONPATH=DIR python DIR/yapf [options] ... |
Eli Bendersky | 07072f8 | 2015-03-23 06:41:14 -0700 | [diff] [blame] | 62 | |
Eli Bendersky | 5eb8823 | 2015-03-27 06:27:11 -0700 | [diff] [blame] | 63 | Python versions |
| 64 | =============== |
| 65 | |
Eli Bendersky | a7bfe7e | 2015-04-05 06:33:18 -0700 | [diff] [blame] | 66 | YAPF supports Python 2.7 and 3.4.1+. |
Eli Bendersky | 5eb8823 | 2015-03-27 06:27:11 -0700 | [diff] [blame] | 67 | |
| 68 | YAPF requires the code it formats to be valid Python for the version YAPF itself |
| 69 | runs under. Therefore, if you format Python 3 code with YAPF, run YAPF itself |
| 70 | under Python 3 (and similarly for Python 2). |
| 71 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 72 | Usage |
| 73 | ===== |
| 74 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 75 | Options:: |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 76 | |
Sam Clegg | 5170c3a | 2015-04-16 12:18:58 -0700 | [diff] [blame] | 77 | usage: yapf [-h] [--version] [--style-help] [--style STYLE] [--verify] |
| 78 | [-d | -i] [-l START-END | -r] |
| 79 | [files [files ...]] |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 80 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 81 | Formatter for Python code. |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 82 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 83 | positional arguments: |
| 84 | files |
| 85 | |
| 86 | optional arguments: |
| 87 | -h, --help show this help message and exit |
Sam Clegg | 5170c3a | 2015-04-16 12:18:58 -0700 | [diff] [blame] | 88 | --version show version number and exit |
| 89 | --style-help show style settings and exit |
Eli Bendersky | 83d2bd0 | 2015-03-23 06:33:48 -0700 | [diff] [blame] | 90 | --style STYLE specify formatting style: either a style name (for |
| 91 | example "pep8" or "google"), or the name of a file |
Sam Clegg | 5170c3a | 2015-04-16 12:18:58 -0700 | [diff] [blame] | 92 | with style settings. The default is pep8 unless a |
| 93 | .style.yapf file located in one of the parent |
| 94 | directories of the source file (or current directory |
| 95 | for stdin) |
| 96 | --verify try to verify refomatted code for syntax errors |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 97 | -d, --diff print the diff for the fixed source |
| 98 | -i, --in-place make changes to files in place |
| 99 | -l START-END, --lines START-END |
| 100 | range of lines to reformat, one-based |
| 101 | -r, --recursive run recursively over directories |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 102 | |
Eli Bendersky | 83d2bd0 | 2015-03-23 06:33:48 -0700 | [diff] [blame] | 103 | Formatting style |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 104 | ================ |
Eli Bendersky | 83d2bd0 | 2015-03-23 06:33:48 -0700 | [diff] [blame] | 105 | |
| 106 | The formatting style used by YAPF is configurable and there are many "knobs" |
| 107 | that can be used to tune how YAPF does formatting. See the ``style.py`` module |
| 108 | for the full list. |
| 109 | |
Bill Wendling | c016779 | 2015-04-02 01:58:39 -0700 | [diff] [blame] | 110 | To control the style, run YAPF with the ``--style`` argument. It accepts one of |
| 111 | the predefined styles (e.g., ``pep8`` or ``google``), a path to a configuration |
| 112 | file that specifies the desired style, or a dictionary of key/value pairs. |
| 113 | |
| 114 | The config file is a simple listing of (case-insensitive) ``key = value`` pairs |
| 115 | with a ``[style]`` heading. For example:: |
Eli Bendersky | 83d2bd0 | 2015-03-23 06:33:48 -0700 | [diff] [blame] | 116 | |
| 117 | [style] |
| 118 | based_on_style = pep8 |
| 119 | spaces_before_comment = 4 |
| 120 | split_before_logical_operator = true |
| 121 | |
| 122 | The ``based_on_style`` setting determines which of the predefined styles this |
| 123 | custom style is based on (think of it like subclassing). |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 124 | |
Bill Wendling | c016779 | 2015-04-02 01:58:39 -0700 | [diff] [blame] | 125 | It's also possible to do the same on the command line with a dictionary. For |
| 126 | example:: |
| 127 | |
| 128 | --style='{based_on_style: google, indent_width: 4}' |
| 129 | |
| 130 | This will take the ``google`` base style and modify it to have four space |
| 131 | indentations. |
| 132 | |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 133 | Example |
| 134 | ======= |
| 135 | |
Sam Clegg | 4357fa3 | 2015-04-08 12:21:46 -0700 | [diff] [blame] | 136 | An example of the type of formatting that YAPF can do, it will take this ugly |
| 137 | code: |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 138 | |
| 139 | .. code-block:: python |
| 140 | |
| 141 | x = { 'a':37,'b':42, |
| 142 | |
| 143 | 'c':927} |
| 144 | |
| 145 | y = 'hello ''world' |
| 146 | z = 'hello '+'world' |
| 147 | a = 'hello {}'.format('world') |
| 148 | class foo ( object ): |
| 149 | def f (self ): |
| 150 | return 37*-+2 |
| 151 | def g(self, x,y=42): |
| 152 | return y |
| 153 | def f ( a ) : |
| 154 | return 37+-+a[42-x : y**3] |
| 155 | |
Bill Wendling | 8fb9c48 | 2015-03-29 17:32:07 -0700 | [diff] [blame] | 156 | and reformat it into: |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 157 | |
| 158 | .. code-block:: python |
| 159 | |
| 160 | x = {'a': 37, 'b': 42, 'c': 927} |
| 161 | |
| 162 | y = 'hello ' 'world' |
| 163 | z = 'hello ' + 'world' |
| 164 | a = 'hello {}'.format('world') |
| 165 | |
| 166 | |
| 167 | class foo(object): |
Bill Wendling | 5632e67 | 2015-03-29 17:06:07 -0700 | [diff] [blame] | 168 | def f(self): |
| 169 | return 37 * -+2 |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 170 | |
Bill Wendling | 5632e67 | 2015-03-29 17:06:07 -0700 | [diff] [blame] | 171 | def g(self, x, y=42): |
| 172 | return y |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 173 | |
| 174 | |
| 175 | def f(a): |
Bill Wendling | 5632e67 | 2015-03-29 17:06:07 -0700 | [diff] [blame] | 176 | return 37 + -+a[42 - x:y ** 3] |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 177 | |
Andy Hayden | a00a6bf | 2015-06-15 18:47:41 -0700 | [diff] [blame] | 178 | Example as a module |
| 179 | =================== |
| 180 | |
Andy Hayden | 4af7168 | 2015-06-17 15:42:43 -0700 | [diff] [blame] | 181 | The two main APIs for calling yapf are ``FormatCode`` and ``FormatFile``, these |
| 182 | share several arguments which are described below: |
Andy Hayden | a00a6bf | 2015-06-15 18:47:41 -0700 | [diff] [blame] | 183 | |
| 184 | .. code-block:: python |
| 185 | |
Andy Hayden | 4af7168 | 2015-06-17 15:42:43 -0700 | [diff] [blame] | 186 | >>> from yapf.yapf_api import FormatCode # reformat a string of code |
Andy Hayden | a00a6bf | 2015-06-15 18:47:41 -0700 | [diff] [blame] | 187 | |
| 188 | >>> FormatCode("f ( a = 1, b = 2 )") |
| 189 | 'f(a=1, b=2)\n' |
| 190 | |
Andy Hayden | 4af7168 | 2015-06-17 15:42:43 -0700 | [diff] [blame] | 191 | A ``style_config`` argument: Either a style name or a path to a file that contains |
Andy Hayden | a00a6bf | 2015-06-15 18:47:41 -0700 | [diff] [blame] | 192 | formatting style settings. If None is specified, use the default style |
| 193 | as set in ``style.DEFAULT_STYLE_FACTORY``. |
| 194 | |
| 195 | .. code-block:: python |
| 196 | |
| 197 | >>> FormatCode("def g():\n return True", style_config='pep8') |
| 198 | 'def g():\n return True\n' |
| 199 | |
Andy Hayden | a00a6bf | 2015-06-15 18:47:41 -0700 | [diff] [blame] | 200 | A ``lines`` argument: A list of tuples of lines (ints), [start, end], |
| 201 | that we want to format. The lines are 1-based indexed. It can be used by |
| 202 | third-party code (e.g., IDEs) when reformatting a snippet of code rather |
| 203 | than a whole file. |
| 204 | |
| 205 | .. code-block:: python |
| 206 | |
| 207 | >>> FormatCode("def g( ):\n a=1\n b = 2\n return a==b", lines=[(1, 1), (2, 3)]) |
| 208 | 'def g():\n a = 1\n b = 2\n return a==b\n' |
| 209 | |
| 210 | A ``print_diff`` (bool): Instead of returning the reformatted source, return a |
| 211 | diff that turns the formatted source into reformatter source. |
| 212 | |
| 213 | .. code-block:: python |
| 214 | |
| 215 | >>> print(FormatCode("a==b", filename="foo.py", print_diff=True)) |
Bill Wendling | b8645ea | 2015-06-30 22:27:56 -0700 | [diff] [blame^] | 216 | --- foo.py (original) |
| 217 | +++ foo.py (reformatted) |
Andy Hayden | a00a6bf | 2015-06-15 18:47:41 -0700 | [diff] [blame] | 218 | @@ -1 +1 @@ |
| 219 | -a==b |
| 220 | +a == b |
| 221 | |
Andy Hayden | 4af7168 | 2015-06-17 15:42:43 -0700 | [diff] [blame] | 222 | Note: the ``filename`` argument for ``FormatCode`` is what is inserted into |
| 223 | the diff, the default is ``<unknown>``. |
Andy Hayden | a00a6bf | 2015-06-15 18:47:41 -0700 | [diff] [blame] | 224 | |
| 225 | ``FormatFile`` returns reformatted code from the passed file along with its encoding: |
| 226 | |
| 227 | .. code-block:: python |
| 228 | |
Andy Hayden | 4af7168 | 2015-06-17 15:42:43 -0700 | [diff] [blame] | 229 | >>> from yapf.yapf_api import FormatFile # reformat a file |
| 230 | |
Andy Hayden | a00a6bf | 2015-06-15 18:47:41 -0700 | [diff] [blame] | 231 | >>> print(open("foo.py").read()) # contents of file |
| 232 | a==b |
| 233 | |
| 234 | >>> FormatFile("foo.py") |
Andy Hayden | 4af7168 | 2015-06-17 15:42:43 -0700 | [diff] [blame] | 235 | ('a == b\n', 'utf-8') |
| 236 | |
| 237 | The ``in_place`` argument saves the reformatted code back to the file: |
| 238 | |
| 239 | .. code-block:: python |
| 240 | |
| 241 | >>> FormatFile("foo.py", in_place=True) |
| 242 | (None, 'utf-8') |
| 243 | |
| 244 | >>> print(open("foo.py").read()) # contents of file (now fixed) |
| 245 | a == b |
| 246 | |
Andy Hayden | a00a6bf | 2015-06-15 18:47:41 -0700 | [diff] [blame] | 247 | |
Bill Wendling | 8fb9c48 | 2015-03-29 17:32:07 -0700 | [diff] [blame] | 248 | (Potentially) Frequently Asked Questions |
| 249 | ======================================== |
| 250 | |
| 251 | Why does YAPF destroy my awesome formatting? |
| 252 | -------------------------------------------- |
| 253 | |
| 254 | YAPF tries very hard to get the formatting correct. But for some code, it won't |
| 255 | be as good as hand-formatting. In particular, large data literals may become |
| 256 | horribly disfigured under YAPF. |
| 257 | |
Diogo Moitinho de Almeida | 24458d0 | 2015-04-02 17:57:22 -0700 | [diff] [blame] | 258 | The reason for this is many-fold. But in essence YAPF is simply a tool to help |
Bill Wendling | 8fb9c48 | 2015-03-29 17:32:07 -0700 | [diff] [blame] | 259 | with development. It will format things to coincide with the style guide, but |
| 260 | that may not equate with readability. |
| 261 | |
| 262 | What can be done to alleviate this situation is to indicate regions YAPF should |
| 263 | ignore when reformatting something: |
| 264 | |
| 265 | .. code-block:: python |
| 266 | |
| 267 | # yapf: disable |
| 268 | FOO = { |
| 269 | # ... some very large, complex data literal. |
| 270 | } |
| 271 | |
| 272 | BAR = [ |
| 273 | # ... another large data literal. |
| 274 | ] |
| 275 | # yapf: enable |
| 276 | |
| 277 | You can also disable formatting for a single literal like this: |
| 278 | |
| 279 | .. code-block:: python |
| 280 | |
| 281 | BAZ = { |
| 282 | [1, 2, 3, 4], |
| 283 | [5, 6, 7, 8], |
| 284 | [9, 10, 11, 12] |
| 285 | } # yapf: disable |
| 286 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 287 | Why Not Improve Existing Tools? |
Bill Wendling | 8fb9c48 | 2015-03-29 17:32:07 -0700 | [diff] [blame] | 288 | ------------------------------- |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 289 | |
| 290 | We wanted to use clang-format's reformatting algorithm. It's very powerful and |
| 291 | designed to come up with the best formatting possible. Existing tools were |
| 292 | created with different goals in mind, and would require extensive modifications |
| 293 | to convert to using clang-format's algorithm. |
| 294 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 295 | Can I Use YAPF In My Program? |
Bill Wendling | 8fb9c48 | 2015-03-29 17:32:07 -0700 | [diff] [blame] | 296 | ----------------------------- |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 297 | |
| 298 | Please do! YAPF was designed to be used as a library as well as a command line |
| 299 | tool. This means that a tool or IDE plugin is free to use YAPF. |
| 300 | |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 301 | Gory Details |
| 302 | ============ |
| 303 | |
| 304 | Algorithm Design |
| 305 | ---------------- |
| 306 | |
Eli Bendersky | d08130d | 2015-03-19 05:20:46 -0700 | [diff] [blame] | 307 | The main data structure in YAPF is the ``UnwrappedLine`` object. It holds a list |
| 308 | of ``FormatToken``\s, that we would want to place on a single line if there were |
| 309 | no column limit. An exception being a comment in the middle of an expression |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 310 | statement will force the line to be formatted on more than one line. The |
Eli Bendersky | d08130d | 2015-03-19 05:20:46 -0700 | [diff] [blame] | 311 | formatter works on one ``UnwrappedLine`` object at a time. |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 312 | |
Eli Bendersky | d08130d | 2015-03-19 05:20:46 -0700 | [diff] [blame] | 313 | An ``UnwrappedLine`` typically won't affect the formatting of lines before or |
| 314 | after it. There is a part of the algorithm that may join two or more |
| 315 | ``UnwrappedLine``\s into one line. For instance, an if-then statement with a |
Bill Wendling | f5e50b6 | 2015-03-28 23:38:12 -0700 | [diff] [blame] | 316 | short body can be placed on a single line: |
| 317 | |
| 318 | .. code-block:: python |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 319 | |
| 320 | if a == 42: continue |
| 321 | |
| 322 | YAPF's formatting algorithm creates a weighted tree that acts as the solution |
| 323 | space for the algorithm. Each node in the tree represents the result of a |
| 324 | formatting decision --- i.e., whether to split or not to split before a token. |
| 325 | Each formatting decision has a cost associated with it. Therefore, the cost is |
| 326 | realized on the edge between two nodes. (In reality, the weighted tree doesn't |
| 327 | have separate edge objects, so the cost resides on the nodes themselves.) |
| 328 | |
| 329 | For example, take the following Python code snippet. For the sake of this |
| 330 | example, assume that line (1) violates the column limit restriction and needs to |
| 331 | be reformatted. |
| 332 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 333 | .. code-block:: python |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 334 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 335 | def xxxxxxxxxxx(aaaaaaaaaaaa, bbbbbbbbb, cccccccc, dddddddd, eeeeee): # 1 |
| 336 | pass # 2 |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 337 | |
| 338 | For line (1), the algorithm will build a tree where each node (a |
Eli Bendersky | d08130d | 2015-03-19 05:20:46 -0700 | [diff] [blame] | 339 | ``FormattingDecisionState`` object) is the state of the line at that token given |
| 340 | the decision to split before the token or not. Note: the ``FormatDecisionState`` |
| 341 | objects are copied by value so each node in the graph is unique and a change in |
| 342 | one doesn't affect other nodes. |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 343 | |
Bill Wendling | fa22c89 | 2015-03-18 13:42:25 -0700 | [diff] [blame] | 344 | Heuristics are used to determine the costs of splitting or not splitting. |
| 345 | Because a node holds the state of the tree up to a token's insertion, it can |
| 346 | easily determine if a splitting decision will violate one of the style |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 347 | requirements. For instance, the heuristic is able to apply an extra penalty to |
| 348 | the edge when not splitting between the previous token and the one being added. |
| 349 | |
| 350 | There are some instances where we will never want to split the line, because |
| 351 | doing so will always be detrimental (i.e., it will require a backslash-newline, |
| 352 | which is very rarely desirable). For line (1), we will never want to split the |
Eli Bendersky | d08130d | 2015-03-19 05:20:46 -0700 | [diff] [blame] | 353 | first three tokens: ``def``, ``xxxxxxxxxxx``, and ``(``. Nor will we want to |
| 354 | split between the ``)`` and the ``:`` at the end. These regions are said to be |
| 355 | "unbreakable." This is reflected in the tree by there not being a "split" |
Bill Wendling | 7d62345 | 2015-03-18 13:36:07 -0700 | [diff] [blame] | 356 | decision (left hand branch) within the unbreakable region. |
| 357 | |
| 358 | Now that we have the tree, we determine what the "best" formatting is by finding |
| 359 | the path through the tree with the lowest cost. |
| 360 | |
| 361 | And that's it! |