Patchwork [2,of,2] byteify-strings: fix misalignment with multi-line parenthesis

login
register
mail settings
Submitter Raphaël Gomès
Date Aug. 4, 2019, 8:21 p.m.
Message ID <3d9a2bb7d210b8fc0928.1564950106@alphare-carbon.lan>
Download mbox | patch
Permalink /patch/41149/
State Accepted
Headers show

Comments

Raphaël Gomès - Aug. 4, 2019, 8:21 p.m.
# HG changeset patch
# User Raphaël Gomès <rgomes@octobus.net>
# Date 1564949666 -7200
#      Sun Aug 04 22:14:26 2019 +0200
# Node ID 3d9a2bb7d210b8fc092894fec7017faad9653496
# Parent  6a81e45cb9f295adff1e8008a67db48b971ccc18
# EXP-Topic byteify-strings
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.
Yuya Nishihara - Aug. 5, 2019, 1:01 p.m.
On Sun, 04 Aug 2019 22:21:46 +0200, Raphaël Gomès wrote:
> # HG changeset patch
> # User Raphaël Gomès <rgomes@octobus.net>
> # Date 1564949666 -7200
> #      Sun Aug 04 22:14:26 2019 +0200
> # Node ID 3d9a2bb7d210b8fc092894fec7017faad9653496
> # Parent  6a81e45cb9f295adff1e8008a67db48b971ccc18
> # EXP-Topic byteify-strings
> byteify-strings: fix misalignment with multi-line parenthesis

Queued, thanks.

>          # Remember the last paren position.
>          if _isop(i, '(', '[', '{'):
> -            parens.append(t.end + (coloffset + coldelta,))
> +            parens.append(t.end + (coloffset + coldelta, tokens[i + 1].type))

Fixed the initial parens[0].

Patch

diff -r 6a81e45cb9f2 -r 3d9a2bb7d210 contrib/byteify-strings.py
--- 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
@@ -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()
 
diff -r 6a81e45cb9f2 -r 3d9a2bb7d210 tests/test-byteify-strings.t
--- 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
@@ -199,3 +199,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",
+   }