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

Support for built-in functions #666

Closed
radeksimko opened this issue Jun 18, 2021 · 2 comments
Closed

Support for built-in functions #666

radeksimko opened this issue Jun 18, 2021 · 2 comments
Assignees
Labels
enhancement New feature or request upstream/tf-core
Milestone

Comments

@radeksimko
Copy link
Member

radeksimko commented Jun 18, 2021

Related to


Problem Statement

Terraform provides builtin functions to help users build reusable configuration.

Without consulting the documentation users may not always know:

  • functions names available
  • purpose of the function
  • signature (arguments & outputs) of each function

Technical Details

Functions are considered to be expression types within HCL and each version of Terraform may contain different functions or the implementation of functions may differ.

User Impact

Any user which uses functions in their configuration would be impacted.

Expected User Experience

User will be guided to get names of functions, arguments and outputs right, and understand what a function does without leaving their editor.

Completion

Given a configuration

resource "aws_vpc" "example" {
  cidr_block = # HERE
}

when user requests completion in the highlighted position, aside from other candidates (such as references), they're presented with function names compatible with the field type (i.e. functions which return string in the above example as cidr_block is of type string):

  • lower (string)
  • upper (string)
  • format (string, string...)

Hover

resource "aws_vpc" "example" {
  cidr_block = format("10.0.%d.0/%d", count.index, var.vpc_size)
}

When user hovers over format they're provided with a description of the function, such as:

format(spec, values...)

format produces a string by formatting a number of other values according to a specification string. It is similar to the printf function in C, and other similar functions in other programming languages.

https://www.terraform.io/docs/language/functions/format.html

Semantic highlighting

resource "aws_vpc" "example" {
  cidr_block = format("10.0.%d.0/%d", count.index, var.vpc_size)
}

The name of a valid function, such as format will be highlighted semantically as function name, name of an invalid function (such as frmt) will not. That way user can tell a difference between typo-ed function name and correct one just from the colours.

Function signature

resource "aws_vpc" "example" {
  cidr_block = format(

After picking function name from completion popup, or typing a "trigger character" ( user is presented with function signature. They're also presented with details relevant to an argument relevant to the position after typing (or completing) "trigger character" ,.

The user-visible content presented is similar to the one presented via hover, except that here we also document each parameter individually, i.e.

  • spec string - The specification is a string that includes formatting verbs that are introduced with the % character.
  • values string - Values to be formatted per specification

Proposal

  • hcl-lang to allow registering function signatures, or whole functions (depending on implementation)
  • terraform-schema to provide function signatures, or whole functions

Blocking Questions

  • Do we want to decouple functions entirely from Terraform Core, maintain a copy somewhere, or provide a way for TF Core to expose functions?
@radeksimko radeksimko added enhancement New feature or request upstream/tf-core labels Jun 18, 2021
@radeksimko radeksimko added the terraform-ls Features/bugs which will be implemented/fixed purely on the LS side label Jun 18, 2021
@danieldreier danieldreier removed enhancement New feature or request terraform-ls Features/bugs which will be implemented/fixed purely on the LS side upstream/tf-core labels Jul 27, 2021
@radeksimko radeksimko added enhancement New feature or request upstream/tf-core labels Aug 10, 2021
@xiehan xiehan added this to the 2.27.0 milestone Oct 27, 2022
@dbanck dbanck modified the milestones: 2.25.2, 2.26.0 Feb 22, 2023
@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request upstream/tf-core
Projects
None yet
Development

No branches or pull requests

4 participants