Skip to content

Commit

Permalink
Move newly added marks to the top.
Browse files Browse the repository at this point in the history
This causes recently added marks to appear first in the completion.
As suggested by @cryzed in qutebrowser#3688.
  • Loading branch information
rcorre committed Mar 31, 2018
1 parent f14613a commit 6c6a83b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
7 changes: 7 additions & 0 deletions qutebrowser/browser/urlmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ def add(self, url, title, tags, *, toggle=False):
raise AlreadyExistsError("Bookmark already exists!")
else:
self._marks[urlstr] = Bookmark(urlstr, title or '', tags or [])
# place new marks at the end
self._marks.move_to_end(urlstr, last=False)
self.changed.emit()
return True

Expand All @@ -154,6 +156,11 @@ def save(self):
self._lineparser.save()

def get(self, url):
"""Get a bookmark, or None if no such mark exists.
Args:
url: The QUrl of the mark to find.
"""
urlstr = url.toString(QUrl.RemovePassword | QUrl.FullyEncoded)
return self._marks.get(urlstr)

Expand Down
18 changes: 9 additions & 9 deletions tests/unit/browser/urlmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,16 @@ def test_add(bm_file, fake_save_manager, qtbot):
with qtbot.wait_signal(bm.changed):
bm.add(QUrl('http://example.com/notitle'), '', [])
assert list(bm) == [
urlmarks.Bookmark('http://example.com', 'Example Site', []),
urlmarks.Bookmark('http://example.com/notitle', '', []),
urlmarks.Bookmark('http://example.com', 'Example Site', []),
]

with qtbot.wait_signal(bm.changed):
bm.add(QUrl('http://example.com/tagged'), '', ['some', 'tag'])
assert list(bm) == [
urlmarks.Bookmark('http://example.com', 'Example Site', []),
urlmarks.Bookmark('http://example.com/notitle', '', []),
urlmarks.Bookmark('http://example.com/tagged', '', ['some', 'tag']),
urlmarks.Bookmark('http://example.com/notitle', '', []),
urlmarks.Bookmark('http://example.com', 'Example Site', []),
]


Expand Down Expand Up @@ -120,8 +120,8 @@ def test_delete(bm_file, fake_save_manager, qtbot):
with qtbot.wait_signal(bm.changed):
bm.delete('http://example.com/bar')
assert list(bm) == [
urlmarks.Bookmark('http://example.com/foo', 'Foo', []),
urlmarks.Bookmark('http://example.com/baz', 'Baz', []),
urlmarks.Bookmark('http://example.com/foo', 'Foo', []),
]


Expand All @@ -133,9 +133,9 @@ def test_save(bm_file, fake_save_manager, qtbot):
bm.add(QUrl('http://example.com/tags'), '', ['a', 'b'])
bm.save()
assert bm_file.read().splitlines() == [
'{"url": "http://example.com", "title": "Example Site", "tags": []}',
'{"url": "http://example.com/notitle", "title": "", "tags": []}',
'{"url": "http://example.com/tags", "title": "", "tags": ["a", "b"]}',
'{"url": "http://example.com/notitle", "title": "", "tags": []}',
'{"url": "http://example.com", "title": "Example Site", "tags": []}',
]


Expand All @@ -144,13 +144,13 @@ def test_get(bm_file, fake_save_manager, qtbot):

bm.add(QUrl('http://example.com'), 'Example Site', ['a', 'b'])

assert bm.get('http://example.com') == urlmarks.Bookmark(
assert bm.get(QUrl('http://example.com')) == urlmarks.Bookmark(
url='http://example.com',
title='Example Site',
tags=['a', 'b'],
)

assert bm.get('http://example.com/nope') is None
assert bm.get(QUrl('http://example.com/nope')) is None


def test_update(bm_file, fake_save_manager, qtbot):
Expand All @@ -167,4 +167,4 @@ def test_update(bm_file, fake_save_manager, qtbot):
with qtbot.wait_signal(bm.changed):
bm.update(newmark)

assert bm.get('http://example.com') == newmark
assert bm.get(QUrl('http://example.com')) == newmark

0 comments on commit 6c6a83b

Please sign in to comment.