##// END OF EJS Templates
byteify-strings: fix misalignment with multi-line parenthesis...
Raphaël Gomès -
r42914:26a31c88 default
parent child Browse files
Show More
@@ -124,7 +124,7 def replacetokens(tokens, opts):
124
124
125 coldelta = 0 # column increment for new opening parens
125 coldelta = 0 # column increment for new opening parens
126 coloffset = -1 # column offset for the current line (-1: TBD)
126 coloffset = -1 # column offset for the current line (-1: TBD)
127 parens = [(0, 0, 0)] # stack of (line, end-column, column-offset)
127 parens = [(0, 0, 0, -1)] # stack of (line, end-column, column-offset, type)
128 ignorenextline = False # don't transform the next line
128 ignorenextline = False # don't transform the next line
129 insideignoreblock = False # don't transform until turned off
129 insideignoreblock = False # don't transform until turned off
130 for i, t in enumerate(tokens):
130 for i, t in enumerate(tokens):
@@ -132,11 +132,15 def replacetokens(tokens, opts):
132 # the current line will be aligned to the last opening paren
132 # the current line will be aligned to the last opening paren
133 # as before.
133 # as before.
134 if coloffset < 0:
134 if coloffset < 0:
135 if t.start[1] == parens[-1][1]:
135 lastparen = parens[-1]
136 coloffset = parens[-1][2]
136 if t.start[1] == lastparen[1]:
137 elif t.start[1] + 1 == parens[-1][1]:
137 coloffset = lastparen[2]
138 elif (
139 t.start[1] + 1 == lastparen[1]
140 and lastparen[3] not in (token.NEWLINE, tokenize.NL)
141 ):
138 # fix misaligned indent of s/util.Abort/error.Abort/
142 # fix misaligned indent of s/util.Abort/error.Abort/
139 coloffset = parens[-1][2] + (parens[-1][1] - t.start[1])
143 coloffset = lastparen[2] + (lastparen[1] - t.start[1])
140 else:
144 else:
141 coloffset = 0
145 coloffset = 0
142
146
@@ -164,7 +168,7 def replacetokens(tokens, opts):
164
168
165 # Remember the last paren position.
169 # Remember the last paren position.
166 if _isop(i, '(', '[', '{'):
170 if _isop(i, '(', '[', '{'):
167 parens.append(t.end + (coloffset + coldelta,))
171 parens.append(t.end + (coloffset + coldelta, tokens[i + 1].type))
168 elif _isop(i, ')', ']', '}'):
172 elif _isop(i, ')', ']', '}'):
169 parens.pop()
173 parens.pop()
170
174
@@ -215,3 +215,47 Test prefixed strings
215 $ byteify_strings testfile.py
215 $ byteify_strings testfile.py
216 obj[b'test'] = b"1234"
216 obj[b'test'] = b"1234"
217 obj[r'test'] = u"1234"
217 obj[r'test'] = u"1234"
218
219 Test multi-line alignment
220
221 $ cat > testfile.py <<'EOF'
222 > def foo():
223 > error.Abort(_("foo"
224 > "bar"
225 > "%s")
226 > % parameter)
227 > {
228 > 'test': dict,
229 > 'test2': dict,
230 > }
231 > [
232 > "thing",
233 > "thing2"
234 > ]
235 > (
236 > "tuple",
237 > "tuple2",
238 > )
239 > {"thing",
240 > }
241 > EOF
242 $ byteify_strings testfile.py
243 def foo():
244 error.Abort(_(b"foo"
245 b"bar"
246 b"%s")
247 % parameter)
248 {
249 b'test': dict,
250 b'test2': dict,
251 }
252 [
253 b"thing",
254 b"thing2"
255 ]
256 (
257 b"tuple",
258 b"tuple2",
259 )
260 {b"thing",
261 }
General Comments 0
You need to be logged in to leave comments. Login now