From ad9b090ddf362220e7879370538db0b5cd14fe34 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 8 Jun 2017 09:53:46 +0100 Subject: [PATCH] Fix quadratic behaviour in when selecting an insert completion Use the fast, linear time SelectionList::insert algorithm instead of an ad-hoc one that needs to call SelectionList::update after each modification of the buffer. Fixes #1417 --- src/insert_completer.cc | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/insert_completer.cc b/src/insert_completer.cc index c2659579..789cb0eb 100644 --- a/src/insert_completer.cc +++ b/src/insert_completer.cc @@ -373,18 +373,19 @@ void InsertCompleter::select(int offset, Vector& keystrokes) const auto suffix_len = std::max(0_byte, buffer.distance(cursor_pos, m_completions.end)); auto ref = buffer.string(m_completions.begin, m_completions.end); + Vector ranges; for (auto& sel : selections) { const auto& cursor = sel.cursor(); auto pos = buffer.iterator_at(cursor); if (cursor.column >= prefix_len and (pos + suffix_len) != buffer.end() and std::equal(ref.begin(), ref.end(), pos - prefix_len)) - { - buffer.replace((pos - prefix_len).coord(), - (pos + suffix_len).coord(), candidate.completion); - selections.update(); - } + ranges.push_back({(pos - prefix_len).coord(), (pos + suffix_len - 1).coord()}); } + if (not ranges.empty()) + SelectionList{buffer, std::move(ranges)}.insert(candidate.completion, InsertMode::Replace); + selections.update(); + m_completions.end = cursor_pos; m_completions.begin = buffer.advance(cursor_pos, -candidate.completion.length()); m_completions.timestamp = buffer.timestamp();