tltypes.lua (1690B)
1 local M = {} 2 local a = vim.api 3 4 local types 5 6 local function get_type(id) 7 local tmp = types.types[tostring(id)] 8 9 local meta = { 10 __index = function(tbl, index) 11 if index == 'file' or index == 'fields' or index == 'str' or index == 'ref' then 12 return rawget(tbl, index) 13 end 14 15 if index == 'y' or index == 'x' then 16 return tonumber(rawget(tbl, index)) 17 end 18 19 if rawget(tbl, 'fields') then 20 return get_type(tbl.fields[index]) 21 end 22 23 return nil 24 end 25 } 26 27 if not tmp then 28 error(string.format("Invalid type identifier: %s", tostring(id))) 29 end 30 31 if tmp.ref then 32 return get_type(tmp.ref) 33 else 34 return setmetatable(tmp, meta) 35 end 36 end 37 38 local function put_lines(lines) 39 a.nvim_buf_set_lines(0, -2, -1, false, vim.split(lines, '\n', { plain = true })) 40 end 41 42 local function input_cb(text) 43 if tostring(tonumber(text)) == text then 44 put_lines(vim.inspect(get_type(text))) 45 elseif text:sub(1, 1) == '*' then 46 put_lines(vim.inspect(types.types[text:sub(2)])) 47 else 48 put_lines(text) 49 end 50 a.nvim_buf_set_option(0, 'modified', false) 51 end 52 53 function M.repl(paths) 54 types = vim.json.decode(vim.fn.system { "tl", "types", unpack(vim.fn.glob(paths, false, true)) }) 55 vim.cmd[[split]] 56 local win = a.nvim_get_current_win() 57 58 local repl_buf = a.nvim_create_buf(false, true) 59 60 vim.fn.prompt_setcallback(repl_buf, input_cb) 61 62 vim.fn.prompt_setprompt(repl_buf, "tltype> ") 63 64 a.nvim_buf_set_option(repl_buf, 'buftype', 'prompt') 65 a.nvim_buf_set_option(repl_buf, 'bufhidden', 'wipe') 66 a.nvim_buf_set_name(repl_buf, [[Architext REPL]]) 67 a.nvim_win_set_buf(win, repl_buf) 68 69 vim.cmd.startinsert() 70 end 71 72 return M