From 5df8073c3c4213075d1c0ee6267cc58848b86721 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sat, 18 Jul 2020 14:29:44 +1000 Subject: [PATCH] Code style tweaks --- src/command_manager.cc | 15 ++++++--------- src/command_manager.hh | 3 --- src/commands.cc | 23 ++++++++++------------- src/hash_map.hh | 3 ++- 4 files changed, 18 insertions(+), 26 deletions(-) diff --git a/src/command_manager.cc b/src/command_manager.cc index c6e52992..a66134f5 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -487,13 +487,10 @@ struct command_not_found : runtime_error : runtime_error(format("no such command: '{}'", name)) {} }; -CommandManager::CommandMap::const_iterator -CommandManager::find_command(const Context& context, StringView name) const +StringView resolve_alias(const Context& context, StringView name) { auto alias = context.aliases()[name]; - StringView cmd_name = alias.empty() ? name : alias; - - return m_commands.find(cmd_name); + return alias.empty() ? name : alias; } void CommandManager::execute_single_command(CommandParameters params, @@ -512,7 +509,7 @@ void CommandManager::execute_single_command(CommandParameters params, auto pop_cmd = on_scope_end([this] { --m_command_depth; }); ParameterList param_view(params.begin()+1, params.end()); - auto command_it = find_command(context, params[0]); + auto command_it = m_commands.find(resolve_alias(context, params[0])); if (command_it == m_commands.end()) throw command_not_found(params[0]); @@ -587,7 +584,7 @@ Optional CommandManager::command_info(const Context& context, Strin tokens.front().type != Token::Type::RawQuoted)) return {}; - auto cmd = find_command(context, tokens.front().content); + auto cmd = m_commands.find(resolve_alias(context, tokens.front().content)); if (cmd == m_commands.end()) return {}; @@ -729,7 +726,7 @@ Completions CommandManager::complete(const Context& context, flags |= CompletionFlags::Start; } - auto command_it = find_command(context, command_name); + auto command_it = m_commands.find(resolve_alias(context, command_name)); if (command_it == m_commands.end()) return Completions{}; @@ -786,7 +783,7 @@ Completions CommandManager::complete(const Context& context, flags |= CompletionFlags::Start; } - auto command_it = find_command(context, command_name); + auto command_it = m_commands.find(resolve_alias(context, command_name)); if (command_it != m_commands.end() and command_it->value.completer) return command_it->value.completer( context, flags, params.subrange(1), diff --git a/src/command_manager.hh b/src/command_manager.hh index a8b3b146..ffd14524 100644 --- a/src/command_manager.hh +++ b/src/command_manager.hh @@ -166,9 +166,6 @@ private: }; using ModuleMap = HashMap; ModuleMap m_modules; - - CommandMap::const_iterator find_command(const Context& context, - StringView name) const; }; String expand(StringView str, const Context& context, diff --git a/src/commands.cc b/src/commands.cc index abf3a898..ca28d7ea 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -2080,23 +2080,22 @@ const CommandDesc prompt_cmd = { const auto flags = parser.get_switch("password") ? PromptFlags::Password : PromptFlags::None; - String on_change = parser.get_switch("on-change").value_or("").str(); - String on_abort = parser.get_switch("on-abort").value_or("").str(); - - CapturedShellContext sc{shell_context}; context.input_handler().prompt( parser[0], initstr.str(), {}, context.faces()["Prompt"], flags, '_', std::move(completer), - [=](StringView str, PromptEvent event, Context& context) mutable + [command, + on_change = parser.get_switch("on-change").value_or("").str(), + on_abort = parser.get_switch("on-abort").value_or("").str(), + sc = CapturedShellContext{shell_context}] + (StringView str, PromptEvent event, Context& context) mutable { if ((event == PromptEvent::Abort and on_abort.empty()) or (event == PromptEvent::Change and on_change.empty())) return; - auto& text = sc.env_vars["text"_sv] = str.str(); - auto clear_password = on_scope_end([&] { - if (flags & PromptFlags::Password) - std::fill(text.begin(), text.end(), '\0'); + sc.env_vars["text"_sv] = String{String::NoCopy{}, str}; + auto remove_text = on_scope_end([&] { + sc.env_vars.erase("text"_sv); }); ScopedSetBool disable_history{context.history_disabled()}; @@ -2114,10 +2113,8 @@ const CommandDesc prompt_cmd = { } catch (Kakoune::runtime_error& error) { - context.print_status({ fix_atom_text(error.what().str()), - context.faces()["Error"] }); - context.hooks().run_hook(Hook::RuntimeError, error.what(), - context); + context.print_status({fix_atom_text(error.what()), context.faces()["Error"]}); + context.hooks().run_hook(Hook::RuntimeError, error.what(), context); } }); } diff --git a/src/hash_map.hh b/src/hash_map.hh index fb9da408..b5f054bc 100644 --- a/src/hash_map.hh +++ b/src/hash_map.hh @@ -249,7 +249,8 @@ struct HashMap } } - constexpr void erase(const Key& key) { unordered_remove(key); } + template> + constexpr void erase(const KeyType& key) { unordered_remove(key); } template> constexpr void remove_all(const KeyType& key)