Skip to content

Commit

Permalink
Merge pull request #5604 from timhoffm/no-unmatched-brace-overwrite
Browse files Browse the repository at this point in the history
PR: Do not overwrite unmatched braces
  • Loading branch information
ccordoba12 authored Nov 1, 2017
2 parents 51f8f6b + 4645a47 commit 7dd3a70
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
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

0 comments on commit 7dd3a70

Please sign in to comment.