irongit

Execute files easily with nvim

40 lines852 BLua
1local M = {
2 target = {
3 path = vim.fn.expand("%"),
4 ft = vim.bo.filetype,
5 },
6}
7
8local datadir = vim.fn.stdpath("data") .. "/execute"
9if vim.fn.isdirectory(datadir) == 0 then
10 vim.fn.mkdir(datadir)
11end
12local datafile = datadir .. "/" .. vim.fn.sha256(vim.fn.getcwd()) .. ".json"
13vim.api.nvim_create_autocmd("VimLeavePre", {
14 callback = function()
15 local ok, encoded
16 ok, encoded = pcall(vim.fn.json_encode, M)
17 if not ok then
18 return
19 end
20 pcall(vim.fn.writefile, { encoded }, datafile)
21 end,
22})
23vim.api.nvim_create_autocmd("VimEnter", {
24 callback = function()
25 local ok, raw, decoded, json
26 ok, raw = pcall(vim.fn.readfile, datafile)
27 if not ok then
28 return
29 end
30 json = table.concat(raw, "\n")
31 ok, decoded = pcall(vim.fn.json_decode, json)
32 if ok then
33 for k, v in pairs(decoded) do
34 M[k] = v
35 end
36 end
37 end,
38})
39
40return M