diff --git a/spyder/widgets/sourcecode/codeeditor.py b/spyder/widgets/sourcecode/codeeditor.py index 32c9aec8d1d..a37460537ae 100644 --- a/spyder/widgets/sourcecode/codeeditor.py +++ b/spyder/widgets/sourcecode/codeeditor.py @@ -2518,15 +2518,27 @@ def __forbidden_colon_end_char(self, text): else: return False - def __unmatched_braces_in_line(self, text): + def __unmatched_braces_in_line(self, text, closing_braces_type=None): + """ + Checks if there is an unmatched brace in the 'text'. + The brace type can be general or specified by closing_braces_type + (')', ']', or '}') + """ + if closing_braces_type is None: + opening_braces = ['(', '[', '{'] + closing_braces = [')', ']', '}'] + else: + closing_braces = [closing_braces_type] + opening_braces = [{')': '(', '}': '{', + ']': '['}[closing_braces_type]] block = self.textCursor().block() line_pos = block.position() for pos, char in enumerate(text): - if char in ['(', '[', '{']: + if char in opening_braces: match = self.find_brace_match(line_pos+pos, char, forward=True) if (match is None) or (match > line_pos+len(text)): return True - if char in [')', ']', '}']: + if char in closing_braces: match = self.find_brace_match(line_pos+pos, char, forward=False) if (match is None) or (match < line_pos): return True @@ -2898,7 +2910,8 @@ def keyPressEvent(self, event): Qt.Key_BracketRight: ']'}[key] ) if (key_matches_next_char - and not self.__unmatched_braces_in_line(cursor.block().text())): + and not self.__unmatched_braces_in_line( + cursor.block().text(), text)): # overwrite an existing brace if all braces in line are matched cursor.clearSelection() self.setTextCursor(cursor)