summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Rosenzweig <mail4jasonr@gmail.com>2021-06-05 13:34:45 +0800
committerJason Rosenzweig <mail4jasonr@gmail.com>2021-06-05 13:34:45 +0800
commit68cdae184a4d5f0c0542c8092c156164511f58a3 (patch)
tree681b3ae64ab3ae16de1df377d26f904307b80b40
parent412e428e9d2da8f85c6a3bb42fe5ac6d2fe5cb78 (diff)
downloadqutebrowser-68cdae184a4d5f0c0542c8092c156164511f58a3.tar.gz
qutebrowser-68cdae184a4d5f0c0542c8092c156164511f58a3.zip
Less redundant code
-rw-r--r--qutebrowser/mainwindow/prompt.py22
-rw-r--r--tests/unit/mainwindow/test_prompt.py8
2 files changed, 13 insertions, 17 deletions
diff --git a/qutebrowser/mainwindow/prompt.py b/qutebrowser/mainwindow/prompt.py
index 65f2e95c0..b096f2da7 100644
--- a/qutebrowser/mainwindow/prompt.py
+++ b/qutebrowser/mainwindow/prompt.py
@@ -629,20 +629,17 @@ class FilenamePrompt(_BasePrompt):
self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
self._to_complete = ''
+ self._root_index = QModelIndex()
def _directories_hide_show_model(self, path):
"""Get rid of non-matching directories."""
- 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")
+ 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)
@pyqtSlot(str)
def _set_fileview_root(self, path, *, tabbed=False):
@@ -679,7 +676,6 @@ class FilenamePrompt(_BasePrompt):
self._file_view.setRootIndex(self._root_index)
self._directories_hide_show_model(path)
- self._file_view.setModel(self._file_model)
@pyqtSlot(QModelIndex)
def _insert_path(self, index, *, clicked=True):
@@ -779,7 +775,7 @@ class FilenamePrompt(_BasePrompt):
self._insert_path(idx, clicked=False)
def _do_completion(self, idx, which):
- while idx.isValid() and self._file_view.isIndexHidden(idx):
+ while idx.isValid() and self._file_view.isIndexHidden(idx):
if which == 'prev':
idx = self._file_view.indexAbove(idx)
else:
diff --git a/tests/unit/mainwindow/test_prompt.py b/tests/unit/mainwindow/test_prompt.py
index d5ef5a363..cb394e8c7 100644
--- a/tests/unit/mainwindow/test_prompt.py
+++ b/tests/unit/mainwindow/test_prompt.py
@@ -98,9 +98,9 @@ class TestFileCompletion:
assert prompt._lineedit.text() == str(testdir / 'bar')
@pytest.mark.parametrize("keys,expected",
- [([], ['bar', 'bat', 'foo']),
- ([Qt.Key_F], ['foo']),
- ([Qt.Key_A], ['bar', 'bat'])])
+ [([], ['..', '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'
@@ -118,7 +118,7 @@ class TestFileCompletion:
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() != "..":
+ if not prompt._file_view.isRowHidden(index.row(), index.parent()):
visible.append(index.data())
assert visible == expected