Skip to content

Instantly share code, notes, and snippets.

@TechIsCool
Last active July 26, 2019 19:13
Show Gist options
  • Save TechIsCool/0e080232d9e8871f9611a4e9a6f0ab91 to your computer and use it in GitHub Desktop.
Save TechIsCool/0e080232d9e8871f9611a4e9a6f0ab91 to your computer and use it in GitHub Desktop.
" Originally from https://github.com/juliosueiras/vim-terraform-completion
" autoload/terraformcomplete.vim
"
" The MIT License (MIT)
"
" Copyright (c) 2017 Julio Tain Sueiras
"
" Permission is hereby granted, free of charge, to any person obtaining a copy
" of this software and associated documentation files (the "Software"), to deal
" in the Software without restriction, including without limitation the rights
" to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
" copies of the Software, and to permit persons to whom the Software is
" furnished to do so, subject to the following conditions:
"
" The above copyright notice and this permission notice shall be included in all
" copies or substantial portions of the Software.
"
" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
" IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
" FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
" AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
" LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
" OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
" SOFTWARE.
""
" Function to open the doc in browser
function! terraformcomplete#OpenDoc()
try
let a_provider = terraformcomplete#GetProvider()
let a_resource = terraformcomplete#GetResource()
let a_arg = matchlist(getline("."), '\s*\([^ ]*\)\s*=\?', '')
if len(a_arg) >= 2
let a_arg = a_arg[1]
else
let a_arg = ''
endif
if a_provider == "digitalocean"
let a_provider = "do"
endif
let a_link = 'https://www.terraform.io/docs/providers/' . a_provider
if terraformcomplete#GetType() ==? 'resource'
let a_link .= '/r'
else
let a_link .= '/d'
endif
let a_link .= '/' . a_resource . '.html\#' . a_arg
"(Windows) cmd /c start filename_or_URL
if system('uname -s') =~ 'Darwin'
silent! execute ':!open ' . a_link
silent! execute ':redraw!'
else
silent! execute ':!xdg-open ' . a_link
silent! execute ':redraw!'
endif
catch
endtry
endfunction
fun! terraformcomplete#GetProvider()
let s:curr_pos = getpos('.')
if getline(".") !~# '^\s*\(resource\|data\)\s*"'
execute '?^\s*\(resource\|data\)\s*"'
endif
let a_provider = split(split(substitute(getline("."),'"', '', ''))[1], "_")[0]
call setpos(".", s:curr_pos)
unlet s:curr_pos
return a_provider
endfun
fun! terraformcomplete#GetResource()
let s:curr_pos = getpos('.')
if getline(".") !~# '^\s*\(resource\|data\)\s*"'
execute '?^\s*\(resource\|data\)\s*"'
endif
let a_provider = split(split(substitute(getline("."),'"', '', ''))[1], "_")[0]
let a_resource = substitute(split(split(getline("."))[1], a_provider . "_")[1], '"','','')
call setpos('.', s:curr_pos)
unlet s:curr_pos
return a_resource
endfun
fun! terraformcomplete#GetType()
let s:curr_pos = getpos('.')
if getline(".") !~# '^\s*\(resource\|data\)\s*"'
execute '?\s*\(resource\|data\)\s*"'
endif
if getline(".") =~? "resource"
let a_res = "resource"
else
let a_res = "data"
endif
call setpos(".", s:curr_pos)
unlet s:curr_pos
return a_res
endfun
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment