@@ -424,6 +424,7 @@ def continued_indentation(logical_line, tokens, indent_level, hang_closing,
424424 parens = [0 ] * nrows
425425 # relative indents of physical lines
426426 rel_indent = [0 ] * nrows
427+ open_rows = [[0 ]]
427428 # visual indents
428429 indent_chances = {}
429430 last_indent = tokens [0 ][2 ]
@@ -448,17 +449,15 @@ def continued_indentation(logical_line, tokens, indent_level, hang_closing,
448449 # record the initial indent.
449450 rel_indent [row ] = expand_indent (line ) - indent_level
450451
451- if depth :
452- # a bracket expression in a continuation line.
453- # find the line that it was opened on
454- for open_row in range (row - 1 , - 1 , - 1 ):
455- if parens [open_row ]:
456- break
457- else :
458- # an unbracketed continuation line (ie, backslash)
459- open_row = 0
460- hang = rel_indent [row ] - rel_indent [open_row ]
452+ # identify closing bracket
461453 close_bracket = (token_type == tokenize .OP and text in ']})' )
454+
455+ # is the indent relative to an opening bracket line?
456+ valid_hang = 4 if (hang_closing or not close_bracket ) else 0
457+ for open_row in reversed (open_rows [depth ]):
458+ if rel_indent [row ] == rel_indent [open_row ] + valid_hang :
459+ break
460+ hang = rel_indent [row ] - rel_indent [open_row ]
462461 visual_indent = (not close_bracket and hang > 0 and
463462 indent_chances .get (start [1 ]))
464463
@@ -519,6 +518,9 @@ def continued_indentation(logical_line, tokens, indent_level, hang_closing,
519518 if text in '([{' :
520519 depth += 1
521520 indent .append (0 )
521+ if len (open_rows ) == depth :
522+ open_rows .append ([])
523+ open_rows [depth ].append (row )
522524 parens [row ] += 1
523525 if verbose >= 4 :
524526 print ("bracket depth %s seen, col %s, visual min = %s" %
@@ -532,13 +534,13 @@ def continued_indentation(logical_line, tokens, indent_level, hang_closing,
532534 for ind in list (indent_chances ):
533535 if ind >= prev_indent :
534536 del indent_chances [ind ]
537+ del open_rows [depth + 1 :]
535538 depth -= 1
536539 if depth :
537540 indent_chances [indent [depth ]] = True
538541 for idx in range (row , - 1 , - 1 ):
539542 if parens [idx ]:
540543 parens [idx ] -= 1
541- rel_indent [row ] = rel_indent [idx ]
542544 break
543545 assert len (indent ) == depth + 1
544546 if start [1 ] not in indent_chances :
0 commit comments