Make completers take "StringView" instead of "const String&" for compatibility with PromptCompleter

We define

	using PromptCompleter = std::function<Completions (const Context&, CompletionFlags,
	                                                   StringView, ByteCount)>;

Some command completers are *almost* convertible to PromptCompleter;
the only difference is the string type of the prefix argument.
The later commits in this series want to use menu() on the
completers. Enable this by harmonizing the types.
This commit is contained in:
Johannes Altmanninger 2022-07-18 21:14:58 +02:00
parent 559af669c7
commit 57a98880ab

View File

@ -105,7 +105,7 @@ template<typename Completer>
auto add_flags(Completer completer, Completions::Flags completions_flags) auto add_flags(Completer completer, Completions::Flags completions_flags)
{ {
return [completer=std::move(completer), completions_flags] return [completer=std::move(completer), completions_flags]
(const Context& context, CompletionFlags flags, const String& prefix, ByteCount cursor_pos) { (const Context& context, CompletionFlags flags, StringView prefix, ByteCount cursor_pos) {
Completions res = completer(context, flags, prefix, cursor_pos); Completions res = completer(context, flags, prefix, cursor_pos);
res.flags |= completions_flags; res.flags |= completions_flags;
return res; return res;
@ -120,7 +120,7 @@ auto menu(Completer completer)
template<bool menu> template<bool menu>
auto filename_completer = make_completer( auto filename_completer = make_completer(
[](const Context& context, CompletionFlags flags, const String& prefix, ByteCount cursor_pos) [](const Context& context, CompletionFlags flags, StringView prefix, ByteCount cursor_pos)
{ return Completions{ 0_byte, cursor_pos, { return Completions{ 0_byte, cursor_pos,
complete_filename(prefix, complete_filename(prefix,
context.options()["ignored_files"].get<Regex>(), context.options()["ignored_files"].get<Regex>(),
@ -179,7 +179,7 @@ auto make_single_word_completer(Func&& func)
{ {
return make_completer( return make_completer(
[func = std::move(func)](const Context& context, CompletionFlags flags, [func = std::move(func)](const Context& context, CompletionFlags flags,
const String& prefix, ByteCount cursor_pos) -> Completions { StringView prefix, ByteCount cursor_pos) -> Completions {
auto candidate = { func(context) }; auto candidate = { func(context) };
return { 0_byte, cursor_pos, complete(prefix, cursor_pos, candidate) }; }); return { 0_byte, cursor_pos, complete(prefix, cursor_pos, candidate) }; });
} }
@ -192,21 +192,21 @@ const ParameterDesc double_params{ {}, ParameterDesc::Flags::None, 2, 2 };
static constexpr auto scopes = { "global", "buffer", "window" }; static constexpr auto scopes = { "global", "buffer", "window" };
static Completions complete_scope(const Context&, CompletionFlags, static Completions complete_scope(const Context&, CompletionFlags,
const String& prefix, ByteCount cursor_pos) StringView prefix, ByteCount cursor_pos)
{ {
return { 0_byte, cursor_pos, complete(prefix, cursor_pos, scopes) }; return { 0_byte, cursor_pos, complete(prefix, cursor_pos, scopes) };
} }
static Completions complete_command_name(const Context& context, CompletionFlags, static Completions complete_command_name(const Context& context, CompletionFlags,
const String& prefix, ByteCount cursor_pos) StringView prefix, ByteCount cursor_pos)
{ {
return CommandManager::instance().complete_command_name( return CommandManager::instance().complete_command_name(
context, prefix.substr(0, cursor_pos)); context, prefix.substr(0, cursor_pos));
} }
static Completions complete_alias_name(const Context& context, CompletionFlags, static Completions complete_alias_name(const Context& context, CompletionFlags,
const String& prefix, ByteCount cursor_pos) StringView prefix, ByteCount cursor_pos)
{ {
return { 0_byte, cursor_pos, complete(prefix, cursor_pos, return { 0_byte, cursor_pos, complete(prefix, cursor_pos,
context.aliases().flatten_aliases() context.aliases().flatten_aliases()
@ -1059,7 +1059,7 @@ const CommandDesc remove_highlighter_cmd = {
}; };
static Completions complete_hooks(const Context&, CompletionFlags, static Completions complete_hooks(const Context&, CompletionFlags,
const String& prefix, ByteCount cursor_pos) StringView prefix, ByteCount cursor_pos)
{ {
return { 0_byte, cursor_pos, complete(prefix, cursor_pos, enum_desc(Meta::Type<Hook>{}) | transform(&EnumDesc<Hook>::name)) }; return { 0_byte, cursor_pos, complete(prefix, cursor_pos, enum_desc(Meta::Type<Hook>{}) | transform(&EnumDesc<Hook>::name)) };
} }
@ -1084,7 +1084,7 @@ const CommandDesc add_hook_cmd = {
CommandHelper{}, CommandHelper{},
make_completer(complete_scope, complete_hooks, complete_nothing, make_completer(complete_scope, complete_hooks, complete_nothing,
[](const Context& context, CompletionFlags flags, [](const Context& context, CompletionFlags flags,
const String& prefix, ByteCount cursor_pos) StringView prefix, ByteCount cursor_pos)
{ return CommandManager::instance().complete( { return CommandManager::instance().complete(
context, flags, prefix, cursor_pos); }), context, flags, prefix, cursor_pos); }),
[](const ParametersParser& parser, Context& context, const ShellContext&) [](const ParametersParser& parser, Context& context, const ShellContext&)
@ -1351,7 +1351,7 @@ const CommandDesc alias_cmd = {
}; };
static Completions complete_alias(const Context& context, CompletionFlags flags, static Completions complete_alias(const Context& context, CompletionFlags flags,
const String& prefix, ByteCount cursor_pos) StringView prefix, ByteCount cursor_pos)
{ {
return {0_byte, cursor_pos, return {0_byte, cursor_pos,
complete(prefix, cursor_pos, context.aliases().flatten_aliases() | complete(prefix, cursor_pos, context.aliases().flatten_aliases() |
@ -1461,7 +1461,7 @@ const CommandDesc debug_cmd = {
CommandHelper{}, CommandHelper{},
make_completer( make_completer(
[](const Context& context, CompletionFlags flags, [](const Context& context, CompletionFlags flags,
const String& prefix, ByteCount cursor_pos) -> Completions { StringView prefix, ByteCount cursor_pos) -> Completions {
auto c = {"info", "buffers", "options", "memory", "shared-strings", auto c = {"info", "buffers", "options", "memory", "shared-strings",
"profile-hash-maps", "faces", "mappings", "regex", "registers"}; "profile-hash-maps", "faces", "mappings", "regex", "registers"};
return { 0_byte, cursor_pos, complete(prefix, cursor_pos, c) }; return { 0_byte, cursor_pos, complete(prefix, cursor_pos, c) };
@ -1779,7 +1779,7 @@ const CommandDesc declare_option_cmd = {
CommandHelper{}, CommandHelper{},
make_completer( make_completer(
[](const Context& context, CompletionFlags flags, [](const Context& context, CompletionFlags flags,
const String& prefix, ByteCount cursor_pos) -> Completions { StringView prefix, ByteCount cursor_pos) -> Completions {
auto c = {"int", "bool", "str", "regex", "int-list", "str-list", "completions", "line-specs", "range-specs", "str-to-str-map"}; auto c = {"int", "bool", "str", "regex", "int-list", "str-list", "completions", "line-specs", "range-specs", "str-to-str-map"};
return { 0_byte, cursor_pos, complete(prefix, cursor_pos, c) }; return { 0_byte, cursor_pos, complete(prefix, cursor_pos, c) };
}), }),
@ -2429,7 +2429,7 @@ const CommandDesc try_catch_cmd = {
}; };
static Completions complete_face(const Context& context, CompletionFlags flags, static Completions complete_face(const Context& context, CompletionFlags flags,
const String& prefix, ByteCount cursor_pos) StringView prefix, ByteCount cursor_pos)
{ {
return {0_byte, cursor_pos, return {0_byte, cursor_pos,
complete(prefix, cursor_pos, context.faces().flatten_faces() | complete(prefix, cursor_pos, context.faces().flatten_faces() |
@ -2523,7 +2523,7 @@ const CommandDesc set_register_cmd = {
CommandHelper{}, CommandHelper{},
make_completer( make_completer(
[](const Context& context, CompletionFlags flags, [](const Context& context, CompletionFlags flags,
const String& prefix, ByteCount cursor_pos) -> Completions { StringView prefix, ByteCount cursor_pos) -> Completions {
return { 0_byte, cursor_pos, return { 0_byte, cursor_pos,
RegisterManager::instance().complete_register_name(prefix, cursor_pos) }; RegisterManager::instance().complete_register_name(prefix, cursor_pos) };
}), }),
@ -2572,7 +2572,7 @@ const CommandDesc change_directory_cmd = {
CommandHelper{}, CommandHelper{},
make_completer( make_completer(
[](const Context& context, CompletionFlags flags, [](const Context& context, CompletionFlags flags,
const String& prefix, ByteCount cursor_pos) -> Completions { StringView prefix, ByteCount cursor_pos) -> Completions {
return { 0_byte, cursor_pos, return { 0_byte, cursor_pos,
complete_filename(prefix, complete_filename(prefix,
context.options()["ignored_files"].get<Regex>(), context.options()["ignored_files"].get<Regex>(),
@ -2719,7 +2719,7 @@ const CommandDesc require_module_cmd = {
CommandFlags::None, CommandFlags::None,
CommandHelper{}, CommandHelper{},
make_completer( make_completer(
[](const Context&, CompletionFlags, const String& prefix, ByteCount cursor_pos) { [](const Context&, CompletionFlags, StringView prefix, ByteCount cursor_pos) {
return CommandManager::instance().complete_module_name(prefix.substr(0, cursor_pos)); return CommandManager::instance().complete_module_name(prefix.substr(0, cursor_pos));
}), }),
[](const ParametersParser& parser, Context& context, const ShellContext&) [](const ParametersParser& parser, Context& context, const ShellContext&)