use memoryview instead of std::vector where applicable

This commit is contained in:
Maxime Coste 2012-02-03 19:14:35 +00:00
parent ac02ccb53d
commit 0483e951ad
5 changed files with 54 additions and 58 deletions

View File

@ -15,7 +15,7 @@ void CommandManager::register_command(const std::string& command_name, Command c
m_commands[command_name] = CommandDescriptor { command, flags, completer }; m_commands[command_name] = CommandDescriptor { command, flags, completer };
} }
void CommandManager::register_command(const std::vector<std::string>& command_names, Command command, void CommandManager::register_commands(const memoryview<std::string>& command_names, Command command,
unsigned flags, unsigned flags,
const CommandCompleter& completer) const CommandCompleter& completer)
{ {
@ -70,7 +70,7 @@ void CommandManager::execute(const std::string& command_line,
if (tokens.empty()) if (tokens.empty())
return; return;
CommandParameters params; std::vector<std::string> params;
for (auto it = tokens.begin(); it != tokens.end(); ++it) for (auto it = tokens.begin(); it != tokens.end(); ++it)
{ {
params.push_back(command_line.substr(it->first, 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) if (command_it == m_commands.end() or not command_it->second.completer)
return Completions(); return Completions();
CommandParameters params; std::vector<std::string> params;
for (auto it = tokens.begin() + 1; it != tokens.end(); ++it) for (auto it = tokens.begin() + 1; it != tokens.end(); ++it)
{ {
params.push_back(command_line.substr(it->first, params.push_back(command_line.substr(it->first,

View File

@ -2,13 +2,13 @@
#define command_manager_hh_INCLUDED #define command_manager_hh_INCLUDED
#include <string> #include <string>
#include <vector>
#include <unordered_map> #include <unordered_map>
#include <functional> #include <functional>
#include <initializer_list> #include <initializer_list>
#include "utils.hh" #include "utils.hh"
#include "completion.hh" #include "completion.hh"
#include "memoryview.hh"
namespace Kakoune namespace Kakoune
{ {
@ -20,7 +20,7 @@ struct wrong_argument_count : runtime_error
wrong_argument_count() : runtime_error("wrong argument count") {} wrong_argument_count() : runtime_error("wrong argument count") {}
}; };
typedef std::vector<std::string> CommandParameters; typedef memoryview<std::string> CommandParameters;
typedef std::function<void (const CommandParameters&, typedef std::function<void (const CommandParameters&,
const Context& context)> Command; const Context& context)> Command;
@ -31,23 +31,17 @@ class PerArgumentCommandCompleter
{ {
public: public:
typedef std::function<CandidateList (const std::string&, size_t)> ArgumentCompleter; typedef std::function<CandidateList (const std::string&, size_t)> ArgumentCompleter;
typedef std::vector<ArgumentCompleter> ArgumentCompleterList; typedef memoryview<ArgumentCompleter> ArgumentCompleterList;
PerArgumentCommandCompleter(const ArgumentCompleterList& completers) PerArgumentCommandCompleter(const ArgumentCompleterList& completers)
: m_completers(completers) {} : m_completers(completers.begin(), completers.end()) {}
PerArgumentCommandCompleter(ArgumentCompleterList&& completers)
: m_completers(completers) {}
PerArgumentCommandCompleter(std::initializer_list<ArgumentCompleter> completers)
: m_completers(completers) {}
CandidateList operator()(const CommandParameters& params, CandidateList operator()(const CommandParameters& params,
size_t token_to_complete, size_t token_to_complete,
size_t pos_in_token) const; size_t pos_in_token) const;
private: private:
ArgumentCompleterList m_completers; std::vector<ArgumentCompleter> m_completers;
}; };
class CommandManager : public Singleton<CommandManager> class CommandManager : public Singleton<CommandManager>
@ -69,7 +63,7 @@ public:
unsigned flags = None, unsigned flags = None,
const CommandCompleter& completer = CommandCompleter()); const CommandCompleter& completer = CommandCompleter());
void register_command(const std::vector<std::string>& command_names, void register_commands(const memoryview<std::string>& command_names,
Command command, Command command,
unsigned flags = None, unsigned flags = None,
const CommandCompleter& completer = CommandCompleter()); const CommandCompleter& completer = CommandCompleter());

View File

@ -7,6 +7,7 @@
#include "filter.hh" #include "filter.hh"
#include "utils.hh" #include "utils.hh"
#include "completion.hh" #include "completion.hh"
#include "memoryview.hh"
#include "idvaluemap.hh" #include "idvaluemap.hh"
namespace Kakoune namespace Kakoune
@ -14,7 +15,7 @@ namespace Kakoune
class Window; class Window;
typedef std::vector<std::string> FilterParameters; typedef memoryview<std::string> FilterParameters;
typedef std::function<FilterAndId (Window& window, typedef std::function<FilterAndId (Window& window,
const FilterParameters& params)> FilterFactory; const FilterParameters& params)> FilterFactory;

View File

@ -3,7 +3,7 @@
#include <string> #include <string>
#include <functional> #include <functional>
#include <vector> #include "memoryview.hh"
namespace Kakoune namespace Kakoune
{ {
@ -17,7 +17,7 @@ class DisplayBuffer;
typedef std::function<void (DisplayBuffer& display_buffer)> HighlighterFunc; typedef std::function<void (DisplayBuffer& display_buffer)> HighlighterFunc;
typedef std::pair<std::string, HighlighterFunc> HighlighterAndId; typedef std::pair<std::string, HighlighterFunc> HighlighterAndId;
typedef std::vector<std::string> HighlighterParameters; typedef memoryview<std::string> HighlighterParameters;
} }

View File

@ -682,10 +682,11 @@ void add_hook(const CommandParameters& params, const Context& context)
if (params.size() < 4) if (params.size() < 4)
throw wrong_argument_count(); throw wrong_argument_count();
CommandParameters hook_params(params.begin()+3, params.end()); std::string regex = params[2];
std::vector<std::string> hook_params(params.begin()+3, params.end());
auto hook_func = [=](const std::string& param, const Context& context) { 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); CommandManager::instance().execute(hook_params, context);
}; };
@ -758,7 +759,7 @@ void exec_commands_in_runtime_file(const CommandParameters& params,
if (ptr) if (ptr)
{ {
strcpy(ptr+1, filename.c_str()); 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; FilterRegistry filter_registry;
GlobalHooksManager hooks_manager; GlobalHooksManager hooks_manager;
command_manager.register_command(std::vector<std::string>{ "e", "edit" }, edit, command_manager.register_commands({ "e", "edit" }, edit,
CommandManager::None, CommandManager::None,
PerArgumentCommandCompleter{ complete_filename }); PerArgumentCommandCompleter({ complete_filename }));
command_manager.register_command(std::vector<std::string>{ "q", "quit" }, quit<false>); command_manager.register_commands({ "q", "quit" }, quit<false>);
command_manager.register_command(std::vector<std::string>{ "q!", "quit!" }, quit<true>); command_manager.register_commands({ "q!", "quit!" }, quit<true>);
command_manager.register_command(std::vector<std::string>{ "w", "write" }, write_buffer, command_manager.register_commands({ "w", "write" }, write_buffer,
CommandManager::None, CommandManager::None,
PerArgumentCommandCompleter{ complete_filename }); PerArgumentCommandCompleter({ complete_filename }));
command_manager.register_command(std::vector<std::string>{ "wq" }, write_and_quit<false>); command_manager.register_command("wq", write_and_quit<false>);
command_manager.register_command(std::vector<std::string>{ "wq!" }, write_and_quit<true>); command_manager.register_command("wq!", write_and_quit<true>);
command_manager.register_command(std::vector<std::string>{ "b", "buffer" }, show_buffer, command_manager.register_commands({ "b", "buffer" }, show_buffer,
CommandManager::None, CommandManager::None,
PerArgumentCommandCompleter { PerArgumentCommandCompleter({
std::bind(&BufferManager::complete_buffername, &buffer_manager, _1, _2) std::bind(&BufferManager::complete_buffername, &buffer_manager, _1, _2)
}); }));
command_manager.register_command(std::vector<std::string>{ "ah", "addhl" }, add_highlighter, command_manager.register_commands({ "ah", "addhl" }, add_highlighter,
CommandManager::None, CommandManager::None,
PerArgumentCommandCompleter { PerArgumentCommandCompleter({
std::bind(&HighlighterRegistry::complete_highlighter, &highlighter_registry, _1, _2) std::bind(&HighlighterRegistry::complete_highlighter, &highlighter_registry, _1, _2)
}); }));
command_manager.register_command(std::vector<std::string>{ "agh", "addgrouphl" }, add_group_highlighter, command_manager.register_commands({ "agh", "addgrouphl" }, add_group_highlighter,
CommandManager::None, CommandManager::None,
PerArgumentCommandCompleter { PerArgumentCommandCompleter({
[&](const std::string& prefix, size_t cursor_pos) [&](const std::string& prefix, size_t cursor_pos)
{ return main_context.window().highlighters().complete_group_id(prefix, cursor_pos); }, { return main_context.window().highlighters().complete_group_id(prefix, cursor_pos); },
std::bind(&HighlighterRegistry::complete_highlighter, &highlighter_registry, _1, _2) std::bind(&HighlighterRegistry::complete_highlighter, &highlighter_registry, _1, _2)
}); }));
command_manager.register_command(std::vector<std::string>{ "rh", "rmhl" }, rm_highlighter, command_manager.register_commands({ "rh", "rmhl" }, rm_highlighter,
CommandManager::None, CommandManager::None,
PerArgumentCommandCompleter { PerArgumentCommandCompleter({
[&](const std::string& prefix, size_t cursor_pos) [&](const std::string& prefix, size_t cursor_pos)
{ return main_context.window().highlighters().complete_group_id(prefix, cursor_pos); } { return main_context.window().highlighters().complete_group_id(prefix, cursor_pos); }
}); }));
command_manager.register_command(std::vector<std::string>{ "rgh", "rmgrouphl" }, rm_group_highlighter, command_manager.register_commands({ "rgh", "rmgrouphl" }, rm_group_highlighter,
CommandManager::None, CommandManager::None,
[&](const CommandParameters& params, size_t token_to_complete, size_t pos_in_token) [&](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) else if (token_to_complete == 1)
return w.highlighters().get_group(params[0]).complete_id(arg, pos_in_token); return w.highlighters().get_group(params[0]).complete_id(arg, pos_in_token);
}); });
command_manager.register_command(std::vector<std::string>{ "af", "addfilter" }, add_filter, command_manager.register_commands({ "af", "addfilter" }, add_filter,
CommandManager::None, CommandManager::None,
PerArgumentCommandCompleter { PerArgumentCommandCompleter({
std::bind(&FilterRegistry::complete_filter, &filter_registry, _1, _2) std::bind(&FilterRegistry::complete_filter, &filter_registry, _1, _2)
}); }));
command_manager.register_command(std::vector<std::string>{ "rf", "rmfilter" }, rm_filter, command_manager.register_commands({ "rf", "rmfilter" }, rm_filter,
CommandManager::None, CommandManager::None,
PerArgumentCommandCompleter { PerArgumentCommandCompleter({
[&](const std::string& prefix, size_t cursor_pos) [&](const std::string& prefix, size_t cursor_pos)
{ return main_context.window().complete_filterid(prefix, cursor_pos); } { return main_context.window().complete_filterid(prefix, cursor_pos); }
}); }));
command_manager.register_command(std::vector<std::string>{ "hook" }, add_hook, CommandManager::IgnoreSemiColons); command_manager.register_command("hook", add_hook, CommandManager::IgnoreSemiColons);
command_manager.register_command(std::vector<std::string>{ "source" }, exec_commands_in_file, command_manager.register_command("source", exec_commands_in_file,
CommandManager::None, CommandManager::None,
PerArgumentCommandCompleter{ complete_filename }); PerArgumentCommandCompleter({ complete_filename }));
command_manager.register_command(std::vector<std::string>{ "runtime" }, exec_commands_in_runtime_file); command_manager.register_command("runtime", exec_commands_in_runtime_file);
command_manager.register_command(std::vector<std::string>{ "exec" }, exec_string); command_manager.register_command("exec", exec_string);
register_highlighters(); register_highlighters();
register_filters(); register_filters();