Something went wrong. Try again.
my neovim config
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132local ts = vim.treesitterlocal table, loadstring = table, loadstringlocal ns_id = vim.api.nvim_create_namespace("cb_run")local cb_id = 0
local M = {}
---@param node TSNode?---@return TSNode?local function find_code_block(node) while node and node:type() ~= "fenced_code_block" do if node:parent() ~= nil then node = assert(node:parent()) else return nil end end return nodeend
---@return string?local get_code_block = function() local buf = vim.api.nvim_get_current_buf() local node = find_code_block(ts.get_node())
-- TODO: node pos if node then return ts.get_node_text(node, buf) endend
---@param code string---@return string code---@return string langfunction M.parse_block(code) local lines = vim.split(code, "\n") local lang = table.remove(lines, 1):sub(4) table.remove(lines, #lines) return table.concat(lines, "\n"), langend
-- TODO: default display positions:-- 1. after last line-- 2. below code block-- TODO: support more lang, with vim.system-- TODO: virtual line like diagnositics
---@param bnr integer---@param line_num integer---@param col_num integer---@param text string---@param id integer---@return integer mark_idlocal function display_result(bnr, line_num, col_num, text, id) local opts = { id = id, virt_text = { { text, "IncSearch" } }, virt_text_pos = "eol", } local mark_id = vim.api.nvim_buf_set_extmark(bnr, ns_id, line_num, col_num, opts) return mark_idend
local runners = { lua = function(code) local f = loadstring(code, ("MyRunCodeBlock[%d]"):format(cb_id)) local ret
assert(f) setfenv(f, { print = function(v) ret = v end, })
local v = f()
if not code:find("print") then ret = v end
return tostring(ret) end,
nix = function(code) local obj = vim.system({ "nix", "eval", "--expr", code, }):wait() if obj.code == 0 and obj.stdout then return vim.trim(obj.stdout) elseif obj.stderr then return vim.trim(obj.stderr) end end,}
local cache = {}
local function run() local code_block = get_code_block() if not code_block then vim.notify("Not on a code block") return end cb_id = cb_id + 1 local code, lang = M.parse_block(code_block) local buf = vim.api.nvim_get_current_buf() local row = vim.api.nvim_win_get_cursor(0)[1] - 1
if runners[lang] == nil then vim.notify("No configured runner") return end local result = runners[lang](code) if result ~= nil then display_result(buf, row, 7, result, cb_id) endend
vim.keymap.set("n", "<Plug>MyRun", run)
function M.enable(enable) if enable then vim.keymap.set("n", "<S-CR>", "<Plug>MyRun") endend
return M