Execute files easily with nvim
first commit
3 files changed, +89 -0
+1-0execute.nvim
| @@ -0,0 +1 @@ | ||
| 1 | +/home/huncho/code/nvim-plugins/execute.nvim |
+40-0lua/execute/cache.lua
| @@ -0,0 +1,40 @@ | ||
| 1 | +local M = { | |
| 2 | + target = { | |
| 3 | + path = vim.fn.expand("%"), | |
| 4 | + ft = vim.bo.filetype, | |
| 5 | + }, | |
| 6 | +} | |
| 7 | + | |
| 8 | +local datadir = vim.fn.stdpath("data") .. "/execute" | |
| 9 | +if vim.fn.isdirectory(datadir) == 0 then | |
| 10 | + vim.fn.mkdir(datadir) | |
| 11 | +end | |
| 12 | +local datafile = datadir .. "/" .. vim.fn.sha256(vim.fn.getcwd()) .. ".json" | |
| 13 | +vim.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 | +}) | |
| 23 | +vim.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 | + | |
| 40 | +return M |
+48-0lua/execute/init.lua
| @@ -0,0 +1,48 @@ | ||
| 1 | +local data = require("execute.cache") | |
| 2 | +local M = {} | |
| 3 | + | |
| 4 | +local cwd = vim.fn.getcwd() | |
| 5 | + | |
| 6 | +function M.mark() | |
| 7 | + data.target.path = vim.fn.expand("%") | |
| 8 | + data.target.ft = vim.bo.filetype | |
| 9 | +end | |
| 10 | + | |
| 11 | +---Runs a given path and filetype | |
| 12 | +---@param path string absolute path | |
| 13 | +---@param ft string file type | |
| 14 | +function M.run(path, ft) | |
| 15 | + vim.cmd("w") | |
| 16 | + local relative_path = "./" .. path:gsub("^" .. vim.pesc(cwd), "") | |
| 17 | + local basename = path:match("([^/]+)$") | |
| 18 | + local stem = basename:match("^(.+)%.") or basename | |
| 19 | + if ft == "python" then | |
| 20 | + vim.cmd("terminal python " .. relative_path) | |
| 21 | + elseif ft == "rust" then | |
| 22 | + if path:find("examples/") then | |
| 23 | + vim.cmd("terminal cargo run --example " .. stem) | |
| 24 | + elseif data.target.path:find("bin/") then | |
| 25 | + vim.cmd("terminal cargo run --bin " .. stem) | |
| 26 | + elseif data.target.path:find("test/") then | |
| 27 | + vim.cmd("terminal cargo test") | |
| 28 | + else | |
| 29 | + vim.cmd("terminal cargo run") | |
| 30 | + end | |
| 31 | + elseif ft == "lua" then | |
| 32 | + vim.cmd("luafile " .. path) | |
| 33 | + else | |
| 34 | + vim.cmd("terminal chmod +x " .. relative_path .. "&& " .. relative_path) | |
| 35 | + end | |
| 36 | +end | |
| 37 | + | |
| 38 | +---Runs the current marked file | |
| 39 | +function M.run_marked() | |
| 40 | + M.run(data.target.path, data.target.ft) | |
| 41 | +end | |
| 42 | + | |
| 43 | +---Runs the current file without marking it | |
| 44 | +function M.run_current() | |
| 45 | + M.run(vim.fn.expand("%"), vim.bo.filetype) | |
| 46 | +end | |
| 47 | + | |
| 48 | +return M |