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

hclwrite: Fix formatting of namespaced functions #658

Merged
merged 5 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 10 additions & 5 deletions hclwrite/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ func spaceAfterToken(subject, before, after *Token) bool {
// Don't split a function name from open paren in a call
return false

case (subject.Type == hclsyntax.TokenIdent && after.Type == hclsyntax.TokenDoubleColon) ||
(subject.Type == hclsyntax.TokenDoubleColon && after.Type == hclsyntax.TokenIdent):
// Don't split namespace segments in a function call
return false

case subject.Type == hclsyntax.TokenDot || after.Type == hclsyntax.TokenDot:
// Don't use spaces around attribute access dots
return false
Expand Down Expand Up @@ -450,11 +455,11 @@ func tokenBracketChange(tok *Token) int {
// formatLine represents a single line of source code for formatting purposes,
// splitting its tokens into up to three "cells":
//
// lead: always present, representing everything up to one of the others
// assign: if line contains an attribute assignment, represents the tokens
// starting at (and including) the equals symbol
// comment: if line contains any non-comment tokens and ends with a
// single-line comment token, represents the comment.
// - lead: always present, representing everything up to one of the others
// - assign: if line contains an attribute assignment, represents the tokens
// starting at (and including) the equals symbol
// - comment: if line contains any non-comment tokens and ends with a
// single-line comment token, represents the comment.
//
// When formatting, the leading spaces of the first tokens in each of these
// cells is adjusted to align vertically their occurences on consecutive
Expand Down
18 changes: 18 additions & 0 deletions hclwrite/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,24 @@ module "x" {
abcde = "456"
}`,
},
{
`attr = provider::framework::example()`,
radeksimko marked this conversation as resolved.
Show resolved Hide resolved
`attr = provider::framework::example()`,
},
{
`attr = provider :: framework :: example()`,
`attr = provider::framework::example()`,
},
{
`attr = provider ::framework:: example()`,
`attr = provider::framework::example()`,
},
{
// This is invalid syntax so formatting it with spaces
// does not have any meaning other than to make the fact more visible
`attr = provider::+example()`,
`attr = provider:: + example()`,
},
}

for i, test := range tests {
Expand Down