Something went wrong. Try again.
my neovim config
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132local ts = vim.treesitterlocal table, loadstring = table, loadstringlocal api = vim.apilocal ns_id = api.nvim_create_namespace("cb_run")local cb_id = 0
local M = {}
local inspect_node = function(node) if node then local buf = vim.api.nvim_get_current_buf() vim.print(node:type(), ts.get_node_text(node, buf)) else vim.notify_once("found no node", 3) endend
---@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 integerlocal 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 = api.nvim_buf_set_extmark(bnr, ns_id, line_num, col_num, opts) print(mark_id)end
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 ret end,
nix = function(code) local obj = vim.system({ "nix", "eval", "--expr", code, }):wait() return vim.trim(obj.stdout) end,}
--- TODO: more runners--- TODO: run based on langlocal 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
local result = runners[lang](code)
display_result(buf, row, 7, result, cb_id)end
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