librime-lua icon indicating copy to clipboard operation
librime-lua copied to clipboard

搞了一個 translator 看lua 變數

Open shewer opened this issue 5 years ago • 1 comments

1 增加 lua_translator@debug 2 增加 pattern: "^[GL].*$" 3 下載 https://github.com/shewer/librime-lua-script ( 要用 reduce string:split...) require 'muti_reverse' or 'muti_reverse.metatable' <---

--[[ 原理:  os table  = _G.os  = _G["os"] 
    function  os.date()   = _G.os.date()   = G["os"]["date"]()
目前只能 list  
--]]

   function objlist(obj,seg, ... )   -- 判斷 type  放出 string
       local  _type= type(obj)
       if _type == "string" then
                   yield(Candidate("debug",seg.start,seg._end, string.format("%s ",obj) ,"string"))
       elseif _type == "number" then
                   yield(Candidate("debug",seg.start,seg._end, string.format("%s ",obj) ,"number"))
       elseif _type == "function" then
                   yield(Candidate("debug",seg.start,seg._end, string.format("%s ",obj) ,"number"))
           --local ptab={ pcall(obj, ... ) }
           --for i,v in ipairs(ptab) do
               --yield(Candidate("debug",seg.start,seg._end, string.format("%s : %s",i,v) ,"table"))
           --end
       elseif _type == "table"  then
               for k,v in pairs(obj) do
                   yield(Candidate("debug",seg.start,seg._end, string.format("%s : %s",k,v) ,"table"))
               end
       end
   end

---    判斷 字串  格式    [ [ [G|L]os.date]   | argv1,argv2 ] G== _G  L=local       
 local function debug(_input,seg,env)
     local localdata={env=env,seg=seg,reverse=revfilter}  -- local table 
     yield(Candidate("debug", seg.start, seg._end,_input , "dubug:"))  -- test
     local t , input = _input:match("^([GLF])(.*)$" )   --  split string
     if not input then return end  --  input == nil   exit
     local _tabb = input:split("|")  -- split   variable  and argv
     local obj_str, argv_str = table.unpack( input:split("|") )   -- 

     local inp_tab = ( argv_str and argv_str:split(",") ) or {}  -- initialize argv  
     if  obj_str then
         yield(Candidate("debug", seg.start, seg._end,string.format("-%s---%s-",obj_str,argv_str) , "dubug:"))  -- test 

         local V=  (t =="L" and localdata) or _G    --  判斷 查找 local or _G
         local obj
         obj=obj_str:split("."):reduce(function(elm,svg)   
             if svg[elm] then    --   如果不是nil  再到下一層
                 return svg[elm]
             else                        -- 如果 nil  將 此層 object  傳 --objlist() 推出
                 objlist(svg,seg, table.unpack(inp_tab ) ) 
             end
         end ,
         V)


     end
 end

shewer avatar Aug 12 '20 01:08 shewer

覺得這主意不錯 但 要將 格式定義 輸入都是字串 number engine 下的 userdata 無法向下尋找

1 list table 2 exec function 3 return value 4 set value 5 env.engine userdata 需要加強方便存取 tool

shewer avatar Aug 12 '20 01:08 shewer