From 1d8ecd91aec8454da4cccd57dddb5a083a19b3eb Mon Sep 17 00:00:00 2001 From: Quentin Peter Date: Tue, 19 Dec 2017 16:23:05 +0000 Subject: [PATCH 1/3] Differenciate between ), }, and ] --- spyder/widgets/sourcecode/codeeditor.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/spyder/widgets/sourcecode/codeeditor.py b/spyder/widgets/sourcecode/codeeditor.py index 32c9aec8d1d..af025003c9a 100644 --- a/spyder/widgets/sourcecode/codeeditor.py +++ b/spyder/widgets/sourcecode/codeeditor.py @@ -2518,7 +2518,18 @@ 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): @@ -2898,7 +2909,7 @@ 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) From 0f61ecb85220966b78179bdec0c095d85f10d631 Mon Sep 17 00:00:00 2001 From: Quentin Peter Date: Tue, 19 Dec 2017 16:42:56 +0000 Subject: [PATCH 2/3] PEP8 --- spyder/widgets/sourcecode/codeeditor.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/spyder/widgets/sourcecode/codeeditor.py b/spyder/widgets/sourcecode/codeeditor.py index af025003c9a..802c0b61893 100644 --- a/spyder/widgets/sourcecode/codeeditor.py +++ b/spyder/widgets/sourcecode/codeeditor.py @@ -2521,7 +2521,7 @@ def __forbidden_colon_end_char(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 + The brace type can be general or specified by closing_braces_type (')', ']', or '}') """ if closing_braces_type is None: @@ -2529,7 +2529,8 @@ def __unmatched_braces_in_line(self, text, closing_braces_type=None): closing_braces = [')', ']', '}'] else: closing_braces = [closing_braces_type] - opening_braces = [{')': '(', '}': '{', ']': '['}[closing_braces_type]] + opening_braces = [{')': '(', '}': '{', + ']': '['}[closing_braces_type]] block = self.textCursor().block() line_pos = block.position() for pos, char in enumerate(text): @@ -2909,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(), 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) From 47f63eaf25fc4f6150b8484b69be9242d6e03eae Mon Sep 17 00:00:00 2001 From: Quentin Peter Date: Tue, 19 Dec 2017 17:36:05 +0000 Subject: [PATCH 3/3] forgot to change the if --- spyder/widgets/sourcecode/codeeditor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spyder/widgets/sourcecode/codeeditor.py b/spyder/widgets/sourcecode/codeeditor.py index 802c0b61893..a37460537ae 100644 --- a/spyder/widgets/sourcecode/codeeditor.py +++ b/spyder/widgets/sourcecode/codeeditor.py @@ -2534,11 +2534,11 @@ def __unmatched_braces_in_line(self, text, closing_braces_type=None): 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