summaryrefslogtreecommitdiffstats
path: root/ftplugin/terraform.vim
diff options
context:
space:
mode:
authorAdam Stankiewicz <sheerun@sher.pl>2018-10-08 19:00:59 +0200
committerAdam Stankiewicz <sheerun@sher.pl>2018-10-08 19:00:59 +0200
commitfd74d8b2b170b540680a9bbf6c64990f8ebafd08 (patch)
treeb1fdef6203a78a21053d1b8e0666ab7a38c36df2 /ftplugin/terraform.vim
parent055f7710b65dfa2df52fc0b5be2486ae36ac5751 (diff)
downloadvim-polyglot-3.3.3.tar.gz
vim-polyglot-3.3.3.zip
Updatev3.3.3
Diffstat (limited to 'ftplugin/terraform.vim')
-rw-r--r--ftplugin/terraform.vim73
1 files changed, 73 insertions, 0 deletions
diff --git a/ftplugin/terraform.vim b/ftplugin/terraform.vim
new file mode 100644
index 00000000..ce646377
--- /dev/null
+++ b/ftplugin/terraform.vim
@@ -0,0 +1,73 @@
+if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'terraform') == -1
+
+" terraform.vim - basic vim/terraform integration
+" Maintainer: HashiVim <https://github.com/hashivim>
+
+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()
+ let l:tmpfile = tempname()
+ 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!
+ autocmd VimEnter *
+ \ command! -nargs=+ -complete=custom,s:commands Terraform execute '!terraform '.<q-args>. ' -no-color'
+ autocmd VimEnter * command! -nargs=0 TerraformFmt call terraform#fmt()
+ if get(g:, "terraform_fmt_on_save", 1)
+ autocmd BufWritePre *.tf call terraform#fmt()
+ autocmd BufWritePre *.tfvars call terraform#fmt()
+ endif
+augroup END
+
+endif