Skip to content

Commit

Permalink
Merge branch 'dev06' into dev_a
Browse files Browse the repository at this point in the history
  • Loading branch information
shewer committed Mar 31, 2021
2 parents 755e062 + a4842ab commit 91e2dce
Show file tree
Hide file tree
Showing 2 changed files with 289 additions and 6 deletions.
48 changes: 48 additions & 0 deletions sample/projection.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

local function init_func(env)

local config=env.engine.schema.config
-- load ConfigList form path
local proedit_fmt_list = conifg:get_list("translastor/preedit_format")
-- print value
for i=0,proedit_fmt_list.size do
print(i, proedit_fmt_list:get_value_at(i).value)
end
-- create Projection obj
local p1=Projection()
p1:load(proedit_fmt_list)

-- user create Projection obj
local pfl2= ConfigList()
-- create ConfigValue & ConfigList ust isnert( index, ConfigValue) append()
local v1= ConfigValue("xlit|abc|xyz|")
pfl2:append(v1)
pfl2:append( ConfigValue("xlit|def|tuv|"))
pfl2:insert(1,ConfigValue("xlit|ghi|qrs|"))
local p2=Projection()
p2:load(pfl2)
local str1= "abcdefg"
--
print(str1 , p1:apply(str1), p2:apply(str1) )
--
env.p1=p1
env.p2=p2
end
local function func(input,seg,env)
for cand in input:iter() do
cand.comment= env.p1(cand.comment) .. env.p2(cand.comment)
yield(cand)
end
end


return { init=init_func, func=func }
--- rime.lua
-- projection_filter= require("projection.lua")
--
--
-- schema.yaml
-- insert to /engine/filter
-- lua_filter@projection_filter
--

247 changes: 241 additions & 6 deletions src/types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
#include <rime/context.h>
#include <rime/schema.h>
#include <rime/config.h>
#include <rime/config/config_component.h>
#include <rime/config/config_types.h>
#include <rime/gear/translator_commons.h>
#include <rime/dict/reverse_lookup_dictionary.h>
#include <rime/key_event.h>
#include <rime/switcher.h>
#include "lua_gears.h"
#include "lib/lua_templates.h"

#include <rime/algo/algebra.h>
using namespace rime;

template<typename T>
Expand Down Expand Up @@ -599,6 +601,234 @@ namespace SchemaReg {
};
}

