printer: now aligned comments are working as expected
This commit is contained in:
parent
3a165313db
commit
b4756273da
@ -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
|
||||
}
|
||||
|
5
printer/testdata/comment_aligned.golden
vendored
5
printer/testdata/comment_aligned.golden
vendored
@ -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"
|
||||
|
4
printer/testdata/comment_aligned.input
vendored
4
printer/testdata/comment_aligned.input
vendored
@ -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"
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user