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

View File

@ -79,12 +79,7 @@ public:
ParameterDesc param_desc, ParameterDesc param_desc,
CommandFlags flags = CommandFlags::None, CommandFlags flags = CommandFlags::None,
CommandCompleter completer = CommandCompleter()); CommandCompleter completer = CommandCompleter());
void register_alias(String alias, String command);
void register_commands(memoryview<String> command_names, Command command,
String docstring,
ParameterDesc param_desc,
CommandFlags flags = CommandFlags::None,
CommandCompleter completer = CommandCompleter());
private: private:
void execute_single_command(CommandParameters params, 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")) if (parser.has_option("docstring"))
docstring = parser.option_value("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]; String commands = parser[1];
Command cmd; Command cmd;
ParameterDesc desc; 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') }; return Completions{ 0_byte, params[token_to_complete].length(), split(output, '\n') };
}; };
} }
if (parser.has_option("alias"))
CommandManager::instance().register_commands( auto& cm = CommandManager::instance();
{ cmd_name, parser.option_value("alias") }, cm.register_command(cmd_name, cmd, std::move(docstring), desc, flags, completer);
cmd, std::move(docstring), desc, flags, completer); if (not alias.empty())
else cm.register_alias(std::move(alias), cmd_name);
CommandManager::instance().register_command(
cmd_name, cmd, std::move(docstring), desc, flags, completer);
} }
const CommandDesc define_command_cmd = { 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) 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); 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() void register_commands()