From 10d887583fc420617cfa70d826d3ada535eb1abb Mon Sep 17 00:00:00 2001 From: Nicolas Ouellet-Payeur Date: Thu, 27 Feb 2020 10:36:15 -0500 Subject: [PATCH 2/2] Make `on_next_key_with_autoinfo()` respect `idle_timeout` The prompt and autocomplete normally wait for `idle_timeout` before showing suggestions, however commands like `g`, `v`, or the lead-key show Clippy instantly. This fixes the issue by making `on_next_key_with_autoinfo()` wait for `idle_timeout` before displaying suggestions. Fixes mawww/kakoune#3365 Fixes mawww/kakoune#2066 --- src/hook_manager.hh | 2 ++ src/input_handler.cc | 33 +++++++++++++++++++++------------ src/input_handler.hh | 13 +++++++++---- src/normal.cc | 2 +- 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/hook_manager.hh b/src/hook_manager.hh index 0132b951..8fc5f8d4 100644 --- a/src/hook_manager.hh +++ b/src/hook_manager.hh @@ -45,6 +45,7 @@ enum class Hook RuntimeError, PromptIdle, NormalIdle, + NextKeyIdle, NormalKey, ModeChange, RawKey, @@ -87,6 +88,7 @@ constexpr auto enum_desc(Meta::Type) {Hook::RuntimeError, "RuntimeError"}, {Hook::PromptIdle, "PromptIdle"}, {Hook::NormalIdle, "NormalIdle"}, + {Hook::NextKeyIdle, "NextKeyIdle"}, {Hook::NormalKey, "NormalKey"}, {Hook::ModeChange, "ModeChange"}, {Hook::RawKey, "RawKey"}, diff --git a/src/input_handler.cc b/src/input_handler.cc index f4029fcd..841de07c 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -273,7 +273,7 @@ public: context.print_status( { format("invalid register '{}'", *cp), context.faces()["Error"] }); - }, "enter target register", register_doc); + }, "enter target register", register_doc.str()); } else { @@ -817,7 +817,7 @@ public: display(); m_line_changed = true; m_refresh_completion_pending = true; - }, "enter register name", register_doc); + }, "enter register name", register_doc.str()); display(); return; } @@ -1100,8 +1100,10 @@ private: class NextKey : public InputMode { public: - NextKey(InputHandler& input_handler, String name, KeymapMode keymap_mode, KeyCallback callback) - : InputMode(input_handler), m_name{std::move(name)}, m_callback(std::move(callback)), m_keymap_mode(keymap_mode) {} + NextKey(InputHandler& input_handler, String name, KeymapMode keymap_mode, KeyCallback callback, + Timer::Callback idle_callback) + : InputMode(input_handler), m_name{std::move(name)}, m_callback(std::move(callback)), m_keymap_mode(keymap_mode), + m_idle_timer{Clock::now() + get_idle_timeout(context()), std::move(idle_callback)} {} void on_key(Key key) override { @@ -1122,9 +1124,10 @@ public: StringView name() const override { return m_name; } private: - String m_name; - KeyCallback m_callback; - KeymapMode m_keymap_mode; + String m_name; + KeyCallback m_callback; + KeymapMode m_keymap_mode; + Timer m_idle_timer; }; class Insert : public InputMode @@ -1274,7 +1277,7 @@ public: if (not cp or key == Key::Escape) return; insert(RegisterManager::instance()[*cp].get(context())); - }, "enter register name", register_doc); + }, "enter register name", register_doc.str()); update_completions = false; } else if (key == ctrl('n')) @@ -1588,9 +1591,11 @@ void InputHandler::menu(Vector choices, MenuCallback callback) push_mode(new InputModes::Menu(*this, std::move(choices), std::move(callback))); } -void InputHandler::on_next_key(StringView mode_name, KeymapMode keymap_mode, KeyCallback callback) +void InputHandler::on_next_key(StringView mode_name, KeymapMode keymap_mode, KeyCallback callback, + Timer::Callback idle_callback) { - push_mode(new InputModes::NextKey(*this, format("next-key[{}]", mode_name), keymap_mode, std::move(callback))); + push_mode(new InputModes::NextKey(*this, format("next-key[{}]", mode_name), keymap_mode, std::move(callback), + std::move(idle_callback))); } InputHandler::ScopedForceNormal::ScopedForceNormal(InputHandler& handler, NormalParams params) @@ -1706,10 +1711,14 @@ std::pair InputHandler::get_cursor_info() const return current_mode().get_cursor_info(); } +bool should_show_info(AutoInfo mask, const Context& context) +{ + return (context.options()["autoinfo"].get() & mask) and context.has_client(); +} + bool show_auto_info_ifn(StringView title, StringView info, AutoInfo mask, const Context& context) { - if (not (context.options()["autoinfo"].get() & mask) or - not context.has_client()) + if (not should_show_info(mask, context)) return false; context.client().info_show(title.str(), info.str(), {}, InfoStyle::Prompt); diff --git a/src/input_handler.hh b/src/input_handler.hh index 3638785b..8c93c141 100644 --- a/src/input_handler.hh +++ b/src/input_handler.hh @@ -11,6 +11,7 @@ #include "utils.hh" #include "safe_ptr.hh" #include "display_buffer.hh" +#include "event_manager.hh" namespace Kakoune { @@ -80,7 +81,8 @@ public: // execute callback on next keypress and returns to normal mode // if callback does not change the mode itself - void on_next_key(StringView mode_name, KeymapMode mode, KeyCallback callback); + void on_next_key(StringView mode_name, KeymapMode mode, KeyCallback callback, + Timer::Callback idle_callback = Timer::Callback{}); // process the given key void handle_key(Key key); @@ -171,19 +173,22 @@ constexpr auto enum_desc(Meta::Type) }); } +bool should_show_info(AutoInfo mask, const Context& context); bool show_auto_info_ifn(StringView title, StringView info, AutoInfo mask, const Context& context); void hide_auto_info_ifn(const Context& context, bool hide); template void on_next_key_with_autoinfo(const Context& context, StringView mode_name, KeymapMode keymap_mode, Cmd cmd, - StringView title, StringView info) + String title, String info) { - const bool hide = show_auto_info_ifn(title, info, AutoInfo::OnKey, context); context.input_handler().on_next_key(mode_name, - keymap_mode, [hide,cmd](Key key, Context& context) mutable { + keymap_mode, [cmd](Key key, Context& context) mutable { + bool hide = should_show_info(AutoInfo::OnKey, context); hide_auto_info_ifn(context, hide); cmd(key, context); + }, [&context, title=std::move(title), info=std::move(info)](Timer&) { + show_auto_info_ifn(title, info, AutoInfo::OnKey, context); }); } diff --git a/src/normal.cc b/src/normal.cc index 89659c4a..84dc8f79 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -1881,7 +1881,7 @@ void combine_selections(Context& context, SelectionList list, Func func, StringV list.set_main_index(sels.main_index()); } func(context, std::move(list)); - }, title, + }, title.str(), "'a': append lists\n" "'u': union\n" "'i': intersection\n"