Skip to content

Commit

Permalink
Merge pull request #257 from qujihan/master
Browse files Browse the repository at this point in the history
增加新的runner
  • Loading branch information
skywind3000 authored Oct 30, 2022
2 parents bd6257d + 1c644c9 commit 4794a74
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ AsyncRun 提供足够的可能性和灵活性让你指定运行命令的各处
| `floaterm_reuse` | 再一个可复用的 floaterm 窗口内运行 | [floaterm](https://github.com/voldikss/vim-floaterm) | [floaterm_reuse.vim](autoload/asyncrun/runner/floaterm.vim) |
| `quickui` | 在 quickui 的浮窗里运行 | [vim-quickui](https://github.com/skywind3000/vim-quickui) | [quickui.vim](autoload/asyncrun/runner/quickui.vim) |
| `toggleterm` | 使用 toggleterm 窗口运行 | [toggleterm.nvim](https://github.com/akinsho/toggleterm.nvim) | [toggleterm.vim](autoload/asyncrun/runner/toggleterm.vim) |
| `toggleterm2` | 使用自定义 toggleterm 窗口运行 | [toggleterm.nvim](https://github.com/akinsho/toggleterm.nvim) | [toggleterm.vim](autoload/asyncrun/runner/toggleterm2.vim) |
| `xfce` | 在 xfce 终端中运行 | xfce4-terminal | [xfce.vim](autoload/asyncrun/runner/xfce.vim) |
| `konsole` | 在 KDE 的自带终端里运行 | KDE | [konsole.vim](autoload/asyncrun/runner/konsole.vim) |
| `macos` | 在 macOS 的系统终端内运行 | macOS | [macos.vim](autoload/asyncrun/runner/macos.vim) |
Expand All @@ -392,6 +393,19 @@ AsyncRun 提供足够的可能性和灵活性让你指定运行命令的各处

当你在 GVim 中使用 `gnome`, `konsole` 或者 `xfce` 之类的 runner 来运行程序,你会觉得就跟 IDE 里面启动命令行程序是一样的感觉。

当你使用toggleterm2这个runner,并且使用packer.nvim管理插件的时候,可以设置快捷键指定打开的窗口,比如:
```lua
use({
"skywind3000/asyncrun.vim",
as = "asyncrun",
config = function()
require("asyncrun_toggleterm").setup({
mapping = "<leader>tt",
start_in_insert = false,
})
end,
})
```
所有 runner 皆可定制,你可以很方便的开发新 runner,详细见下一节 “自定义 Runner”。

### 自定义 Runner
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,20 @@ Screenshot for `gnome` runner:

When using `gnome`, `konsole`, or `xfce` runner in GVim, you get exactly the same experience like starting a command-line program from IDEs.

When you use toggleterm2 and use the packer.nvim management plugin, you can set shortcut keys to specify the open window, such as:
```lua
use({
"skywind3000/asyncrun.vim",
as = "asyncrun",
config = function()
require("asyncrun_toggleterm").setup({
mapping = "<leader>tt",
start_in_insert = false,
})
end,
})
```

All runners are customizable, you can modify or define your own runners, see the next section "customize runner".

### Customize Runner
Expand Down
3 changes: 3 additions & 0 deletions autoload/asyncrun/runner/toggleterm2.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function! asyncrun#runner#toggleterm2#run(opts)
lua require("asyncrun_toggleterm").runner(vim.fn.eval("a:opts"))
endfunction
57 changes: 57 additions & 0 deletions lua/asyncrun_toggleterm.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
local terminal = require("toggleterm.terminal").Terminal
local M = {}

M.setup = function(opts)
M._asyncrun_mapping = opts.mapping
M._start_in_insert = opts.start_in_insert
end

function M.reset()
if M._asyncrun_term ~= nil then
if vim.g.asynctasks_term_reuse ~= 1 then
-- TODO: handle multiple terminals
error("Terminal existed is not support . please set g.asynctasks_term_reuse = 1")
else
vim.notify("Delete existing terminal", "info")
end
M._asyncrun_term:shutdown()
end

M._asyncrun_term = nil
M._asyncrun_term_toggle = nil
end

function M.runner(opts)
M.reset()
M._asyncrun_term = terminal:new({
cmd = opts.cmd,
dir = opts.cwd,
close_on_exit = false,
hidden = true,
on_open = function(term)
if M._start_in_insert then
vim.cmd("startinsert!")
else
vim.cmd("stopinsert!")
end
end
})

function M._asyncrun_term_toggle()
M._asyncrun_term:toggle()
end

if not opts.silent then
M._asyncrun_term_toggle()
end

if M._asyncrun_mapping then
vim.api.nvim_set_keymap("n", M._asyncrun_mapping,
"<cmd>lua require('asyncrun_toggleterm')._asyncrun_term_toggle()<CR>", {
noremap = true,
silent = true
})
end
end

return M

0 comments on commit 4794a74

Please sign in to comment.