Merge remote-tracking branch 'origin/pr/4382'

This commit is contained in:
Florian Bruhin 2018-10-30 08:56:03 +01:00
commit e23b6ef64e
2 changed files with 22 additions and 10 deletions

View File

@ -94,7 +94,7 @@ class CommandLineEdit(QLineEdit):
We use __ here to avoid accidentally overriding it in subclasses. We use __ here to avoid accidentally overriding it in subclasses.
""" """
if new < self._promptlen: if new < self._promptlen:
self.setCursorPosition(self._promptlen) self.cursorForward(self.hasSelectedText(), self._promptlen - new)
def set_prompt(self, text): def set_prompt(self, text):
"""Set the current prompt to text. """Set the current prompt to text.
@ -105,13 +105,6 @@ class CommandLineEdit(QLineEdit):
self._validator.prompt = text self._validator.prompt = text
self._promptlen = len(text) self._promptlen = len(text)
def home(self, mark):
"""Override home so it works properly with our cursor restriction."""
oldpos = self.cursorPosition()
self.setCursorPosition(self._promptlen)
if mark:
self.setSelection(self._promptlen, oldpos - self._promptlen)
class _CommandValidator(QValidator): class _CommandValidator(QValidator):

View File

@ -59,8 +59,8 @@ class TestCommandLineEdit:
assert cmd_edit.text() == ':hello' assert cmd_edit.text() == ':hello'
assert cmd_edit.cursorPosition() == len(':hello') assert cmd_edit.cursorPosition() == len(':hello')
cmd_edit.home(mark=True) cmd_edit.home(True)
assert cmd_edit.cursorPosition() == len(':hello') assert cmd_edit.cursorPosition() == len(':')
qtbot.keyClick(cmd_edit, Qt.Key_Delete) qtbot.keyClick(cmd_edit, Qt.Key_Delete)
assert cmd_edit.text() == ':' assert cmd_edit.text() == ':'
qtbot.keyClick(cmd_edit, Qt.Key_Backspace) qtbot.keyClick(cmd_edit, Qt.Key_Backspace)
@ -74,6 +74,25 @@ class TestCommandLineEdit:
qtbot.keyClicks(cmd_edit, '$hello') qtbot.keyClicks(cmd_edit, '$hello')
assert cmd_edit.text() == '' assert cmd_edit.text() == ''
def test_selection_home(self, qtbot, cmd_edit):
"""Test selection persisting when pressing home."""
qtbot.keyClicks(cmd_edit, ':hello')
assert cmd_edit.text() == ':hello'
assert cmd_edit.cursorPosition() == len(':hello')
cmd_edit.home(True)
assert cmd_edit.cursorPosition() == len(':')
assert cmd_edit.selectionStart() == len(':')
def test_selection_backspace(self, qtbot, cmd_edit):
"""Test selection persisting when backspacing to the first char."""
qtbot.keyClicks(cmd_edit, ':hello')
assert cmd_edit.text() == ':hello'
assert cmd_edit.cursorPosition() == len(':hello')
for _ in range(len(':hello')):
qtbot.keyClick(cmd_edit, Qt.Key_Left, modifier=Qt.ShiftModifier)
assert cmd_edit.cursorPosition() == len(':')
assert cmd_edit.selectionStart() == len(':')
class WrappedWidget(QWidget): class WrappedWidget(QWidget):