Skip to content

Commit 410ef9f

Browse files
committed
feat: toggleterm2 runner: add option clear_env, go_back; support AsyncRunStop, autoclose
1 parent d6ba5dc commit 410ef9f

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,11 @@ When you use toggleterm2 and use the packer.nvim management plugin, you can set
431431
"skywind3000/asyncrun.vim",
432432
as = "asyncrun",
433433
config = function()
434-
require("asyncrun_toggleterm").setup({
434+
require("asyncrun.toggleterm2").setup({
435435
mapping = "<leader>tt",
436436
start_in_insert = false,
437+
clear_env = false,
438+
go_back = true,
437439
})
438440
end,
439441
})

lua/asyncrun/toggleterm2.lua

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,40 @@ local M = {}
44
M.setup = function(opts)
55
M._asyncrun_mapping = opts.mapping
66
M._start_in_insert = opts.start_in_insert
7+
M._clear_env = opts.clear_env
8+
M._go_back = opts.go_back
79
end
810

911
function M.reset()
1012
if M._asyncrun_term ~= nil then
1113
if vim.g.asynctasks_term_reuse ~= 1 then
1214
-- TODO: handle multiple terminals
1315
error("Terminal existed is not support . please set g.asynctasks_term_reuse = 1")
14-
else
15-
vim.notify("Delete existing terminal", "info")
1616
end
1717
M._asyncrun_term:shutdown()
18+
M._asyncrun_term = nil
1819
end
19-
20-
M._asyncrun_term = nil
21-
M._asyncrun_term_toggle = nil
2220
end
2321

2422
function M.runner(opts)
2523
M.reset()
2624
M._asyncrun_term = terminal:new({
2725
cmd = opts.cmd,
2826
dir = opts.cwd,
29-
close_on_exit = false,
27+
close_on_exit = (opts.close == "1") and true or false,
3028
hidden = true,
29+
clear_env = M._clear_env or false,
3130
on_open = function(term)
3231
if M._start_in_insert then
3332
vim.cmd("startinsert!")
3433
else
35-
vim.cmd("stopinsert!")
34+
vim.cmd("stopinsert")
3635
end
37-
end
36+
end,
37+
on_exit = function(term, job_id, exit_code, event_name)
38+
vim.g.asyncrun_code = exit_code
39+
vim.cmd("doautocmd User AsyncRunStop")
40+
end,
3841
})
3942

4043
function M._asyncrun_term_toggle()
@@ -43,11 +46,14 @@ function M.runner(opts)
4346

4447
if not opts.silent then
4548
M._asyncrun_term_toggle()
49+
if M._go_back then
50+
vim.cmd("wincmd p")
51+
end
4652
end
4753

4854
if M._asyncrun_mapping then
4955
vim.api.nvim_set_keymap("n", M._asyncrun_mapping,
50-
"<cmd>lua require('asyncrun_toggleterm')._asyncrun_term_toggle()<CR>", {
56+
"<cmd>lua require('asyncrun.toggleterm2')._asyncrun_term_toggle()<CR>", {
5157
noremap = true,
5258
silent = true
5359
})

0 commit comments

Comments
 (0)