]> git.madduck.net Git - etc/vim.git/commitdiff

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:

Split imports like isort
authorŁukasz Langa <lukasz@langa.pl>
Tue, 24 Apr 2018 20:44:28 +0000 (13:44 -0700)
committerŁukasz Langa <lukasz@langa.pl>
Tue, 24 Apr 2018 20:44:28 +0000 (13:44 -0700)
Fixes #127

Partially addresses #152

README.md
black.py
tests/import_spacing.py

index a589d0d65d3e92db6c5951a0a3fa9330d4cffdd9..489bda5e42d96da0640d22b1a05766bbb033ce23 100644 (file)
--- a/README.md
+++ b/README.md
@@ -176,6 +176,13 @@ between two distinct sections of the code that otherwise share the same
 indentation level (like the arguments list and the docstring in the
 example above).
 
+If a line of "from" imports cannot fit in the allotted length, it's always split
+into one per line.  Imports tend to change often and this minimizes diffs, as well
+as enables readers of code to easily find which commit introduced a particular
+import.  This exception also makes *Black* compatible with
+[isort](https://pypi.org/p/isort/).  Use `multi_line_output=3` and
+`include_trailing_comma=True` in your isort config.
+
 
 ### Line length
 
@@ -528,6 +535,8 @@ More details can be found in [CONTRIBUTING](CONTRIBUTING.md).
 * Black no longer enforces putting empty lines behind control flow statements
   (#90)
 
+* Black now splits imports like "Mode 3 + trailing comma" of isort (#127)
+
 * fixed comment indentation when a standalone comment closes a block (#16, #32)
 
 * fixed standalone comments receiving extra empty lines if immediately preceding
index 95489f3c0c990a650c8efdc4aacb43159d7f8553..21e37435105164883391ecabe83e1f3510e24434 100644 (file)
--- a/black.py
+++ b/black.py
@@ -1712,6 +1712,8 @@ def split_line(
     split_funcs: List[SplitFunc]
     if line.is_def:
         split_funcs = [left_hand_split]
+    elif line.is_import:
+        split_funcs = [explode_split]
     elif line.inside_brackets:
         split_funcs = [delimiter_split, standalone_comment_split, right_hand_split]
     else:
@@ -1978,6 +1980,24 @@ def standalone_comment_split(line: Line, py36: bool = False) -> Iterator[Line]:
         yield current_line
 
 
+def explode_split(
+    line: Line, py36: bool = False, omit: Collection[LeafID] = ()
+) -> Iterator[Line]:
+    """Split by RHS and immediately split contents by a delimiter."""
+    new_lines = list(right_hand_split(line, py36, omit))
+    if len(new_lines) != 3:
+        yield from new_lines
+        return
+
+    yield new_lines[0]
+    try:
+        yield from delimiter_split(new_lines[1], py36)
+    except CannotSplit:
+        yield new_lines[1]
+
+    yield new_lines[2]
+
+
 def is_import(leaf: Leaf) -> bool:
     """Return True if the given leaf starts an import statement."""
     p = leaf.parent
index 4091148d16231106377f1e43d82be344b809f21c..f095ba13371a245597f3631db471021b41a0a3fe 100644 (file)
@@ -17,6 +17,10 @@ from ..runners import *  # comment here
 from ..queues import *
 from ..streams import *
 
+from some_library import (
+    Just, Enough, Libraries, To, Fit, In, This, Nice, Split, Which, We, No, Longer, Use
+)
+
 from .a.b.c.subprocess import *
 from . import (tasks)
 from . import (A, B, C)
@@ -59,6 +63,23 @@ from ..runners import *  # comment here
 from ..queues import *
 from ..streams import *
 
+from some_library import (
+    Just,
+    Enough,
+    Libraries,
+    To,
+    Fit,
+    In,
+    This,
+    Nice,
+    Split,
+    Which,
+    We,
+    No,
+    Longer,
+    Use,
+)
+
 from .a.b.c.subprocess import *
 from . import tasks
 from . import A, B, C