Replace CommandManager::register_command*s* with register_alias

This commit is contained in:
Maxime Coste 2014-06-06 00:17:15 +01:00
parent ffd860c1da
commit 2d5c730441
3 changed files with 20 additions and 30 deletions

View File

@ -30,21 +30,11 @@ void CommandManager::register_command(String command_name,
std::move(completer) };
}
void CommandManager::register_commands(memoryview<String> command_names,
Command command,
String docstring,
ParameterDesc param_desc,
CommandFlags flags,
CommandCompleter completer)
void CommandManager::register_alias(String alias, String command)
{
kak_assert(not command_names.empty());
m_commands[command_names[0]] = { std::move(command),
std::move(docstring),
std::move(param_desc),
flags,
completer };
for (size_t i = 1; i < command_names.size(); ++i)
m_aliases[command_names[i]] = command_names[0];
kak_assert(not alias.empty());
kak_assert(command_defined(command));
m_aliases[alias] = std::move(command);
}
struct parse_error : runtime_error

View File

@ -79,12 +79,7 @@ public:
ParameterDesc param_desc,
CommandFlags flags = CommandFlags::None,
CommandCompleter completer = CommandCompleter());
void register_commands(memoryview<String> command_names, Command command,
String docstring,
ParameterDesc param_desc,
CommandFlags flags = CommandFlags::None,
CommandCompleter completer = CommandCompleter());
void register_alias(String alias, String command);
private:
void execute_single_command(CommandParameters params,

View File

@ -567,6 +567,14 @@ void define_command(const ParametersParser& parser, Context& context)
if (parser.has_option("docstring"))
docstring = parser.option_value("docstring");
String alias;
if (parser.has_option("alias"))
{
alias = parser.option_value("alias");
if (alias.empty())
throw runtime_error("alias should not be an empty string");
}
String commands = parser[1];
Command cmd;
ParameterDesc desc;
@ -648,13 +656,11 @@ void define_command(const ParametersParser& parser, Context& context)
return Completions{ 0_byte, params[token_to_complete].length(), split(output, '\n') };
};
}
if (parser.has_option("alias"))
CommandManager::instance().register_commands(
{ cmd_name, parser.option_value("alias") },
cmd, std::move(docstring), desc, flags, completer);
else
CommandManager::instance().register_command(
cmd_name, cmd, std::move(docstring), desc, flags, completer);
auto& cm = CommandManager::instance();
cm.register_command(cmd_name, cmd, std::move(docstring), desc, flags, completer);
if (not alias.empty())
cm.register_alias(std::move(alias), cmd_name);
}
const CommandDesc define_command_cmd = {
@ -1316,10 +1322,9 @@ void exec_keys(const KeyList& keys, Context& context)
static void register_command(CommandManager& cm, const CommandDesc& c)
{
if (c.alias)
cm.register_commands({ c.name, c.alias }, c.func, c.docstring, c.params, c.flags, c.completer);
else
cm.register_command(c.name, c.func, c.docstring, c.params, c.flags, c.completer);
if (c.alias)
cm.register_alias(c.alias, c.name);
}
void register_commands()