changeset 42710:26a31c88e1a5

byteify-strings: fix misalignment with multi-line parenthesis This improves the current fix to also take into account cases where the last line ended on the opening `(`, `[` or `{` and adds a regression test.
author Raphaël Gomès <rgomes@octobus.net>
date Sun, 04 Aug 2019 22:14:26 +0200
parents 3364b4da5271
children 716d575df275
files contrib/byteify-strings.py tests/test-byteify-strings.t
diffstat 2 files changed, 54 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/contrib/byteify-strings.py	Fri Aug 02 16:54:02 2019 +0200
+++ b/contrib/byteify-strings.py	Sun Aug 04 22:14:26 2019 +0200
@@ -124,7 +124,7 @@
 
     coldelta = 0  # column increment for new opening parens
     coloffset = -1  # column offset for the current line (-1: TBD)
-    parens = [(0, 0, 0)]  # stack of (line, end-column, column-offset)
+    parens = [(0, 0, 0, -1)]  # stack of (line, end-column, column-offset, type)
     ignorenextline = False  # don't transform the next line
     insideignoreblock = False # don't transform until turned off
     for i, t in enumerate(tokens):
@@ -132,11 +132,15 @@
         # the current line will be aligned to the last opening paren
         # as before.
         if coloffset < 0:
-            if t.start[1] == parens[-1][1]:
-                coloffset = parens[-1][2]
-            elif t.start[1] + 1 == parens[-1][1]:
+            lastparen = parens[-1]
+            if t.start[1] == lastparen[1]:
+                coloffset = lastparen[2]
+            elif (
+                t.start[1] + 1 == lastparen[1]
+                and lastparen[3] not in (token.NEWLINE, tokenize.NL)
+            ):
                 # fix misaligned indent of s/util.Abort/error.Abort/
-                coloffset = parens[-1][2] + (parens[-1][1] - t.start[1])
+                coloffset = lastparen[2] + (lastparen[1] - t.start[1])
             else:
                 coloffset = 0
 
@@ -164,7 +168,7 @@
 
         # Remember the last paren position.
         if _isop(i, '(', '[', '{'):
-            parens.append(t.end + (coloffset + coldelta,))
+            parens.append(t.end + (coloffset + coldelta, tokens[i + 1].type))
         elif _isop(i, ')', ']', '}'):
             parens.pop()
 
--- a/tests/test-byteify-strings.t	Fri Aug 02 16:54:02 2019 +0200
+++ b/tests/test-byteify-strings.t	Sun Aug 04 22:14:26 2019 +0200
@@ -215,3 +215,47 @@
   $ byteify_strings testfile.py
   obj[b'test'] = b"1234"
   obj[r'test'] = u"1234"
+
+Test multi-line alignment
+
+  $ cat > testfile.py <<'EOF'
+  > def foo():
+  >     error.Abort(_("foo"
+  >                  "bar"
+  >                  "%s")
+  >                % parameter)
+  > {
+  >     'test': dict,
+  >     'test2': dict,
+  > }
+  > [
+  >    "thing",
+  >    "thing2"
+  > ]
+  > (
+  >    "tuple",
+  >    "tuple2",
+  > )
+  > {"thing",
+  >  }
+  > EOF
+  $ byteify_strings testfile.py
+  def foo():
+      error.Abort(_(b"foo"
+                    b"bar"
+                    b"%s")
+                  % parameter)
+  {
+      b'test': dict,
+      b'test2': dict,
+  }
+  [
+     b"thing",
+     b"thing2"
+  ]
+  (
+     b"tuple",
+     b"tuple2",
+  )
+  {b"thing",
+   }