]> git.madduck.net Git - etc/vim.git/blobdiff - docs/the_black_code_style/current_style.md

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Reorganize docs v2 (GH-2174)
[etc/vim.git] / docs / the_black_code_style / current_style.md
similarity index 91%
rename from docs/the_black_code_style.md
rename to docs/the_black_code_style/current_style.md
index 39a452ff9a81d74abf20ce88e962e579e7a9cfae..7d08bc9cad50887e47433aaff70960d1ec1c95e9 100644 (file)
@@ -2,11 +2,13 @@
 
 ## Code style
 
-_Black_ reformats entire files in place. It is not configurable. It doesn't take
-previous formatting into account. It doesn't reformat blocks that start with
-`# fmt: off` and end with `# fmt: on`. `# fmt: on/off` have to be on the same level of
-indentation. It also recognizes [YAPF](https://github.com/google/yapf)'s block comments
-to the same effect, as a courtesy for straddling code.
+_Black_ reformats entire files in place. Style configuration options are deliberately
+limited and rarely added. It doesn't take previous formatting into account, except for
+the magic trailing comma and preserving newlines. It doesn't reformat blocks that start
+with `# fmt: off` and end with `# fmt: on`, or lines that ends with `# fmt: skip`.
+`# fmt: on/off` have to be on the same level of indentation. It also recognizes
+[YAPF](https://github.com/google/yapf)'s block comments to the same effect, as a
+courtesy for straddling code.
 
 ### How _Black_ wraps lines
 
@@ -75,6 +77,8 @@ def very_important_function(
         ...
 ```
 
+(labels/why-no-backslashes)=
+
 _Black_ prefers parentheses over backslashes, and will remove backslashes if found.
 
 ```py3
@@ -115,29 +119,6 @@ If you're reaching for backslashes, that's a clear signal that you can do better
 slightly refactor your code. I hope some of the examples above show you that there are
 many ways in which you can do it.
 
-However there is one exception: `with` statements using multiple context managers.
-Python's grammar does not allow organizing parentheses around the series of context
-managers.
-
-We don't want formatting like:
-
-```py3
-with make_context_manager1() as cm1, make_context_manager2() as cm2, make_context_manager3() as cm3, make_context_manager4() as cm4:
-    ...  # nothing to split on - line too long
-```
-
-So _Black_ will now format it like this:
-
-```py3
-with \
-     make_context_manager(1) as cm1, \
-     make_context_manager(2) as cm2, \
-     make_context_manager(3) as cm3, \
-     make_context_manager(4) as cm4 \
-:
-    ...  # backslashes and an ugly stranded colon
-```
-
 You might have noticed that closing brackets are always dedented and that a trailing
 comma is always added. Such formatting produces smaller diffs; when you add or remove an
 element, it's always just one line. Also, having the closing bracket dedented provides a
@@ -289,13 +270,13 @@ If you are adopting _Black_ in a large project with pre-existing string conventi
 you can pass `--skip-string-normalization` on the command line. This is meant as an
 adoption helper, avoid using this for new projects.
 
-As an experimental option, _Black_ splits long strings (using parentheses where
-appropriate) and merges short ones. When split, parts of f-strings that don't need
-formatting are converted to plain strings. User-made splits are respected when they do
-not exceed the line length limit. Line continuation backslashes are converted into
-parenthesized strings. Unnecessary parentheses are stripped. To enable experimental
-string processing, pass `--experimental-string-processing` on the command line. Because
-the functionality is experimental, feedback and issue reports are highly encouraged!
+As an experimental option (can be enabled by `--experimental-string-processing`),
+_Black_ splits long strings (using parentheses where appropriate) and merges short ones.
+When split, parts of f-strings that don't need formatting are converted to plain
+strings. User-made splits are respected when they do not exceed the line length limit.
+Line continuation backslashes are converted into parenthesized strings. Unnecessary
+parentheses are stripped. Because the functionality is experimental, feedback and issue
+reports are highly encouraged!
 
 _Black_ also processes docstrings. Firstly the indentation of docstrings is corrected
 for both quotations and the text within, although relative indentation in the text is
@@ -354,7 +335,7 @@ pair of parentheses to form an atom. There are a few interesting cases:
 In those cases, parentheses are removed when the entire statement fits in one line, or
 if the inner expression doesn't have any delimiters to further split on. If there is
 only a single delimiter and the expression starts or ends with a bracket, the
-parenthesis can also be successfully omitted since the existing bracket pair will
+parentheses can also be successfully omitted since the existing bracket pair will
 organize the expression neatly anyway. Otherwise, the parentheses are added.
 
 Please note that _Black_ does not add or remove any additional nested parentheses that
@@ -453,7 +434,7 @@ into one item per line.
 How do you make it stop? Just delete that trailing comma and _Black_ will collapse your
 collection into one line if it fits.
 
-If you must, you can recover the behaviour of early versions of Black with the option
+If you must, you can recover the behaviour of early versions of _Black_ with the option
 `--skip-magic-trailing-comma` / `-C`.
 
 ### r"strings" and R"strings"
@@ -478,11 +459,11 @@ target. There are three limited cases in which the AST does differ:
    of docstrings that we're aware of sanitizes indentation and leading/trailing
    whitespace anyway.
 
-2. _Black_ manages optional parentheses for some statements. In the case of the `del`
+1. _Black_ manages optional parentheses for some statements. In the case of the `del`
    statement, presence of wrapping parentheses or lack of thereof changes the resulting
    AST but is semantically equivalent in the interpreter.
 
-3. _Black_ might move comments around, which includes type comments. Those are part of
+1. _Black_ might move comments around, which includes type comments. Those are part of
    the AST as of Python 3.8. While the tool implements a number of special cases for
    those comments, there is no guarantee they will remain where they were in the source.
    Note that this doesn't change runtime behavior of the source code.