From b100c59783efb7d9ef021ad9c515a66040bb2a3d Mon Sep 17 00:00:00 2001 From: tamton-aquib Date: Sun, 9 Oct 2022 23:56:59 +0530 Subject: [PATCH 01/12] add execute module base --- lua/neorg/modules/core/execute/module.lua | 176 ++++++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100644 lua/neorg/modules/core/execute/module.lua diff --git a/lua/neorg/modules/core/execute/module.lua b/lua/neorg/modules/core/execute/module.lua new file mode 100644 index 000000000..029249dcb --- /dev/null +++ b/lua/neorg/modules/core/execute/module.lua @@ -0,0 +1,176 @@ +-- TODO: better colors +require("neorg.modules.base") + +local module = neorg.modules.create("core.execute") +local ts = require("nvim-treesitter.ts_utils") + +module.setup = function() + return { success = true, requires = { "core.neorgcmd", "core.integrations.treesitter" } } +end + +module.load = function() + module.required["core.neorgcmd"].add_commands_from_table({ + execute = { + args = 1, + subcommands = { + view = { args=0, name="execute.view" }, + -- bruh2 = { args=0, name="execute.bruh2" }, + } + } + }) +end + +module.config.public = { + lang_cmds = { + python = 'python3', + lua = 'lua', + javascript = 'node', + }, +} +module.config.private = {} + +module.private = { + buf = vim.api.nvim_get_current_buf(), + ns = vim.api.nvim_create_namespace("execute"), + code_block = {}, + interrupted = false, + jobid = 0, + temp_filename = '', + + + create_dir_if_doesnt_exist = function() + if vim.fn.isdirectory(module.public.tmpdir) == 0 then + vim.fn.mkdir(module.public.tmpdir, "p") + end + end, + + virtual = { + init = function() + table.insert(module.public.output, {{"", 'Keyword'}}) + table.insert(module.public.output, {{"Result:", 'Keyword'}}) + + local id = vim.api.nvim_buf_set_extmark( + module.private.buf, + module.private.ns, + module.private.code_block['end'].row, + module.private.code_block['end'].column, + { virt_lines = module.public.output } + ) + + vim.api.nvim_create_autocmd('CursorMoved', { + once = true, + callback = function() + vim.api.nvim_buf_del_extmark(module.private.buf, module.private.ns, id) + module.private.interrupted = true + module.public.output = {} + vim.fn.delete(module.private.temp_filename) + end + }) + return id + end, + + update = function(id) + vim.api.nvim_buf_set_extmark( + module.private.buf, + module.private.ns, + module.private.code_block['end'].row, + 0, + { id=id, virt_lines = module.public.output } + ) + end + }, + + spawn = function(command) + module.private.interrupted = false + local id = module.private.virtual.init() + + module.private.jobid = vim.fn.jobstart(command, { + stdout_buffered = false, + -- TODO: check exit code conditions and colors + on_stdout = function(_, data) + if module.private.interrupted then + vim.fn.jobstop(module.private.jobid) + return + end + + for _, line in ipairs(data) do + if line ~= "" then + table.insert(module.public.output, {{line, 'Function'}}) + module.private.virtual.update(id) + end + end + end, + + on_stderr = function(_, data) + if module.private.interrupted then + vim.fn.jobstop(module.private.jobid) + return + end + + for _, line in ipairs(data) do + if line ~= "" then + table.insert(module.public.output, {{line, 'Error'}}) + module.private.virtual.update(id) + end + end + end + + }) + end +} + +module.public = { + tmpdir = "/tmp/neorg-execute/", + output = {}, + + view = function() + local node = ts.get_node_at_cursor(0, true) + local p = module.required["core.integrations.treesitter"].find_parent(node, "^ranged_tag$") + + -- TODO: Add checks here + local code_block = module.required["core.integrations.treesitter"].get_tag_info(p, true) + if not code_block then + vim.pretty_print("Not inside a code block!") + return + end + + if code_block.name == "code" then + module.private.code_block = code_block + local ft = code_block.parameters[1] + + module.private.create_dir_if_doesnt_exist() + module.private.temp_filename = module.public.tmpdir + .. code_block.start.row .. "_" + .. code_block['end'].row + .. "." .. ft + + local file = io.open(module.private.temp_filename, "w") + if file == nil then return end + file:write(table.concat(code_block.content, '\n')) + file:close() + + local command = module.config.public.lang_cmds[ft] + command = {command, module.private.temp_filename} + + module.private.spawn(command) + end + + -- {attributes, content, ["end"], name, parameters, ["start"]} + end, + -- bruh2 = function() end +} + +module.on_event = function(event) + if event.split_type[2] == "execute.view" then + vim.schedule(module.public.view) + end +end + +module.events.subscribed = { + ["core.neorgcmd"] = { + ["execute.view"] = true, + -- ["execute.bruh2"] = true + } +} + +return module From b1c1f97551a1348c61f67f74c3ddce144c538323 Mon Sep 17 00:00:00 2001 From: tamton-aquib Date: Thu, 13 Oct 2022 22:14:30 +0530 Subject: [PATCH 02/12] adding unstable normal subcommand --- lua/neorg/modules/core/execute/module.lua | 86 ++++++++++++++++++++--- 1 file changed, 75 insertions(+), 11 deletions(-) diff --git a/lua/neorg/modules/core/execute/module.lua b/lua/neorg/modules/core/execute/module.lua index 029249dcb..a6bb8c8c4 100644 --- a/lua/neorg/modules/core/execute/module.lua +++ b/lua/neorg/modules/core/execute/module.lua @@ -1,4 +1,6 @@ +---@diagnostic disable: undefined-global -- TODO: better colors +-- TODO: avoid code duplication. require("neorg.modules.base") local module = neorg.modules.create("core.execute") @@ -14,7 +16,7 @@ module.load = function() args = 1, subcommands = { view = { args=0, name="execute.view" }, - -- bruh2 = { args=0, name="execute.bruh2" }, + normal = { args=0, name="execute.normal" }, } } }) @@ -79,13 +81,49 @@ module.private = { ) end }, + normal = { + line_set = 0, + + init = function() + module.private.normal.line_set = module.private.code_block['end'].row + 1 + table.insert(module.public.output, '') + table.insert(module.public.output, 'Result:') + + vim.api.nvim_buf_set_lines( + module.private.buf, + module.private.normal.line_set, + module.private.normal.line_set, + true, + module.public.output + ) + module.private.normal.line_set = module.private.normal.line_set + #module.public.output + end, + + update = function(line) + vim.api.nvim_buf_set_lines( + module.private.buf, + module.private.normal.line_set, + module.private.normal.line_set, + true, + {line} + ) + module.private.normal.line_set = module.private.normal.line_set + 1 + end + }, spawn = function(command) module.private.interrupted = false - local id = module.private.virtual.init() + local mode = module.public.mode + local id + if mode == "view" then + id = module.private.virtual.init() + else + module.private.normal.init() + end module.private.jobid = vim.fn.jobstart(command, { - stdout_buffered = false, + stdout_buffered = true, + -- TODO: check exit code conditions and colors on_stdout = function(_, data) if module.private.interrupted then @@ -95,8 +133,13 @@ module.private = { for _, line in ipairs(data) do if line ~= "" then - table.insert(module.public.output, {{line, 'Function'}}) - module.private.virtual.update(id) + if mode == "view" then + table.insert(module.public.output, {{line, 'Function'}}) + module.private.virtual.update(id) + else + table.insert(module.public.output, line) + module.private.normal.update(line) + end end end end, @@ -109,10 +152,16 @@ module.private = { for _, line in ipairs(data) do if line ~= "" then - table.insert(module.public.output, {{line, 'Error'}}) - module.private.virtual.update(id) + if mode == "view" then + table.insert(module.public.output, {{line, 'Error'}}) + module.private.virtual.update(id) + else + table.insert(module.public.output, line) + module.private.normal.update(line) + end end end + end }) @@ -122,8 +171,9 @@ module.private = { module.public = { tmpdir = "/tmp/neorg-execute/", output = {}, + mode = "normal", - view = function() + base = function() local node = ts.get_node_at_cursor(0, true) local p = module.required["core.integrations.treesitter"].find_parent(node, "^ranged_tag$") @@ -150,26 +200,40 @@ module.public = { file:close() local command = module.config.public.lang_cmds[ft] + if not command then + vim.pretty_print("Language not supported currently!") + return + end command = {command, module.private.temp_filename} module.private.spawn(command) end + end, - -- {attributes, content, ["end"], name, parameters, ["start"]} + view = function() + module.public.output = {} + module.public.mode = "view" + module.public.base() + end, + normal = function() + module.public.output = {} + module.public.mode = "normal" + module.public.base() end, - -- bruh2 = function() end } module.on_event = function(event) if event.split_type[2] == "execute.view" then vim.schedule(module.public.view) + elseif event.split_type[2] == "execute.normal" then + vim.schedule(module.public.normal) end end module.events.subscribed = { ["core.neorgcmd"] = { ["execute.view"] = true, - -- ["execute.bruh2"] = true + ["execute.normal"] = true } } From 5e9abcc9463ce5d7dc23d9e4544bf6f1f82adb04 Mon Sep 17 00:00:00 2001 From: tamton-aquib Date: Wed, 26 Oct 2022 21:10:56 +0530 Subject: [PATCH 03/12] add extra interpreted langs --- lua/neorg/modules/core/execute/module.lua | 48 ++++++++++++----------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/lua/neorg/modules/core/execute/module.lua b/lua/neorg/modules/core/execute/module.lua index a6bb8c8c4..621ed294d 100644 --- a/lua/neorg/modules/core/execute/module.lua +++ b/lua/neorg/modules/core/execute/module.lua @@ -7,6 +7,9 @@ local module = neorg.modules.create("core.execute") local ts = require("nvim-treesitter.ts_utils") module.setup = function() + if vim.fn.isdirectory(module.public.tmpdir) == 0 then + vim.fn.mkdir(module.public.tmpdir, "p") + end return { success = true, requires = { "core.neorgcmd", "core.integrations.treesitter" } } end @@ -24,9 +27,11 @@ end module.config.public = { lang_cmds = { - python = 'python3', - lua = 'lua', - javascript = 'node', + python = {cmd='python3 ${0}', type = 'interpreted'}, + lua = {cmd='lua ${0}', type='interpreted'}, + javascript = {cmd='node ${0}', type='interpreted'}, + bash = {cmd='bash ${0}', type='interpreted'}, + php = {cmd='php ${0}', type='interpreted'} }, } module.config.private = {} @@ -40,14 +45,9 @@ module.private = { temp_filename = '', - create_dir_if_doesnt_exist = function() - if vim.fn.isdirectory(module.public.tmpdir) == 0 then - vim.fn.mkdir(module.public.tmpdir, "p") - end - end, - virtual = { init = function() + module.public.output = {} table.insert(module.public.output, {{"", 'Keyword'}}) table.insert(module.public.output, {{"Result:", 'Keyword'}}) @@ -59,15 +59,15 @@ module.private = { { virt_lines = module.public.output } ) - vim.api.nvim_create_autocmd('CursorMoved', { - once = true, - callback = function() - vim.api.nvim_buf_del_extmark(module.private.buf, module.private.ns, id) - module.private.interrupted = true - module.public.output = {} - vim.fn.delete(module.private.temp_filename) - end - }) + -- vim.api.nvim_create_autocmd('CursorMoved', { + -- once = true, + -- callback = function() + -- vim.api.nvim_buf_del_extmark(module.private.buf, module.private.ns, id) + -- module.private.interrupted = true + -- -- module.public.output = {} + -- vim.fn.delete(module.private.temp_filename) + -- end + -- }) return id end, @@ -122,7 +122,7 @@ module.private = { end module.private.jobid = vim.fn.jobstart(command, { - stdout_buffered = true, + stdout_buffered = false, -- TODO: check exit code conditions and colors on_stdout = function(_, data) @@ -162,8 +162,11 @@ module.private = { end end - end + end, + on_exit = function() + vim.fn.delete(module.private.temp_filename) + end }) end } @@ -188,7 +191,6 @@ module.public = { module.private.code_block = code_block local ft = code_block.parameters[1] - module.private.create_dir_if_doesnt_exist() module.private.temp_filename = module.public.tmpdir .. code_block.start.row .. "_" .. code_block['end'].row @@ -201,10 +203,10 @@ module.public = { local command = module.config.public.lang_cmds[ft] if not command then - vim.pretty_print("Language not supported currently!") + vim.notify("Language not supported currently!") return end - command = {command, module.private.temp_filename} + command = command.cmd:gsub("${0}", module.private.temp_filename) module.private.spawn(command) end From 4e199e10d90f2ec042f5bf5ae62512336356be06 Mon Sep 17 00:00:00 2001 From: tamton-aquib Date: Wed, 26 Oct 2022 21:41:35 +0530 Subject: [PATCH 04/12] starting compiled langs --- lua/neorg/modules/core/execute/module.lua | 33 +++++++++++++++++++---- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/lua/neorg/modules/core/execute/module.lua b/lua/neorg/modules/core/execute/module.lua index 621ed294d..0c73ff598 100644 --- a/lua/neorg/modules/core/execute/module.lua +++ b/lua/neorg/modules/core/execute/module.lua @@ -27,11 +27,34 @@ end module.config.public = { lang_cmds = { - python = {cmd='python3 ${0}', type = 'interpreted'}, - lua = {cmd='lua ${0}', type='interpreted'}, - javascript = {cmd='node ${0}', type='interpreted'}, - bash = {cmd='bash ${0}', type='interpreted'}, - php = {cmd='php ${0}', type='interpreted'} + python = { + cmd='python3 ${0}', type = 'interpreted' + }, + lua = { + cmd='lua ${0}', type='interpreted' + }, + javascript = { + cmd='node ${0}', + type='interpreted' + }, + bash = { + cmd='bash ${0}', type='interpreted' + }, + php = { + cmd='php ${0}', type='interpreted' + }, + cpp = { + cmd='g++ ${0} && ./a.out && rm ./a.out', + type='compiled' + }, + c = { + cmd='gcc ${0} && ./a.out && rm ./a.out', + type='compiled' + }, + rust = { + cmd='rustc ${0} -o ./a.out && ./a.out && rm ./a.out', + type = 'compiled' + } }, } module.config.private = {} From 775b2e748c6741eed748374327843e29a00dcb59 Mon Sep 17 00:00:00 2001 From: tamton-aquib Date: Wed, 26 Oct 2022 22:55:07 +0530 Subject: [PATCH 05/12] adding spinner --- lua/neorg/modules/core/execute/module.lua | 5 ++- lua/neorg/modules/core/execute/spinner.lua | 44 ++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 lua/neorg/modules/core/execute/spinner.lua diff --git a/lua/neorg/modules/core/execute/module.lua b/lua/neorg/modules/core/execute/module.lua index 0c73ff598..348d1bc13 100644 --- a/lua/neorg/modules/core/execute/module.lua +++ b/lua/neorg/modules/core/execute/module.lua @@ -2,6 +2,7 @@ -- TODO: better colors -- TODO: avoid code duplication. require("neorg.modules.base") +local spinner = require("neorg.modules.core.execute.spinner") local module = neorg.modules.create("core.execute") local ts = require("nvim-treesitter.ts_utils") @@ -67,9 +68,10 @@ module.private = { jobid = 0, temp_filename = '', - virtual = { init = function() + spinner:start(module.private) + module.public.output = {} table.insert(module.public.output, {{"", 'Keyword'}}) table.insert(module.public.output, {{"Result:", 'Keyword'}}) @@ -188,6 +190,7 @@ module.private = { end, on_exit = function() + spinner:shut() vim.fn.delete(module.private.temp_filename) end }) diff --git a/lua/neorg/modules/core/execute/spinner.lua b/lua/neorg/modules/core/execute/spinner.lua new file mode 100644 index 000000000..f5b7a67f4 --- /dev/null +++ b/lua/neorg/modules/core/execute/spinner.lua @@ -0,0 +1,44 @@ +-- TODO: code cleanup +local Spinner = {} +--> from fidget.nvim +local list = { "⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏" } + +function Spinner:start(s) + local block = s.code_block + local r, c = block['start'].row, block['start'].column + local t = vim.loop.new_timer() + + local idx = 0 + local id = vim.api.nvim_buf_set_extmark( + s.buf, + s.ns, + r, c, { + virt_text_pos = 'eol', + virt_text={{list[idx+1], 'Function'}} + } + ) + + Spinner.state = { + id = id, + buf = s.buf, ns = s.ns, + r=r, c=c, t=t + } + + t:start(0, 100, vim.schedule_wrap(function() + idx = (idx + 1) % #list + vim.api.nvim_buf_set_extmark( + s.buf, + s.ns, + Spinner.state.r, Spinner.state.c, + { virt_text = {{list[idx+1], 'Function'}}, id=Spinner.state.id } + ) + end)) +end + +function Spinner:shut() + local s = Spinner.state + vim.api.nvim_buf_del_extmark(s.buf, s.ns, s.id) + s.t:stop() +end + +return Spinner From 5219ce1817c6915b04f2cc25cee39004fd0a6186 Mon Sep 17 00:00:00 2001 From: tamton-aquib Date: Thu, 27 Oct 2022 20:35:21 +0530 Subject: [PATCH 06/12] check for `:main` param for compiled langs && moved config --- lua/neorg/modules/core/execute/config.lua | 54 +++++++++++++++++++++ lua/neorg/modules/core/execute/module.lua | 59 ++++++++--------------- 2 files changed, 73 insertions(+), 40 deletions(-) create mode 100644 lua/neorg/modules/core/execute/config.lua diff --git a/lua/neorg/modules/core/execute/config.lua b/lua/neorg/modules/core/execute/config.lua new file mode 100644 index 000000000..97a718dfd --- /dev/null +++ b/lua/neorg/modules/core/execute/config.lua @@ -0,0 +1,54 @@ +return { + lang_cmds = { + --> Interpreted + python = { + cmd='python3 ${0}', type = 'interpreted' + }, + lua = { + cmd='lua ${0}', type='interpreted' + }, + javascript = { + cmd='node ${0}', + type='interpreted' + }, + bash = { + cmd='bash ${0}', type='interpreted' + }, + php = { + cmd='php ${0}', type='interpreted' + }, + + --> Compiled + cpp = { + cmd='g++ ${0} && ./a.out && rm ./a.out', + type='compiled', + main_wrap = [[ + #include + int main() { + ${1} + } + ]] + }, + c = { + cmd='gcc ${0} && ./a.out && rm ./a.out', + type='compiled', + main_wrap = [[ + #include + #include + + int main() { + ${1} + } + ]] + }, + rust = { + cmd='rustc ${0} -o ./a.out && ./a.out && rm ./a.out', + type = 'compiled', + main_wrap = [[ + fn main() { + ${1} + } + ]] + } + }, +} diff --git a/lua/neorg/modules/core/execute/module.lua b/lua/neorg/modules/core/execute/module.lua index 348d1bc13..18164c546 100644 --- a/lua/neorg/modules/core/execute/module.lua +++ b/lua/neorg/modules/core/execute/module.lua @@ -26,38 +26,7 @@ module.load = function() }) end -module.config.public = { - lang_cmds = { - python = { - cmd='python3 ${0}', type = 'interpreted' - }, - lua = { - cmd='lua ${0}', type='interpreted' - }, - javascript = { - cmd='node ${0}', - type='interpreted' - }, - bash = { - cmd='bash ${0}', type='interpreted' - }, - php = { - cmd='php ${0}', type='interpreted' - }, - cpp = { - cmd='g++ ${0} && ./a.out && rm ./a.out', - type='compiled' - }, - c = { - cmd='gcc ${0} && ./a.out && rm ./a.out', - type='compiled' - }, - rust = { - cmd='rustc ${0} -o ./a.out && ./a.out && rm ./a.out', - type = 'compiled' - } - }, -} +module.config.public = require("neorg.modules.core.execute.config") module.config.private = {} module.private = { @@ -110,6 +79,7 @@ module.private = { line_set = 0, init = function() + spinner:start(module.private) module.private.normal.line_set = module.private.code_block['end'].row + 1 table.insert(module.public.output, '') table.insert(module.public.output, 'Result:') @@ -209,12 +179,14 @@ module.public = { -- TODO: Add checks here local code_block = module.required["core.integrations.treesitter"].get_tag_info(p, true) if not code_block then - vim.pretty_print("Not inside a code block!") + vim.notify("Not inside a code block!") return end if code_block.name == "code" then module.private.code_block = code_block + -- FIX: temp fix remove this! + module.private.code_block['parameters'] = vim.split(module.private.code_block['parameters'][1], ' ') local ft = code_block.parameters[1] module.private.temp_filename = module.public.tmpdir @@ -222,18 +194,25 @@ module.public = { .. code_block['end'].row .. "." .. ft + local lang_cfg = module.config.public.lang_cmds[ft] + if not lang_cfg then + vim.notify("Language not supported currently!") + return + end + local file = io.open(module.private.temp_filename, "w") + -- TODO: better error. if file == nil then return end - file:write(table.concat(code_block.content, '\n')) - file:close() - local command = module.config.public.lang_cmds[ft] - if not command then - vim.notify("Language not supported currently!") - return + local file_content = table.concat(code_block.content, '\n') + if not vim.tbl_contains(code_block.parameters, ":main") and lang_cfg.type == "compiled" then + local c = lang_cfg.main_wrap + file_content = c:gsub("${1}", file_content) end - command = command.cmd:gsub("${0}", module.private.temp_filename) + file:write(file_content) + file:close() + local command = lang_cfg.cmd:gsub("${0}", module.private.temp_filename) module.private.spawn(command) end end, From 067272065a3dd4ffa458fe82aa729f7a626b8d2a Mon Sep 17 00:00:00 2001 From: tamton-aquib Date: Fri, 28 Oct 2022 12:29:06 +0530 Subject: [PATCH 07/12] refactor: each block has a state now --- lua/neorg/modules/core/execute/config.lua | 3 + lua/neorg/modules/core/execute/module.lua | 176 +++++++++++---------- lua/neorg/modules/core/execute/spinner.lua | 29 ++-- 3 files changed, 111 insertions(+), 97 deletions(-) diff --git a/lua/neorg/modules/core/execute/config.lua b/lua/neorg/modules/core/execute/config.lua index 97a718dfd..b0864eb5c 100644 --- a/lua/neorg/modules/core/execute/config.lua +++ b/lua/neorg/modules/core/execute/config.lua @@ -17,6 +17,9 @@ return { php = { cmd='php ${0}', type='interpreted' }, + ruby = { + cmd='ruby ${0}', type='interpreted' + }, --> Compiled cpp = { diff --git a/lua/neorg/modules/core/execute/module.lua b/lua/neorg/modules/core/execute/module.lua index 18164c546..82fb88a35 100644 --- a/lua/neorg/modules/core/execute/module.lua +++ b/lua/neorg/modules/core/execute/module.lua @@ -30,129 +30,143 @@ module.config.public = require("neorg.modules.core.execute.config") module.config.private = {} module.private = { - buf = vim.api.nvim_get_current_buf(), + tasks = {}, + ns = vim.api.nvim_create_namespace("execute"), - code_block = {}, - interrupted = false, - jobid = 0, - temp_filename = '', virtual = { - init = function() - spinner:start(module.private) + init = function(id) + local curr_task = module.private.tasks[id] + curr_task.spinner = spinner.start(curr_task, module.private.ns) + + --> NICE: nice - module.public.output = {} - table.insert(module.public.output, {{"", 'Keyword'}}) - table.insert(module.public.output, {{"Result:", 'Keyword'}}) + table.insert(curr_task.output, {{"", 'Keyword'}}) + table.insert(curr_task.output, {{"Result:", 'Keyword'}}) - local id = vim.api.nvim_buf_set_extmark( - module.private.buf, + -- module.private.tasks[id] + vim.api.nvim_buf_set_extmark( + curr_task.buf, module.private.ns, - module.private.code_block['end'].row, - module.private.code_block['end'].column, - { virt_lines = module.public.output } + curr_task.code_block['end'].row, + 0, + { id=id, virt_lines = curr_task.output } ) - - -- vim.api.nvim_create_autocmd('CursorMoved', { - -- once = true, - -- callback = function() - -- vim.api.nvim_buf_del_extmark(module.private.buf, module.private.ns, id) - -- module.private.interrupted = true - -- -- module.public.output = {} - -- vim.fn.delete(module.private.temp_filename) - -- end - -- }) return id end, update = function(id) + local curr_task = module.private.tasks[id] + vim.api.nvim_buf_set_extmark( - module.private.buf, + curr_task.buf, module.private.ns, - module.private.code_block['end'].row, + curr_task.code_block['end'].row, 0, - { id=id, virt_lines = module.public.output } + { id=id, virt_lines = curr_task.output } ) end }, - normal = { - line_set = 0, - init = function() - spinner:start(module.private) - module.private.normal.line_set = module.private.code_block['end'].row + 1 - table.insert(module.public.output, '') - table.insert(module.public.output, 'Result:') - - vim.api.nvim_buf_set_lines( - module.private.buf, - module.private.normal.line_set, - module.private.normal.line_set, - true, - module.public.output - ) - module.private.normal.line_set = module.private.normal.line_set + #module.public.output + normal = { + init = function(id) + local curr_task = module.private.tasks[id] + curr_task.spinner = spinner.start(curr_task, module.private.ns) + + table.insert(curr_task.output, '') + table.insert(curr_task.output, 'Result:') + + for i, line in ipairs(curr_task.output) do + vim.pretty_print(line) + vim.api.nvim_buf_set_lines( + curr_task.buf, + curr_task.code_block['end'].row + i, + curr_task.code_block['end'].row + i, + true, + {line} + ) + end end, - update = function(line) + update = function(id, line) + local curr_task = module.private.tasks[id] vim.api.nvim_buf_set_lines( - module.private.buf, - module.private.normal.line_set, - module.private.normal.line_set, + curr_task.buf, + curr_task.code_block['end'].row + #curr_task.output, + curr_task.code_block['end'].row + #curr_task.output, true, {line} ) - module.private.normal.line_set = module.private.normal.line_set + 1 end }, - spawn = function(command) - module.private.interrupted = false + init = function() + local id = vim.api.nvim_buf_set_extmark( + 0, -- NICE: check if errors + module.private.ns, + 0, 0, {} + ) + + module.private.tasks[id] = { + buf = vim.api.nvim_get_current_buf(), + output = {}, + interrupted = false, + jobid = nil, + temp_filename = nil, + code_block = {}, + spinner = nil + } + + return id + end, + + spawn = function(id, command) + -- module.private.interrupted = false local mode = module.public.mode - local id + if mode == "view" then - id = module.private.virtual.init() + module.private.virtual.init(id) else - module.private.normal.init() + module.private.normal.init(id) end - module.private.jobid = vim.fn.jobstart(command, { + module.private.tasks[id].jobid = vim.fn.jobstart(command, { stdout_buffered = false, -- TODO: check exit code conditions and colors on_stdout = function(_, data) - if module.private.interrupted then - vim.fn.jobstop(module.private.jobid) + if module.private.tasks[id].interrupted then + vim.fn.jobstop(module.private.tasks[id].jobid) return end for _, line in ipairs(data) do if line ~= "" then if mode == "view" then - table.insert(module.public.output, {{line, 'Function'}}) + table.insert(module.private.tasks[id].output, {{line, 'Function'}}) module.private.virtual.update(id) else - table.insert(module.public.output, line) - module.private.normal.update(line) + table.insert(module.private.tasks[id].output, line) + module.private.normal.update(id, line) end end end end, on_stderr = function(_, data) - if module.private.interrupted then - vim.fn.jobstop(module.private.jobid) + if module.private.tasks[id].interrupted then + vim.fn.jobstop(module.private.tasks[id].jobid) return end for _, line in ipairs(data) do if line ~= "" then if mode == "view" then - table.insert(module.public.output, {{line, 'Error'}}) + table.insert(module.public.tasks[id].output, {{line, 'Error'}}) module.private.virtual.update(id) else - table.insert(module.public.output, line) - module.private.normal.update(line) + table.insert(module.public.tasks[id].output, line) + module.private.normal.update(id, line) end end end @@ -160,8 +174,8 @@ module.private = { end, on_exit = function() - spinner:shut() - vim.fn.delete(module.private.temp_filename) + spinner.shut(module.private.tasks[id].spinner, module.private.ns) + vim.fn.delete(module.private.tasks[id].temp_filename) end }) end @@ -169,10 +183,9 @@ module.private = { module.public = { tmpdir = "/tmp/neorg-execute/", - output = {}, mode = "normal", - base = function() + base = function(id) local node = ts.get_node_at_cursor(0, true) local p = module.required["core.integrations.treesitter"].find_parent(node, "^ranged_tag$") @@ -184,14 +197,15 @@ module.public = { end if code_block.name == "code" then - module.private.code_block = code_block + module.private.tasks[id]['code_block'] = code_block -- FIX: temp fix remove this! - module.private.code_block['parameters'] = vim.split(module.private.code_block['parameters'][1], ' ') + code_block['parameters'] = vim.split(code_block['parameters'][1], ' ') local ft = code_block.parameters[1] - module.private.temp_filename = module.public.tmpdir - .. code_block.start.row .. "_" - .. code_block['end'].row + module.private.tasks[id].temp_filename = module.public.tmpdir + .. id + -- .. code_block.start.row .. "_" + -- .. code_block['end'].row .. "." .. ft local lang_cfg = module.config.public.lang_cmds[ft] @@ -200,7 +214,7 @@ module.public = { return end - local file = io.open(module.private.temp_filename, "w") + local file = io.open(module.private.tasks[id].temp_filename, "w") -- TODO: better error. if file == nil then return end @@ -212,20 +226,20 @@ module.public = { file:write(file_content) file:close() - local command = lang_cfg.cmd:gsub("${0}", module.private.temp_filename) - module.private.spawn(command) + local command = lang_cfg.cmd:gsub("${0}", module.private.tasks[id].temp_filename) + module.private.spawn(id, command) end end, view = function() - module.public.output = {} module.public.mode = "view" - module.public.base() + local id = module.private.init() + module.public.base(id) end, normal = function() - module.public.output = {} module.public.mode = "normal" - module.public.base() + local id = module.private.init() + module.public.base(id) end, } diff --git a/lua/neorg/modules/core/execute/spinner.lua b/lua/neorg/modules/core/execute/spinner.lua index f5b7a67f4..8342a27c2 100644 --- a/lua/neorg/modules/core/execute/spinner.lua +++ b/lua/neorg/modules/core/execute/spinner.lua @@ -3,41 +3,38 @@ local Spinner = {} --> from fidget.nvim local list = { "⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏" } -function Spinner:start(s) +function Spinner.start(s, ns) local block = s.code_block local r, c = block['start'].row, block['start'].column local t = vim.loop.new_timer() local idx = 0 local id = vim.api.nvim_buf_set_extmark( - s.buf, - s.ns, - r, c, { + s.buf, ns, r, c, + { virt_text_pos = 'eol', virt_text={{list[idx+1], 'Function'}} } ) - Spinner.state = { - id = id, - buf = s.buf, ns = s.ns, - r=r, c=c, t=t - } - t:start(0, 100, vim.schedule_wrap(function() idx = (idx + 1) % #list vim.api.nvim_buf_set_extmark( s.buf, - s.ns, - Spinner.state.r, Spinner.state.c, - { virt_text = {{list[idx+1], 'Function'}}, id=Spinner.state.id } + ns, + r, c, + { virt_text = {{list[idx+1], 'Function'}}, id=id } ) end)) + + return { + id = id, buf=s.buf, ns=ns, r=r, c=c, t=t + } end -function Spinner:shut() - local s = Spinner.state - vim.api.nvim_buf_del_extmark(s.buf, s.ns, s.id) +function Spinner.shut(s, ns) + -- local s = Spinner.state + vim.api.nvim_buf_del_extmark(s.buf, ns, s.id) s.t:stop() end From daad5e0285256015d8bac77293d0f00e87a50858 Mon Sep 17 00:00:00 2001 From: tamton-aquib Date: Fri, 28 Oct 2022 16:29:31 +0530 Subject: [PATCH 08/12] fix: working re-execution --- lua/neorg/modules/core/execute/module.lua | 55 ++++++++++++++--------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/lua/neorg/modules/core/execute/module.lua b/lua/neorg/modules/core/execute/module.lua index 82fb88a35..4b1691a78 100644 --- a/lua/neorg/modules/core/execute/module.lua +++ b/lua/neorg/modules/core/execute/module.lua @@ -1,6 +1,4 @@ ---@diagnostic disable: undefined-global --- TODO: better colors --- TODO: avoid code duplication. require("neorg.modules.base") local spinner = require("neorg.modules.core.execute.spinner") @@ -39,12 +37,14 @@ module.private = { local curr_task = module.private.tasks[id] curr_task.spinner = spinner.start(curr_task, module.private.ns) - --> NICE: nice + -- Fix for re-execution + if not vim.tbl_isempty(curr_task.output) then + curr_task.output = {} + end table.insert(curr_task.output, {{"", 'Keyword'}}) table.insert(curr_task.output, {{"Result:", 'Keyword'}}) - -- module.private.tasks[id] vim.api.nvim_buf_set_extmark( curr_task.buf, module.private.ns, @@ -73,11 +73,20 @@ module.private = { local curr_task = module.private.tasks[id] curr_task.spinner = spinner.start(curr_task, module.private.ns) + if not vim.tbl_isempty(curr_task.output) then + vim.api.nvim_buf_set_lines( + curr_task.buf, + curr_task.code_block['end'].row + 1, + curr_task.code_block['end'].row + #curr_task.output + 1, + true, {} + ) + curr_task.output = {} + end + table.insert(curr_task.output, '') table.insert(curr_task.output, 'Result:') for i, line in ipairs(curr_task.output) do - vim.pretty_print(line) vim.api.nvim_buf_set_lines( curr_task.buf, curr_task.code_block['end'].row + i, @@ -101,11 +110,19 @@ module.private = { }, init = function() - local id = vim.api.nvim_buf_set_extmark( - 0, -- NICE: check if errors - module.private.ns, - 0, 0, {} - ) + -- IMP: check for existng marks and return if it exists. + local cr, _ = unpack(vim.api.nvim_win_get_cursor(0)) + + for id_idx,id_cfg in pairs(module.private.tasks) do + local code_start, code_end = id_cfg.code_block['start'].row + 1, id_cfg.code_block['end'].row + 1 + + if code_start <= cr and code_end >= cr then + return id_idx + end + end + + + local id = vim.api.nvim_buf_set_extmark(0, module.private.ns, 0, 0, {}) module.private.tasks[id] = { buf = vim.api.nvim_get_current_buf(), @@ -114,22 +131,19 @@ module.private = { jobid = nil, temp_filename = nil, code_block = {}, - spinner = nil + spinner = nil, + running = false } return id end, spawn = function(id, command) - -- module.private.interrupted = false local mode = module.public.mode - if mode == "view" then - module.private.virtual.init(id) - else - module.private.normal.init(id) - end + module.private[mode == 'view' and 'virtual' or 'normal'].init(id) + module.private.tasks[id].running = true module.private.tasks[id].jobid = vim.fn.jobstart(command, { stdout_buffered = false, @@ -162,10 +176,10 @@ module.private = { for _, line in ipairs(data) do if line ~= "" then if mode == "view" then - table.insert(module.public.tasks[id].output, {{line, 'Error'}}) + table.insert(module.private.tasks[id].output, {{line, 'Error'}}) module.private.virtual.update(id) else - table.insert(module.public.tasks[id].output, line) + table.insert(module.private.tasks[id].output, line) module.private.normal.update(id, line) end end @@ -176,6 +190,7 @@ module.private = { on_exit = function() spinner.shut(module.private.tasks[id].spinner, module.private.ns) vim.fn.delete(module.private.tasks[id].temp_filename) + module.private.tasks[id].running = false end }) end @@ -204,8 +219,6 @@ module.public = { module.private.tasks[id].temp_filename = module.public.tmpdir .. id - -- .. code_block.start.row .. "_" - -- .. code_block['end'].row .. "." .. ft local lang_cfg = module.config.public.lang_cmds[ft] From 22cb9fe6dc7516abd8a7063ea96d871c15a21d1e Mon Sep 17 00:00:00 2001 From: Aquib Date: Mon, 16 Jan 2023 18:22:49 +0530 Subject: [PATCH 09/12] less duplication, ranged_tag -> ranged_verbatim_tag, skeleton for `hide` --- lua/neorg/modules/core/execute/module.lua | 73 ++++++++++++----------- 1 file changed, 38 insertions(+), 35 deletions(-) diff --git a/lua/neorg/modules/core/execute/module.lua b/lua/neorg/modules/core/execute/module.lua index 4b1691a78..9769ac349 100644 --- a/lua/neorg/modules/core/execute/module.lua +++ b/lua/neorg/modules/core/execute/module.lua @@ -18,6 +18,7 @@ module.load = function() args = 1, subcommands = { view = { args=0, name="execute.view" }, + hide = { args=0, name="execute.hide" }, normal = { args=0, name="execute.normal" }, } } @@ -138,6 +139,25 @@ module.private = { return id end, + handle_lines = function(id, data, hl) + if module.private.tasks[id].interrupted then + vim.fn.jobstop(module.private.tasks[id].jobid) + return + end + + for _, line in ipairs(data) do + if line ~= "" then + if module.public.mode == "view" then + table.insert(module.private.tasks[id].output, {{line, hl}}) + module.private.virtual.update(id) + else + table.insert(module.private.tasks[id].output, line) + module.private.normal.update(id, line) + end + end + end + end, + spawn = function(id, command) local mode = module.public.mode @@ -149,42 +169,11 @@ module.private = { -- TODO: check exit code conditions and colors on_stdout = function(_, data) - if module.private.tasks[id].interrupted then - vim.fn.jobstop(module.private.tasks[id].jobid) - return - end - - for _, line in ipairs(data) do - if line ~= "" then - if mode == "view" then - table.insert(module.private.tasks[id].output, {{line, 'Function'}}) - module.private.virtual.update(id) - else - table.insert(module.private.tasks[id].output, line) - module.private.normal.update(id, line) - end - end - end + module.private.handle_lines(id, data, "Function") end, on_stderr = function(_, data) - if module.private.tasks[id].interrupted then - vim.fn.jobstop(module.private.tasks[id].jobid) - return - end - - for _, line in ipairs(data) do - if line ~= "" then - if mode == "view" then - table.insert(module.private.tasks[id].output, {{line, 'Error'}}) - module.private.virtual.update(id) - else - table.insert(module.private.tasks[id].output, line) - module.private.normal.update(id, line) - end - end - end - + module.private.handle_lines(id, data, "Error") end, on_exit = function() @@ -200,9 +189,9 @@ module.public = { tmpdir = "/tmp/neorg-execute/", mode = "normal", - base = function(id) + current_node_info = function() local node = ts.get_node_at_cursor(0, true) - local p = module.required["core.integrations.treesitter"].find_parent(node, "^ranged_tag$") + local p = module.required["core.integrations.treesitter"].find_parent(node, "^ranged_verbatim_tag$") -- TODO: Add checks here local code_block = module.required["core.integrations.treesitter"].get_tag_info(p, true) @@ -211,8 +200,16 @@ module.public = { return end + return code_block + end, + + base = function(id) + local code_block = module.public.current_node_info() + if not code_block then return end + if code_block.name == "code" then module.private.tasks[id]['code_block'] = code_block + -- FIX: temp fix remove this! code_block['parameters'] = vim.split(code_block['parameters'][1], ' ') local ft = code_block.parameters[1] @@ -254,11 +251,16 @@ module.public = { local id = module.private.init() module.public.base(id) end, + hide = function() + vim.pretty_print("Hide, quick!") + end } module.on_event = function(event) if event.split_type[2] == "execute.view" then vim.schedule(module.public.view) + elseif event.split_type[2] == "execute.hide" then + vim.schedule(module.public.hide) elseif event.split_type[2] == "execute.normal" then vim.schedule(module.public.normal) end @@ -267,6 +269,7 @@ end module.events.subscribed = { ["core.neorgcmd"] = { ["execute.view"] = true, + ["execute.hide"] = true, ["execute.normal"] = true } } From e91f0d16668ac763fb5ce9474c8e9bccc1c94171 Mon Sep 17 00:00:00 2001 From: Aquib Date: Mon, 16 Jan 2023 20:22:10 +0530 Subject: [PATCH 10/12] feat: added hide subcommand for `view` and `normal` --- lua/neorg/modules/core/execute/module.lua | 26 ++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/lua/neorg/modules/core/execute/module.lua b/lua/neorg/modules/core/execute/module.lua index 9769ac349..372197c99 100644 --- a/lua/neorg/modules/core/execute/module.lua +++ b/lua/neorg/modules/core/execute/module.lua @@ -114,7 +114,7 @@ module.private = { -- IMP: check for existng marks and return if it exists. local cr, _ = unpack(vim.api.nvim_win_get_cursor(0)) - for id_idx,id_cfg in pairs(module.private.tasks) do + for id_idx, id_cfg in pairs(module.private.tasks) do local code_start, code_end = id_cfg.code_block['start'].row + 1, id_cfg.code_block['end'].row + 1 if code_start <= cr and code_end >= cr then @@ -194,13 +194,13 @@ module.public = { local p = module.required["core.integrations.treesitter"].find_parent(node, "^ranged_verbatim_tag$") -- TODO: Add checks here - local code_block = module.required["core.integrations.treesitter"].get_tag_info(p, true) - if not code_block then + local cb = module.required["core.integrations.treesitter"].get_tag_info(p, true) + if not cb then vim.notify("Not inside a code block!") return end - return code_block + return cb end, base = function(id) @@ -252,7 +252,23 @@ module.public = { module.public.base(id) end, hide = function() - vim.pretty_print("Hide, quick!") + -- HACK: Duplication + local cr, _ = unpack(vim.api.nvim_win_get_cursor(0)) + + for id_idx, id_cfg in pairs(module.private.tasks) do + local code_start, code_end = id_cfg.code_block['start'].row + 1, id_cfg.code_block['end'].row + 1 + + if code_start <= cr and code_end >= cr then + if module.public.mode == "view" then + vim.api.nvim_buf_del_extmark(0, module.private.ns, id_idx) + else + vim.api.nvim_buf_set_lines(0, code_end, code_end+#id_cfg["output"], false, {}) + end + + module.private.tasks[id_idx] = nil + return + end + end end } From c8a1e3a5e30a917656624daf3655a66a50473bd1 Mon Sep 17 00:00:00 2001 From: Aquib Date: Mon, 16 Jan 2023 20:54:27 +0530 Subject: [PATCH 11/12] feat: adding `materialize` command (bugs present) --- lua/neorg/modules/core/execute/module.lua | 48 ++++++++++++++++++++--- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/lua/neorg/modules/core/execute/module.lua b/lua/neorg/modules/core/execute/module.lua index 372197c99..ec953e46b 100644 --- a/lua/neorg/modules/core/execute/module.lua +++ b/lua/neorg/modules/core/execute/module.lua @@ -18,8 +18,9 @@ module.load = function() args = 1, subcommands = { view = { args=0, name="execute.view" }, - hide = { args=0, name="execute.hide" }, normal = { args=0, name="execute.normal" }, + hide = { args=0, name="execute.hide" }, + materialize = { args=0, name="execute.materialize" }, } } }) @@ -187,7 +188,8 @@ module.private = { module.public = { tmpdir = "/tmp/neorg-execute/", - mode = "normal", + -- mode = "normal", + mode = nil, current_node_info = function() local node = ts.get_node_at_cursor(0, true) @@ -269,24 +271,60 @@ module.public = { return end end + end, + materialize = function() + local cr, _ = unpack(vim.api.nvim_win_get_cursor(0)) + + -- FIX: DUPLICATION AGAIN!!! + for id_idx, id_cfg in pairs(module.private.tasks) do + local code_start = id_cfg.code_block['start'].row + 1 + local code_end = id_cfg.code_block['end'].row + 1 + + if code_start <= cr and code_end >= cr then + local curr_task = module.private.tasks[id_idx] + vim.api.nvim_buf_set_extmark( + curr_task.buf, + module.private.ns, + curr_task.code_block['end'].row, + 0, + { id=id_idx, virt_lines = nil } + ) + + local t = vim.tbl_map(function(line) return line[1][1] end, curr_task.output) + + for i, line in ipairs(t) do + vim.api.nvim_buf_set_lines( + curr_task.buf, + curr_task.code_block['end'].row + i, + curr_task.code_block['end'].row + i, + true, + {line} + ) + end + + end + end end } module.on_event = function(event) if event.split_type[2] == "execute.view" then vim.schedule(module.public.view) - elseif event.split_type[2] == "execute.hide" then - vim.schedule(module.public.hide) elseif event.split_type[2] == "execute.normal" then vim.schedule(module.public.normal) + elseif event.split_type[2] == "execute.hide" then + vim.schedule(module.public.hide) + elseif event.split_type[2] == "execute.materialize" then + vim.schedule(module.public.materialize) end end module.events.subscribed = { ["core.neorgcmd"] = { ["execute.view"] = true, + ["execute.normal"] = true, ["execute.hide"] = true, - ["execute.normal"] = true + ["execute.materialize"] = true, } } From d78c36d920ed218eb846a544e54636f6b6790713 Mon Sep 17 00:00:00 2001 From: Aquib Date: Mon, 16 Jan 2023 21:08:17 +0530 Subject: [PATCH 12/12] fixed 90% bugs (might need to make public.mode local to each cell) --- lua/neorg/modules/core/execute/module.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lua/neorg/modules/core/execute/module.lua b/lua/neorg/modules/core/execute/module.lua index ec953e46b..92b8095ce 100644 --- a/lua/neorg/modules/core/execute/module.lua +++ b/lua/neorg/modules/core/execute/module.lua @@ -302,6 +302,8 @@ module.public = { ) end + module.public.mode = "normal" + end end end