@@ -164,25 +164,28 @@ def main():
164
164
# If line is larger than console_width, try to optimize it.
165
165
# Smart Python word wrap to be used with valid indentation.
166
166
if line_length + w > console_width : # 5 is line number ws
167
- if line .count (' # ' ) == 1 : # Has comments like this
168
- if get_width (
169
- line .split (
170
- ' # ' )[0 ]) + w <= console_width :
171
- new_sb_lines .append (line )
172
- continue
173
- elif line .count (' # ' ) == 1 : # Has bad flake8 comment
174
- if get_width (
175
- line .split (
176
- ' # ' )[0 ]) + w <= console_width :
177
- new_sb_lines .append (line )
178
- continue
179
- elif line .startswith ("from" ) and " import " in line :
167
+ if line .strip ().startswith ("#" ):
168
+ new_sb_lines .append (line )
169
+ continue
170
+ elif line .count (' # ' ) == 1 and get_width (line .split (
171
+ ' # ' )[0 ]) + w <= console_width :
172
+ # Line is short enough once comment is removed
173
+ line = line .split (' # ' )[0 ]
174
+ new_sb_lines .append (line )
175
+ continue
176
+ elif line .count (' # ' ) == 1 and get_width (line .split (
177
+ ' # ' )[0 ]) + w <= console_width :
178
+ # L-Length good if removing bad flake8 comment
179
+ line = line .split (' # ' )[0 ]
180
+ new_sb_lines .append (line )
181
+ continue
182
+ if line .startswith ("from" ) and " import " in line :
180
183
line1 = line .split (" import " )[0 ] + " \\ "
181
184
line2 = " import " + line .split (" import " )[1 ]
182
185
new_sb_lines .append (line1 )
183
186
new_sb_lines .append (line2 )
184
187
continue
185
- elif line .count ('(' ) == 1 and line .count (')' ) == 1 :
188
+ if line .count ('(' ) == 1 and line .count (')' ) == 1 :
186
189
whitespace = line_length2 - len (line .lstrip ())
187
190
new_ws = line [0 :whitespace ] + " "
188
191
line1 = line .split ('(' )[0 ] + '('
@@ -210,6 +213,20 @@ def main():
210
213
line2b = new_ws + '"' + (
211
214
line2 .split ("://" )[1 ])
212
215
new_sb_lines .append (line2a )
216
+ if get_width (line2b ) + w > (
217
+ console_width ):
218
+ if line2b .count ('/' ) > 0 :
219
+ slash_one = line2b .find ('/' )
220
+ line2b1 = (
221
+ line2b [:slash_one + 1 ] + '"' )
222
+ line2b2 = new_ws + '"' + (
223
+ line2b [slash_one + 1 :])
224
+ new_sb_lines .append (line2b1 )
225
+ if line2b2 .count (') # ' ) == 1 :
226
+ line2b2 = line2b2 .split (
227
+ ') # ' )[0 ] + ')'
228
+ new_sb_lines .append (line2b2 )
229
+ continue
213
230
new_sb_lines .append (line2b )
214
231
continue
215
232
elif line2 .count ("://" ) == 1 and (
@@ -218,6 +235,20 @@ def main():
218
235
line2b = new_ws + "'" + (
219
236
line2 .split ("://" )[1 ])
220
237
new_sb_lines .append (line2a )
238
+ if get_width (line2b ) + w > (
239
+ console_width ):
240
+ if line2b .count ('/' ) > 0 :
241
+ slash_one = line2b .find ('/' )
242
+ line2b1 = (
243
+ line2b [:slash_one + 1 ] + "'" )
244
+ line2b2 = new_ws + "'" + (
245
+ line2b [slash_one + 1 :])
246
+ new_sb_lines .append (line2b1 )
247
+ if line2b2 .count (') # ' ) == 1 :
248
+ line2b2 = line2b2 .split (
249
+ ') # ' )[0 ] + ')'
250
+ new_sb_lines .append (line2b2 )
251
+ continue
221
252
new_sb_lines .append (line2b )
222
253
continue
223
254
elif line2 .count (", " ) == 1 :
@@ -251,7 +282,7 @@ def main():
251
282
else :
252
283
new_sb_lines .append (line )
253
284
continue
254
- elif line .count ('("' ) == 1 :
285
+ if line .count ('("' ) == 1 :
255
286
whitespace = line_length2 - len (line .lstrip ())
256
287
new_ws = line [0 :whitespace ] + " "
257
288
line1 = line .split ('("' )[0 ] + '('
@@ -275,7 +306,7 @@ def main():
275
306
else :
276
307
new_sb_lines .append (line )
277
308
continue
278
- elif line .count ("('" ) == 1 :
309
+ if line .count ("('" ) == 1 :
279
310
whitespace = line_length2 - len (line .lstrip ())
280
311
new_ws = line [0 :whitespace ] + " "
281
312
line1 = line .split ("('" )[0 ] + '('
@@ -299,7 +330,7 @@ def main():
299
330
else :
300
331
new_sb_lines .append (line )
301
332
continue
302
- elif line .count ('= "' ) == 1 and line .count ('://' ) == 1 :
333
+ if line .count ('= "' ) == 1 and line .count ('://' ) == 1 :
303
334
whitespace = line_length2 - len (line .lstrip ())
304
335
new_ws = line [0 :whitespace ] + " "
305
336
line1 = line .split ('://' )[0 ] + '://" \\ '
@@ -315,7 +346,7 @@ def main():
315
346
continue
316
347
new_sb_lines .append (line2 )
317
348
continue
318
- elif line .count ("= '" ) == 1 and line .count ('://' ) == 1 :
349
+ if line .count ("= '" ) == 1 and line .count ('://' ) == 1 :
319
350
whitespace = line_length2 - len (line .lstrip ())
320
351
new_ws = line [0 :whitespace ] + " "
321
352
line1 = line .split ('://' )[0 ] + "://' \\ "
@@ -331,7 +362,7 @@ def main():
331
362
continue
332
363
new_sb_lines .append (line2 )
333
364
continue
334
- elif line .count ('(self.' ) == 1 and not ('):' ) in line :
365
+ if line .count ('(self.' ) == 1 and not ('):' ) in line :
335
366
whitespace = line_length2 - len (line .lstrip ())
336
367
new_ws = line [0 :whitespace ] + " "
337
368
line1 = line .split ('(self.' )[0 ] + "("
@@ -340,7 +371,46 @@ def main():
340
371
new_sb_lines .append (line1 )
341
372
new_sb_lines .append (line2 )
342
373
continue
343
- elif line .count (' % ' ) == 1 and not ('):' ) in line :
374
+ if line .count (' == ' ) == 1 and not (
375
+ line .endswith (':' ) or (': #' ) in line ):
376
+ whitespace = line_length2 - len (line .lstrip ())
377
+ new_ws = line [0 :whitespace ] + " "
378
+ line1 = line .split (' == ' )[0 ] + " == ("
379
+ line2 = new_ws + line .split (' == ' )[1 ] + ')'
380
+ if get_width (line1 ) + w <= console_width and (
381
+ get_width (line2 ) + w <= console_width ):
382
+ new_sb_lines .append (line1 )
383
+ new_sb_lines .append (line2 )
384
+ continue
385
+ if line .count (' == ' ) == 1 and line .endswith (':' ):
386
+ whitespace = line_length2 - len (line .lstrip ())
387
+ new_ws = line [0 :whitespace ] + " "
388
+ line1 = line .split (' == ' )[0 ] + " == ("
389
+ line2 = new_ws + line .split (' == ' )[1 ][:- 1 ] + '):'
390
+ if get_width (line1 ) + w <= console_width and (
391
+ get_width (line2 ) + w <= console_width ):
392
+ new_sb_lines .append (line1 )
393
+ new_sb_lines .append (line2 )
394
+ continue
395
+ if line .count (' == ' ) == 1 and (
396
+ line .count (': #' ) == 1 ) and (
397
+ line .find (' == ' ) < line .find (': #' )):
398
+ whitespace = line_length2 - len (line .lstrip ())
399
+ new_ws = line [0 :whitespace ] + " "
400
+ comments = " #" + line .split (': #' )[1 ]
401
+ line0 = line .split (': #' )[0 ] + ':'
402
+ line1 = line0 .split (' == ' )[0 ] + " == ("
403
+ line2 = new_ws + line0 .split (' == ' )[1 ][:- 1 ] + '):'
404
+ if get_width (line1 ) + w <= console_width and (
405
+ get_width (line2 ) + w <= console_width ):
406
+ new_sb_lines .append (line1 )
407
+ if get_width (
408
+ line2 + comments ) + w <= console_width :
409
+ new_sb_lines .append (line2 + comments )
410
+ else :
411
+ new_sb_lines .append (line2 )
412
+ continue
413
+ if line .count (' % ' ) == 1 and not ('):' ) in line :
344
414
whitespace = line_length2 - len (line .lstrip ())
345
415
new_ws = line [0 :whitespace ] + " "
346
416
line1 = line .split (' % ' )[0 ] + " \\ "
@@ -349,6 +419,49 @@ def main():
349
419
new_sb_lines .append (line1 )
350
420
new_sb_lines .append (line2 )
351
421
continue
422
+ if line .count (' = ' ) == 1 and not (' # ' ) in line :
423
+ whitespace = line_length2 - len (line .lstrip ())
424
+ new_ws = line [0 :whitespace ] + " "
425
+ line1 = line .split (' = ' )[0 ] + " = ("
426
+ line2 = new_ws + line .split (' = ' )[1 ] + ')'
427
+ if get_width (line1 ) + w <= console_width and (
428
+ get_width (line2 ) + w <= console_width ):
429
+ new_sb_lines .append (line1 )
430
+ new_sb_lines .append (line2 )
431
+ continue
432
+ elif get_width (line1 ) + w <= console_width :
433
+ if line2 .count (' % ' ) == 1 and not (
434
+ line2 .endswith (':' )):
435
+ whitespace = line_length2 - len (
436
+ line2 .lstrip ())
437
+ line2a = line2 .split (' % ' )[0 ] + " \\ "
438
+ line2b = new_ws + "% " + line2 .split (
439
+ ' % ' )[1 ]
440
+ if get_width (line2a ) + w <= console_width :
441
+ if get_width (
442
+ line2b ) + w <= console_width :
443
+ new_sb_lines .append (line1 )
444
+ new_sb_lines .append (line2a )
445
+ new_sb_lines .append (line2b )
446
+ continue
447
+ if line .count (' = ' ) == 1 and (
448
+ line .count (' # ' ) == 1 ) and (
449
+ line .find (' = ' ) < line .find (' # ' )):
450
+ whitespace = line_length2 - len (line .lstrip ())
451
+ new_ws = line [0 :whitespace ] + " "
452
+ comments = " # " + line .split (' # ' )[1 ]
453
+ line0 = line .split (' # ' )[0 ]
454
+ line1 = line0 .split (' = ' )[0 ] + " = ("
455
+ line2 = new_ws + line0 .split (' = ' )[1 ] + ')'
456
+ if get_width (line1 ) + w <= console_width and (
457
+ get_width (line2 ) + w <= console_width ):
458
+ new_sb_lines .append (line1 )
459
+ if get_width (
460
+ line2 + comments ) + w <= console_width :
461
+ new_sb_lines .append (line2 + comments )
462
+ else :
463
+ new_sb_lines .append (line2 )
464
+ continue
352
465
new_sb_lines .append (line )
353
466
354
467
if new_sb_lines :
0 commit comments