Improve 'def' command completion behaviour and clean it up a little

This commit is contained in:
Maxime Coste 2015-03-12 19:45:05 +00:00
parent d9cd5fef8f
commit 2f20399d03

View File

@ -650,11 +650,10 @@ Vector<String> params_to_shell(const ParametersParser& parser)
void define_command(const ParametersParser& parser, Context& context) void define_command(const ParametersParser& parser, Context& context)
{ {
auto begin = parser.begin(); const String& cmd_name = parser[0];
const String& cmd_name = *begin; auto& cm = CommandManager::instance();
if (CommandManager::instance().command_defined(cmd_name) and if (cm.command_defined(cmd_name) and not parser.has_option("allow-override"))
not parser.has_option("allow-override"))
throw runtime_error("command '" + cmd_name + "' already defined"); throw runtime_error("command '" + cmd_name + "' already defined");
CommandFlags flags = CommandFlags::None; CommandFlags flags = CommandFlags::None;
@ -665,7 +664,7 @@ 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 commands = parser[1]; const String& commands = parser[1];
Command cmd; Command cmd;
ParameterDesc desc; ParameterDesc desc;
if (parser.has_option("shell-params")) if (parser.has_option("shell-params"))
@ -692,7 +691,7 @@ void define_command(const ParametersParser& parser, Context& context)
{ {
const String& prefix = params[token_to_complete]; const String& prefix = params[token_to_complete];
auto& ignored_files = context.options()["ignored_files"].get<Regex>(); auto& ignored_files = context.options()["ignored_files"].get<Regex>();
return Completions{ 0_byte, prefix.length(), return Completions{ 0_byte, pos_in_token,
complete_filename(prefix, ignored_files, complete_filename(prefix, ignored_files,
pos_in_token) }; pos_in_token) };
}; };
@ -705,7 +704,7 @@ void define_command(const ParametersParser& parser, Context& context)
{ {
const String& prefix = params[token_to_complete]; const String& prefix = params[token_to_complete];
auto& cm = ClientManager::instance(); auto& cm = ClientManager::instance();
return Completions{ 0_byte, prefix.length(), return Completions{ 0_byte, pos_in_token,
cm.complete_client_name(prefix, pos_in_token) }; cm.complete_client_name(prefix, pos_in_token) };
}; };
} }
@ -716,13 +715,13 @@ void define_command(const ParametersParser& parser, Context& context)
size_t token_to_complete, ByteCount pos_in_token) size_t token_to_complete, ByteCount pos_in_token)
{ {
const String& prefix = params[token_to_complete]; const String& prefix = params[token_to_complete];
return Completions{ 0_byte, prefix.length(), return Completions{ 0_byte, pos_in_token,
complete_buffer_name(prefix, pos_in_token) }; complete_buffer_name(prefix, pos_in_token) };
}; };
} }
else if (parser.has_option("shell-completion")) else if (parser.has_option("shell-completion"))
{ {
String shell_cmd = parser.option_value("shell-completion"); const String& shell_cmd = parser.option_value("shell-completion");
completer = [=](const Context& context, CompletionFlags flags, completer = [=](const Context& context, CompletionFlags flags,
CommandParameters params, CommandParameters params,
size_t token_to_complete, ByteCount pos_in_token) size_t token_to_complete, ByteCount pos_in_token)
@ -734,11 +733,10 @@ void define_command(const ParametersParser& parser, Context& context)
{ "pos_in_token", to_string(pos_in_token) } { "pos_in_token", to_string(pos_in_token) }
}; };
String output = ShellManager::instance().eval(shell_cmd, context, params, vars); String output = ShellManager::instance().eval(shell_cmd, context, params, vars);
return Completions{ 0_byte, params[token_to_complete].length(), split(output, '\n', 0) }; return Completions{ 0_byte, pos_in_token, split(output, '\n', 0) };
}; };
} }
auto& cm = CommandManager::instance();
cm.register_command(cmd_name, cmd, std::move(docstring), desc, flags, CommandHelper{}, completer); cm.register_command(cmd_name, cmd, std::move(docstring), desc, flags, CommandHelper{}, completer);
} }