Skip to content

Commit

Permalink
feat:(language support)! added support for other languages through
Browse files Browse the repository at this point in the history
external code runners
  • Loading branch information
YaroSpace committed Nov 16, 2024
1 parent 9e99254 commit 9e021bb
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 24 deletions.
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
set_lua_paths = eval $$(luarocks path --lua-version 5.1 --bin)
test_unit = busted -o spec/utfTerminal.lua --run unit
test_unit = busted -o spec/utfTerminal.lua --run=unit
tag ?= wip

watch = '*.lua'

.PHONY: api_documentation luacheck stylua test
Expand All @@ -27,4 +26,4 @@ watch:
@$(set_lua_paths); while sleep 0.1; do find . -name $(watch) | entr -d -c $(test_unit); done

watch_tag:
@$(set_lua_paths); while sleep 0.1; do find . -name $(watch) | entr -d -c $(test_unit) -t=$(tag); done
@$(set_lua_paths); while sleep 0.1; do find . -name $(watch) | entr -d -c $(test_unit) --tags=$(tag); done
8 changes: 8 additions & 0 deletions lua/lua-console/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ local default_config = {
resize_up = '<C-Up>',
resize_down = '<C-Down>',
help = '?'
},
external_evaluators = {
ruby = {
cmd = { 'ruby', '-e' },
env = {},
prepend_code = '$stdout.sync = true;',
formatter = nil
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion lua/lua-console/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ M.set_buf_keymap = function()
vim.keymap.set({"n", "v"}, mappings.eval, "", {
buffer = buf,
desc = "Eval lua code in current line or visual selection",
callback = utils.eval_lua_in_buffer
callback = utils.eval_code_in_buffer
})

vim.keymap.set({"n"}, mappings.save, "", {
Expand Down
84 changes: 70 additions & 14 deletions lua/lua-console/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ local function remove_empty_lines(tbl)
return vim.tbl_filter(function(el) return vim.fn.trim(el) ~= '' end, tbl)
end

local function trim_empty_lines(tbl)
if #tbl == 0 then return tbl end

if vim.trim(tbl[#tbl] or '') == '' then table.remove(tbl, #tbl) end
if vim.trim(tbl[1] or '') == '' then table.remove(tbl, 1) end

return tbl
end

--- Remove the stacktrace preceeding the call from lua-console
local function clean_stacktrace(error)
local lines = to_table(error)
Expand Down Expand Up @@ -168,6 +177,7 @@ local function add_return(tbl)
end

local get_ctx = function()
-- TODO: move context to buffer variable
if config.buffer.preserve_context and Lua_console.ctx then return Lua_console.ctx end
local env, mt = {}, {}

Expand Down Expand Up @@ -196,9 +206,6 @@ end
local eval_lua = function(lines)
vim.validate({ lines = { lines, 'table'} })

lines = remove_empty_lines(lines)
if vim.tbl_isempty(lines) then return {} end

local lines_with_return = add_return(lines)
local env = get_ctx()

Expand All @@ -224,8 +231,61 @@ local eval_lua = function(lines)
return print_buffer
end

local get_external_evaluator = function(lang)
local eval_config = config.external_evaluators[lang]
if not (eval_config and eval_config.cmd) then
vim.notify(string.format("No external evaluator for language '%s' found", lang), vim.log.levels.WARN)
return
end

local job_opts = {
env = eval_config or {},
on_stdout = function(_, ret, _)
ret = trim_empty_lines(ret or {})
if #ret > 0 then append_current_buffer(ret) end
end,
on_stderr = function(_, ret, _)
ret = trim_empty_lines(ret or {})
if #ret > 0 then append_current_buffer(ret) end
end,
on_exit = function() end,
stderr_buffered = true,
stdout_buffered = true,
}

return function(lines)
local code = eval_config.prepend_code .. ' ' .. to_string(lines)
local cmd = eval_config.cmd
vim.list_extend(cmd, { code })

vim.fn.jobstart(cmd, job_opts)
return {}
end
end

local get_evaluator = function(buf, lines)
local evaluator
local lang = lines[1]:match("```(.+)")

if lang then table.remove(lines, 1) end
lang = lang and lang or vim.bo[buf].filetype

if lang == '' then
vim.notify('Plese specify the language to evaluate', 2)
return
end

if lang == 'lua' then
evaluator = eval_lua
else
evaluator = get_external_evaluator(lang)
end

return evaluator
end

---Evaluates lua in the current line or visual selections and appends to current buffer
local eval_lua_in_buffer = function()
local eval_code_in_buffer = function()
local buf = vim.fn.bufnr()

if vim.api.nvim_get_mode().mode == "V" then
Expand All @@ -238,17 +298,13 @@ local eval_lua_in_buffer = function()
end

local lines = vim.api.nvim_buf_get_lines(buf, v_start - 1, v_end, false)
if #lines == 0 or (#lines == 1 and vim.trim(lines[1]) == '') then return end

local result
local filetype = vim.bo.filetype
lines = remove_empty_lines(lines)
if #lines == 0 then return end

if filetype == 'ruby' then
result = eval_ruby(lines)
else
result = eval_lua(lines)
end
local evaluator = get_evaluator(buf, lines)
if not evaluator then return end

local result = evaluator(lines)
if #result == 0 then return end

if #result == 1 and result[1]:find('nil') then
Expand Down Expand Up @@ -284,7 +340,7 @@ return {
load_console = load_console,
append_current_buffer = append_current_buffer,
eval_lua = eval_lua,
eval_lua_in_buffer = eval_lua_in_buffer,
eval_code_in_buffer = eval_code_in_buffer,
get_plugin_path = get_plugin_path,
load_messages = load_messages,
get_path_lnum = get_path_lnum
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/mappings_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ describe("lua-console.nvim - mappings", function()
assert.has_string(result, config.mappings.help .. ' - help')
end)

it("toggles help message #current", function()
it("toggles help message", function()
vim.api.nvim_buf_delete(vim.fn.bufnr(buf), { force = true })

console.toggle_console()
Expand Down
11 changes: 6 additions & 5 deletions spec/unit/utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ describe("lua-console.utils", function()

before_each(function()
buf = vim.api.nvim_create_buf(true, true)
vim.bo[buf].filetype = 'lua'
win = vim.api.nvim_open_win(buf, true, { split = "left" })
_G.Lua_console = { buf = buf, win = win }
end)
Expand Down Expand Up @@ -307,7 +308,7 @@ describe("lua-console.utils", function()

it("evaluates lua in current buffer - single line", function()
vim.api.nvim_win_set_cursor(win, { 1, 0 })
utils.eval_lua_in_buffer()
utils.eval_code_in_buffer()

expected = h.to_string([[
{a=1, b=2}
Expand All @@ -326,7 +327,7 @@ describe("lua-console.utils", function()
vim.api.nvim_win_set_cursor(win, { 2, 0 })
vim.cmd.exe("'normal V3j'")

utils.eval_lua_in_buffer()
utils.eval_code_in_buffer()

expected = h.to_string([[
{a=1, b=2}
Expand All @@ -353,7 +354,7 @@ describe("lua-console.utils", function()
assert.has_string(result, expected)
end)

it("evaluates lua in current buffer - shows nil as virtual text", function()
it("evaluates lua in current buffer - shows nil as virtual text #wip", function()
vim.api.nvim_win_set_cursor(win, { 1, 0 })
h.send_keys('V2j')

Expand All @@ -363,7 +364,7 @@ describe("lua-console.utils", function()
end
]])
h.set_buffer(buf, content)
utils.eval_lua_in_buffer()
utils.eval_code_in_buffer()

expected = config.buffer.prepend_result_with .. 'nil'

Expand All @@ -377,7 +378,7 @@ describe("lua-console.utils", function()
content = h.to_table[[ for i, ]]
h.set_buffer(buf, content)

utils.eval_lua_in_buffer()
utils.eval_code_in_buffer()
expected = h.to_string([[
=> [string "Lua console: "]:1: '<name>' expected near '<eof>'
]])
Expand Down

0 comments on commit 9e021bb

Please sign in to comment.