Dotfiles.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

184 lines
4.8 KiB

" terraform.vim - basic vim/terraform integration
" Maintainer: HashiVim <https://github.com/hashivim>
let s:cpo_save = &cpoptions
set cpoptions&vim
" Ensure no conflict with arguments from the environment
let $TF_CLI_ARGS_fmt=''
function! TerraformFmt() abort
" Save the view.
let curw = winsaveview()
" Make a fake change so that the undo point is right.
normal! ix
normal! "_x
" Execute `terraform fmt`, redirecting stderr to a temporary file.
let tmpfile = tempname()
let shellredir_save = &shellredir
let &shellredir = '>%s 2>'.tmpfile
silent execute '%!'.g:terraform_binary_path.' fmt -no-color -'
let &shellredir = shellredir_save
" If there was an error, undo any changes and show stderr.
if v:shell_error != 0
silent undo
let output = readfile(tmpfile)
echo join(output, "\n")
endif
" Delete the temporary file, and restore the view.
call delete(tmpfile)
call winrestview(curw)
endfunction
function! TerraformAlign() abort
let p = '^.*=[^>]*$'
if exists(':Tabularize') && getline('.') =~# '^.*=' && (getline(line('.')-1) =~# p || getline(line('.')+1) =~# p)
let column = strlen(substitute(getline('.')[0:col('.')],'[^=]','','g'))
let position = strlen(matchstr(getline('.')[0:col('.')],'.*=\s*\zs.*'))
Tabularize/=/l1
normal! 0
call search(repeat('[^=]*=',column).'\s\{-\}'.repeat('.',position),'ce',line('.'))
endif
endfunction
function! TerraformCommands(ArgLead, CmdLine, CursorPos) abort
let commands = [
\ 'init',
\ 'validate',
\ 'plan',
\ 'apply',
\ 'destroy',
\ 'console',
\ 'fmt',
\ 'force-unlock',
\ 'get',
\ 'graph',
\ 'import',
\ 'login',
\ 'logout',
\ 'output',
\ 'providers',
\ 'refresh',
\ 'show',
\ 'state',
\ 'taint',
\ 'untaint',
\ 'version',
\ 'workspace'
\ ]
return join(commands, "\n")
endfunction
let &cpoptions = s:cpo_save
unlet s:cpo_save
" ------------------------------------------------------------------------------
if exists('b:did_ftplugin') || v:version < 700 || &compatible
finish
endif
let b:did_ftplugin = 1
let s:cpo_save = &cpoptions
set cpoptions&vim
" j is a relatively recent addition; silence warnings when setting it.
setlocal formatoptions-=t formatoptions+=croql
silent! setlocal formatoptions+=j
let b:undo_ftplugin = 'setlocal formatoptions<'
if !has('patch-7.4.1142')
" Include hyphens as keyword characters so that a keyword appearing as
" part of a longer name doesn't get partially highlighted.
setlocal iskeyword+=-
let b:undo_ftplugin .= ' iskeyword<'
endif
if get(g:, 'terraform_fold_sections', 0)
setlocal foldmethod=syntax
let b:undo_ftplugin .= ' foldmethod<'
endif
" Set the commentstring
setlocal commentstring=#%s
let b:undo_ftplugin .= ' commentstring<'
if get(g:, 'terraform_align', 0) && exists(':Tabularize')
inoremap <buffer> <silent> = =<Esc>:call TerraformAlign()<CR>a
let b:undo_ftplugin .= '|iunmap <buffer> ='
endif
let &cpoptions = s:cpo_save
unlet s:cpo_save
if !exists('g:terraform_binary_path')
let g:terraform_binary_path='terraform'
endif
if !executable(g:terraform_binary_path)
finish
endif
let s:cpo_save = &cpoptions
set cpoptions&vim
command! -nargs=+ -complete=custom,TerraformCommands -buffer Terraform
\ execute '!'.g:terraform_binary_path.' '.<q-args>.' -no-color'
command! -nargs=0 -buffer TerraformFmt call TerraformFmt()
let b:undo_ftplugin .= '|delcommand Terraform|delcommand TerraformFmt'
if get(g:, 'terraform_fmt_on_save', 0)
augroup vim.terraform.fmt
autocmd!
autocmd BufWritePre *.tf call TerraformFmt()
autocmd BufWritePre *.tfvars call TerraformFmt()
augroup END
endif
let &cpoptions = s:cpo_save
unlet s:cpo_save
" ------------------------------------------------------------------------------
" https://github.com/jgerry/terraform-vim-folding
" ------------------------------------------------------------------------------
function! TerraformFolds()
let thisline = getline(v:lnum)
if match(thisline, '^resource') >= 0
return ">1"
elseif match(thisline, '^provider') >= 0
return ">1"
elseif match(thisline, '^module') >= 0
return ">1"
elseif match(thisline, '^variable') >= 0
return ">1"
elseif match(thisline, '^output') >= 0
return ">1"
elseif match(thisline, '^data') >= 0
return ">1"
elseif match(thisline, '^locals') >= 0
return ">1"
elseif match(thisline, '^terraform') >= 0
return ">1"
else
return "="
endif
endfunction
function! TerraformFoldText()
let foldsize = (v:foldend-v:foldstart)
return getline(v:foldstart).' ('.foldsize.' lines)'
endfunction
setlocal foldmethod=expr
setlocal foldexpr=TerraformFolds()
setlocal foldlevel=0
setlocal foldtext=TerraformFoldText()
" ------------------------------------------------------------------------------