namespace ConfigValueReg {
typedef ConfigValue T;

// an<T> make(){
// return New<T>();
// };
an<T> make(string s){
return New<T>(s);
};


optional<bool> get_bool(T &t) {
bool v;
if (t.GetBool( &v))
return v;
else
return {};

}

optional<int> get_int(T &t) {
int v;
if (t.GetInt( &v))
return v;
else
return optional<int>{};

}

optional<double> get_double(T &t) {
double v;
if (t.GetDouble( &v))
return v;
else
return optional<double>{};

}

optional<string> get_string(T &t) {
string v;
if (t.GetString( &v))
return v;
else
return optional<string>{};
};

bool set_string(T &t, const string &value) {
return t.SetString( value);
};
string type(T &t){
switch (t.type()) {
case T::kNull: return "kNull";
case T::kScalar: return "kScalar";
case T::kList: return "kList";
case T::kMap: return "kMap";
}
return "";

}
static const luaL_Reg funcs[] = {
{"ConfigValue", WRAP(make)},
{ NULL, NULL },
};

static const luaL_Reg methods[] = {
{"get_bool",WRAP(get_bool)},
{"get_int",WRAP(get_int)},
{"get_double",WRAP(get_double)},
{"get_string",WRAP(get_string)},
{"set_string",WRAP(set_string)},
{ NULL, NULL },
};

static const luaL_Reg vars_get[] = {
{"value",WRAP(get_string)},
{"type",WRAP(type)},
{ NULL, NULL },
};

static const luaL_Reg vars_set[] = {
{"value",WRAP(set_string)},
{ NULL, NULL },
};
}
namespace ConfigListReg {
typedef ConfigList T;

an<T> make(){
return New<T>();
};
//
bool append(T &t, an<ConfigValue> l ){
an<ConfigItem> p=l;
return t.Append( l );
};
bool insert(T &t, size_t i, an<ConfigValue> l) {
an<ConfigItem> p=l;
return t.Insert(i, p );
};

string type(T &t){
switch (t.type()) {
case T::kNull: return "kNull";
case T::kScalar: return "kScalar";
case T::kList: return "kList";
case T::kMap: return "kMap";
}
return "";

}


static const luaL_Reg funcs[] = {
{"ConfigList", WRAP(make)},
{ NULL, NULL },
};

static const luaL_Reg methods[] = {
{"get_at", WRAPMEM(T::GetAt)},
{"get_value_at", WRAPMEM(T::GetValueAt)},
{"set_at", WRAPMEM(T::SetAt)},
//{"append", WRAPMEM(T::Append)},
//{"insert", WRAPMEM(T::Insert)},
{"append", WRAP(append)},
{"insert", WRAP(insert)},
{"clear", WRAPMEM(T::Clear)},
{"empty", WRAPMEM(T::empty)},
{"resize", WRAPMEM(T::Resize)},
{ NULL, NULL },
};

static const luaL_Reg vars_get[] = {
{"size", WRAPMEM(T::size)},
{"type",WRAP(type)},
{ NULL, NULL },
};

static const luaL_Reg vars_set[] = {
{ NULL, NULL },
};
}
namespace ConfigItemReg {
typedef ConfigItem T;

string type(T &t){
switch (t.type()) {
case T::kNull: return "kNull";
case T::kScalar: return "kScalar";
case T::kList: return "kList";
case T::kMap: return "kMap";
}
return "";

}
ConfigValue* get_value(T &t){
if (t.type() == T::kScalar)
return (ConfigValue *)( &t);
else
return nullptr ;
}
ConfigList * get_list(T &t){
if (t.type() == T::kList)
return (ConfigList *) &t;
else
return nullptr ;
//return {};
}
ConfigMap * get_map(T &t){
if (t.type() == T::kMap)
return (ConfigMap *) &t;
else
//return {};
return nullptr ;
}
static const luaL_Reg funcs[] = {
{ NULL, NULL },
};

static const luaL_Reg methods[] = {
{"get_value",WRAP(get_value)},
{"get_list",WRAP(get_list)},
// {"get_map",WRAP(get_map)},
{ NULL, NULL },
};
static const luaL_Reg vars_get[] = {
{"type",WRAP(type)},
{ NULL, NULL },
};

static const luaL_Reg vars_set[] = {
{ NULL, NULL },
};

}
namespace ProjectionReg{
typedef Projection T;
an<T> make(){
return New<T>();
};


string apply(T &t, const string &s){
string res= s;
if (t.Apply(&res))
return res;
else
return "";
}

static const luaL_Reg funcs[] = {
{"Projection",WRAP(make)},
{ NULL, NULL },
};

static const luaL_Reg methods[] = {
{"load",WRAPMEM(T::Load)},
{"apply",WRAP(apply)},
{ NULL, NULL },
};
static const luaL_Reg vars_get[] = {
{ NULL, NULL },
};

static const luaL_Reg vars_set[] = {
{ NULL, NULL },
};

}
namespace ConfigReg {
typedef Config T;

Expand All @@ -608,35 +838,35 @@ namespace ConfigReg {
return v;
else
return {};
}
};

optional<int> get_int(T &t, const string &path) {
int v;
if (t.GetInt(path, &v))
return v;
else
return optional<int>{};
}
};

optional<double> get_double(T &t, const string &path) {
double v;
if (t.GetDouble(path, &v))
return v;
else
return optional<double>{};
}
};

optional<string> get_string(T &t, const string &path) {
string v;
if (t.GetString(path, &v))
return v;
else
return optional<string>{};
}
};

bool set_string(T &t, const string &path, const string &value) {
return t.SetString(path, value);
}
};

static const luaL_Reg funcs[] = {
{ NULL, NULL },
Expand All @@ -657,6 +887,7 @@ namespace ConfigReg {
{ "get_double", WRAP(get_double) },
{ "get_string", WRAP(get_string) },
{ "get_list_size", WRAPMEM(T::GetListSize) },
{ "get_list", WRAPMEM(T::GetList) },

//an<ConfigItem> GetItem(const string& path);
//an<ConfigValue> GetValue(const string& path);
Expand Down Expand Up @@ -941,6 +1172,10 @@ void types_init(lua_State *L) {
EXPORT(CompositionReg, L);
EXPORT(SchemaReg, L);
EXPORT(ConfigReg, L);
EXPORT(ConfigItemReg, L);
EXPORT(ConfigListReg, L);
EXPORT(ConfigValueReg, L);
EXPORT(ProjectionReg, L);
EXPORT(NotifierReg, L);
EXPORT(OptionUpdateNotifierReg, L);
EXPORT(PropertyUpdateNotifierReg, L);
Expand Down

0 comments on commit 91e2dce

Please sign in to comment.