Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add core.execute #618

Closed
wants to merge 15 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 34 additions & 21 deletions lua/neorg/modules/core/execute/module.lua
Original file line number Diff line number Diff line change
@@ -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")

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be the same as the start
if not lines below the code block are overwritten

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for deleting the previous output lines if present, havent experienced any bugs yet on my side(on this line).

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,
Expand All @@ -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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couldn't you just get rid of the , _ ?
the value would just be ignored
also
I think cr isn't a really descriptive variable name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couldn't you just get rid of the , _ ?

Was indexing the first item(row), but was testing with both.

I think cr isn't a really descriptive variable name

let me know what else you want to call it


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(),
Expand All @@ -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,

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand Down