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: Do not overwrite unmatched braces #5604

Merged
merged 2 commits into from
Nov 1, 2017
Merged
Show file tree
Hide file tree
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
9 changes: 7 additions & 2 deletions spyder/widgets/sourcecode/codeeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2892,8 +2892,13 @@ def keyPressEvent(self, event):
cursor.movePosition(QTextCursor.NextCharacter,
QTextCursor.KeepAnchor)
text = to_text_string(cursor.selectedText())
if text == {Qt.Key_ParenRight: ')', Qt.Key_BraceRight: '}',
Qt.Key_BracketRight: ']'}[key]:
key_matches_next_char = (
text == {Qt.Key_ParenRight: ')', Qt.Key_BraceRight: '}',
Qt.Key_BracketRight: ']'}[key]
)
if (key_matches_next_char
and not self.__unmatched_braces_in_line(cursor.block().text())):
# overwrite an existing brace if all braces in line are matched
cursor.clearSelection()
self.setTextCursor(cursor)
else:
Expand Down
36 changes: 35 additions & 1 deletion spyder/widgets/tests/test_codeeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,38 @@ def test_editor_lower_to_upper(editorbot):
widget.setTextCursor(cursor)
widget.transform_to_uppercase()
new_text = widget.get_text('sof', 'eof')
assert text != new_text
assert text != new_text

def test_editor_complete_backet(editorbot):
qtbot, editor = editorbot
editor.textCursor().insertText('foo')
qtbot.keyClicks(editor, '(')
assert editor.toPlainText() == 'foo()'
assert editor.textCursor().columnNumber() == 4

def test_editor_complete_bracket_nested(editorbot):
qtbot, editor = editorbot
editor.textCursor().insertText('foo(bar)')
editor.move_cursor(-1)
qtbot.keyClicks(editor, '(')
assert editor.toPlainText() == 'foo(bar())'
assert editor.textCursor().columnNumber() == 8

def test_editor_bracket_closing(editorbot):
qtbot, editor = editorbot
editor.textCursor().insertText('foo(bar(x')
qtbot.keyClicks(editor, ')')
assert editor.toPlainText() == 'foo(bar(x)'
assert editor.textCursor().columnNumber() == 10
qtbot.keyClicks(editor, ')')
assert editor.toPlainText() == 'foo(bar(x))'
assert editor.textCursor().columnNumber() == 11
# same ')' closing with existing brackets starting at 'foo(bar(x|))'
editor.move_cursor(-2)
qtbot.keyClicks(editor, ')')
assert editor.toPlainText() == 'foo(bar(x))'
assert editor.textCursor().columnNumber() == 10
qtbot.keyClicks(editor, ')')
assert editor.toPlainText() == 'foo(bar(x))'
assert editor.textCursor().columnNumber() == 11