Make DynamicRegister statically dispatch to its function
This commit is contained in:
parent
9b4bd611ef
commit
8badcdc0d5
27
src/main.cc
27
src/main.cc
|
@ -145,29 +145,28 @@ void register_registers()
|
|||
register_manager.add_register(c, make_unique<StaticRegister>());
|
||||
|
||||
using StringList = Vector<String, MemoryDomain::Registers>;
|
||||
static const struct {
|
||||
char name;
|
||||
StringList (*func)(const Context&);
|
||||
} dyn_regs[] = {
|
||||
{ '%', [](const Context& context) { return StringList{{context.buffer().display_name()}}; } },
|
||||
{ '.', [](const Context& context) {
|
||||
|
||||
register_manager.add_register('%', make_dyn_reg(
|
||||
[](const Context& context)
|
||||
{ return StringList{{context.buffer().display_name()}}; }));
|
||||
|
||||
register_manager.add_register('.', make_dyn_reg(
|
||||
[](const Context& context) {
|
||||
auto content = context.selections_content();
|
||||
return StringList{content.begin(), content.end()};
|
||||
} },
|
||||
{ '#', [](const Context& context) {
|
||||
}));
|
||||
|
||||
register_manager.add_register('#', make_dyn_reg(
|
||||
[](const Context& context) {
|
||||
StringList res;
|
||||
for (size_t i = 1; i < context.selections().size()+1; ++i)
|
||||
res.push_back(to_string((int)i));
|
||||
return res;
|
||||
} }
|
||||
};
|
||||
|
||||
for (auto& dyn_reg : dyn_regs)
|
||||
register_manager.add_register(dyn_reg.name, make_unique<DynamicRegister>(dyn_reg.func));
|
||||
}));
|
||||
|
||||
for (size_t i = 0; i < 10; ++i)
|
||||
{
|
||||
register_manager.add_register('0'+i, make_unique<DynamicRegister>(
|
||||
register_manager.add_register('0'+i, make_dyn_reg(
|
||||
[i](const Context& context) {
|
||||
StringList result;
|
||||
for (auto& sel : context.selections())
|
||||
|
|
|
@ -8,8 +8,6 @@
|
|||
#include "string.hh"
|
||||
#include "vector.hh"
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace Kakoune
|
||||
{
|
||||
|
||||
|
@ -46,14 +44,13 @@ protected:
|
|||
Vector<String, MemoryDomain::Registers> m_content;
|
||||
};
|
||||
|
||||
using RegisterRetriever = std::function<Vector<String, MemoryDomain::Registers> (const Context&)>;
|
||||
|
||||
// Dynamic value register, use it's RegisterRetriever
|
||||
// to get it's value when needed.
|
||||
template<typename Func>
|
||||
class DynamicRegister : public StaticRegister
|
||||
{
|
||||
public:
|
||||
DynamicRegister(RegisterRetriever function)
|
||||
DynamicRegister(Func function)
|
||||
: m_function(std::move(function)) {}
|
||||
|
||||
Register& operator=(ConstArrayView<String> values) override
|
||||
|
@ -68,9 +65,15 @@ public:
|
|||
}
|
||||
|
||||
private:
|
||||
RegisterRetriever m_function;
|
||||
Func m_function;
|
||||
};
|
||||
|
||||
template<typename Func>
|
||||
std::unique_ptr<Register> make_dyn_reg(Func func)
|
||||
{
|
||||
return make_unique<DynamicRegister<Func>>(std::move(func));
|
||||
}
|
||||
|
||||
class NullRegister : public Register
|
||||
{
|
||||
public:
|
||||
|
|
Loading…
Reference in New Issue
Block a user