From a16955dcadf9d9af6c74845b44e2dae0f04bc060 Mon Sep 17 00:00:00 2001 From: Fatih Arslan Date: Wed, 28 Oct 2015 00:59:54 +0300 Subject: [PATCH] printer: partially fixed aligned comments, still WIP --- printer/nodes.go | 81 +++++++++++++++++-------- printer/testdata/comment_aligned.golden | 10 ++- printer/testdata/comment_aligned.input | 3 + 3 files changed, 67 insertions(+), 27 deletions(-) diff --git a/printer/nodes.go b/printer/nodes.go index 34ba4b8..9e753e2 100644 --- a/printer/nodes.go +++ b/printer/nodes.go @@ -131,38 +131,59 @@ func (p *printer) objectType(o *ast.ObjectType) []byte { // 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 { + var index int + for { + var oneLines []*ast.ObjectItem + for _, item := range o.List.Items[index:] { + // protect agains slice bounds + if index == len(o.List.Items)-1 { + // check for the latest item of a series of one liners in the + // end of a list. + if index != 0 && // do not check if the list length is one + lines(string(p.objectItem(item))) < 1 && // be sure it's really a one line + o.List.Items[index-1].Pos().Line == item.Pos().Line-1 { + + oneLines = append(oneLines, item) + index++ + } + break + } + + if o.List.Items[1+index].Pos().Line == item.Pos().Line+1 { + oneLines = append(oneLines, item) + index++ + } 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)) + + if index != len(o.List.Items) { + buf.WriteByte(newline) + } + } + + if index == len(o.List.Items) { 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.Write(p.indent(p.objectItem(o.List.Items[index]))) buf.WriteByte(newline) + index++ } buf.WriteString("}") + buf.WriteByte(newline) return buf.Bytes() } @@ -216,3 +237,13 @@ func (p *printer) indent(buf []byte) []byte { } return res } + +func lines(txt string) int { + endline := 0 + for i := 0; i < len(txt); i++ { + if txt[i] == '\n' { + endline++ + } + } + return endline +} diff --git a/printer/testdata/comment_aligned.golden b/printer/testdata/comment_aligned.golden index ce72661..bab08b0 100644 --- a/printer/testdata/comment_aligned.golden +++ b/printer/testdata/comment_aligned.golden @@ -7,6 +7,12 @@ aligned = { deneme = { bar = "fatih" } + projects = ["vim-go"] # yoo5 - default = "foo" # yoo6 -} \ No newline at end of file + default = "foo" # yoo6 + + example = { + bar = "fatih" + } + +} diff --git a/printer/testdata/comment_aligned.input b/printer/testdata/comment_aligned.input index 4a27145..1b98b48 100644 --- a/printer/testdata/comment_aligned.input +++ b/printer/testdata/comment_aligned.input @@ -8,4 +8,7 @@ aligned { } projects = ["vim-go"] # yoo5 default = "foo" # yoo6 + example = { + bar = "fatih" + } }