Don't instantiate completion models nedlessly.

For real this time. A mistake on the last commit like this meant models
were still spuriously instantiated.
Now that the completion model is reused, the layoutChanged signal needs
to be forwarded through, otherwise the view will not update.
This commit is contained in:
Ryan Roden-Corrent 2017-06-06 20:40:24 -04:00
parent 42243d3d97
commit 565ba23f8c
4 changed files with 9 additions and 11 deletions

View File

@ -224,20 +224,19 @@ class Completer(QObject):
if func is None: if func is None:
log.completion.debug('Clearing completion') log.completion.debug('Clearing completion')
completion.set_model(None) completion.set_model(None)
elif func != self._last_completion_func: self._last_completion_func = None
return
if func != self._last_completion_func:
self._last_completion_func = func self._last_completion_func = func
args = (x for x in before_cursor[1:] if not x.startswith('-')) args = (x for x in before_cursor[1:] if not x.startswith('-'))
with debug.log_time(log.completion, with debug.log_time(log.completion,
'Instantiate {} completion'.format(func.__name__)): 'Starting {} completion'.format(func.__name__)):
model = func(*args) model = func(*args)
with debug.log_time(log.completion, 'Set completion model'): with debug.log_time(log.completion, 'Set completion model'):
completion.set_model(model) completion.set_model(model)
completion.set_pattern(pattern)
else:
log.completion.debug('Setting pattern {}'.format(pattern))
completion.set_pattern(pattern)
self._last_completion_func = None completion.set_pattern(pattern)
def _change_completed_part(self, newtext, before, after, immediate=False): def _change_completed_part(self, newtext, before, after, immediate=False):
"""Change the part we're currently completing in the commandline. """Change the part we're currently completing in the commandline.

View File

@ -283,8 +283,6 @@ class CompletionView(QTreeView):
self._column_widths = model.column_widths self._column_widths = model.column_widths
self._active = True self._active = True
self.set_pattern('')
if (config.get('completion', 'show') == 'always' and if (config.get('completion', 'show') == 'always' and
model.count() > 0): model.count() > 0):
self.show() self.show()

View File

@ -63,6 +63,7 @@ class CompletionModel(QAbstractItemModel):
def add_category(self, cat): def add_category(self, cat):
"""Add a completion category to the model.""" """Add a completion category to the model."""
self._categories.append(cat) self._categories.append(cat)
cat.layoutChanged.connect(self.layoutChanged)
def data(self, index, role=Qt.DisplayRole): def data(self, index, role=Qt.DisplayRole):
"""Return the item data for index. """Return the item data for index.

View File

@ -56,7 +56,7 @@ def test_first_last_item(counts):
def test_count(counts): def test_count(counts):
model = completionmodel.CompletionModel() model = completionmodel.CompletionModel()
for c in counts: for c in counts:
cat = mock.Mock(spec=['rowCount']) cat = mock.Mock(spec=['rowCount', 'layoutChanged'])
cat.rowCount = mock.Mock(return_value=c) cat.rowCount = mock.Mock(return_value=c)
model.add_category(cat) model.add_category(cat)
assert model.count() == sum(counts) assert model.count() == sum(counts)
@ -66,7 +66,7 @@ def test_count(counts):
def test_set_pattern(pat): def test_set_pattern(pat):
"""Validate the filtering and sorting results of set_pattern.""" """Validate the filtering and sorting results of set_pattern."""
model = completionmodel.CompletionModel() model = completionmodel.CompletionModel()
cats = [mock.Mock(spec=['set_pattern'])] * 3 cats = [mock.Mock(spec=['set_pattern', 'layoutChanged'])] * 3
for c in cats: for c in cats:
c.set_pattern = mock.Mock() c.set_pattern = mock.Mock()
model.add_category(c) model.add_category(c)