diff options
author | Jason Rosenzweig <mail4jasonr@gmail.com> | 2021-06-04 22:35:37 +0800 |
---|---|---|
committer | Jason Rosenzweig <mail4jasonr@gmail.com> | 2021-06-04 22:35:37 +0800 |
commit | 412e428e9d2da8f85c6a3bb42fe5ac6d2fe5cb78 (patch) | |
tree | 45bb7975fa36a3e4c793c5ff1ae8dbf0b8f66b41 | |
parent | 344a2d1f72bb881c66445b4450fab33890966bdc (diff) | |
download | qutebrowser-412e428e9d2da8f85c6a3bb42fe5ac6d2fe5cb78.tar.gz qutebrowser-412e428e9d2da8f85c6a3bb42fe5ac6d2fe5cb78.zip |
Took suggestions, added '..' back to tabbing, fixed tests
-rw-r--r-- | qutebrowser/mainwindow/prompt.py | 60 | ||||
-rw-r--r-- | tests/unit/mainwindow/test_prompt.py | 41 |
2 files changed, 40 insertions, 61 deletions
diff --git a/qutebrowser/mainwindow/prompt.py b/qutebrowser/mainwindow/prompt.py index ab84f902d..65f2e95c0 100644 --- a/qutebrowser/mainwindow/prompt.py +++ b/qutebrowser/mainwindow/prompt.py @@ -632,16 +632,17 @@ class FilenamePrompt(_BasePrompt): def _directories_hide_show_model(self, path): """Get rid of non-matching directories.""" - dirs = self._get_dirs() - for directory in dirs['invalid']: - directory = os.path.join(path, directory) - index = self._file_model.index(directory) - self._file_view.setRowHidden(index.row(), index.parent(), True) - - for directory in dirs['valid']: - directory = os.path.join(path, directory) - index = self._file_model.index(directory) - self._file_view.setRowHidden(index.row(), index.parent(), False) + try: + num_rows = self._file_model.rowCount(self._root_index) + for row in range(num_rows): + index = self._file_model.index(row, 0, self._root_index) + hidden = self._to_complete not in index.data() + if index.data() == '..': + hidden = False + self._file_view.setRowHidden(index.row(), index.parent(), hidden) + except FileNotFoundError: + log.prompt.debug("Directory doesn't exist, can't \ + get valid dirs") @pyqtSlot(str) def _set_fileview_root(self, path, *, tabbed=False): @@ -728,36 +729,6 @@ class FilenamePrompt(_BasePrompt): self._file_model.directoryLoaded.connect( lambda: self._file_model.sort(0)) - def _get_dirs(self): - dirs: Dict[str, List[str]] = {"valid": [], "invalid": []} - try: - num_rows = self._file_model.rowCount(self._root_index) - for row in range(num_rows): - index = self._file_model.index(row, 0, self._root_index) - hidden = self._to_complete not in index.data() - if not hidden: - dirs['valid'].append(index.data()) - else: - dirs['invalid'].append(index.data()) - except FileNotFoundError: - log.prompt.debug("Directory doesn't exist, can't \ - get valid dirs") - - return dirs - - def _do_completion(self, idx, which): - filename = self._file_model.fileName(idx) - valid_dirs = self._get_dirs() - while filename not in valid_dirs['valid'] and idx.isValid(): - if which == 'prev': - idx = self._file_view.indexAbove(idx) - else: - assert which == 'next', which - idx = self._file_view.indexBelow(idx) - filename = self._file_model.fileName(idx) - - return idx - def accept(self, value=None, save=False): self._check_save_support(save) text = value if value is not None else self._lineedit.text() @@ -807,6 +778,15 @@ class FilenamePrompt(_BasePrompt): QItemSelectionModel.Rows) self._insert_path(idx, clicked=False) + def _do_completion(self, idx, which): + while idx.isValid() and self._file_view.isIndexHidden(idx): + if which == 'prev': + idx = self._file_view.indexAbove(idx) + else: + assert which == 'next', which + idx = self._file_view.indexBelow(idx) + return idx + def _allowed_commands(self): return [('prompt-accept', 'Accept'), ('mode-leave', 'Abort')] diff --git a/tests/unit/mainwindow/test_prompt.py b/tests/unit/mainwindow/test_prompt.py index 09c8ef6f6..d5ef5a363 100644 --- a/tests/unit/mainwindow/test_prompt.py +++ b/tests/unit/mainwindow/test_prompt.py @@ -80,11 +80,12 @@ class TestFileCompletion: with qtbot.wait_signal(prompt._file_model.directoryLoaded): for _ in range(3): qtbot.keyPress(prompt._lineedit, Qt.Key_Backspace) + prompt._set_fileview_root(prompt._lineedit.text()) - # foo should get completed from f - + # ...and foo should get completed from f + prompt.item_focus('next') + assert prompt._lineedit.text() == str(tmp_path) prompt.item_focus('next') - assert prompt._lineedit.text() == str(testdir / 'foo') # Deleting /[foo] @@ -97,9 +98,9 @@ class TestFileCompletion: assert prompt._lineedit.text() == str(testdir / 'bar') @pytest.mark.parametrize("keys,expected", - [([], ['bar', 'foo', 'bat'].sort()), - (['F'], ['foo'].sort()), - (['Backspace', 'A'], ['bar', 'bat'].sort())]) + [([], ['bar', 'bat', 'foo']), + ([Qt.Key_F], ['foo']), + ([Qt.Key_A], ['bar', 'bat'])]) def test_filtering_path(self, qtbot, tmp_path, get_prompt, keys, expected): testdir = tmp_path / 'test' @@ -107,21 +108,19 @@ class TestFileCompletion: (testdir / directory).mkdir(parents=True) prompt = get_prompt(str(testdir) + os.sep) - - for _ in range(len(keys)): - for key in keys: - eval("qtbot.keyPress(prompt._lineedit, Qt.Key_{})".format(key)) - - num_rows = prompt._file_model.rowCount(prompt._root_index) - visible = [] - for row in range(num_rows): - parent = prompt._file_model.index( - os.path.basename(prompt._lineedit.text())) - index = prompt._file_model.index(row, 0, parent) - if prompt._file_view.isRowHidden(index.row(), index.parent()): - visible.append(index.data()) - - assert visible.sort() == expected + for key in keys: + qtbot.keyPress(prompt._lineedit, key) + prompt._set_fileview_root(prompt._lineedit.text()) + + num_rows = prompt._file_model.rowCount(prompt._file_view.rootIndex()) + visible = [] + for row in range(num_rows): + parent = prompt._file_model.index( + os.path.dirname(prompt._lineedit.text())) + index = prompt._file_model.index(row, 0, parent) + if not prompt._file_view.isRowHidden(index.row(), index.parent()) and index.data() != "..": + visible.append(index.data()) + assert visible == expected @pytest.mark.linux def test_root_path(self, get_prompt): |