mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-03-12 17:55:41 +08:00
Add stdin support in runner (#2535)
* Add stdin support in runner * Add stdin support for javascript
This commit is contained in:
parent
9f9122a4b3
commit
b8ac6d1b31
@ -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
|
||||
|
@ -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'))
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -80,7 +80,7 @@ SpaceVim 自带工程管理插件,可以识别项目根目录,自动跳转al
|
||||
|
||||
在编辑 JavaScript 文件时,可以快速运行当前文件,这个功能有点类似于 vscode 的 code runner 插件,默认的快捷键是 `SPC l r`。按下后,
|
||||
会在屏幕下方打开一个插件窗口,运行的结果会被展示在窗口内。于此同时,光标并不会跳到该插件窗口,避免影响编辑。在这里需要说明下,
|
||||
这一功能是根据当前文件的路径调用相对应的 `coffee` 命令。因此,在执行这个快捷键之前,应当先保存一下该文件。
|
||||
这一功能是根据当前缓存器调用相对应的 `node` 命令,无需保存文件。
|
||||
|
||||

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

|
||||
|
@ -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`.
|
||||
|
@ -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.
|
||||
|
Loading…
x
Reference in New Issue
Block a user