preservim / tagbar

Vim plugin that displays tags in a window, ordered by scope
https://preservim.github.io/tagbar
Other
6.12k stars 486 forks source link

How to add support for Terraform (HCL) #757

Open antonysouthworth-halter opened 3 years ago

antonysouthworth-halter commented 3 years ago

Putting this here for others to find; I don't know how to contribute to the project wiki.

Assuming you are using Universal Ctags, (and for reference I'm on macOS Catalina).

  1. Add the following to ~/.ctags.d/terraform.ctags
--langdef=tf
--langmap=tf:.tf.tfvars
--regex-tf=/^[[:space:]]*resource[[:space:]]*"([^"]*)"[[:space:]]*"([^"]*)"/\2/r,Resource/
--regex-tf=/^[[:space:]]*data[[:space:]]*"([^"]*)"[[:space:]]*"([^"]*)"/\2/d,Data/
--regex-tf=/^[[:space:]]*variable[[:space:]]*"([^"]*)"/\1/v,Variable/
--regex-tf=/^[[:space:]]*provider[[:space:]]*"([^"]*)"/\1/p,Provider/
--regex-tf=/^[[:space:]]*module[[:space:]]*"([^"]*)"/\1/m,Module/
--regex-tf=/^[[:space:]]*output[[:space:]]*"([^"]*)"/\1/o,Output/
--regex-tf=/^([a-z0-9_]+)[[:space:]]*=/\1/f,TFVar/

Basically this explains to ctags how to parse .tf files.

  1. Add the following to ~/.vimrc
let g:tagbar_type_tf = {
  \ 'ctagstype': 'tf',
  \ 'kinds': [
    \ 'r:Resource',
    \ 'R:Resource',
    \ 'd:Data',
    \ 'D:Data',
    \ 'v:Variable',
    \ 'V:Variable',
    \ 'p:Provider',
    \ 'P:Provider',
    \ 'm:Module',
    \ 'M:Module',
    \ 'o:Output',
    \ 'O:Output',
    \ 'f:TFVar',
    \ 'F:TFVar'
  \ ]
\ }

AFAIU this basically explains to tagbar how to read the tags generated by ctags.

Example usage:

Screen Shot 2021-04-09 at 9 45 52 AM

alerque commented 3 years ago

Thanks for the contribution. Adding things to the Wiki should be as easy as hitting "edit" on the page you think it could go on, but I have another suggestion. Why don't you just add the code to the plugin core so it is supported out of the box for everybody without needing to copy a config from the Wiki at all?

I think the file you'd want to edit is this one.

alerque commented 3 years ago

On further reading I see I missed the bit where this requires modifications to the Ctags config. In that case you are right this will need to go into the Wiki for now. We usually don't add tag maps to the plugin until at least one tag provider program supports them out of the box. Perhaps you could contribute the relevant part upstream to Universal Ctags?

antonysouthworth-halter commented 3 years ago

@alerque thanks for reply 😄. Have added to wiki: https://github.com/preservim/tagbar/wiki#terraform-hcl

And PR open on universal ctags: https://github.com/universal-ctags/ctags/pull/2952