@@ -1474,6 +1474,8 @@ def p_key(self):
14741474 elif self .c == 'KEY_DC' : # Del
14751475 self .delete ()
14761476 self .complete ()
1477+ # Redraw (as there might have been highlighted parens)
1478+ self .print_line (self .s )
14771479 return ''
14781480
14791481 elif self .c in key_dispatch ['C-r' ]: # C-r
@@ -1672,29 +1674,38 @@ def reprint_line(lineno, s, to_replace=[]):
16721674 source = '\n ' .join (self .buffer ) + '\n %s' % (s , )
16731675 i = line = 0
16741676 pos = 3
1677+ parens = dict (zip ('{([' , '})]' ))
16751678 for (token , value ) in PythonLexer ().get_tokens (source ):
16761679 pos += len (value )
16771680 under_cursor = (line == len (self .buffer ) and pos == x )
16781681 if token is Token .Punctuation :
1679- if value in '({[' :
1682+ if value in parens :
16801683 if under_cursor :
16811684 tokens [i ] = (Parenthesis .UnderCursor , value )
16821685 # Push marker on the stack
1683- stack .append (Parenthesis )
1686+ stack .append (( Parenthesis , value ) )
16841687 else :
16851688 stack .append ((line , i , value ))
1686- elif value in ')}]' :
1689+ elif value in parens .itervalues ():
1690+ saved_stack = list (stack )
16871691 try :
1688- opening = stack .pop ()
1692+ while True :
1693+ opening = stack .pop ()
1694+ if parens [opening [- 1 ]] == value :
1695+ break
16891696 except IndexError :
16901697 # SyntaxError.. more closed parentheses than
1691- # opened
1692- break
1693- if opening is Parenthesis :
1698+ # opened or a wrong closing paren
1699+ if not saved_stack :
1700+ break
1701+ else :
1702+ opening = None
1703+ stack = saved_stack
1704+ if opening and opening [0 ] is Parenthesis :
16941705 # Marker found
16951706 tokens [i ] = (Parenthesis , value )
16961707 break
1697- elif under_cursor :
1708+ elif opening and under_cursor :
16981709 if self .cpos :
16991710 tokens [i ] = (Parenthesis .UnderCursor , value )
17001711 else :
0 commit comments