Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terraform (HCL) (*.tf): new parser #2952

Conversation

antonysouthworth-halter
Copy link
Contributor

@antonysouthworth-halter antonysouthworth-halter commented Apr 11, 2021

Add support for Terraform (HCL) files.

Related:

TODO

  • add unit tests
  • update documentation
    • manpages
    • codebase

@antonysouthworth-halter
Copy link
Contributor Author

Have opened this as a quick thing, please advise if there are other things I need to do before merging (I read through https://docs.ctags.io/en/latest/optlib.html but may have missed some things).

main/parsers_p.h Outdated
@@ -149,6 +149,7 @@
SystemTapParser, \
TclParser, \
TclOOParser, \
TfParser, \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TerraformParser or TerraFormParser is better.

# GNU General Public License version 2 or (at your option) any later version.
#
# Derived from `vim-terraform-completion`:
# - https://github.com/juliosueiras/vim-terraform-completion/blob/master/ctags/terraform.ctags
Copy link
Member

@masatake masatake Apr 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you reuse the regex patterns of @juliosueiras's work, ask @juliosueiras to allow to use one's .ctags in GPLv2 or later. Copy & paste the evidence of alowing to use here as comments.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what would I need to paste?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you fill and paste it at the head of your .ctags?

#
#  Copyright (c) 2021, <YOUR NAME HERE>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
#

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

# - https://github.com/juliosueiras/vim-terraform-completion/blob/master/ctags/terraform.ctags
#
# Changed the name from `terraform` to `tf` so vim will recognise it properly based
# on file extension (*.tf).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean the name of the parser must be "tf"?

# - https://www.terraform.io/docs/language/values/locals.html

--langdef=tf
--map-tf=+tf:.tf.tfvars
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this should be

--map-tf=+.tf
--map-tf=+.tfvars

See https://docs.ctags.io/en/latest/man/ctags.1.html#options for more details.


--langdef=tf
--map-tf=+tf:.tf.tfvars
--regex-tf=/^[[:space:]]*resource[[:space:]]*"([^"]*)"[[:space:]]*"([^"]*)"/\2/r,Resource/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use --kinddef-=... option before using kinds.
https://docs.ctags.io/en/latest/man/ctags-optlib.7.html#options

</Project>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't put a newline at the end of file.

</Project>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't put a newline at the end of file.

# A notable abscence is `local`, because `locals` are defined inside a block and
# it's way harder to write a good regex for that.
# - https://www.terraform.io/docs/language/values/locals.html

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you put a URL for the language reference?

@antonysouthworth-halter
Copy link
Contributor Author

@masatake thank you for fast and thorough review 😄, I will address comments as/when I can.

@juliosueiras apologies, perhaps I should have talked to you first? 😅

@juliosueiras
Copy link

@antonysouthworth-halter no, thats fine

# it's way harder to write a good regex for that.
# - https://www.terraform.io/docs/language/values/locals.html

--langdef=tf
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Terraform or TerraformHCL is better. I'm not sure which one is better.
If you are not sure, too, use longer name as I did in "PupppetManifest".

</Project>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About this file, no newline at the of file

</Project>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About this file, no newline at the end of file.

--kinddef-terraform=o,Output,Terraform Output
--regex-terraform=/^[[:space:]]*resource[[:space:]]*"([^"]*)"[[:space:]]*"([^"]*)"/\2/r,Resource/
--regex-terraform=/^[[:space:]]*data[[:space:]]*"([^"]*)"[[:space:]]*"([^"]*)"/\2/d,Data/
--regex-terraform=/^[[:space:]]*variable[[:space:]]*"([^"]*)"/\1/v,Variable/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v is already defined with --kinddef-.... So you don't have to specify v,Variable. Just v is enough:

--regex-Terraform=/^[[:space:]]*variable[[:space:]]*"([^"]*)"/\1/v/

I don't know well about the syntax of Terraforms. However, I guess a whitespace may be needed after variable keyword. We don't record a variable having an empty string as name. So + is better than *.

--regex-Terraform=/^[[:space:]]*variable[[:space:]]+"([^"]+)"/\1/v/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is applicable to other lines of --regex-....

--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/
--langdef=terraform
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I forgot writing one thing. A parser that is part of ctags should have Capitalized name.
Please, do s/terraform/Terraform/g.

@masatake masatake changed the title Add Terraform (HCL) (*.tf) support Terraform (HCL) (*.tf): new parser May 23, 2021
@tonglil
Copy link

tonglil commented Aug 10, 2021

I was able to add locals parsing if this PR still has any traction.

@masatake
Copy link
Member

@tonglil, feel free to make a new pull request that includes the changes of this pull request.
(It means you take over this pull request.)

@masatake
Copy link
Member

The biggest reason I didn't merge this is there is no test case.
@tonglil do you have an interest in polishing this pull request?

@tonglil
Copy link

tonglil commented Jan 26, 2022

Sorry, I will not be able to contribute to this PR.

@Haggus
Copy link
Contributor

Haggus commented Mar 29, 2023

@masatake I'd like to take over and finish this PR

@masatake
Copy link
Member

@Haggus You are welcome. optlib/terraform.c is automatically generated by misc/optlib2c.
So what you have to edit is optlib/terraform.ctags.
Test cases are needed.

@antonysouthworth-halter
Copy link
Contributor Author

thanks @Haggus , unfortunately I never got time to come back to this. @masatake should we close this PR in favour of #3683 ?

@masatake
Copy link
Member

I will close this via #3684.

@masatake
Copy link
Member

masatake commented Apr 1, 2023

@antonysouthworth-halter, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants