From 0483e951ade67989f762cfcd3796b261ffae41dd Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 3 Feb 2012 19:14:35 +0000 Subject: [PATCH] use memoryview instead of std::vector where applicable --- src/command_manager.cc | 10 +++--- src/command_manager.hh | 24 ++++++-------- src/filter_registry.hh | 3 +- src/highlighter.hh | 4 +-- src/main.cc | 71 +++++++++++++++++++++--------------------- 5 files changed, 54 insertions(+), 58 deletions(-) diff --git a/src/command_manager.cc b/src/command_manager.cc index 33815be41..b8d032ac2 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -15,9 +15,9 @@ void CommandManager::register_command(const std::string& command_name, Command c m_commands[command_name] = CommandDescriptor { command, flags, completer }; } -void CommandManager::register_command(const std::vector& command_names, Command command, - unsigned flags, - const CommandCompleter& completer) +void CommandManager::register_commands(const memoryview& command_names, Command command, + unsigned flags, + const CommandCompleter& completer) { for (auto command_name : command_names) register_command(command_name, command, flags, completer); @@ -70,7 +70,7 @@ void CommandManager::execute(const std::string& command_line, if (tokens.empty()) return; - CommandParameters params; + std::vector params; for (auto it = tokens.begin(); it != tokens.end(); ++it) { params.push_back(command_line.substr(it->first, @@ -152,7 +152,7 @@ Completions CommandManager::complete(const std::string& command_line, size_t cur if (command_it == m_commands.end() or not command_it->second.completer) return Completions(); - CommandParameters params; + std::vector params; for (auto it = tokens.begin() + 1; it != tokens.end(); ++it) { params.push_back(command_line.substr(it->first, diff --git a/src/command_manager.hh b/src/command_manager.hh index 125db6e11..aa9e68186 100644 --- a/src/command_manager.hh +++ b/src/command_manager.hh @@ -2,13 +2,13 @@ #define command_manager_hh_INCLUDED #include -#include #include #include #include #include "utils.hh" #include "completion.hh" +#include "memoryview.hh" namespace Kakoune { @@ -20,7 +20,7 @@ struct wrong_argument_count : runtime_error wrong_argument_count() : runtime_error("wrong argument count") {} }; -typedef std::vector CommandParameters; +typedef memoryview CommandParameters; typedef std::function Command; @@ -31,23 +31,17 @@ class PerArgumentCommandCompleter { public: typedef std::function ArgumentCompleter; - typedef std::vector ArgumentCompleterList; + typedef memoryview ArgumentCompleterList; PerArgumentCommandCompleter(const ArgumentCompleterList& completers) - : m_completers(completers) {} - - PerArgumentCommandCompleter(ArgumentCompleterList&& completers) - : m_completers(completers) {} - - PerArgumentCommandCompleter(std::initializer_list completers) - : m_completers(completers) {} + : m_completers(completers.begin(), completers.end()) {} CandidateList operator()(const CommandParameters& params, size_t token_to_complete, size_t pos_in_token) const; private: - ArgumentCompleterList m_completers; + std::vector m_completers; }; class CommandManager : public Singleton @@ -69,10 +63,10 @@ public: unsigned flags = None, const CommandCompleter& completer = CommandCompleter()); - void register_command(const std::vector& command_names, - Command command, - unsigned flags = None, - const CommandCompleter& completer = CommandCompleter()); + void register_commands(const memoryview& command_names, + Command command, + unsigned flags = None, + const CommandCompleter& completer = CommandCompleter()); private: struct CommandDescriptor diff --git a/src/filter_registry.hh b/src/filter_registry.hh index b00cc19c2..a5069362e 100644 --- a/src/filter_registry.hh +++ b/src/filter_registry.hh @@ -7,6 +7,7 @@ #include "filter.hh" #include "utils.hh" #include "completion.hh" +#include "memoryview.hh" #include "idvaluemap.hh" namespace Kakoune @@ -14,7 +15,7 @@ namespace Kakoune class Window; -typedef std::vector FilterParameters; +typedef memoryview FilterParameters; typedef std::function FilterFactory; diff --git a/src/highlighter.hh b/src/highlighter.hh index 93fb25952..5c9ce5e9b 100644 --- a/src/highlighter.hh +++ b/src/highlighter.hh @@ -3,7 +3,7 @@ #include #include -#include +#include "memoryview.hh" namespace Kakoune { @@ -17,7 +17,7 @@ class DisplayBuffer; typedef std::function HighlighterFunc; typedef std::pair HighlighterAndId; -typedef std::vector HighlighterParameters; +typedef memoryview HighlighterParameters; } diff --git a/src/main.cc b/src/main.cc index bd50a97b4..31107fb6a 100644 --- a/src/main.cc +++ b/src/main.cc @@ -682,10 +682,11 @@ void add_hook(const CommandParameters& params, const Context& context) if (params.size() < 4) throw wrong_argument_count(); - CommandParameters hook_params(params.begin()+3, params.end()); + std::string regex = params[2]; + std::vector hook_params(params.begin()+3, params.end()); auto hook_func = [=](const std::string& param, const Context& context) { - if (boost::regex_match(param, boost::regex(params[2]))) + if (boost::regex_match(param, boost::regex(regex))) CommandManager::instance().execute(hook_params, context); }; @@ -758,7 +759,7 @@ void exec_commands_in_runtime_file(const CommandParameters& params, if (ptr) { strcpy(ptr+1, filename.c_str()); - exec_commands_in_file({ buffer }, main_context); + exec_commands_in_file(CommandParameters(buffer), main_context); } } @@ -1090,40 +1091,40 @@ int main(int argc, char* argv[]) FilterRegistry filter_registry; GlobalHooksManager hooks_manager; - command_manager.register_command(std::vector{ "e", "edit" }, edit, + command_manager.register_commands({ "e", "edit" }, edit, CommandManager::None, - PerArgumentCommandCompleter{ complete_filename }); - command_manager.register_command(std::vector{ "q", "quit" }, quit); - command_manager.register_command(std::vector{ "q!", "quit!" }, quit); - command_manager.register_command(std::vector{ "w", "write" }, write_buffer, + PerArgumentCommandCompleter({ complete_filename })); + command_manager.register_commands({ "q", "quit" }, quit); + command_manager.register_commands({ "q!", "quit!" }, quit); + command_manager.register_commands({ "w", "write" }, write_buffer, CommandManager::None, - PerArgumentCommandCompleter{ complete_filename }); - command_manager.register_command(std::vector{ "wq" }, write_and_quit); - command_manager.register_command(std::vector{ "wq!" }, write_and_quit); - command_manager.register_command(std::vector{ "b", "buffer" }, show_buffer, + PerArgumentCommandCompleter({ complete_filename })); + command_manager.register_command("wq", write_and_quit); + command_manager.register_command("wq!", write_and_quit); + command_manager.register_commands({ "b", "buffer" }, show_buffer, CommandManager::None, - PerArgumentCommandCompleter { + PerArgumentCommandCompleter({ std::bind(&BufferManager::complete_buffername, &buffer_manager, _1, _2) - }); - command_manager.register_command(std::vector{ "ah", "addhl" }, add_highlighter, + })); + command_manager.register_commands({ "ah", "addhl" }, add_highlighter, CommandManager::None, - PerArgumentCommandCompleter { + PerArgumentCommandCompleter({ std::bind(&HighlighterRegistry::complete_highlighter, &highlighter_registry, _1, _2) - }); - command_manager.register_command(std::vector{ "agh", "addgrouphl" }, add_group_highlighter, + })); + command_manager.register_commands({ "agh", "addgrouphl" }, add_group_highlighter, CommandManager::None, - PerArgumentCommandCompleter { + PerArgumentCommandCompleter({ [&](const std::string& prefix, size_t cursor_pos) { return main_context.window().highlighters().complete_group_id(prefix, cursor_pos); }, std::bind(&HighlighterRegistry::complete_highlighter, &highlighter_registry, _1, _2) - }); - command_manager.register_command(std::vector{ "rh", "rmhl" }, rm_highlighter, + })); + command_manager.register_commands({ "rh", "rmhl" }, rm_highlighter, CommandManager::None, - PerArgumentCommandCompleter { + PerArgumentCommandCompleter({ [&](const std::string& prefix, size_t cursor_pos) { return main_context.window().highlighters().complete_group_id(prefix, cursor_pos); } - }); - command_manager.register_command(std::vector{ "rgh", "rmgrouphl" }, rm_group_highlighter, + })); + command_manager.register_commands({ "rgh", "rmgrouphl" }, rm_group_highlighter, CommandManager::None, [&](const CommandParameters& params, size_t token_to_complete, size_t pos_in_token) { @@ -1135,25 +1136,25 @@ int main(int argc, char* argv[]) else if (token_to_complete == 1) return w.highlighters().get_group(params[0]).complete_id(arg, pos_in_token); }); - command_manager.register_command(std::vector{ "af", "addfilter" }, add_filter, + command_manager.register_commands({ "af", "addfilter" }, add_filter, CommandManager::None, - PerArgumentCommandCompleter { + PerArgumentCommandCompleter({ std::bind(&FilterRegistry::complete_filter, &filter_registry, _1, _2) - }); - command_manager.register_command(std::vector{ "rf", "rmfilter" }, rm_filter, + })); + command_manager.register_commands({ "rf", "rmfilter" }, rm_filter, CommandManager::None, - PerArgumentCommandCompleter { + PerArgumentCommandCompleter({ [&](const std::string& prefix, size_t cursor_pos) { return main_context.window().complete_filterid(prefix, cursor_pos); } - }); - command_manager.register_command(std::vector{ "hook" }, add_hook, CommandManager::IgnoreSemiColons); + })); + command_manager.register_command("hook", add_hook, CommandManager::IgnoreSemiColons); - command_manager.register_command(std::vector{ "source" }, exec_commands_in_file, + command_manager.register_command("source", exec_commands_in_file, CommandManager::None, - PerArgumentCommandCompleter{ complete_filename }); - command_manager.register_command(std::vector{ "runtime" }, exec_commands_in_runtime_file); + PerArgumentCommandCompleter({ complete_filename })); + command_manager.register_command("runtime", exec_commands_in_runtime_file); - command_manager.register_command(std::vector{ "exec" }, exec_string); + command_manager.register_command("exec", exec_string); register_highlighters(); register_filters();