Use and extract functions for completing scope arguments

Not really attached to this but it enables the next commit to use
menu() for completing scopes.

This refactoring is possible because we always have

	params[token_to_complete].length() == pos_in_token

---

Instead of three separate functions, I originally tried to add
template arguments to complete_scope().  That worked fine with g++
12.1 but clang 14.0 complained when wrapping a menu() around a
complete_scope() that relied on defaulted template arguments:

	commands.cc:1087:20: error: no matching function for call to 'menu'
	    make_completer(menu(complete_scope), menu(complete_hooks), complete_nothing,
	                   ^~~~
	commands.cc:116:6: note: candidate template ignored: couldn't infer template argument 'Completer'
	auto menu(Completer completer)
	     ^
This commit is contained in:
Johannes Altmanninger 2022-07-19 11:26:56 +02:00
parent 23fcf77160
commit 5aa0241124

View File

@ -190,11 +190,24 @@ const ParameterDesc single_param{ {}, ParameterDesc::Flags::None, 1, 1 };
const ParameterDesc single_optional_param{ {}, ParameterDesc::Flags::None, 0, 1 }; const ParameterDesc single_optional_param{ {}, ParameterDesc::Flags::None, 0, 1 };
const ParameterDesc double_params{ {}, ParameterDesc::Flags::None, 2, 2 }; const ParameterDesc double_params{ {}, ParameterDesc::Flags::None, 2, 2 };
static constexpr auto scopes = { "global", "buffer", "window" };
static Completions complete_scope(const Context&, CompletionFlags, static Completions complete_scope(const Context&, CompletionFlags,
StringView prefix, ByteCount cursor_pos) StringView prefix, ByteCount cursor_pos)
{ {
static constexpr StringView scopes[] = { "global", "buffer", "window", };
return { 0_byte, cursor_pos, complete(prefix, cursor_pos, scopes) };
}
static Completions complete_scope_including_current(const Context&, CompletionFlags,
StringView prefix, ByteCount cursor_pos)
{
static constexpr StringView scopes[] = { "global", "buffer", "window", "current" };
return { 0_byte, cursor_pos, complete(prefix, cursor_pos, scopes) };
}
static Completions complete_scope_no_global(const Context&, CompletionFlags,
StringView prefix, ByteCount cursor_pos)
{
static constexpr StringView scopes[] = { "buffer", "window", "current" };
return { 0_byte, cursor_pos, complete(prefix, cursor_pos, scopes) }; return { 0_byte, cursor_pos, complete(prefix, cursor_pos, scopes) };
} }
@ -1120,8 +1133,7 @@ const CommandDesc remove_hook_cmd = {
ByteCount pos_in_token) -> Completions ByteCount pos_in_token) -> Completions
{ {
if (token_to_complete == 0) if (token_to_complete == 0)
return { 0_byte, params[0].length(), return complete_scope(context, flags, params[0], pos_in_token);
complete(params[0], pos_in_token, scopes) };
else if (token_to_complete == 1) else if (token_to_complete == 1)
{ {
if (auto scope = get_scope_ifp(params[0], context)) if (auto scope = get_scope_ifp(params[0], context))
@ -1662,15 +1674,12 @@ const CommandDesc set_option_cmd = {
}, },
CommandFlags::None, CommandFlags::None,
option_doc_helper, option_doc_helper,
[](const Context& context, CompletionFlags, [](const Context& context, CompletionFlags flags,
CommandParameters params, size_t token_to_complete, CommandParameters params, size_t token_to_complete,
ByteCount pos_in_token) -> Completions ByteCount pos_in_token) -> Completions
{ {
static constexpr auto scopes = { "global", "buffer", "window", "current" };
if (token_to_complete == 0) if (token_to_complete == 0)
return { 0_byte, params[0].length(), return complete_scope_including_current(context, flags, params[0], pos_in_token);
complete(params[0], pos_in_token, scopes) };
else if (token_to_complete == 1) else if (token_to_complete == 1)
return { 0_byte, params[1].length(), return { 0_byte, params[1].length(),
GlobalScope::instance().option_registry().complete_option_name(params[1], pos_in_token) }; GlobalScope::instance().option_registry().complete_option_name(params[1], pos_in_token) };
@ -1701,15 +1710,12 @@ const CommandDesc set_option_cmd = {
} }
}; };
Completions complete_option(const Context& context, CompletionFlags, Completions complete_option(const Context& context, CompletionFlags flags,
CommandParameters params, size_t token_to_complete, CommandParameters params, size_t token_to_complete,
ByteCount pos_in_token) ByteCount pos_in_token)
{ {
if (token_to_complete == 0) if (token_to_complete == 0)
{ return complete_scope_no_global(context, flags, params[0], pos_in_token);
static constexpr auto scopes = { "buffer", "window", "current" };
return { 0_byte, params[0].length(), complete(params[0], pos_in_token, scopes) };
}
else if (token_to_complete == 1) else if (token_to_complete == 1)
return { 0_byte, params[1].length(), return { 0_byte, params[1].length(),
GlobalScope::instance().option_registry().complete_option_name(params[1], pos_in_token) }; GlobalScope::instance().option_registry().complete_option_name(params[1], pos_in_token) };
@ -1828,8 +1834,7 @@ static Completions map_key_completer(const Context& context, CompletionFlags fla
ByteCount pos_in_token) ByteCount pos_in_token)
{ {
if (token_to_complete == 0) if (token_to_complete == 0)
return { 0_byte, params[0].length(), return complete_scope(context, flags, params[0], pos_in_token);
complete(params[0], pos_in_token, scopes) };
if (token_to_complete == 1) if (token_to_complete == 1)
{ {
auto& user_modes = get_scope(params[0], context).keymaps().user_modes(); auto& user_modes = get_scope(params[0], context).keymaps().user_modes();