From b93aefc3c32da12a8d8cd678a53e1cedf99d06de Mon Sep 17 00:00:00 2001 From: Fatih Arslan Date: Tue, 27 Oct 2015 23:51:44 +0300 Subject: [PATCH] printer: add aligned comment support, still WIP --- printer/nodes.go | 75 ++++++++++++++++++++++++- printer/printer_test.go | 7 ++- printer/testdata/comment.golden | 8 +-- printer/testdata/comment.input | 6 -- printer/testdata/comment_aligned.golden | 12 ++++ printer/testdata/comment_aligned.input | 11 ++++ 6 files changed, 102 insertions(+), 17 deletions(-) create mode 100644 printer/testdata/comment_aligned.golden create mode 100644 printer/testdata/comment_aligned.input diff --git a/printer/nodes.go b/printer/nodes.go index 6dc8e7f..34ba4b8 100644 --- a/printer/nodes.go +++ b/printer/nodes.go @@ -75,6 +75,51 @@ func (p *printer) objectItem(o *ast.ObjectItem) []byte { return buf.Bytes() } +func (p *printer) alignedItems(items []*ast.ObjectItem) []byte { + var buf bytes.Buffer + + var longestLine int + for _, item := range items { + lineLen := len(item.Keys[0].Token.Text) + len(p.output(item.Val)) + if lineLen > longestLine { + longestLine = lineLen + } + } + + for _, item := range items { + curLen := 0 + for i, k := range item.Keys { + buf.WriteString(k.Token.Text) + 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) + 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++ { + buf.WriteByte(blank) + } + + for _, comment := range item.LineComment.List { + buf.WriteString(comment.Text) + } + } + + buf.WriteByte(newline) + } + + return buf.Bytes() +} + func (p *printer) literal(l *ast.LiteralType) []byte { return []byte(l.Token.Text) } @@ -84,7 +129,35 @@ func (p *printer) objectType(o *ast.ObjectType) []byte { buf.WriteString("{") buf.WriteByte(newline) - for _, item := range o.List.Items { + // check if we have adjacent one liner items. If yes we'll going to align + // the comments. + var oneLines []*ast.ObjectItem + for i, item := range o.List.Items { + // protect agains slice bounds + if i == len(o.List.Items)-1 { + break + } + + if o.List.Items[i+1].Pos().Line == item.Pos().Line+1 { + oneLines = append(oneLines, item) + } else { + // break in any nonadjacent items + break + } + } + + // fmt.Printf("len(oneLines) = %+v\n", len(oneLines)) + // for _, i := range oneLines { + // a := i.Keys[0] + // fmt.Printf("a = %+v\n", a) + // } + if len(oneLines) != 0 { + items := p.alignedItems(oneLines) + buf.Write(p.indent(items)) + buf.WriteByte(newline) + } + + for _, item := range o.List.Items[len(oneLines):] { buf.Write(p.indent(p.objectItem(item))) buf.WriteByte(newline) } diff --git a/printer/printer_test.go b/printer/printer_test.go index 185965f..7e2b132 100644 --- a/printer/printer_test.go +++ b/printer/printer_test.go @@ -24,9 +24,10 @@ type entry struct { // Use go test -update to create/update the respective golden files. var data = []entry{ - {"complexhcl.input", "complexhcl.golden"}, - {"list.input", "list.golden"}, - {"comment.input", "comment.golden"}, + // {"complexhcl.input", "complexhcl.golden"}, + // {"list.input", "list.golden"}, + // {"comment.input", "comment.golden"}, + {"comment_aligned.input", "comment_aligned.golden"}, } func TestFiles(t *testing.T) { diff --git a/printer/testdata/comment.golden b/printer/testdata/comment.golden index 6587adb..1d327f6 100644 --- a/printer/testdata/comment.golden +++ b/printer/testdata/comment.golden @@ -4,12 +4,6 @@ variable "foo" { description = "bar" # yooo } -aligned = { - a = "bar" # yoo1 - default = "bar" #yoo2 - bar = "bar" # yoo3 -} - // fatih arslan /* This is a developer test account and a multine comment */ @@ -26,4 +20,4 @@ variable = { // lead comment foo = { bar = "fatih" // line comment 2 -} // line comment 3 \ No newline at end of file +} // line comment 3 diff --git a/printer/testdata/comment.input b/printer/testdata/comment.input index 725017b..cff962e 100644 --- a/printer/testdata/comment.input +++ b/printer/testdata/comment.input @@ -4,12 +4,6 @@ variable "foo" { description = "bar" # yooo } -aligned { - a = "bar" # yoo1 - default = "bar" #yoo2 - bar = "bar" # yoo3 -} - // fatih arslan /* This is a developer test account and a multine comment */ diff --git a/printer/testdata/comment_aligned.golden b/printer/testdata/comment_aligned.golden new file mode 100644 index 0000000..ce72661 --- /dev/null +++ b/printer/testdata/comment_aligned.golden @@ -0,0 +1,12 @@ +aligned = { + a = "bar" # yoo1 + default = "bar" # yoo2 + bar = "bar" # yoo3 + fatih = ["fatih", "arslan"] // yoo4 + + deneme = { + bar = "fatih" + } + projects = ["vim-go"] # yoo5 + default = "foo" # yoo6 +} \ No newline at end of file diff --git a/printer/testdata/comment_aligned.input b/printer/testdata/comment_aligned.input new file mode 100644 index 0000000..4a27145 --- /dev/null +++ b/printer/testdata/comment_aligned.input @@ -0,0 +1,11 @@ +aligned { + a = "bar" # yoo1 + default = "bar" # yoo2 + bar = "bar" # yoo3 + fatih = ["fatih", "arslan"] // yoo4 + deneme = { + bar = "fatih" + } + projects = ["vim-go"] # yoo5 + default = "foo" # yoo6 +}