Skip to content

Commit

Permalink
Added Async Run for NeoVim and Vim 8
Browse files Browse the repository at this point in the history
  • Loading branch information
juliosueiras committed May 18, 2017
1 parent b991456 commit 32907dd
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ let g:tagbar_type_terraform = {

## General Todo
- [x] Support for Neomake(Require further testing)
- [X] Run terraform plan and output to a new window(`<leader>rr`) [This is just incase someone want it]
- [ ] Add Async Run support(For Neovim/Vim 8)
- [X] Run terraform plan and output to a new window(`<leader>rr`)
- [X] Async Run support(For Neovim/Vim 8)
- [ ] Move regex code to a json(for easier extension)
- [x] Test from zero to useful setup using Docker
- [x] Jump Reference (Ctrl-L first time to jump to resource definition, second time
Expand Down
85 changes: 85 additions & 0 deletions autoload/terraformcomplete.vim
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,91 @@ function! terraformcomplete#OutputFold()
end
endfunction

function! terraformcomplete#NeovimRunHandler(job_id, data, event) dict
if a:event == 'stdout'
elseif a:event == 'stderr'
else
let file = expand('%')
botright new
execute ':r ' . g:planOutputFile
setlocal buftype=nofile
setlocal bufhidden=hide
setlocal nowrap
setlocal noswapfile
setlocal readonly
setlocal foldmethod=expr
setlocal foldexpr=terraformcomplete#OutputFold()
execute 'normal! GG'
noremap <silent><buffer> q :q<CR>
unlet g:planOutputFile
endif
endfunction

fun! terraformcomplete#NeovimRun()
let s:callbacks = {
\ 'on_stdout': function('terraformcomplete#NeovimRunHandler'),
\ 'on_stderr': function('terraformcomplete#NeovimRunHandler'),
\ 'on_exit': function('terraformcomplete#NeovimRunHandler')
\ }

if exists('g:planOutputFile')
echo 'Already running Plan in background'
else
echo 'Running Plan in background'

let g:planOutputFile = tempname()
let job1 = jobstart(['bash', '-c', 'terraform plan -input=false -no-color > ' . g:planOutputFile ], extend({'shell': ''}, s:callbacks))
endif
endfun

fun! terraformcomplete#AsyncRun()
if v:version < 800
echoerr 'AsyncRun requires VIM version 8 or higher'
return
endif

if exists('g:planOutputFile')
echo 'Already running Plan in background'
else
echo 'Running Plan in background'

let g:planOutputFile = tempname()
call job_start('terraform plan -input=false -no-color', {'close_cb': 'terraformcomplete#AsyncRunHandler', 'out_io': 'file', 'out_name': g:planOutputFile})
endif
endfun

function! terraformcomplete#AsyncRunHandler(channel)
let file = expand('%')
botright new
execute ":r " . g:planOutputFile
setlocal buftype=nofile
setlocal bufhidden=hide
setlocal nowrap
setlocal noswapfile
setlocal readonly
setlocal foldmethod=expr
setlocal foldexpr=terraformcomplete#OutputFold()
execute 'normal! GG'
noremap <silent><buffer> q :q<CR>
unlet g:planOutputFile
endfunction

fun! terraformcomplete#AsyncRun()
if v:version < 800
echoerr 'AsyncRun requires VIM version 8 or higher'
return
endif

if exists('g:planOutputFile')
echo 'Already running Plan in background'
else
echo 'Running Plan in background'

let g:planOutputFile = tempname()
call job_start('terraform plan -input=false -no-color', {'close_cb': 'terraformcomplete#AsyncRunHandler', 'out_io': 'file', 'out_name': g:planOutputFile})
endif
endfun

fun! terraformcomplete#Run()
let file = expand('%')
botright new
Expand Down
9 changes: 8 additions & 1 deletion ftplugin/terraform.vim
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,12 @@ augroup TerraformCompleteKeys
autocmd FileType terraform noremap <buffer> <C-L> :call terraformcomplete#JumpRef()<CR>
augroup END

silent! map <unique> <buffer> <Leader>rr :call terraformcomplete#Run()<CR>

if has('nvim')
silent! map <unique> <buffer> <Leader>rr :call terraformcomplete#NeovimRun()<CR>
elseif v:version >= 800
silent! map <unique> <buffer> <Leader>rr :call terraformcomplete#AsyncRun()<CR>
else
silent! map <unique> <buffer> <Leader>rr :call terraformcomplete#Run()<CR>
end

0 comments on commit 32907dd

Please sign in to comment.