From e12bf4978c1c4bfe9e6b1d731cb835badf04255d Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 2 Jan 2014 19:20:16 +0000 Subject: [PATCH] Try prefix completion and then subsequence completion for option names fixes #4 --- src/option_manager.cc | 20 +++++++++++++++----- src/option_manager.hh | 3 +++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/option_manager.cc b/src/option_manager.cc index 4bb8d2da..58589624 100644 --- a/src/option_manager.cc +++ b/src/option_manager.cc @@ -63,25 +63,35 @@ const Option& OptionManager::operator[](const String& name) const throw option_not_found(name); } -CandidateList OptionManager::complete_option_name(const String& prefix, - ByteCount cursor_pos) +template +CandidateList OptionManager::get_matching_names(MatchingFunc func) { - String real_prefix = prefix.substr(0, cursor_pos); CandidateList result; if (m_parent) - result = m_parent->complete_option_name(prefix, cursor_pos); + result = m_parent->get_matching_names(func); for (auto& option : m_options) { if (option->flags() & Option::Flags::Hidden) continue; const auto& name = option->name(); - if (prefix_match(name, real_prefix) and not contains(result, name)) + if (func(name) and not contains(result, name)) result.push_back(name); } return result; } +CandidateList OptionManager::complete_option_name(const String& prefix, + ByteCount cursor_pos) +{ + using namespace std::placeholders; + String real_prefix = prefix.substr(0, cursor_pos); + auto result = get_matching_names(std::bind(prefix_match, _1, std::ref(real_prefix))); + if (result.empty()) + result = get_matching_names(std::bind(subsequence_match, _1, std::ref(real_prefix))); + return result; +} + OptionManager::OptionList OptionManager::flatten_options() const { OptionList res = m_parent ? m_parent->flatten_options() : OptionList{}; diff --git a/src/option_manager.hh b/src/option_manager.hh index a8688b27..ae73e4f8 100644 --- a/src/option_manager.hh +++ b/src/option_manager.hh @@ -91,6 +91,9 @@ private: // the only one allowed to construct a root option manager friend class GlobalOptions; + template + CandidateList get_matching_names(MatchingFunc func); + std::vector> m_options; OptionManager* m_parent;