Async Code Runner for Neovim
Using nvim-plug
require("plug").add({
{
"wsdjeg/code-runner.nvim",
depends = {
{ "wsdjeg/job.nvim" },
{ "wsdjeg/notify.nvim" },
},
},
})
Using luarocks
luarocks install code-runner.nvim
require("code-runner").setup({
runners = {
lua = { exe = "lua", opt = { "-" }, usestdin = true },
},
enter_win = false,
})
lua require('code-runner').open()
lua require('code-runner').open('make test')
The exe option accepts a string, table, or function.
When it is a string or table, it is passed directly to job.start as the job command.
When it is a function, the function is executed first,
and its return value is then used as the command to start the job.
example:
require('code-runner').setup({
runners = {
lua = {
exe = function()
if vim.fn.executable('luajit') == 1 then
return 'luajit'
elseif vim.fn.executable('lua') == 1 then
return 'lua'
else
return { 'nvim', '-l' }
end
end,
opt = { '-' },
usestdin = true,
},
},
})
job command arguments which will be appended to job commands.
if usestdin is true, then runner use context in current buffer as stdin instead of using file name.
if usestdin is true, then code-runner.nvim use vim.fn.getline(runner.range[1], runner.range[2]) as default stdin, the default range is {1, '$'}).
This option is useful when run code block in markdown.
ftplugin/markdown.lua
vim.keymap.set('n', '<leader>lr', function()
-- require https://github.com/Shougo/context_filetype.vim
local cf = vim.fn['context_filetype#get']()
if cf.filetype ~= 'markdown' then
local runner = require('code-runner').get(cf.filetype)
runner['usestdin'] = true
runner['range'] = { cf['range'][1][1], cf['range'][2][1] }
require('code-runner').open(runner)
end
end, { silent = true, buffer = true })
To replace text in output, use transform option, this should be a function which accept one string argument and return a string.
for example:
require('code-runner').setup({
runners = {
lua = {
exe = 'lua',
opt = { '-' },
usestdin = true,
transform = function(line)
return '[Lua output]' .. line
end,
},
},
})
close(): close code runner windowget(ft): get default runner for specific filetypeget(ft): get default runner for specific filetypeLike this plugin? Star the repository on GitHub.
Love this plugin? Follow me on GitHub.
This project is licensed under the GPL-3.0 License.