From 71524080d43cecb876119527fba66fdc395ff231 Mon Sep 17 00:00:00 2001 From: Shidong Wang Date: Mon, 11 Feb 2019 10:24:44 +0800 Subject: [PATCH] Improve c stdin support --- autoload/SpaceVim/layers/lang/c.vim | 8 +++++++- autoload/SpaceVim/plugins/runner.vim | 24 ++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/autoload/SpaceVim/layers/lang/c.vim b/autoload/SpaceVim/layers/lang/c.vim index 319493d0c..df6ebdc50 100644 --- a/autoload/SpaceVim/layers/lang/c.vim +++ b/autoload/SpaceVim/layers/lang/c.vim @@ -93,7 +93,13 @@ function! SpaceVim#layers#lang#c#config() abort call SpaceVim#mapping#gd#add('cpp', \ function('s:go_to_def')) " TODO: add stdin suport flex -t lexer.l | gcc -o lexer.o -xc - - call SpaceVim#plugins#runner#reg_runner('c', ['gcc -o #TEMP# %s', '#TEMP#']) + let runner1 = { + \ 'exe' : 'gcc', + \ 'targetopt' : '-o', + \ 'opt' : ['-xc', '-'], + \ 'usestdin' : 1, + \ } + call SpaceVim#plugins#runner#reg_runner('c', [runner1, '#TEMP#']) call SpaceVim#mapping#space#regesit_lang_mappings('c', function('s:language_specified_mappings')) call SpaceVim#plugins#runner#reg_runner('cpp', ['g++ -o #TEMP# %s', '#TEMP#']) call SpaceVim#mapping#space#regesit_lang_mappings('cpp', funcref('s:language_specified_mappings')) diff --git a/autoload/SpaceVim/plugins/runner.vim b/autoload/SpaceVim/plugins/runner.vim index 60e283ed8..ca3909c72 100644 --- a/autoload/SpaceVim/plugins/runner.vim +++ b/autoload/SpaceVim/plugins/runner.vim @@ -61,9 +61,24 @@ function! s:async_run(runner) abort " the runner is a list " the first item is compile cmd, and the second one is running cmd. let s:target = s:FILE.unify_path(tempname(), ':p') - let compile_cmd = substitute(printf(a:runner[0], bufname('%')), '#TEMP#', s:target, 'g') + if type(a:runner[0]) == type({}) + if type(a:runner[0].exe) == 2 + let exe = call(a:runner[0].exe, []) + elseif type(a:runner[0].exe) ==# type('') + let exe = [a:runner[0].exe] + endif + let usestdin = get(a:runner[0], 'usestdin', 0) + let compile_cmd = exe + [get(a:runner[0], 'targetopt', '')] + [s:target] + if usestdin + let compile_cmd = compile_cmd + a:runner[0].opt + else + let compile_cmd = compile_cmd + a:runner[0].opt + [get(s:, 'selected_file', bufname('%'))] + endif + else + let compile_cmd = substitute(printf(a:runner[0], bufname('%')), '#TEMP#', s:target, 'g') + endif call s:BUFFER.buf_set_lines(s:bufnr, s:lines , s:lines + 3, 0, [ - \ '[Compile] ' . compile_cmd, + \ '[Compile] ' . join(compile_cmd) . (usestdin ? ' STDIN' : ''), \ '[Running] ' . s:target, \ '', \ repeat('-', 20)]) @@ -74,6 +89,11 @@ function! s:async_run(runner) abort \ 'on_stderr' : function('s:on_stderr'), \ 'on_exit' : function('s:on_compile_exit'), \ }) + if usestdin + let range = get(a:runner[0], 'range', [1, '$']) + call s:JOB.send(s:job_id, call('getline', range)) + call s:JOB.chanclose(s:job_id, 'stdin') + endif elseif type(a:runner) == type({}) " the runner is a dict " keys: