Add completion support for alias/unalias commands

This commit is contained in:
Maxime Coste 2016-03-24 00:05:40 +00:00
parent abd5afe012
commit 1d646c03f5
3 changed files with 37 additions and 13 deletions

View File

@ -513,16 +513,20 @@ CommandInfo CommandManager::command_info(const Context& context, StringView comm
}
Completions CommandManager::complete_command_name(const Context& context,
StringView query) const
StringView query, bool with_aliases) const
{
auto candidates = Kakoune::complete(
query, query.length(), concatenated(
m_commands
auto commands = m_commands
| filter([](const CommandMap::value_type& cmd) { return not (cmd.second.flags & CommandFlags::Hidden); })
| transform([](const CommandMap::value_type& cmd) { return StringView{cmd.first}; }),
context.aliases().flatten_aliases()
| transform([](AliasRegistry::AliasDesc alias) { return alias.first; })));
| transform([](const CommandMap::value_type& cmd) { return StringView{cmd.first}; });
if (not with_aliases)
return {0, query.length(), Kakoune::complete(query, query.length(), commands)};
auto candidates = Kakoune::complete(query, query.length(),
concatenated(commands,
context.aliases().flatten_aliases()
| transform([](AliasRegistry::AliasDesc alias)
{ return alias.first; })));
return {0, query.length(), std::move(candidates)};
}
@ -556,7 +560,7 @@ Completions CommandManager::complete(const Context& context,
{
auto cmd_start = is_last_token ? cursor_pos : tokens[tok_idx].begin();
StringView query = command_line.substr(cmd_start, cursor_pos - cmd_start);
return offset_pos(complete_command_name(context, query), cmd_start);
return offset_pos(complete_command_name(context, query, true), cmd_start);
}
kak_assert(not tokens.empty());
@ -625,7 +629,7 @@ Completions CommandManager::complete(const Context& context,
StringView prefix = params[token_to_complete].substr(0, pos_in_token);
if (token_to_complete == 0)
return complete_command_name(context, prefix);
return complete_command_name(context, prefix, true);
else
{
const String& command_name = params[0];

View File

@ -124,14 +124,14 @@ public:
CommandHelper helper = CommandHelper(),
CommandCompleter completer = CommandCompleter());
Completions complete_command_name(const Context& context, StringView query, bool with_aliases) const;
private:
void execute_single_command(CommandParameters params,
Context& context,
const ShellContext& shell_context,
CharCoord pos) const;
Completions complete_command_name(const Context& context, StringView query) const;
struct CommandDescriptor
{
Command command;

View File

@ -867,6 +867,21 @@ const CommandDesc define_command_cmd = {
define_command
};
static Completions complete_scope(const Context&, CompletionFlags,
const String& prefix, ByteCount cursor_pos)
{
auto scopes = {"global", "buffer", "window"};
return { 0_byte, cursor_pos, complete(prefix, cursor_pos, scopes) };
}
static Completions complete_command_name(const Context& context, CompletionFlags,
const String& prefix, ByteCount cursor_pos)
{
return CommandManager::instance().complete_command_name(
context, prefix.substr(0, cursor_pos), false);
}
const CommandDesc alias_cmd = {
"alias",
nullptr,
@ -874,7 +889,9 @@ const CommandDesc alias_cmd = {
ParameterDesc{{}, ParameterDesc::Flags::None, 3, 3},
CommandFlags::None,
CommandHelper{},
CommandCompleter{},
PerArgumentCommandCompleter({
complete_scope, complete_nothing, complete_command_name
}),
[](const ParametersParser& parser, Context& context, const ShellContext&)
{
if (not CommandManager::instance().command_defined(parser[2]))
@ -893,7 +910,10 @@ const CommandDesc unalias_cmd = {
ParameterDesc{{}, ParameterDesc::Flags::None, 2, 3},
CommandFlags::None,
CommandHelper{},
CommandCompleter{},
PerArgumentCommandCompleter({
complete_scope, complete_nothing, complete_command_name
}),
[](const ParametersParser& parser, Context& context, const ShellContext&)
{
AliasRegistry& aliases = get_scope(parser[0], context).aliases();