Refactor Completer/CompletionView interface, removing set_pattern()

This commit is contained in:
Marshall Lochbaum 2016-08-21 22:53:00 -04:00
parent e4cec43cf4
commit cbecd48871
3 changed files with 23 additions and 35 deletions

View File

@ -257,25 +257,19 @@ class Completer(QObject):
model = self._get_new_completion(parts, self._cursor_part)
if model != self._model():
completion.set_model(model)
if model is None:
log.completion.debug("No completion model for {}.".format(parts))
return
try:
pattern = parts[self._cursor_part].strip()
except IndexError:
pattern = ''
completion.set_pattern(pattern)
log.completion.debug(
"New completion for {}: {}, with pattern '{}'".format(
parts, model.srcmodel.__class__.__name__, pattern))
if model is None:
log.completion.debug("No completion model for {}.".format(parts))
else:
log.completion.debug(
"New completion for {}: {}, with pattern '{}'".format(
parts, model.srcmodel.__class__.__name__, pattern))
if self._model().count() == 0:
completion.hide()
completion.set_model(model, pattern)
def _split(self, keep=False):
"""Get the text split up in parts.

View File

@ -246,13 +246,14 @@ class CompletionView(QTreeView):
elif config.get('completion', 'show') == 'auto':
self.show()
def set_model(self, model):
def set_model(self, model, pattern=None):
"""Switch completion to a new model.
Called from on_update_completion().
Args:
model: The model to use.
pattern: The filter pattern to set (what the user entered).
"""
if model is None:
self._active = False
@ -260,17 +261,19 @@ class CompletionView(QTreeView):
return
old_model = self.model()
sel_model = self.selectionModel()
if model != old_model:
sel_model = self.selectionModel()
self.setModel(model)
self._active = True
self.setModel(model)
self._active = True
if sel_model is not None:
sel_model.deleteLater()
if old_model is not None:
old_model.deleteLater()
if sel_model is not None:
sel_model.deleteLater()
if old_model is not None:
old_model.deleteLater()
if config.get('completion', 'show') == 'always':
if (config.get('completion', 'show') == 'always'
and model.count() > 0):
self.show()
else:
self.hide()
@ -278,21 +281,13 @@ class CompletionView(QTreeView):
for i in range(model.rowCount()):
self.expand(model.index(i, 0))
if pattern is not None:
model.set_pattern(pattern)
self._column_widths = model.srcmodel.COLUMN_WIDTHS
self._resize_columns()
self.maybe_resize_completion()
def set_pattern(self, pattern):
"""Set the completion pattern for the current model.
Called from on_update_completion().
Args:
pattern: The filter pattern to set (what the user entered).
"""
self.model().set_pattern(pattern)
self.maybe_resize_completion()
@pyqtSlot()
def maybe_resize_completion(self):
"""Emit the resize_completion signal if the config says so."""

View File

@ -83,8 +83,7 @@ def test_set_model(completionview):
def test_set_pattern(completionview):
model = sortfilter.CompletionFilterModel(base.BaseCompletionModel())
model.set_pattern = unittest.mock.Mock()
completionview.set_model(model)
completionview.set_pattern('foo')
completionview.set_model(model, 'foo')
model.set_pattern.assert_called_with('foo')