2019-03-04 03:28:35 -05:00
|
|
|
if exists('g:polyglot_disabled') && index(g:polyglot_disabled, 'terraform') != -1
|
|
|
|
finish
|
|
|
|
endif
|
|
|
|
|
2018-10-08 13:00:59 -04:00
|
|
|
" terraform.vim - basic vim/terraform integration
|
|
|
|
" Maintainer: HashiVim <https://github.com/hashivim>
|
|
|
|
|
2019-03-04 03:15:44 -05:00
|
|
|
set formatoptions-=t
|
|
|
|
|
2018-10-08 13:00:59 -04:00
|
|
|
if exists("g:loaded_terraform") || v:version < 700 || &cp || !executable('terraform')
|
|
|
|
finish
|
|
|
|
endif
|
|
|
|
let g:loaded_terraform = 1
|
|
|
|
|
|
|
|
if !exists("g:terraform_fmt_on_save")
|
|
|
|
let g:terraform_fmt_on_save = 0
|
|
|
|
endif
|
|
|
|
|
|
|
|
function! s:commands(A, L, P)
|
|
|
|
return join([
|
|
|
|
\ "apply",
|
|
|
|
\ "console",
|
|
|
|
\ "destroy",
|
|
|
|
\ "env",
|
|
|
|
\ "fmt",
|
|
|
|
\ "get",
|
|
|
|
\ "graph",
|
|
|
|
\ "import",
|
|
|
|
\ "init",
|
|
|
|
\ "output",
|
|
|
|
\ "plan",
|
|
|
|
\ "providers",
|
|
|
|
\ "push",
|
|
|
|
\ "refresh",
|
|
|
|
\ "show",
|
|
|
|
\ "taint",
|
|
|
|
\ "untaint",
|
|
|
|
\ "validate",
|
|
|
|
\ "version",
|
|
|
|
\ "workspace",
|
|
|
|
\ "debug",
|
|
|
|
\ "force-unlock",
|
|
|
|
\ "state"
|
|
|
|
\ ], "\n")
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Adapted from vim-hclfmt:
|
|
|
|
" https://github.com/fatih/vim-hclfmt/blob/master/autoload/fmt.vim
|
|
|
|
function! terraform#fmt()
|
|
|
|
let l:curw = winsaveview()
|
2019-03-04 03:15:44 -05:00
|
|
|
let l:tmpfile = tempname() . ".tf"
|
2018-10-08 13:00:59 -04:00
|
|
|
call writefile(getline(1, "$"), l:tmpfile)
|
|
|
|
let output = system("terraform fmt -write " . l:tmpfile)
|
|
|
|
if v:shell_error == 0
|
|
|
|
try | silent undojoin | catch | endtry
|
|
|
|
call rename(l:tmpfile, resolve(expand("%")))
|
|
|
|
silent edit!
|
|
|
|
let &syntax = &syntax
|
|
|
|
else
|
|
|
|
echo output
|
|
|
|
call delete(l:tmpfile)
|
|
|
|
endif
|
|
|
|
call winrestview(l:curw)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
augroup terraform
|
|
|
|
autocmd!
|
2018-12-26 04:41:57 -05:00
|
|
|
autocmd BufEnter *
|
2018-10-08 13:00:59 -04:00
|
|
|
\ command! -nargs=+ -complete=custom,s:commands Terraform execute '!terraform '.<q-args>. ' -no-color'
|
2018-12-26 04:41:57 -05:00
|
|
|
autocmd BufEnter * command! -nargs=0 TerraformFmt call terraform#fmt()
|
2018-10-08 13:00:59 -04:00
|
|
|
if get(g:, "terraform_fmt_on_save", 1)
|
|
|
|
autocmd BufWritePre *.tf call terraform#fmt()
|
|
|
|
autocmd BufWritePre *.tfvars call terraform#fmt()
|
|
|
|
endif
|
|
|
|
augroup END
|