Cleanup InsertCompleter get rid of unneeded candidate vector copy

This commit is contained in:
Maxime Coste 2015-12-28 00:18:38 +00:00
parent 5217089902
commit e938040e35
2 changed files with 13 additions and 14 deletions

View File

@ -339,10 +339,10 @@ void InsertCompleter::select(int offset, Vector<Key>& keystrokes)
return; return;
auto& buffer = m_context.buffer(); auto& buffer = m_context.buffer();
m_current_candidate = (m_current_candidate + offset) % (int)m_matching_candidates.size(); m_current_candidate = (m_current_candidate + offset) % (int)m_completions.candidates.size();
if (m_current_candidate < 0) if (m_current_candidate < 0)
m_current_candidate += m_matching_candidates.size(); m_current_candidate += m_completions.candidates.size();
const InsertCompletion::Candidate& candidate = m_matching_candidates[m_current_candidate]; const InsertCompletion::Candidate& candidate = m_completions.candidates[m_current_candidate];
auto& selections = m_context.selections(); auto& selections = m_context.selections();
const auto& cursor_pos = selections.main().cursor(); const auto& cursor_pos = selections.main().cursor();
const auto prefix_len = buffer.distance(m_completions.begin, cursor_pos); const auto prefix_len = buffer.distance(m_completions.begin, cursor_pos);
@ -438,7 +438,7 @@ void InsertCompleter::menu_show()
CharCoord menu_pos = m_context.window().display_position(m_completions.begin); CharCoord menu_pos = m_context.window().display_position(m_completions.begin);
Vector<DisplayLine> menu_entries; Vector<DisplayLine> menu_entries;
for (auto& candidate : m_matching_candidates) for (auto& candidate : m_completions.candidates)
menu_entries.push_back(candidate.menu_entry); menu_entries.push_back(candidate.menu_entry);
m_context.ui().menu_show(menu_entries, menu_pos, m_context.ui().menu_show(menu_entries, menu_pos,
@ -451,8 +451,8 @@ void InsertCompleter::menu_show()
void InsertCompleter::on_option_changed(const Option& opt) void InsertCompleter::on_option_changed(const Option& opt)
{ {
// Do not reset the menu if the user has selected an entry // Do not reset the menu if the user has selected an entry
if (not m_matching_candidates.empty() and if (not m_completions.candidates.empty() and
m_current_candidate != m_matching_candidates.size() - 1) m_current_candidate != m_completions.candidates.size() - 1)
return; return;
auto& completers = m_options["completers"].get<InsertCompleterDescList>(); auto& completers = m_options["completers"].get<InsertCompleterDescList>();
@ -486,10 +486,9 @@ bool InsertCompleter::try_complete(Func complete_func)
return false; return false;
kak_assert(cursor_pos >= m_completions.begin); kak_assert(cursor_pos >= m_completions.begin);
m_matching_candidates = m_completions.candidates; m_current_candidate = m_completions.candidates.size();
m_current_candidate = m_matching_candidates.size();
menu_show(); menu_show();
m_matching_candidates.push_back({buffer.string(m_completions.begin, m_completions.end), ""}); m_completions.candidates.push_back({buffer.string(m_completions.begin, m_completions.end), ""});
return true; return true;
} }

View File

@ -1,15 +1,18 @@
#ifndef insert_completer_hh_INCLUDED #ifndef insert_completer_hh_INCLUDED
#define insert_completer_hh_INCLUDED #define insert_completer_hh_INCLUDED
#include "buffer.hh"
#include "option_manager.hh" #include "option_manager.hh"
#include "display_buffer.hh" #include "display_buffer.hh"
#include "vector.hh"
#include "optional.hh" #include "optional.hh"
namespace Kakoune namespace Kakoune
{ {
class Buffer;
struct Key;
struct InsertCompleterDesc struct InsertCompleterDesc
{ {
enum Mode enum Mode
@ -82,16 +85,13 @@ private:
void menu_show(); void menu_show();
using CandidateList = InsertCompletion::CandidateList;
const Context& m_context; const Context& m_context;
OptionManager& m_options; OptionManager& m_options;
InsertCompletion m_completions; InsertCompletion m_completions;
CandidateList m_matching_candidates;
int m_current_candidate = -1; int m_current_candidate = -1;
using CompleteFunc = InsertCompletion (const Buffer&, ByteCoord, const OptionManager& options); using CompleteFunc = InsertCompletion (const Buffer&, ByteCoord, const OptionManager& options);
std::function<CompleteFunc> m_explicit_completer; CompleteFunc* m_explicit_completer = nullptr;
}; };
} }