From 6f84551a04d95b37a678ff090589dfee14ec271e Mon Sep 17 00:00:00 2001 From: Fatih Arslan Date: Wed, 4 Nov 2015 17:21:17 +0300 Subject: [PATCH 1/2] printer: aligned adjacent attributes, closes #3 --- printer/nodes.go | 29 ++++++++++++++-------- printer/testdata/comment.golden | 2 +- printer/testdata/comment_aligned.golden | 6 ++--- printer/testdata/comment_aligned.input | 4 +-- printer/testdata/comment_standalone.golden | 2 +- printer/testdata/complexhcl.golden | 8 +++--- 6 files changed, 30 insertions(+), 21 deletions(-) diff --git a/printer/nodes.go b/printer/nodes.go index abd85c1..da7f197 100644 --- a/printer/nodes.go +++ b/printer/nodes.go @@ -323,11 +323,19 @@ func (p *printer) objectType(o *ast.ObjectType) []byte { func (p *printer) alignedItems(items []*ast.ObjectItem) []byte { var buf bytes.Buffer - var longestLine int + // find the longest key and value length, needed for alignment + var longestKeyLen int // longest key length + var longestValLen int // longest value length for _, item := range items { - lineLen := len(item.Keys[0].Token.Text) + len(p.output(item.Val)) - if lineLen > longestLine { - longestLine = lineLen + key := len(item.Keys[0].Token.Text) + val := len(p.output(item.Val)) + + if key > longestKeyLen { + longestKeyLen = key + } + + if val > longestValLen { + longestValLen = val } } @@ -339,25 +347,26 @@ func (p *printer) alignedItems(items []*ast.ObjectItem) []byte { } } - curLen := 0 for i, k := range item.Keys { + keyLen := len(k.Token.Text) buf.WriteString(k.Token.Text) - buf.WriteByte(blank) + for i := 0; i < longestKeyLen-keyLen+1; i++ { + buf.WriteByte(blank) + } // reach end of key if i == len(item.Keys)-1 && len(item.Keys) == 1 { buf.WriteString("=") buf.WriteByte(blank) } - - curLen = len(k.Token.Text) // two blanks and one assign } + val := p.output(item.Val) + valLen := len(val) buf.Write(val) - curLen += len(val) if item.Val.Pos().Line == item.Keys[0].Pos().Line && item.LineComment != nil { - for i := 0; i < longestLine-curLen+1; i++ { + for i := 0; i < longestValLen-valLen+1; i++ { buf.WriteByte(blank) } diff --git a/printer/testdata/comment.golden b/printer/testdata/comment.golden index e77ebe9..8263733 100644 --- a/printer/testdata/comment.golden +++ b/printer/testdata/comment.golden @@ -4,7 +4,7 @@ variable "foo" { # Standalone comment should be still here - default = "bar" + default = "bar" description = "bar" # yooo } diff --git a/printer/testdata/comment_aligned.golden b/printer/testdata/comment_aligned.golden index 16fc6ff..da8f9d0 100644 --- a/printer/testdata/comment_aligned.golden +++ b/printer/testdata/comment_aligned.golden @@ -1,8 +1,8 @@ aligned = { # We have some aligned items below - foo = "bar" # yoo1 - default = "bar" # yoo2 - bar = "bar" # yoo3 + foo = "fatih" # yoo1 + default = "bar" # yoo2 + bar = "bar and foo" # yoo3 default = { bar = "example" diff --git a/printer/testdata/comment_aligned.input b/printer/testdata/comment_aligned.input index 2233738..1c6ba0d 100644 --- a/printer/testdata/comment_aligned.input +++ b/printer/testdata/comment_aligned.input @@ -1,8 +1,8 @@ aligned { # We have some aligned items below - foo = "bar" # yoo1 + foo = "fatih" # yoo1 default = "bar" # yoo2 - bar = "bar" # yoo3 + bar = "bar and foo" # yoo3 default = { bar = "example" } diff --git a/printer/testdata/comment_standalone.golden b/printer/testdata/comment_standalone.golden index 448bb89..a1d28ef 100644 --- a/printer/testdata/comment_standalone.golden +++ b/printer/testdata/comment_standalone.golden @@ -3,7 +3,7 @@ aligned = { # Standalone 1 - a = "bar" # yoo1 + a = "bar" # yoo1 default = "bar" # yoo2 # Standalone 2 diff --git a/printer/testdata/complexhcl.golden b/printer/testdata/complexhcl.golden index 3bf4e06..8c01253 100644 --- a/printer/testdata/complexhcl.golden +++ b/printer/testdata/complexhcl.golden @@ -1,5 +1,5 @@ variable "foo" { - default = "bar" + default = "bar" description = "bar" } @@ -28,17 +28,17 @@ resource aws_instance "web" { network_interface = { device_index = 0 - description = "Main network interface" + description = "Main network interface" } } resource "aws_instance" "db" { security_groups = "${aws_security_group.firewall.*.id}" - VPC = "foo" + VPC = "foo" depends_on = ["aws_instance.web"] } output "web_ip" { value = "${aws_instance.web.private_ip}" -} \ No newline at end of file +} From b0913285c3344d11eabd1813be555ee1009681f2 Mon Sep 17 00:00:00 2001 From: Fatih Arslan Date: Wed, 4 Nov 2015 17:27:17 +0300 Subject: [PATCH 2/2] printer: update golden example --- printer/testdata/complexhcl.golden | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/printer/testdata/complexhcl.golden b/printer/testdata/complexhcl.golden index 8c01253..1639944 100644 --- a/printer/testdata/complexhcl.golden +++ b/printer/testdata/complexhcl.golden @@ -41,4 +41,4 @@ resource "aws_instance" "db" { output "web_ip" { value = "${aws_instance.web.private_ip}" -} +} \ No newline at end of file