Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Differenciate between ), }, and ] when closing brackets #6017

Merged
merged 3 commits into from
Jan 26, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions spyder/widgets/sourcecode/codeeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down