Option name completion support

This commit is contained in:
Maxime Coste 2012-04-03 18:25:27 +00:00
parent 1d106fd31e
commit 46c65fea10
3 changed files with 58 additions and 23 deletions

View File

@ -1004,15 +1004,27 @@ int main(int argc, char* argv[])
command_manager.register_commands({ "setg", "setglobal" }, command_manager.register_commands({ "setg", "setglobal" },
[&](const CommandParameters& params, const Context&) { set_option(option_manager, params); }, [&](const CommandParameters& params, const Context&) { set_option(option_manager, params); },
CommandManager::None); CommandManager::None,
PerArgumentCommandCompleter({
[&](const std::string& prefix, size_t cursor_pos)
{ return option_manager.complete_option_name(prefix, cursor_pos); }
}));
command_manager.register_commands({ "setb", "setbuffer" }, command_manager.register_commands({ "setb", "setbuffer" },
[&](const CommandParameters& params, const Context& context) [&](const CommandParameters& params, const Context& context)
{ set_option(context.buffer().option_manager(), params); }, { set_option(context.buffer().option_manager(), params); },
CommandManager::None); CommandManager::None,
PerArgumentCommandCompleter({
[&](const std::string& prefix, size_t cursor_pos)
{ return main_context.buffer().option_manager().complete_option_name(prefix, cursor_pos); }
}));
command_manager.register_commands({ "setw", "setwindow" }, command_manager.register_commands({ "setw", "setwindow" },
[&](const CommandParameters& params, const Context& context) [&](const CommandParameters& params, const Context& context)
{ set_option(context.window().option_manager(), params); }, { set_option(context.window().option_manager(), params); },
CommandManager::None); CommandManager::None,
PerArgumentCommandCompleter({
[&](const std::string& prefix, size_t cursor_pos)
{ return main_context.window().option_manager().complete_option_name(prefix, cursor_pos); }
}));
register_highlighters(); register_highlighters();
register_filters(); register_filters();

View File

@ -12,6 +12,44 @@ std::string int_to_str(int value)
return oss.str(); return oss.str();
} }
Option& OptionManager::operator[] (const std::string& name)
{
auto it = m_options.find(name);
if (it != m_options.end())
return it->second;
else if (m_parent)
return (*m_parent)[name];
else
return m_options[name];
}
const Option& OptionManager::operator[] (const std::string& name) const
{
auto it = m_options.find(name);
if (it != m_options.end())
return it->second;
else if (m_parent)
return (*m_parent)[name];
else
throw option_not_found(name);
}
CandidateList OptionManager::complete_option_name(const std::string& prefix,
size_t cursor_pos)
{
std::string real_prefix = prefix.substr(0, cursor_pos);
CandidateList result;
if (m_parent)
result = m_parent->complete_option_name(prefix, cursor_pos);
for (auto& option : m_options)
{
if (option.first.substr(0, real_prefix.length()) == real_prefix and
not contains(result, option.first))
result.push_back(option.first);
}
return result;
}
GlobalOptionManager::GlobalOptionManager() GlobalOptionManager::GlobalOptionManager()
: OptionManager() : OptionManager()
{ {

View File

@ -3,6 +3,7 @@
#include "utils.hh" #include "utils.hh"
#include "exception.hh" #include "exception.hh"
#include "completion.hh"
#include <unordered_map> #include <unordered_map>
@ -39,27 +40,11 @@ public:
OptionManager(OptionManager& parent) OptionManager(OptionManager& parent)
: m_parent(&parent) {} : m_parent(&parent) {}
Option& operator[] (const std::string& name) Option& operator[] (const std::string& name);
{ const Option& operator[] (const std::string& name) const;
auto it = m_options.find(name);
if (it != m_options.end())
return it->second;
else if (m_parent)
return (*m_parent)[name];
else
return m_options[name];
}
const Option& operator[] (const std::string& name) const CandidateList complete_option_name(const std::string& prefix,
{ size_t cursor_pos);
auto it = m_options.find(name);
if (it != m_options.end())
return it->second;
else if (m_parent)
return (*m_parent)[name];
else
throw option_not_found(name);
}
private: private:
OptionManager() OptionManager()