Code style cleanups around insert completer

This commit is contained in:
Maxime Coste 2022-06-04 10:50:09 +10:00
parent a16de52f9c
commit 6ffec75406
3 changed files with 29 additions and 33 deletions

View File

@ -1345,10 +1345,7 @@ public:
selections.sort_and_merge_overlapping(); selections.sort_and_merge_overlapping();
} }
else if (auto cp = key.codepoint()) else if (auto cp = key.codepoint())
{
m_completer.try_accept();
insert(*cp); insert(*cp);
}
else if (key == ctrl('r')) else if (key == ctrl('r'))
{ {
on_next_key_with_autoinfo(context(), "register", KeymapMode::None, on_next_key_with_autoinfo(context(), "register", KeymapMode::None,
@ -1356,7 +1353,6 @@ public:
auto cp = key.codepoint(); auto cp = key.codepoint();
if (not cp or key == Key::Escape) if (not cp or key == Key::Escape)
return; return;
m_completer.try_accept();
insert(RegisterManager::instance()[*cp].get(context())); insert(RegisterManager::instance()[*cp].get(context()));
}, "enter register name", register_doc.str()); }, "enter register name", register_doc.str());
update_completions = false; update_completions = false;
@ -1419,7 +1415,6 @@ public:
[this, transient](Key key, Context&) { [this, transient](Key key, Context&) {
if (auto cp = get_raw_codepoint(key)) if (auto cp = get_raw_codepoint(key))
{ {
m_completer.try_accept();
insert(*cp); insert(*cp);
context().hooks().run_hook(Hook::InsertKey, key_to_str(key), context()); context().hooks().run_hook(Hook::InsertKey, key_to_str(key), context());
if (enabled() and not transient) if (enabled() and not transient)
@ -1473,6 +1468,7 @@ private:
void insert(ConstArrayView<String> strings) void insert(ConstArrayView<String> strings)
{ {
m_completer.try_accept();
context().selections().for_each([strings, &buffer=context().buffer()] context().selections().for_each([strings, &buffer=context().buffer()]
(size_t index, Selection& sel) { (size_t index, Selection& sel) {
Kakoune::insert(buffer, sel, sel.cursor(), strings[std::min(strings.size()-1, index)]); Kakoune::insert(buffer, sel, sel.cursor(), strings[std::min(strings.size()-1, index)]);
@ -1482,10 +1478,7 @@ private:
void insert(Codepoint key) void insert(Codepoint key)
{ {
String str{key}; String str{key};
context().selections().for_each([&buffer=context().buffer(), &str] insert(str);
(size_t index, Selection& sel) {
Kakoune::insert(buffer, sel, sel.cursor(), str);
});
context().hooks().run_hook(Hook::InsertChar, str, context()); context().hooks().run_hook(Hook::InsertChar, str, context());
} }

View File

@ -474,38 +474,40 @@ void InsertCompleter::update(bool allow_implicit)
auto& get_first(BufferRange& range) { return range.begin; } auto& get_first(BufferRange& range) { return range.begin; }
auto& get_last(BufferRange& range) { return range.end; } auto& get_last(BufferRange& range) { return range.end; }
bool InsertCompleter::has_candidate_selected() const
{
return m_current_candidate >= 0 and m_current_candidate < m_completions.candidates.size() - 1;
}
void InsertCompleter::try_accept() void InsertCompleter::try_accept()
{ {
if (m_completions.is_valid() and m_context.has_client() if (m_completions.is_valid() and m_context.has_client() and has_candidate_selected())
and m_current_candidate >= 0 and m_current_candidate < m_completions.candidates.size() - 1)
{
reset(); reset();
}
} }
void InsertCompleter::reset() void InsertCompleter::reset()
{ {
if (m_explicit_completer or m_completions.is_valid()) if (not m_explicit_completer and not m_completions.is_valid())
{ return;
String hook_param;
if (m_context.has_client() and m_current_candidate >= 0 and m_current_candidate < m_completions.candidates.size() - 1)
{
auto& buffer = m_context.buffer();
update_ranges(buffer, m_completions.timestamp, m_inserted_ranges);
hook_param = join(m_inserted_ranges | filter([](auto&& r) { return not r.empty(); }) | transform([&](auto&& r) {
return selection_to_string(ColumnType::Byte, buffer, {r.begin, buffer.char_prev(r.end)});
}), ' ');
}
m_explicit_completer = nullptr; String hook_param;
m_completions = InsertCompletion{}; if (m_context.has_client() and has_candidate_selected())
m_inserted_ranges.clear(); {
if (m_context.has_client()) auto& buffer = m_context.buffer();
{ update_ranges(buffer, m_completions.timestamp, m_inserted_ranges);
m_context.client().menu_hide(); hook_param = join(m_inserted_ranges | filter([](auto&& r) { return not r.empty(); }) | transform([&](auto&& r) {
m_context.client().info_hide(); return selection_to_string(ColumnType::Byte, buffer, {r.begin, buffer.char_prev(r.end)});
m_context.hooks().run_hook(Hook::InsertCompletionHide, hook_param, m_context); }), ' ');
} }
m_explicit_completer = nullptr;
m_completions = InsertCompletion{};
m_inserted_ranges.clear();
if (m_context.has_client())
{
m_context.client().menu_hide();
m_context.client().info_hide();
m_context.hooks().run_hook(Hook::InsertCompletionHide, hook_param, m_context);
} }
} }

View File

@ -101,6 +101,7 @@ private:
void on_option_changed(const Option& opt) override; void on_option_changed(const Option& opt) override;
void menu_show(); void menu_show();
bool has_candidate_selected() const;
Context& m_context; Context& m_context;
OptionManager& m_options; OptionManager& m_options;