Skip to content

Commit

Permalink
Explicitely call try_accept on InsertCompleter
Browse files Browse the repository at this point in the history
Calling it as part of the insert method was error prone and often
led to slightly surprising behaviour, such as the <c-r><esc> hiding
the menu on <esc>.
  • Loading branch information
mawww committed Sep 2, 2024
1 parent 6e5bc9d commit c1ce1d7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
17 changes: 8 additions & 9 deletions src/input_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1303,15 +1303,19 @@ class Insert : public InputMode
selections.sort_and_merge_overlapping();
}
else if (auto cp = key.codepoint())
{
m_completer.try_accept();
insert(*cp);
}
else if (key == ctrl('r'))
{
m_completer.try_accept();
on_next_key_with_autoinfo(context(), "register", KeymapMode::None,
[this](Key key, Context&) {
auto cp = key.codepoint();
if (not cp or key == Key::Escape)
return;
insert([&] { return RegisterManager::instance()[*cp].get(context()); });
insert(RegisterManager::instance()[*cp].get(context()));
}, "enter register name", register_doc.str());
update_completions = false;
}
Expand Down Expand Up @@ -1359,6 +1363,7 @@ class Insert : public InputMode
}
else if (key == ctrl('v'))
{
m_completer.try_accept();
on_next_key_with_autoinfo(context(), "raw-insert", KeymapMode::None,
[this, transient](Key key, Context&) {
if (auto cp = get_raw_codepoint(key))
Expand Down Expand Up @@ -1387,6 +1392,7 @@ class Insert : public InputMode

void paste(StringView content) override
{
m_completer.try_accept();
insert(ConstArrayView<StringView>{content});
m_idle_timer.set_next_date(Clock::now() + get_idle_timeout(context()));
}
Expand Down Expand Up @@ -1424,20 +1430,13 @@ class Insert : public InputMode
template<typename S>
void insert(ConstArrayView<S> strings)
{
m_completer.try_accept();
kak_assert(not m_completer.has_candidate_selected());
context().selections().for_each([strings, &buffer=context().buffer()]
(size_t index, Selection& sel) {
Kakoune::insert(buffer, sel, sel.cursor(), strings[std::min(strings.size()-1, index)]);
}, false);
}

template<std::invocable Func>
void insert(Func&& lazy_strings)
{
m_completer.try_accept();
insert(std::forward<Func>(lazy_strings)());
}

void insert(Codepoint key)
{
String str{key};
Expand Down
4 changes: 2 additions & 2 deletions src/insert_completer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -481,12 +481,12 @@ 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;
return m_completions.is_valid() and m_current_candidate >= 0 and m_current_candidate < m_completions.candidates.size() - 1;
}

void InsertCompleter::try_accept()
{
if (m_completions.is_valid() and has_candidate_selected())
if (has_candidate_selected())
reset();
}

Expand Down
3 changes: 2 additions & 1 deletion src/insert_completer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public:
void explicit_line_buffer_complete();
void explicit_line_all_complete();

bool has_candidate_selected() const;

private:
bool setup_ifn();

Expand All @@ -98,7 +100,6 @@ private:
void on_option_changed(const Option& opt) override;

void menu_show();
bool has_candidate_selected() const;

Context& m_context;
OptionManager& m_options;
Expand Down

0 comments on commit c1ce1d7

Please sign in to comment.