Skip to content

Commit

Permalink
Fix autorun function
Browse files Browse the repository at this point in the history
When saving a file, now function now checks if `_terminal_windows` exists and call the last shell command, otherwise it calls the vim command.
  • Loading branch information
kristoferssolo committed Sep 3, 2023
1 parent 4cf6802 commit ef407f6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions lua/runner/handlers/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local config = require('runner.config')
M._terminal_buffer = nil
M._terminal_window = nil
M._last_command = nil
M._last_handler = nil

M.create_buffer = function()
if M._terminal_buffer then
Expand Down
11 changes: 8 additions & 3 deletions lua/runner/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ M.set_handler = function(filetype, handler)
M._handlers[filetype] = handler
end

--- @param bufnr integer|nil
--- @param bufnr integer?
M.run = function(bufnr)
if bufnr == nil or bufnr == 0 then
bufnr = vim.api.nvim_get_current_buf()
Expand All @@ -48,17 +48,22 @@ M.run = function(bufnr)
return
end

utils._last_handler = handler
handler(bufnr)
end

--- @param bufnr integer|nil
--- @param bufnr integer?
M.autorun = function(bufnr)
M.run(bufnr)
vim.api.nvim_create_autocmd('BufWritePost', {
group = vim.api.nvim_create_augroup('AutoRunner', { clear = true }),
pattern = '*',
callback = function()
helpers.shell_handler(utils._last_command, false)()
if utils._terminal_window then
helpers.shell_handler(utils._last_command, false)()
else
utils._last_handler(bufnr)
end
end,
})
end
Expand Down

0 comments on commit ef407f6

Please sign in to comment.