From 565ba23f8cd33fd777324a85794cd3dc7dc4b72f Mon Sep 17 00:00:00 2001 From: Ryan Roden-Corrent Date: Tue, 6 Jun 2017 20:40:24 -0400 Subject: [PATCH] Don't instantiate completion models nedlessly. For real this time. A mistake on the last commit like this meant models were still spuriously instantiated. Now that the completion model is reused, the layoutChanged signal needs to be forwarded through, otherwise the view will not update. --- qutebrowser/completion/completer.py | 13 ++++++------- qutebrowser/completion/completionwidget.py | 2 -- qutebrowser/completion/models/completionmodel.py | 1 + tests/unit/completion/test_completionmodel.py | 4 ++-- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/qutebrowser/completion/completer.py b/qutebrowser/completion/completer.py index ae2e9e4bb..491fe2e04 100644 --- a/qutebrowser/completion/completer.py +++ b/qutebrowser/completion/completer.py @@ -224,20 +224,19 @@ class Completer(QObject): if func is None: log.completion.debug('Clearing completion') completion.set_model(None) - elif func != self._last_completion_func: + self._last_completion_func = None + return + + if func != self._last_completion_func: self._last_completion_func = func args = (x for x in before_cursor[1:] if not x.startswith('-')) with debug.log_time(log.completion, - 'Instantiate {} completion'.format(func.__name__)): + 'Starting {} completion'.format(func.__name__)): model = func(*args) with debug.log_time(log.completion, 'Set completion model'): completion.set_model(model) - completion.set_pattern(pattern) - else: - log.completion.debug('Setting pattern {}'.format(pattern)) - completion.set_pattern(pattern) - self._last_completion_func = None + completion.set_pattern(pattern) def _change_completed_part(self, newtext, before, after, immediate=False): """Change the part we're currently completing in the commandline. diff --git a/qutebrowser/completion/completionwidget.py b/qutebrowser/completion/completionwidget.py index 1f8b3d9f4..711301b4c 100644 --- a/qutebrowser/completion/completionwidget.py +++ b/qutebrowser/completion/completionwidget.py @@ -283,8 +283,6 @@ class CompletionView(QTreeView): self._column_widths = model.column_widths self._active = True - self.set_pattern('') - if (config.get('completion', 'show') == 'always' and model.count() > 0): self.show() diff --git a/qutebrowser/completion/models/completionmodel.py b/qutebrowser/completion/models/completionmodel.py index 9543792ed..cbc20470c 100644 --- a/qutebrowser/completion/models/completionmodel.py +++ b/qutebrowser/completion/models/completionmodel.py @@ -63,6 +63,7 @@ class CompletionModel(QAbstractItemModel): def add_category(self, cat): """Add a completion category to the model.""" self._categories.append(cat) + cat.layoutChanged.connect(self.layoutChanged) def data(self, index, role=Qt.DisplayRole): """Return the item data for index. diff --git a/tests/unit/completion/test_completionmodel.py b/tests/unit/completion/test_completionmodel.py index 8d43f2883..8f8acced2 100644 --- a/tests/unit/completion/test_completionmodel.py +++ b/tests/unit/completion/test_completionmodel.py @@ -56,7 +56,7 @@ def test_first_last_item(counts): def test_count(counts): model = completionmodel.CompletionModel() for c in counts: - cat = mock.Mock(spec=['rowCount']) + cat = mock.Mock(spec=['rowCount', 'layoutChanged']) cat.rowCount = mock.Mock(return_value=c) model.add_category(cat) assert model.count() == sum(counts) @@ -66,7 +66,7 @@ def test_count(counts): def test_set_pattern(pat): """Validate the filtering and sorting results of set_pattern.""" model = completionmodel.CompletionModel() - cats = [mock.Mock(spec=['set_pattern'])] * 3 + cats = [mock.Mock(spec=['set_pattern', 'layoutChanged'])] * 3 for c in cats: c.set_pattern = mock.Mock() model.add_category(c)