From 57155e329ada002245ab3fac45d906f6707c14cf Mon Sep 17 00:00:00 2001 From: mohite-abhi Date: Sun, 23 Jan 2022 19:06:47 +0530 Subject: Fixes qutebrowser/qutebrowser#6967 by adding win id param in _tabs & using it in delete_tabs As delete_tab was assuming that completion column contains window ID, it was showing exception in case of tab-focus, as it doesn't have the window ID in completion column. So instead a new parameter named current_win_id is used in _tabs which is also passed in all uses of the function. --- qutebrowser/completion/models/miscmodels.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/qutebrowser/completion/models/miscmodels.py b/qutebrowser/completion/models/miscmodels.py index d8ebafb29..34948a452 100644 --- a/qutebrowser/completion/models/miscmodels.py +++ b/qutebrowser/completion/models/miscmodels.py @@ -103,17 +103,21 @@ def session(*, info=None): return model -def _tabs(*, win_id_filter=lambda _win_id: True, add_win_id=True): +def _tabs(*, win_id_filter=lambda _win_id: True, add_win_id=True, current_win_id): """Helper to get the completion model for tabs/other_tabs. Args: win_id_filter: A filter function for window IDs to include. Should return True for all included windows. add_win_id: Whether to add the window ID to the completion items. + current_win_id: Window ID to be passed from info.win_id """ def delete_tab(data): """Close the selected tab.""" - win_id, tab_index = data[0].split('/') + + win_id = current_win_id + tab_index = data[0].split('/')[-1] # data[0] can be 'tabInd' or 'winID/tabInd' + tabbed_browser = objreg.get('tabbed-browser', scope='window', window=int(win_id)) tabbed_browser.on_tab_close_requested(int(tab_index) - 1) @@ -177,13 +181,13 @@ def other_tabs(*, info): Used for the tab-take command. """ - return _tabs(win_id_filter=lambda win_id: win_id != info.win_id) + return _tabs(win_id_filter=lambda win_id: win_id != info.win_id, current_win_id=info.win_id) def tab_focus(*, info): """A model to complete on open tabs in the current window.""" model = _tabs(win_id_filter=lambda win_id: win_id == info.win_id, - add_win_id=False) + add_win_id=False, current_win_id=info.win_id) special = [ ("last", "Focus the last-focused tab"), -- cgit v1.2.3-54-g00ecf From 3b27bf9fb7b968025b9d348b311679c6111d3968 Mon Sep 17 00:00:00 2001 From: mohite-abhi Date: Sun, 23 Jan 2022 21:49:33 +0530 Subject: Fixed linter errors --- qutebrowser/completion/models/miscmodels.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/qutebrowser/completion/models/miscmodels.py b/qutebrowser/completion/models/miscmodels.py index 34948a452..f461a0fbb 100644 --- a/qutebrowser/completion/models/miscmodels.py +++ b/qutebrowser/completion/models/miscmodels.py @@ -103,7 +103,7 @@ def session(*, info=None): return model -def _tabs(*, win_id_filter=lambda _win_id: True, add_win_id=True, current_win_id): +def _tabs(*, win_id_filter=lambda _win_id: True, add_win_id=True, current_win_id=0): """Helper to get the completion model for tabs/other_tabs. Args: @@ -114,7 +114,6 @@ def _tabs(*, win_id_filter=lambda _win_id: True, add_win_id=True, current_win_id """ def delete_tab(data): """Close the selected tab.""" - win_id = current_win_id tab_index = data[0].split('/')[-1] # data[0] can be 'tabInd' or 'winID/tabInd' -- cgit v1.2.3-54-g00ecf From 426b280b1bb8fc7cc336d56e0ae7e9cca584ab89 Mon Sep 17 00:00:00 2001 From: mohite-abhi Date: Sun, 23 Jan 2022 22:33:27 +0530 Subject: fix more linter issues --- qutebrowser/completion/models/miscmodels.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/qutebrowser/completion/models/miscmodels.py b/qutebrowser/completion/models/miscmodels.py index f461a0fbb..2b106d904 100644 --- a/qutebrowser/completion/models/miscmodels.py +++ b/qutebrowser/completion/models/miscmodels.py @@ -103,19 +103,20 @@ def session(*, info=None): return model -def _tabs(*, win_id_filter=lambda _win_id: True, add_win_id=True, current_win_id=0): +def _tabs(*, win_id_filter=lambda _win_id: True, add_win_id=True, cur_win_id=0): """Helper to get the completion model for tabs/other_tabs. Args: win_id_filter: A filter function for window IDs to include. Should return True for all included windows. add_win_id: Whether to add the window ID to the completion items. - current_win_id: Window ID to be passed from info.win_id + cur_win_id: Window ID to be passed from info.win_id """ def delete_tab(data): """Close the selected tab.""" - win_id = current_win_id - tab_index = data[0].split('/')[-1] # data[0] can be 'tabInd' or 'winID/tabInd' + win_id = cur_win_id + # data[0] can be 'tabInd' or 'winID/tabInd' + tab_index = data[0].split('/')[-1] tabbed_browser = objreg.get('tabbed-browser', scope='window', window=int(win_id)) @@ -180,13 +181,14 @@ def other_tabs(*, info): Used for the tab-take command. """ - return _tabs(win_id_filter=lambda win_id: win_id != info.win_id, current_win_id=info.win_id) + return _tabs(win_id_filter=lambda win_id: win_id != info.win_id, + cur_win_id=info.win_id) def tab_focus(*, info): """A model to complete on open tabs in the current window.""" model = _tabs(win_id_filter=lambda win_id: win_id == info.win_id, - add_win_id=False, current_win_id=info.win_id) + add_win_id=False, cur_win_id=info.win_id) special = [ ("last", "Focus the last-focused tab"), -- cgit v1.2.3-54-g00ecf From d89d5853d393390b3a62c33084bf8150858a2fee Mon Sep 17 00:00:00 2001 From: mohite-abhi Date: Sun, 23 Jan 2022 23:43:20 +0530 Subject: Fix linter errors --- qutebrowser/completion/models/miscmodels.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/qutebrowser/completion/models/miscmodels.py b/qutebrowser/completion/models/miscmodels.py index 2b106d904..0aa30d184 100644 --- a/qutebrowser/completion/models/miscmodels.py +++ b/qutebrowser/completion/models/miscmodels.py @@ -181,8 +181,9 @@ def other_tabs(*, info): Used for the tab-take command. """ - return _tabs(win_id_filter=lambda win_id: win_id != info.win_id, - cur_win_id=info.win_id) + return _tabs( + win_id_filter=lambda win_id: win_id != info.win_id, + cur_win_id=info.win_id) def tab_focus(*, info): -- cgit v1.2.3-54-g00ecf From ef87b5df725efe89cff68f8b4d1f60905a6f4b8f Mon Sep 17 00:00:00 2001 From: mohite-abhi Date: Tue, 25 Jan 2022 04:22:50 +0530 Subject: Add tests for tab focus completion delete --- qutebrowser/completion/models/miscmodels.py | 10 ++++++---- tests/unit/completion/test_models.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/qutebrowser/completion/models/miscmodels.py b/qutebrowser/completion/models/miscmodels.py index 0aa30d184..77072c720 100644 --- a/qutebrowser/completion/models/miscmodels.py +++ b/qutebrowser/completion/models/miscmodels.py @@ -103,7 +103,7 @@ def session(*, info=None): return model -def _tabs(*, win_id_filter=lambda _win_id: True, add_win_id=True, cur_win_id=0): +def _tabs(*, win_id_filter=lambda _win_id: True, add_win_id=True, cur_win_id=None): """Helper to get the completion model for tabs/other_tabs. Args: @@ -114,9 +114,11 @@ def _tabs(*, win_id_filter=lambda _win_id: True, add_win_id=True, cur_win_id=0): """ def delete_tab(data): """Close the selected tab.""" - win_id = cur_win_id - # data[0] can be 'tabInd' or 'winID/tabInd' - tab_index = data[0].split('/')[-1] + if cur_win_id is None: + win_id, tab_index = data[0].split('/') + else: + win_id = cur_win_id + tab_index = data[0] tabbed_browser = objreg.get('tabbed-browser', scope='window', window=int(win_id)) diff --git a/tests/unit/completion/test_models.py b/tests/unit/completion/test_models.py index 9e6743083..b94c19a06 100644 --- a/tests/unit/completion/test_models.py +++ b/tests/unit/completion/test_models.py @@ -867,6 +867,34 @@ def test_tab_completion_delete(qtmodeltester, fake_web_tab, win_registry, QUrl('https://duckduckgo.com')] +def test_tab_focus_completion_delete(qtmodeltester, fake_web_tab, win_registry, + tabbed_browser_stubs, info): + """Verify closing a tab by deleting it from the completion widget.""" + tabbed_browser_stubs[0].widget.tabs = [ + fake_web_tab(QUrl('https://github.com'), 'GitHub', 0), + fake_web_tab(QUrl('https://wikipedia.org'), 'Wikipedia', 1), + fake_web_tab(QUrl('https://duckduckgo.com'), 'DuckDuckGo', 2) + ] + tabbed_browser_stubs[1].widget.tabs = [ + fake_web_tab(QUrl('https://wiki.archlinux.org'), 'ArchWiki', 0), + ] + model = miscmodels.tab_focus(info=info) + model.set_pattern('') + qtmodeltester.check(model) + + parent = model.index(0, 0) + idx = model.index(1, 0, parent) + + # # sanity checks + assert model.data(parent) == "Tabs" + assert model.data(idx) == '2' + + model.delete_cur_item(idx) + actual = [tab.url() for tab in tabbed_browser_stubs[0].widget.tabs] + assert actual == [QUrl('https://github.com'), + QUrl('https://duckduckgo.com')] + + def test_tab_completion_not_sorted(qtmodeltester, fake_web_tab, win_registry, tabbed_browser_stubs): """Ensure that the completion row order is the same as tab index order. -- cgit v1.2.3-54-g00ecf