Repository: pausan/cblack (language: python)

Issue:
Incorrect continuations.
Continuations under a two-column style should be four-column indented, but cblack currently uses two spaces instead.

Incorrect continuations.
Continuations under a two-column style should be four-column indented, but cblack currently uses two spaces instead.



Discussion / hints:

Fix patch:
diff --git a/cblack.py b/cblack.py
index 5615885..5063e45 100644
--- a/cblack.py
+++ b/cblack.py
@@ -3,40 +3,59 @@
 import re
 import sys
 
-import black
+from black import main as black_main
 
-__version__ = '0.9.1'
+try:
+  import black.linegen as black
+except ImportError:
+  import black
+
+
+__version__ = "0.9.1"
 
 _orgLineStr = black.Line.__str__
 _orgFixDocString = black.fix_docstring
+_orgBracketSplit = black.bracket_split_build_line
+
 
 def lineStrIndentTwoSpaces(self) -> str:
-  """ Intended to replace Line.__str__ to produce 2-space indentation blocks
+  """Intended to replace Line.__str__ to produce 2-space indentation blocks
   instead of the 4 by default.
   """
   original = _orgLineStr(self)
-  if not original.startswith(' '):
+  if not original.startswith(" "):
     return original
 
-  noLeftSpaces   = original.lstrip(' ')
+  noLeftSpaces = original.lstrip(" ")
   nLeadingSpaces = len(original) - len(noLeftSpaces)
 
   # reindent by generating half the spaces (from 4-space blocks to 2-space blocks)
-  reindented = '%s%s' % (' ' * (nLeadingSpaces >> 1), noLeftSpaces)
+  reindented = "%s%s" % (" " * (nLeadingSpaces >> 1), noLeftSpaces)
   return reindented
 
+
 def fixDocString(docstring, prefix):
-  """ Indent doc strings by 2 spaces instead of 4
-  """
-  return _orgFixDocString(docstring, ' ' * (len(prefix) >> 1))
+  """Indent doc strings by 2 spaces instead of 4"""
+  return _orgFixDocString(docstring, " " * (len(prefix) >> 1))
+
+
+def bracketSplitBuildLine(*args, is_body: bool = False, **kwargs):
+  result = _orgBracketSplit(*args, **kwargs, is_body=is_body)
+  if is_body:
+    result.depth += 1
+  return result
+
 
 black.Line.__str__ = lineStrIndentTwoSpaces
 black.fix_docstring = fixDocString
+black.bracket_split_build_line = bracketSplitBuildLine
 
-def main ():
+
+def main():
   # behabe like normal black code
-  sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
-  sys.exit(black.main())
+  sys.argv[0] = re.sub(r"(-script\.pyw?|\.exe)?$", "", sys.argv[0])
+  sys.exit(black_main())
+
 
-if __name__ == '__main__':
+if __name__ == "__main__":
   sys.exit(main())
diff --git a/setup.py b/setup.py
index 5e39a6f..4c74722 100644
--- a/setup.py
+++ b/setup.py
@@ -9,19 +9,20 @@
 
 CURRENT_DIR = Path(__file__).parent
 
+
 def get_long_description() -> str:
-    readme_md = CURRENT_DIR / "README.md"
-    with open(readme_md, encoding="utf8") as ld_file:
-        return ld_file.read()
+  readme_md = CURRENT_DIR / "README.md"
+  with open(readme_md, encoding="utf8") as ld_file:
+    return ld_file.read()
 
 
 def get_version() -> str:
-    cblack_py = CURRENT_DIR / "cblack.py"
-    _version_re = re.compile(r"__version__\s+=\s+(?P<version>.*)")
-    with open(cblack_py, "r", encoding="utf8") as f:
-        match = _version_re.search(f.read())
-        version = match.group("version") if match is not None else '"unknown"'
-    return str(ast.literal_eval(version))
+  cblack_py = CURRENT_DIR / "cblack.py"
+  _version_re = re.compile(r"__version__\s+=\s+(?P<version>.*)")
+  with open(cblack_py, "r", encoding="utf8") as f:
+    match = _version_re.search(f.read())
+    version = match.group("version") if match is not None else '"unknown"'
+  return str(ast.literal_eval(version))
 
 
 setup(
@@ -56,4 +57,4 @@ def get_version() -> str:
         "Topic :: Software Development :: Quality Assurance",
     ],
     entry_points={"console_scripts": ["cblack=cblack:main"]},
-)
\ No newline at end of file
+)


Test patch:
diff --git a/test.py b/test.py
new file mode 100644
index 0000000..03f19ad
--- /dev/null
+++ b/test.py
@@ -0,0 +1,31 @@
+"""This file should be unchanged by running cblack on it."""
+from x import (
+    a,
+    b,
+    c,
+)
+
+
+def main():
+  """Hoo.
+
+  Wat.
+  """
+  if len(argv) > 1:
+    raise app.UsageError("Too many command-line arguments.")
+
+  x = (
+      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
+      "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
+  )
+  y = [
+      1,
+      2,
+      [
+          3,
+      ],
+  ]
+
+
+if __name__ == "__main__":
+  exit(main())