From 0f4cecfa3ee5323557d08d0834b508eff20a8764 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 4 Aug 2013 18:59:31 +0200 Subject: [PATCH] BufferCompleter refactoring, add support for explicit filename completion with c-f --- src/input_handler.cc | 52 ++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/src/input_handler.cc b/src/input_handler.cc index 83579f02..bf0cd368 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -593,7 +593,24 @@ public: m_completions = BufferCompletion{}; m_context.ui().menu_hide(); } -private: + + template + bool try_complete() + { + auto& buffer = m_context.buffer(); + BufferCoord cursor_pos = m_context.editor().main_selection().last(); + m_completions = (this->*complete_func)(buffer, cursor_pos); + if (not m_completions.is_valid()) + return false; + + kak_assert(cursor_pos >= m_completions.begin); + m_matching_candidates = m_completions.candidates; + m_current_candidate = m_matching_candidates.size(); + m_context.ui().menu_hide(); + menu_show(); + m_matching_candidates.push_back(buffer.string(m_completions.begin, m_completions.end)); + return true; + } using StringList = std::vector; template @@ -706,6 +723,7 @@ private: return {}; } +private: void on_option_changed(const Option& opt) override { if (opt.name() == "completions") @@ -729,26 +747,17 @@ private: { if (not m_completions.is_valid()) { - auto& buffer = m_context.buffer(); auto& completers = options()["completers"].get(); - BufferCoord cursor_pos = m_context.editor().main_selection().last(); - if (contains(completers, "option")) - m_completions = complete_opt(buffer, cursor_pos); - if (not m_completions.is_valid() and contains(completers, "word=buffer")) - m_completions = complete_word(buffer, cursor_pos); - if (not m_completions.is_valid() and contains(completers, "word=all")) - m_completions = complete_word(buffer, cursor_pos); - if (not m_completions.is_valid() and contains(completers, "filename")) - m_completions = complete_filename(buffer, cursor_pos); - if (not m_completions.is_valid()) - return false; + if (contains(completers, "option") and try_complete<&BufferCompleter::complete_opt>()) + return true; + if (contains(completers, "word=buffer") and try_complete<&BufferCompleter::complete_word>()) + return true; + if (contains(completers, "word=all") and try_complete<&BufferCompleter::complete_word>()) + return true; + if (contains(completers, "filename") and try_complete<&BufferCompleter::complete_filename>()) + return true; - kak_assert(cursor_pos >= m_completions.begin); - - m_matching_candidates = m_completions.candidates; - m_current_candidate = m_matching_candidates.size(); - menu_show(); - m_matching_candidates.push_back(buffer.string(m_completions.begin, m_completions.end)); + return false; } return true; } @@ -841,6 +850,11 @@ public: m_completer.select(-1); update_completions = false; } + else if ( key == Key{ Key::Modifiers::Control, 'f' }) + { + m_completer.try_complete<&BufferCompleter::complete_filename>(); + update_completions = false; + } if (update_completions) m_idle_timer.set_next_date(Clock::now() + idle_timeout);