diff --git a/autoload/SpaceVim/api/job.vim b/autoload/SpaceVim/api/job.vim index d7a98be25..872acf3cf 100644 --- a/autoload/SpaceVim/api/job.vim +++ b/autoload/SpaceVim/api/job.vim @@ -272,6 +272,17 @@ function! s:self.info(id) abort endif endfunction +function! s:self.chanclose(id, type) abort + if self.nvim_job + call chanclose(a:id, a:type) + elseif self.vim_job + if has_key(self.jobs, a:id) && a:type ==# 'stdin' + call ch_close_in(get(self.jobs, a:id)) + endif + endif +endfunction + + function! s:self.debug() abort echo join(self._message, "\n") endfunction diff --git a/autoload/SpaceVim/layers/lang/javascript.vim b/autoload/SpaceVim/layers/lang/javascript.vim index ad61a2360..cfbf8cb0a 100644 --- a/autoload/SpaceVim/layers/lang/javascript.vim +++ b/autoload/SpaceVim/layers/lang/javascript.vim @@ -51,7 +51,11 @@ function! SpaceVim#layers#lang#javascript#config() abort call add(g:spacevim_project_rooter_patterns, 'package.json') - call SpaceVim#plugins#runner#reg_runner('javascript', 'node %s') + call SpaceVim#plugins#runner#reg_runner('javascript', { + \ 'exe' : 'node', + \ 'usestdin' : 1, + \ 'opt': ['-'], + \ }) call SpaceVim#mapping#space#regesit_lang_mappings('javascript', \ function('s:on_ft')) diff --git a/autoload/SpaceVim/plugins/runner.vim b/autoload/SpaceVim/plugins/runner.vim index 82fd15479..428ce335b 100644 --- a/autoload/SpaceVim/plugins/runner.vim +++ b/autoload/SpaceVim/plugins/runner.vim @@ -72,10 +72,19 @@ function! s:async_run(runner) abort \ 'on_exit' : function('s:on_compile_exit'), \ }) elseif type(a:runner) == type({}) - let exe = call(a:runner.exe, []) - let cmd = exe + a:runner.opt + [get(s:, 'selected_file', bufname('%'))] + if type(a:runner.exe) == 2 + let exe = call(a:runner.exe, []) + elseif type(a:runner.exe) ==# type('') + let exe = [a:runner.exe] + endif + let usestdin = get(a:runner, 'usestdin', 0) + if usestdin + let cmd = exe + a:runner.opt + else + let cmd = exe + a:runner.opt + [get(s:, 'selected_file', bufname('%'))] + endif call SpaceVim#logger#info(' cmd:' . string(cmd)) - call s:BUFFER.buf_set_lines(s:bufnr, s:lines , s:lines + 3, 0, ['[Running] ' . join(cmd), '', repeat('-', 20)]) + call s:BUFFER.buf_set_lines(s:bufnr, s:lines , s:lines + 3, 0, ['[Running] ' . join(cmd) . (usestdin ? ' STDIN' : ''), '', repeat('-', 20)]) let s:lines += 3 let s:start_time = reltime() let s:job_id = s:JOB.start(cmd,{ @@ -83,6 +92,10 @@ function! s:async_run(runner) abort \ 'on_stderr' : function('s:on_stderr'), \ 'on_exit' : function('s:on_exit'), \ }) + if usestdin + call s:JOB.send(s:job_id, getline(1, '$')) + call s:JOB.chanclose(s:job_id, 'stdin') + endif endif endfunction diff --git a/docs/_posts/2019-01-27-use-vim-as-a-javascript-ide.md b/docs/_posts/2019-01-27-use-vim-as-a-javascript-ide.md index c911be558..21fff8a42 100644 --- a/docs/_posts/2019-01-27-use-vim-as-a-javascript-ide.md +++ b/docs/_posts/2019-01-27-use-vim-as-a-javascript-ide.md @@ -80,7 +80,7 @@ SpaceVim 自带工程管理插件,可以识别项目根目录,自动跳转al 在编辑 JavaScript 文件时,可以快速运行当前文件,这个功能有点类似于 vscode 的 code runner 插件,默认的快捷键是 `SPC l r`。按下后, 会在屏幕下方打开一个插件窗口,运行的结果会被展示在窗口内。于此同时,光标并不会跳到该插件窗口,避免影响编辑。在这里需要说明下, -这一功能是根据当前文件的路径调用相对应的 `coffee` 命令。因此,在执行这个快捷键之前,应当先保存一下该文件。 +这一功能是根据当前缓存器调用相对应的 `node` 命令,无需保存文件。 ![jsrunner](https://user-images.githubusercontent.com/13142418/51972835-4cf12d00-24b7-11e9-9693-5e1eea9853b0.png) diff --git a/docs/_posts/2019-01-30-use-vim-as-a-javascript-ide.md b/docs/_posts/2019-01-30-use-vim-as-a-javascript-ide.md index f7c01274f..5910ea5d0 100644 --- a/docs/_posts/2019-01-30-use-vim-as-a-javascript-ide.md +++ b/docs/_posts/2019-01-30-use-vim-as-a-javascript-ide.md @@ -74,8 +74,8 @@ with this configuration, you can jump between the source code and test file via ### running code -To run current script, you can press `SPC l r`, and a split windows -will be openen, the output of the script will be shown in this windows. +To run code in current buffer, you can press `SPC l r`, and a split windows +will be openen, the output will be shown in this windows. It is running asynchronously, and will not block your vim. ![jsrunner](https://user-images.githubusercontent.com/13142418/51972835-4cf12d00-24b7-11e9-9693-5e1eea9853b0.png) diff --git a/docs/layers/lang/javascript.md b/docs/layers/lang/javascript.md index f7b690d46..91f7f628d 100644 --- a/docs/layers/lang/javascript.md +++ b/docs/layers/lang/javascript.md @@ -14,6 +14,7 @@ description: "This layer is for JaveScript development" - [Key bindings](#key-bindings) - [Import key bindings](#import-key-bindings) - [Generate key bindings](#generate-key-bindings) + - [Code runner](#code-runner) - [Inferior REPL process](#inferior-repl-process) @@ -72,6 +73,11 @@ enable_flow_syntax = true | ------------- | ----------- | ------------------------------------- | | normal | `SPC l g d` | Generate JSDoc | +### Code runner + +To run javascript code in current buffer, you can press `SPC l r`. It will run without loss focus, +and the result will be shown in a runner buffer. + ### Inferior REPL process Start a `node -i` inferior REPL process with `SPC l s i`. diff --git a/docs/layers/lang/lua.md b/docs/layers/lang/lua.md index ade69879a..1dfa0161f 100644 --- a/docs/layers/lang/lua.md +++ b/docs/layers/lang/lua.md @@ -62,4 +62,4 @@ Send code to inferior process commands: ### Running current script -To running a ruby script, you can press `SPC l r` to run current file without loss focus, and the result will be shown in a runner buffer. +To running a lua script, you can press `SPC l r` to run current file without loss focus, and the result will be shown in a runner buffer.