-
Notifications
You must be signed in to change notification settings - Fork 135
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 operator expressions (+
, -
, !=
, !
...) and parentheses
#529
Comments
+
, -
, !=
...) and unary (e.g. !
) operator expressions+
, -
, !=
, !
...) and parentheses
Need to research what happens if you e.g. try to |
There is a decent documentation in the form of code here: https://github.com/hashicorp/hcl/blob/31e2c60c39cb2464101d7ad49b9be15442785aa9/hclsyntax/expression_ops.go#L18-L121 |
I think the discussion above is asking how the Terraform language treats addition of non-numeric types, which I can answer! The direct answer to that question is: Terraform tries to convert both operands to numbers in the same way that the
A more general answer: In HCL, most of the operators are specified to take operands of a particular type only. ( As a general rule, the operators will try to convert their operands to the prescribed type and then act on the result, just as we saw for numbers above. For example,
The operators are actually implemented as just function calls with a special syntax, so the automatic type conversion behavior is the same as for function calls where the underlying function has parameters with type constraints. Those functions are not exposed as normal named functions so the built-in operators are the only way to access them, but their implementations are written using the same approach as for the more conventional functions accessible using the function call syntax. Because of that, it might be possible to reuse some existing logic for providing completion for function arguments, although of course only for semantics and not for syntax since the syntax is different. The functions themselves actually belong to my underlying |
@apparentlymart Thanks, that is helpful, although in the context of LS we more often deal with situations where we either lack values or we just cannot or do not evaluate it (since we'd effectively have to re-implement pretty much the whole DAG inside LS, which is a problem of its own), so we are more often left with just types. With that in mind, while we could be "smarter" and could avoid e.g. completing variable "foo" {
default = "not-a-number"
}
locals {
baz = 42 + var. # HERE
} This will be fine for smaller configurations but it will get less helpful with larger ones where the user will have to spend more time either typing or scrolling. I am hopeful that we can at least start filtering the simplest of cases, e.g. variable defaults, which are resolvable without further context (i.e. without DAG) but I am not too optimistic about the other ones. Either way I admit this (convertibility based on values) is not a new problem - we face it when filtering references and functions, whether or not operators are involved. |
Support for unary and binary operators was added in hashicorp/hcl-lang#320 but parentheses (i.e. |
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. |
Background
Terraform supports a number of operators, which can be combined with other expressions, for example:
-42
!var.bool
1 + 2 * 3
(1 + 2) * 3
var.number * 2
Language server currently doesn't support any of these operators, which in practice means that e.g.
+
), nor near unary operator (e.g. after!
)var.bool
is not reported as traversal if there is!
in the frontProposal
Completion
TODO
-
!
Hover
TODO
Semantic Tokens
TODO
The text was updated successfully, but these errors were encountered: