From 2a7335edefc8bc47e5f0501c4a973aacf15d47fc Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sat, 29 Mar 2014 13:18:46 +0000 Subject: [PATCH] Add basic support for colalias completion --- src/color_registry.cc | 13 +++++++++++++ src/color_registry.hh | 3 +++ src/commands.cc | 9 ++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/color_registry.cc b/src/color_registry.cc index 36d19e72e..0820409f9 100644 --- a/src/color_registry.cc +++ b/src/color_registry.cc @@ -36,6 +36,19 @@ void ColorRegistry::register_alias(const String& name, const String& colordesc, it->second : parse_color_pair(colordesc); } +CandidateList ColorRegistry::complete_alias_name(const String& prefix, + ByteCount cursor_pos) const +{ + CandidateList res; + String real_prefix = prefix.substr(0, cursor_pos); + for (auto& alias : m_aliases) + { + if (prefix_match(alias.first, real_prefix)) + res.push_back(alias.first); + } + return res; +} + ColorRegistry::ColorRegistry() : m_aliases{ { "PrimarySelection", { Colors::Cyan, Colors::Blue } }, diff --git a/src/color_registry.hh b/src/color_registry.hh index 730ea0d16..3eff6fabf 100644 --- a/src/color_registry.hh +++ b/src/color_registry.hh @@ -3,6 +3,7 @@ #include "color.hh" #include "utils.hh" +#include "completion.hh" #include @@ -18,6 +19,8 @@ public: void register_alias(const String& name, const String& colordesc, bool override = false); + CandidateList complete_alias_name(const String& prefix, + ByteCount cursor_pos) const; private: std::unordered_map m_aliases; }; diff --git a/src/commands.cc b/src/commands.cc index 3b19e5f78..48e549bc6 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -1113,13 +1113,20 @@ const CommandDesc try_catch_cmd = { } }; +static Completions complete_colalias(const Context&, CompletionFlags flags, + const String& prefix, ByteCount cursor_pos) +{ + return {0_byte, prefix.length(), + ColorRegistry::instance().complete_alias_name(prefix, cursor_pos)}; +} + const CommandDesc define_color_alias_cmd = { "colalias", "ca", "colalias : set to refer to color (which can be an alias itself)", ParameterDesc{ SwitchMap{}, ParameterDesc::Flags::None, 2, 2 }, CommandFlags::None, - CommandCompleter{}, + PerArgumentCommandCompleter({ complete_colalias, complete_colalias }), [](const ParametersParser& parser, Context& context) { ColorRegistry::instance().register_alias(parser[0], parser[1], true);