From 57a98880ab4ca81d1904aa0b18c6a72bc2600fb6 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Mon, 18 Jul 2022 21:14:58 +0200 Subject: [PATCH] Make completers take "StringView" instead of "const String&" for compatibility with PromptCompleter We define using PromptCompleter = std::function; 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. --- src/commands.cc | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/commands.cc b/src/commands.cc index 5783b0b8..3462822c 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -105,7 +105,7 @@ template auto add_flags(Completer completer, Completions::Flags 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); res.flags |= completions_flags; return res; @@ -120,7 +120,7 @@ auto menu(Completer completer) template 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, complete_filename(prefix, context.options()["ignored_files"].get(), @@ -179,7 +179,7 @@ auto make_single_word_completer(Func&& func) { return make_completer( [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) }; 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 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) }; } 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( context, prefix.substr(0, cursor_pos)); } 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, context.aliases().flatten_aliases() @@ -1059,7 +1059,7 @@ const CommandDesc remove_highlighter_cmd = { }; 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{}) | transform(&EnumDesc::name)) }; } @@ -1084,7 +1084,7 @@ const CommandDesc add_hook_cmd = { CommandHelper{}, make_completer(complete_scope, complete_hooks, complete_nothing, [](const Context& context, CompletionFlags flags, - const String& prefix, ByteCount cursor_pos) + StringView prefix, ByteCount cursor_pos) { return CommandManager::instance().complete( context, flags, prefix, cursor_pos); }), [](const ParametersParser& parser, Context& context, const ShellContext&) @@ -1351,7 +1351,7 @@ const CommandDesc alias_cmd = { }; 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, complete(prefix, cursor_pos, context.aliases().flatten_aliases() | @@ -1461,7 +1461,7 @@ const CommandDesc debug_cmd = { CommandHelper{}, make_completer( [](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", "profile-hash-maps", "faces", "mappings", "regex", "registers"}; return { 0_byte, cursor_pos, complete(prefix, cursor_pos, c) }; @@ -1779,7 +1779,7 @@ const CommandDesc declare_option_cmd = { CommandHelper{}, make_completer( [](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"}; 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, - const String& prefix, ByteCount cursor_pos) + StringView prefix, ByteCount cursor_pos) { return {0_byte, cursor_pos, complete(prefix, cursor_pos, context.faces().flatten_faces() | @@ -2523,7 +2523,7 @@ const CommandDesc set_register_cmd = { CommandHelper{}, make_completer( [](const Context& context, CompletionFlags flags, - const String& prefix, ByteCount cursor_pos) -> Completions { + StringView prefix, ByteCount cursor_pos) -> Completions { return { 0_byte, cursor_pos, RegisterManager::instance().complete_register_name(prefix, cursor_pos) }; }), @@ -2572,7 +2572,7 @@ const CommandDesc change_directory_cmd = { CommandHelper{}, make_completer( [](const Context& context, CompletionFlags flags, - const String& prefix, ByteCount cursor_pos) -> Completions { + StringView prefix, ByteCount cursor_pos) -> Completions { return { 0_byte, cursor_pos, complete_filename(prefix, context.options()["ignored_files"].get(), @@ -2719,7 +2719,7 @@ const CommandDesc require_module_cmd = { CommandFlags::None, CommandHelper{}, 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)); }), [](const ParametersParser& parser, Context& context, const ShellContext&)