diff --git a/qutebrowser/completion/completer.py b/qutebrowser/completion/completer.py index 1434d549e..11d9abe0e 100644 --- a/qutebrowser/completion/completer.py +++ b/qutebrowser/completion/completer.py @@ -257,25 +257,19 @@ class Completer(QObject): model = self._get_new_completion(parts, self._cursor_part) - if model != self._model(): - completion.set_model(model) - - if model is None: - log.completion.debug("No completion model for {}.".format(parts)) - return - try: pattern = parts[self._cursor_part].strip() except IndexError: pattern = '' - completion.set_pattern(pattern) - log.completion.debug( - "New completion for {}: {}, with pattern '{}'".format( - parts, model.srcmodel.__class__.__name__, pattern)) + if model is None: + log.completion.debug("No completion model for {}.".format(parts)) + else: + log.completion.debug( + "New completion for {}: {}, with pattern '{}'".format( + parts, model.srcmodel.__class__.__name__, pattern)) - if self._model().count() == 0: - completion.hide() + completion.set_model(model, pattern) def _split(self, keep=False): """Get the text split up in parts. diff --git a/qutebrowser/completion/completionwidget.py b/qutebrowser/completion/completionwidget.py index 9d53df17a..5ca8e29de 100644 --- a/qutebrowser/completion/completionwidget.py +++ b/qutebrowser/completion/completionwidget.py @@ -246,13 +246,14 @@ class CompletionView(QTreeView): elif config.get('completion', 'show') == 'auto': self.show() - def set_model(self, model): + def set_model(self, model, pattern=None): """Switch completion to a new model. Called from on_update_completion(). Args: model: The model to use. + pattern: The filter pattern to set (what the user entered). """ if model is None: self._active = False @@ -260,17 +261,19 @@ class CompletionView(QTreeView): return old_model = self.model() - sel_model = self.selectionModel() + if model != old_model: + sel_model = self.selectionModel() - self.setModel(model) - self._active = True + self.setModel(model) + self._active = True - if sel_model is not None: - sel_model.deleteLater() - if old_model is not None: - old_model.deleteLater() + if sel_model is not None: + sel_model.deleteLater() + if old_model is not None: + old_model.deleteLater() - if config.get('completion', 'show') == 'always': + if (config.get('completion', 'show') == 'always' + and model.count() > 0): self.show() else: self.hide() @@ -278,21 +281,13 @@ class CompletionView(QTreeView): for i in range(model.rowCount()): self.expand(model.index(i, 0)) + if pattern is not None: + model.set_pattern(pattern) + self._column_widths = model.srcmodel.COLUMN_WIDTHS self._resize_columns() self.maybe_resize_completion() - def set_pattern(self, pattern): - """Set the completion pattern for the current model. - - Called from on_update_completion(). - - Args: - pattern: The filter pattern to set (what the user entered). - """ - self.model().set_pattern(pattern) - self.maybe_resize_completion() - @pyqtSlot() def maybe_resize_completion(self): """Emit the resize_completion signal if the config says so.""" diff --git a/tests/unit/completion/test_completionwidget.py b/tests/unit/completion/test_completionwidget.py index deb7a55f7..9e67a83db 100644 --- a/tests/unit/completion/test_completionwidget.py +++ b/tests/unit/completion/test_completionwidget.py @@ -83,8 +83,7 @@ def test_set_model(completionview): def test_set_pattern(completionview): model = sortfilter.CompletionFilterModel(base.BaseCompletionModel()) model.set_pattern = unittest.mock.Mock() - completionview.set_model(model) - completionview.set_pattern('foo') + completionview.set_model(model, 'foo') model.set_pattern.assert_called_with('foo')