From 32fa1ff1e990e08110dfc7fe41b57d82bbdf2c6d Mon Sep 17 00:00:00 2001 From: Ryan Roden-Corrent Date: Sun, 23 Jul 2017 19:32:24 -0400 Subject: [PATCH] Expand history completion results if on last index. When tabbing to the last index of history completion, call expandAll which will call fetchMore to retrieve more query results, if available. Calling fetchMore directly will not update the view, and for some reason self.expand(idx.parent()) and self.expand(self.model().index(idx.row(), 0)) did not work, so I'm using expandAll. Fixes #2841. --- qutebrowser/completion/completionwidget.py | 4 +++ .../unit/completion/test_completionwidget.py | 34 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/qutebrowser/completion/completionwidget.py b/qutebrowser/completion/completionwidget.py index 6e1e51680..f6c72027b 100644 --- a/qutebrowser/completion/completionwidget.py +++ b/qutebrowser/completion/completionwidget.py @@ -255,6 +255,10 @@ class CompletionView(QTreeView): selmodel.setCurrentIndex( idx, QItemSelectionModel.ClearAndSelect | QItemSelectionModel.Rows) + # if the last item is focused, try to fetch more + if idx.row() == self.model().rowCount(idx.parent()) - 1: + self.expandAll() + count = self.model().count() if count == 0: self.hide() diff --git a/tests/unit/completion/test_completionwidget.py b/tests/unit/completion/test_completionwidget.py index 207e557a8..09552bcda 100644 --- a/tests/unit/completion/test_completionwidget.py +++ b/tests/unit/completion/test_completionwidget.py @@ -65,6 +65,9 @@ def completionview(qtbot, status_command_stub, config_stub, win_registry, } # mock the Completer that the widget creates in its constructor mocker.patch('qutebrowser.completion.completer.Completer', autospec=True) + mocker.patch( + 'qutebrowser.completion.completiondelegate.CompletionItemDelegate', + new=lambda *_: None) view = completionwidget.CompletionView(win_id=0) qtbot.addWidget(view) return view @@ -185,6 +188,37 @@ def test_completion_item_focus_no_model(which, completionview, qtbot): completionview.completion_item_focus(which) +def test_completion_item_focus_fetch(completionview, qtbot): + """Test that on_next_prev_item moves the selection properly. + + Args: + which: the direction in which to move the selection. + tree: Each list represents a completion category, with each string + being an item under that category. + expected: expected argument from on_selection_changed for each + successive movement. None implies no signal should be + emitted. + """ + model = completionmodel.CompletionModel() + cat = mock.Mock(spec=['layoutChanged', 'layoutAboutToBeChanged', + 'canFetchMore', 'fetchMore', 'rowCount', 'index', 'data']) + cat.canFetchMore = lambda *_: True + cat.rowCount = lambda *_: 2 + cat.fetchMore = mock.Mock() + model.add_category(cat) + completionview.set_model(model) + # clear the fetchMore call that happens on set_model + cat.reset_mock() + + # not at end, fetchMore shouldn't be called + completionview.completion_item_focus('next') + assert not cat.fetchMore.called + + # at end, fetchMore should be called + completionview.completion_item_focus('next') + assert cat.fetchMore.called + + @pytest.mark.parametrize('show', ['always', 'auto', 'never']) @pytest.mark.parametrize('rows', [[], ['Aa'], ['Aa', 'Bb']]) @pytest.mark.parametrize('quick_complete', [True, False])