CommandManager: use aliases for commands rather than duplicating data

Completion now only proposes full command names
This commit is contained in:
Maxime Coste 2013-12-24 02:06:22 +00:00
parent 01e1169e2c
commit fd17ea00dd
3 changed files with 37 additions and 22 deletions

View File

@ -13,7 +13,7 @@ namespace Kakoune
bool CommandManager::command_defined(const String& command_name) const bool CommandManager::command_defined(const String& command_name) const
{ {
return m_commands.find(command_name) != m_commands.end(); return find_command(command_name) != m_commands.end();
} }
void CommandManager::register_command(String command_name, void CommandManager::register_command(String command_name,
@ -29,8 +29,10 @@ void CommandManager::register_commands(memoryview<String> command_names,
CommandFlags flags, CommandFlags flags,
CommandCompleter completer) CommandCompleter completer)
{ {
for (auto command_name : command_names) kak_assert(not command_names.empty());
m_commands[command_name] = { command, flags, completer }; m_commands[command_names[0]] = { std::move(command), 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
@ -246,16 +248,24 @@ struct command_not_found : runtime_error
: runtime_error(command + " : no such command") {} : runtime_error(command + " : no such command") {}
}; };
CommandManager::CommandMap::const_iterator CommandManager::find_command(const String& name) const
{
auto it = m_aliases.find(name);
const String& cmd_name = it == m_aliases.end() ? name : it->second;
return m_commands.find(cmd_name);
}
void CommandManager::execute_single_command(CommandParameters params, void CommandManager::execute_single_command(CommandParameters params,
Context& context) const Context& context) const
{ {
if (params.empty()) if (params.empty())
return; return;
auto command_it = m_commands.find(params[0]); memoryview<String> param_view(params.begin()+1, params.end());
auto command_it = find_command(params[0]);
if (command_it == m_commands.end()) if (command_it == m_commands.end())
throw command_not_found(params[0]); throw command_not_found(params[0]);
memoryview<String> param_view(params.begin()+1, params.end());
command_it->second.command(param_view, context); command_it->second.command(param_view, context);
} }
@ -350,7 +360,7 @@ Completions CommandManager::complete(const Context& context, CompletionFlags fla
const String& command_name = tokens[0].content(); const String& command_name = tokens[0].content();
auto command_it = m_commands.find(command_name); auto command_it = find_command(command_name);
if (command_it == m_commands.end() or not command_it->second.completer) if (command_it == m_commands.end() or not command_it->second.completer)
return Completions(); return Completions();

View File

@ -79,13 +79,18 @@ public:
private: private:
void execute_single_command(CommandParameters params, void execute_single_command(CommandParameters params,
Context& context) const; Context& context) const;
struct CommandDescriptor struct CommandDescriptor
{ {
Command command; Command command;
CommandFlags flags; CommandFlags flags;
CommandCompleter completer; CommandCompleter completer;
}; };
std::unordered_map<String, CommandDescriptor> m_commands; using CommandMap = std::unordered_map<String, CommandDescriptor>;
CommandMap m_commands;
std::unordered_map<String, String> m_aliases;
CommandMap::const_iterator find_command(const String& name) const;
}; };
} }

View File

