From d703862140c8b1ea84a2533330978110a7ef988a Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Thu, 15 Feb 2024 15:24:33 +0000 Subject: [PATCH 1/5] hclwrite: Fix formatting of namespaced functions --- hclwrite/format.go | 5 +++++ hclwrite/format_test.go | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/hclwrite/format.go b/hclwrite/format.go index d5f974c3..01a6ba5c 100644 --- a/hclwrite/format.go +++ b/hclwrite/format.go @@ -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 diff --git a/hclwrite/format_test.go b/hclwrite/format_test.go index e64b551f..08d0ed05 100644 --- a/hclwrite/format_test.go +++ b/hclwrite/format_test.go @@ -615,6 +615,10 @@ module "x" { abcde = "456" }`, }, + { + `attr = provider::framework::example()`, + `attr = provider::framework::example()`, + }, } for i, test := range tests { From 583d01b289c989892dc6d68778999886c8f999aa Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Thu, 15 Feb 2024 15:24:40 +0000 Subject: [PATCH 2/5] gofmt --- hclwrite/format.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/hclwrite/format.go b/hclwrite/format.go index 01a6ba5c..be3dce47 100644 --- a/hclwrite/format.go +++ b/hclwrite/format.go @@ -457,9 +457,12 @@ func tokenBracketChange(tok *Token) int { // // 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 +// +// 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. +// +// 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 From f022482158e1110101d8fcedf1e3628548a140e9 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Thu, 15 Feb 2024 15:32:13 +0000 Subject: [PATCH 3/5] hclwrite: Add negative test --- hclwrite/format_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/hclwrite/format_test.go b/hclwrite/format_test.go index 08d0ed05..be236b5d 100644 --- a/hclwrite/format_test.go +++ b/hclwrite/format_test.go @@ -619,6 +619,12 @@ module "x" { `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 { From 8dddc351e4d2782b3a62844f3c3f87d72f9f5e13 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Thu, 15 Feb 2024 16:02:44 +0000 Subject: [PATCH 4/5] hclwrite: add two more test cases --- hclwrite/format_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/hclwrite/format_test.go b/hclwrite/format_test.go index be236b5d..5c4eb7a6 100644 --- a/hclwrite/format_test.go +++ b/hclwrite/format_test.go @@ -619,6 +619,14 @@ module "x" { `attr = provider::framework::example()`, `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 From 9926eaf00193efbfe340f3461f5241c92b6aac6a Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Fri, 16 Feb 2024 09:56:54 +0000 Subject: [PATCH 5/5] correct comment formatting --- hclwrite/format.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/hclwrite/format.go b/hclwrite/format.go index be3dce47..c9722dd2 100644 --- a/hclwrite/format.go +++ b/hclwrite/format.go @@ -455,14 +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