Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix completion sorting #3237

Merged
merged 2 commits into from
Nov 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions qutebrowser/completion/models/configmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def option(*, info):
model = completionmodel.CompletionModel(column_widths=(20, 70, 10))
options = ((opt.name, opt.description, info.config.get_str(opt.name))
for opt in configdata.DATA.values())
model.add_category(listcategory.ListCategory("Options", sorted(options)))
model.add_category(listcategory.ListCategory("Options", options))
return model


Expand All @@ -39,7 +39,7 @@ def customized_option(*, info):
options = ((opt.name, opt.description, info.config.get_str(opt.name))
for opt, _value in info.config)
model.add_category(listcategory.ListCategory("Customized options",
sorted(options)))
options))
return model


Expand All @@ -66,8 +66,7 @@ def value(optname, *_values, info):

vals = opt.typ.complete()
if vals is not None:
model.add_category(listcategory.ListCategory("Completions",
sorted(vals)))
model.add_category(listcategory.ListCategory("Completions", vals))
return model


Expand Down
27 changes: 21 additions & 6 deletions qutebrowser/completion/models/listcategory.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ListCategory(QSortFilterProxyModel):

"""Expose a list of items as a category for the CompletionModel."""

def __init__(self, name, items, delete_func=None, parent=None):
def __init__(self, name, items, sort=True, delete_func=None, parent=None):
super().__init__(parent)
self.name = name
self.srcmodel = QStandardItemModel(parent=self)
Expand All @@ -43,6 +43,7 @@ def __init__(self, name, items, delete_func=None, parent=None):
self.srcmodel.appendRow([QStandardItem(x) for x in item])
self.setSourceModel(self.srcmodel)
self.delete_func = delete_func
self._sort = sort

def set_pattern(self, val):
"""Setter for pattern.
Expand All @@ -60,19 +61,33 @@ def set_pattern(self, val):
sortcol = 0
self.sort(sortcol)

def lessThan(self, _lindex, rindex):
def lessThan(self, lindex, rindex):
"""Custom sorting implementation.

Prefers all items which start with self._pattern. Other than that, keep
items in their original order.
Prefers all items which start with self._pattern. Other than that, uses
normal Python string sorting.

Args:
_lindex: The QModelIndex of the left item (*left* < right)
lindex: The QModelIndex of the left item (*left* < right)
rindex: The QModelIndex of the right item (left < *right*)

Return:
True if left < right, else False
"""
qtutils.ensure_valid(lindex)
qtutils.ensure_valid(rindex)

left = self.srcmodel.data(lindex)
right = self.srcmodel.data(rindex)
return not right.startswith(self._pattern)

leftstart = left.startswith(self._pattern)
rightstart = right.startswith(self._pattern)

if leftstart and not rightstart:
return True
elif rightstart and not leftstart:
return False
elif self._sort:
return left < right
else:
return False
8 changes: 5 additions & 3 deletions qutebrowser/completion/models/miscmodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def helptopic(*, info):
for opt in configdata.DATA.values())

model.add_category(listcategory.ListCategory("Commands", cmdlist))
model.add_category(listcategory.ListCategory("Settings", sorted(settings)))
model.add_category(listcategory.ListCategory("Settings", settings))
return model


Expand All @@ -59,7 +59,8 @@ def delete(data):
model = completionmodel.CompletionModel(column_widths=(30, 70, 0))
marks = objreg.get('quickmark-manager').marks.items()
model.add_category(listcategory.ListCategory('Quickmarks', marks,
delete_func=delete))
delete_func=delete,
sort=False))
return model


Expand All @@ -75,7 +76,8 @@ def delete(data):
model = completionmodel.CompletionModel(column_widths=(30, 70, 0))
marks = objreg.get('bookmark-manager').marks.items()
model.add_category(listcategory.ListCategory('Bookmarks', marks,
delete_func=delete))
delete_func=delete,
sort=False))
return model


Expand Down
4 changes: 2 additions & 2 deletions qutebrowser/completion/models/urlmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ def url(*, info):
bookmarks = objreg.get('bookmark-manager').marks.items()

model.add_category(listcategory.ListCategory(
'Quickmarks', quickmarks, delete_func=_delete_quickmark))
'Quickmarks', quickmarks, delete_func=_delete_quickmark, sort=False))
model.add_category(listcategory.ListCategory(
'Bookmarks', bookmarks, delete_func=_delete_bookmark))
'Bookmarks', bookmarks, delete_func=_delete_bookmark, sort=False))

if info.config.get('completion.web_history_max_items') != 0:
hist_cat = histcategory.HistoryCategory(delete_func=_delete_history)
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/completion/test_histcategory.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def test_remove_rows(hist, model_validator):
cat.set_pattern('')
hist.delete('url', 'foo')
cat.removeRows(0, 1)
model_validator.validate([('bar', 'Bar', '1970-01-01')])
model_validator.validate([('bar', 'Bar')])


def test_remove_rows_fetch(hist):
Expand Down
17 changes: 13 additions & 4 deletions tests/unit/completion/test_listcategory.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,36 @@
from qutebrowser.completion.models import listcategory


@pytest.mark.parametrize('pattern, before, after', [
@pytest.mark.parametrize('pattern, before, after, after_nosort', [
('foo',
[('foo', ''), ('bar', '')],
[('foo', '')],
[('foo', '')]),

('foo',
[('foob', ''), ('fooc', ''), ('fooa', '')],
[('fooa', ''), ('foob', ''), ('fooc', '')],
[('foob', ''), ('fooc', ''), ('fooa', '')]),

# prefer foobar as it starts with the pattern
('foo',
[('barfoo', ''), ('foobar', '')],
[('foobar', ''), ('barfoo', '')]),
[('barfoo', ''), ('foobaz', ''), ('foobar', '')],
[('foobar', ''), ('foobaz', ''), ('barfoo', '')],
[('foobaz', ''), ('foobar', ''), ('barfoo', '')]),

('foo',
[('foo', 'bar'), ('bar', 'foo'), ('bar', 'bar')],
[('foo', 'bar'), ('bar', 'foo')],
[('foo', 'bar'), ('bar', 'foo')]),
])
def test_set_pattern(pattern, before, after, model_validator):
def test_set_pattern(pattern, before, after, after_nosort, model_validator):
"""Validate the filtering and sorting results of set_pattern."""
cat = listcategory.ListCategory('Foo', before)
model_validator.set_model(cat)
cat.set_pattern(pattern)
model_validator.validate(after)

cat = listcategory.ListCategory('Foo', before, sort=False)
model_validator.set_model(cat)
cat.set_pattern(pattern)
model_validator.validate(after_nosort)
6 changes: 3 additions & 3 deletions tests/unit/completion/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,9 @@ def test_session_completion(qtmodeltester, session_manager_stub):
qtmodeltester.check(model)

_check_completions(model, {
"Sessions": [('default', None, None),
('1', None, None),
('2', None, None)]
"Sessions": [('1', None, None),
('2', None, None),
('default', None, None)]
})


Expand Down