@ -819,12 +819,12 @@ void register_commands()
[](const Context& context, CompletionFlags flags, const String& prefix, ByteCount cursor_pos) [](const Context& context, CompletionFlags flags, const String& prefix, ByteCount cursor_pos)
{ return complete_filename(prefix, context.options()["ignored_files"].get<Regex>(), cursor_pos); } { return complete_filename(prefix, context.options()["ignored_files"].get<Regex>(), cursor_pos); }
}); });
cm.register_commands({ "e", "edit" }, edit<false>, CommandFlags::None, filename_completer); cm.register_commands({ "edit", "e" }, edit<false>, CommandFlags::None, filename_completer);
cm.register_commands({ "e!", "edit!" }, edit<true>, CommandFlags::None, filename_completer); cm.register_commands({ "edit!", "e!" }, edit<true>, CommandFlags::None, filename_completer);
cm.register_commands({ "w", "write" }, write_buffer, CommandFlags::None, filename_completer); cm.register_commands({ "write", "w" }, write_buffer, CommandFlags::None, filename_completer);
cm.register_commands({ "wa", "writeall" }, write_all_buffers); cm.register_commands({ "writeall", "wa" }, write_all_buffers);
cm.register_commands({ "q", "quit" }, quit<false>); cm.register_commands({ "quit", "q" }, quit<false>);
cm.register_commands({ "q!", "quit!" }, quit<true>); cm.register_commands({ "quit!", "q!" }, quit<true>);
cm.register_command("wq", write_and_quit<false>); cm.register_command("wq", write_and_quit<false>);
cm.register_command("wq!", write_and_quit<true>); cm.register_command("wq!", write_and_quit<true>);
@ -832,15 +832,15 @@ void register_commands()
[](const Context& context, CompletionFlags flags, const String& prefix, ByteCount cursor_pos) [](const Context& context, CompletionFlags flags, const String& prefix, ByteCount cursor_pos)
{ return BufferManager::instance().complete_buffername(prefix, cursor_pos); } { return BufferManager::instance().complete_buffername(prefix, cursor_pos); }
}); });
cm.register_commands({ "b", "buffer" }, show_buffer, CommandFlags::None, buffer_completer); cm.register_commands({ "buffer", "b" }, show_buffer, CommandFlags::None, buffer_completer);
cm.register_commands({ "db", "delbuf" }, delete_buffer<false>, CommandFlags::None, buffer_completer); cm.register_commands({ "delbuf", "db" }, delete_buffer<false>, CommandFlags::None, buffer_completer);
cm.register_commands({ "db!", "delbuf!" }, delete_buffer<true>, CommandFlags::None, buffer_completer); cm.register_commands({ "delbuf!", "db!" }, delete_buffer<true>, CommandFlags::None, buffer_completer);
cm.register_commands({"nb", "namebuf"}, set_buffer_name); cm.register_commands({ "namebuf", "nb" }, set_buffer_name);
auto get_highlighters = [](const Context& c) -> HighlighterGroup& { return c.window().highlighters(); }; auto get_highlighters = [](const Context& c) -> HighlighterGroup& { return c.window().highlighters(); };
cm.register_commands({ "ah", "addhl" }, add_highlighter, CommandFlags::None, group_add_completer<HighlighterRegistry>(get_highlighters)); cm.register_commands({ "addhl", "ah" }, add_highlighter, CommandFlags::None, group_add_completer<HighlighterRegistry>(get_highlighters));
cm.register_commands({ "rh", "rmhl" }, rm_highlighter, CommandFlags::None, group_rm_completer(get_highlighters)); cm.register_commands({ "rmhl", "rh" }, rm_highlighter, CommandFlags::None, group_rm_completer(get_highlighters));
cm.register_commands({ "dh", "defhl" }, define_highlighter); cm.register_commands({ "defhl", "dh" }, define_highlighter);
cm.register_command("hook", add_hook); cm.register_command("hook", add_hook);
cm.register_command("rmhooks", rm_hooks); cm.register_command("rmhooks", rm_hooks);
@ -881,8 +881,8 @@ void register_commands()
return CandidateList{}; return CandidateList{};
} ); } );
cm.register_commands({"ca", "colalias"}, define_color_alias); cm.register_commands({ "colalias", "ca" }, define_color_alias);
cm.register_commands({"nc", "nameclient"}, set_client_name); cm.register_commands({ "nameclient", "nc" }, set_client_name);
cm.register_command("cd", change_working_directory, CommandFlags::None, filename_completer); cm.register_command("cd", change_working_directory, CommandFlags::None, filename_completer);
cm.register_command("map", map_key); cm.register_command("map", map_key);