From b4756273daed51c066000648409d75be6b76f5df Mon Sep 17 00:00:00 2001 From: Fatih Arslan Date: Wed, 28 Oct 2015 02:40:51 +0300 Subject: [PATCH] printer: now aligned comments are working as expected --- printer/nodes.go | 51 +++++++++++++++++-------- printer/testdata/comment_aligned.golden | 5 ++- printer/testdata/comment_aligned.input | 4 +- 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/printer/nodes.go b/printer/nodes.go index 92a7c4c..2fcd6be 100644 --- a/printer/nodes.go +++ b/printer/nodes.go @@ -129,39 +129,52 @@ func (p *printer) objectType(o *ast.ObjectType) []byte { buf.WriteString("{") buf.WriteByte(newline) - // check if we have adjacent one liner items. If yes we'll going to align - // the comments. var index int for { + // check if we have adjacent one liner items. If yes we'll going to align + // the comments. 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++ - } + // we don't group one line lists + if len(o.List.Items) == 1 { break } - if o.List.Items[1+index].Pos().Line == item.Pos().Line+1 { + cur := lines(string(p.objectItem(item))) + if cur != 1 { + break + } + + next := 0 + if index != len(o.List.Items)-1 { + next = lines(string(p.objectItem(o.List.Items[index+0]))) + } + + prev := 0 + if index != 0 { + prev = lines(string(p.objectItem(o.List.Items[index-1]))) + } + + if cur == next { + oneLines = append(oneLines, item) + index++ + } else if cur == prev { oneLines = append(oneLines, item) index++ } else { - // break in any nonadjacent items break } } if len(oneLines) != 0 { items := p.alignedItems(oneLines) + + // put newlines if the items are between other non aligned items + if index != len(oneLines) { + buf.WriteByte(newline) + } buf.Write(p.indent(items)) - if index != len(o.List.Items) { + if index != len(o.List.Items) && len(oneLines) > 1 { buf.WriteByte(newline) } } @@ -237,5 +250,11 @@ func lines(txt string) int { endline++ } } + + // some txt do not have any kinde of newlines, treat them also as a one + // liner + if endline == 0 { + endline = 1 + } return endline } diff --git a/printer/testdata/comment_aligned.golden b/printer/testdata/comment_aligned.golden index 60347c3..16f90c8 100644 --- a/printer/testdata/comment_aligned.golden +++ b/printer/testdata/comment_aligned.golden @@ -7,8 +7,9 @@ aligned = { deneme = { bar = "fatih" } - bar = "bar" # yoo3 - fatih = ["fatih", "arslan"] // yoo4 + + bar = "bar" # yoo5 + fatih = ["fatih", "arslan"] // yoo6 deneme = { bar = "fatih" diff --git a/printer/testdata/comment_aligned.input b/printer/testdata/comment_aligned.input index 87c330a..477aa7b 100644 --- a/printer/testdata/comment_aligned.input +++ b/printer/testdata/comment_aligned.input @@ -6,8 +6,8 @@ aligned { deneme = { bar = "fatih" } - bar = "bar" # yoo3 - fatih = ["fatih", "arslan"] // yoo4 + bar = "bar" # yoo5 + fatih = ["fatih", "arslan"] // yoo6 deneme = { bar = "fatih" }