From 541872cafac3b905ab59c5a4f49fbfd5cde4c54a Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 18 Jan 2012 11:33:58 +0000 Subject: [PATCH] completion support for addgrouphl and rmgrouphl --- src/highlighters.cc | 6 ++++++ src/highlighters.hh | 3 +++ src/main.cc | 21 +++++++++++++++++++-- src/window.cc | 15 +++++++++++++++ src/window.hh | 4 +++- 5 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/highlighters.cc b/src/highlighters.cc index 0a20fbd9..dd5ddf1b 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -282,6 +282,12 @@ void HighlighterGroup::remove_highlighter(const std::string& id) m_highlighters.remove(id); } +CandidateList HighlighterGroup::complete_highlighterid(const std::string& prefix, + size_t cursor_pos) +{ + return m_highlighters.complete_id(prefix, cursor_pos); +} + HighlighterAndId HighlighterGroup::create(Window& window, const HighlighterParameters& params) { diff --git a/src/highlighters.hh b/src/highlighters.hh index 803a0bf9..10903b17 100644 --- a/src/highlighters.hh +++ b/src/highlighters.hh @@ -20,6 +20,9 @@ public: void add_highlighter(HighlighterAndId&& highlighter); void remove_highlighter(const std::string& id); + CandidateList complete_highlighterid(const std::string& prefix, + size_t cursor_pos); + static HighlighterAndId create(Window& window, const HighlighterParameters& params); private: diff --git a/src/main.cc b/src/main.cc index ff9e54f0..8ae523a7 100644 --- a/src/main.cc +++ b/src/main.cc @@ -966,14 +966,31 @@ int main(int argc, char* argv[]) PerArgumentCommandCompleter { std::bind(&HighlighterRegistry::complete_highlighter, &highlighter_registry, _1, _2) }); - command_manager.register_command(std::vector{ "agh", "addgrouphl" }, add_group_highlighter); + command_manager.register_command(std::vector{ "agh", "addgrouphl" }, add_group_highlighter, + CommandManager::None, + PerArgumentCommandCompleter { + [&](const std::string& prefix, size_t cursor_pos) + { return main_context.window().complete_highlighter_groupid(prefix, cursor_pos); }, + std::bind(&HighlighterRegistry::complete_highlighter, &highlighter_registry, _1, _2) + }); command_manager.register_command(std::vector{ "rh", "rmhl" }, rm_highlighter, CommandManager::None, PerArgumentCommandCompleter { [&](const std::string& prefix, size_t cursor_pos) { return main_context.window().complete_highlighterid(prefix, cursor_pos); } }); - command_manager.register_command(std::vector{ "rgh", "rmgrouphl" }, rm_group_highlighter); + command_manager.register_command(std::vector{ "rgh", "rmgrouphl" }, rm_group_highlighter, + CommandManager::None, + [&](const CommandParameters& params, size_t token_to_complete, size_t pos_in_token) + { + Window& w = main_context.window(); + const std::string& arg = token_to_complete < params.size() ? + params[token_to_complete] : std::string(); + if (token_to_complete == 0) + return w.complete_highlighter_groupid(arg, pos_in_token); + else if (token_to_complete == 1) + return w.get_highlighter_group(params[0]).complete_highlighterid(arg, pos_in_token); + }); command_manager.register_command(std::vector{ "af", "addfilter" }, add_filter, CommandManager::None, PerArgumentCommandCompleter { diff --git a/src/window.cc b/src/window.cc index 966ffdb6..e0033a78 100644 --- a/src/window.cc +++ b/src/window.cc @@ -424,6 +424,21 @@ CandidateList Window::complete_highlighterid(const std::string& prefix, return m_highlighters.complete_id(prefix, cursor_pos); } +CandidateList Window::complete_highlighter_groupid(const std::string& prefix, + size_t cursor_pos) +{ + CandidateList all = m_highlighters.complete_id(prefix, cursor_pos); + CandidateList result; + for (auto& id : all) + { + auto group_it = m_highlighters.find(id); + if (group_it != m_highlighters.end() and + group_it->second.target()) + result.push_back(id); + } + return result; +} + void Window::add_filter(FilterAndId&& filter) { if (m_filters.contains(filter.first)) diff --git a/src/window.hh b/src/window.hh index da49c0d1..fe7b458d 100644 --- a/src/window.hh +++ b/src/window.hh @@ -107,7 +107,9 @@ public: HighlighterGroup& get_highlighter_group(const std::string& id); CandidateList complete_highlighterid(const std::string& prefix, - size_t cursor_pos = std::string::npos); + size_t cursor_pos); + CandidateList complete_highlighter_groupid(const std::string& prefix, + size_t cursor_pos); void add_filter(FilterAndId&& filter); void remove_filter(const std::string& id);