1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-03-13 02:05:40 +08:00

Add stdin support in runner (#2535)

* Add stdin support in runner

* Add stdin support for javascript
This commit is contained in:
Wang Shidong 2019-02-01 00:00:16 +08:00 committed by GitHub
parent 9f9122a4b3
commit b8ac6d1b31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 42 additions and 8 deletions

View File

@ -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

View File

@ -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'))

View File

@ -72,10 +72,19 @@ function! s:async_run(runner) abort
\ 'on_exit' : function('s:on_compile_exit'),
\ })
elseif type(a:runner) == type({})
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

View File

@ -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)

View File

@ -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)

View File

@ -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)
<!-- vim-markdown-toc -->
@ -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`.

View File

@ -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.