Skip to content

Commit

Permalink
Merge from 3.x: PR #6530
Browse files Browse the repository at this point in the history
Fixes #4231
  • Loading branch information
ccordoba12 committed Feb 27, 2018
2 parents 5f3d7fb + 239bf8a commit 6aeade9
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
11 changes: 8 additions & 3 deletions spyder/widgets/sourcecode/codeeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -924,9 +924,14 @@ def get_current_object(self):

@Slot()
def delete(self):
"""Remove selected text"""
if self.has_selected_text():
self.remove_selected_text()
"""Remove selected text or next character."""
if not self.has_selected_text():
cursor = self.textCursor()
position = cursor.position()
if not cursor.atEnd():
cursor.setPosition(position + 1, QTextCursor.KeepAnchor)
self.setTextCursor(cursor)
self.remove_selected_text()

#------Find occurrences
def __find_first(self, text):
Expand Down
75 changes: 75 additions & 0 deletions spyder/widgets/sourcecode/tests/test_codeeditor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# -*- coding: utf-8 -*-
#
# Copyright © Spyder Project Contributors
# Licensed under the terms of the MIT License
#

"""
Tests for codeeditor.py.
"""

try:
from unittest.mock import Mock
except ImportError:
from mock import Mock # Python 2

# Third party imports
import pytest
from qtpy.QtGui import QTextCursor

# Local imports
from spyder import version_info
import spyder.widgets.sourcecode.codeeditor as codeeditor


@pytest.fixture
def code_editor_bot(qtbot):
"""Create code editor with default Python code."""
editor = codeeditor.CodeEditor(parent=None)
indent_chars = ' ' * 4
tab_stop_width_spaces = 4
editor.setup_editor(language='Python', indent_chars=indent_chars,
tab_stop_width_spaces=tab_stop_width_spaces)
# Mock the screen updates and signal emits to test when they've been
# called.
editor.linenumberarea = Mock()
if version_info > (4, ):
editor.sig_flags_changed = Mock()
else:
editor.get_linenumberarea_width = Mock(return_value=1)
editor.breakpoints_changed = Mock()
return editor, qtbot


# --- Tests
# -----------------------------------------------------------------------------
def test_delete(code_editor_bot, mocker):
"""Test CodeEditor.delete()."""
editor, qtbot = code_editor_bot
text = ('def f1(a, b):\n')
editor.set_text(text)

# Move to start and delete next character without selection.
cursor = editor.textCursor()
cursor.movePosition(QTextCursor.Start)
editor.setTextCursor(cursor)
editor.delete()
assert editor.get_text_line(0) == 'ef f1(a, b):'

# Delete selection.
cursor = editor.textCursor()
cursor.select(QTextCursor.WordUnderCursor)
editor.setTextCursor(cursor)
editor.delete()
assert editor.get_text_line(0) == ' f1(a, b):'

# Move to end of document - nothing to delete after cursor.
cursor = editor.textCursor()
cursor.movePosition(QTextCursor.End)
editor.setTextCursor(cursor)
editor.delete()
assert editor.get_text_line(0) == ' f1(a, b):'


if __name__ == "__main__":
pytest.main()

0 comments on commit 6aeade9

Please sign in to comment.