Merge pull request #3208 from 7lb/refactor/make_completer_less_stateful

[RDY] Make completer less stateful
This commit is contained in:
Florian Bruhin 2017-11-03 10:47:52 +01:00 committed by GitHub
commit d53a96d9f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 15 deletions

View File

@ -43,7 +43,6 @@ class Completer(QObject):
Attributes:
_cmd: The statusbar Command object this completer belongs to.
_ignore_change: Whether to ignore the next completion update.
_timer: The timer used to trigger the completion update.
_last_cursor_pos: The old cursor position so we avoid double completion
updates.
@ -54,7 +53,6 @@ class Completer(QObject):
def __init__(self, cmd, parent=None):
super().__init__(parent)
self._cmd = cmd
self._ignore_change = False
self._timer = QTimer()
self._timer.setSingleShot(True)
self._timer.setInterval(0)
@ -178,13 +176,15 @@ class Completer(QObject):
text = self._quote(text)
model = self._model()
if model.count() == 1 and config.val.completion.quick:
# If we only have one item, we want to apply it immediately
# and go on to the next part.
self._change_completed_part(text, before, after, immediate=True)
# If we only have one item, we want to apply it immediately and go
# on to the next part, unless we are quick-completing the part
# after maxsplit, so that we don't keep offering completions
# (see issue #1519)
if maxsplit is not None and maxsplit < len(before):
# If we are quick-completing the part after maxsplit, don't
# keep offering completions (see issue #1519)
self._ignore_change = True
self._change_completed_part(text, before, after)
else:
self._change_completed_part(text, before, after,
immediate=True)
else:
self._change_completed_part(text, before, after)
@ -219,12 +219,6 @@ class Completer(QObject):
@pyqtSlot()
def _update_completion(self):
"""Check if completions are available and activate them."""
if self._ignore_change:
log.completion.debug("Ignoring completion update because "
"ignore_change is True.")
self._ignore_change = False
return
completion = self.parent()
if self._cmd.prefix() != ':':

View File

@ -274,7 +274,11 @@ def test_on_selection_changed(before, newtxt, after, completer_obj,
check(True, 2, after_txt, after_pos)
# quick-completing a single item should move the cursor ahead by 1 and add
# a trailing space if at the end of the cmd string
# a trailing space if at the end of the cmd string, unless the command has
# maxsplit < len(before) (such as :open in these tests)
if after_txt.startswith(':open'):
return
after_pos += 1
if after_pos > len(after_txt):
after_txt += ' '
@ -299,6 +303,11 @@ def test_quickcomplete_flicker(status_command_stub, completer_obj,
config_stub.val.completion.quick = True
_set_cmd_prompt(status_command_stub, ':open |')
completer_obj.schedule_completion_update()
assert completion_widget_stub.set_model.called
completion_widget_stub.set_model.reset_mock()
# selecting a completion should not re-set the model
completer_obj.on_selection_changed('http://example.com')
completer_obj.schedule_completion_update()
assert not completion_widget_stub.set_model.called