printer: imropve alignment printing for standalone comments
This commit is contained in:
parent
07cb426729
commit
c9ef0afb41
@ -117,6 +117,7 @@ func (p *printer) output(n interface{}) []byte {
|
|||||||
fmt.Printf("[%d] OBJECTTYPE t.Pos() = %+v\n", count, t.Pos())
|
fmt.Printf("[%d] OBJECTTYPE t.Pos() = %+v\n", count, t.Pos())
|
||||||
if comment.Pos().After(p.prev.Pos()) && comment.Pos().Before(t.Pos()) {
|
if comment.Pos().After(p.prev.Pos()) && comment.Pos().Before(t.Pos()) {
|
||||||
buf.WriteString(comment.Text)
|
buf.WriteString(comment.Text)
|
||||||
|
// TODO(arslan): do not print new lines if the comments are one lines
|
||||||
buf.WriteByte(newline)
|
buf.WriteByte(newline)
|
||||||
buf.WriteByte(newline)
|
buf.WriteByte(newline)
|
||||||
}
|
}
|
||||||
@ -182,26 +183,37 @@ func (p *printer) objectType(o *ast.ObjectType) []byte {
|
|||||||
buf.WriteString("{")
|
buf.WriteString("{")
|
||||||
buf.WriteByte(newline)
|
buf.WriteByte(newline)
|
||||||
|
|
||||||
for _, c := range p.standaloneComments {
|
var index int
|
||||||
for _, comment := range c.List {
|
var nextItem token.Pos
|
||||||
fmt.Printf("[%d] OBJECTTYPE p.prev = %+v\n", count, p.prev.Pos())
|
for {
|
||||||
fmt.Printf("[%d] OBJECTTYPE comment.Pos() = %+v\n", count, comment.Pos())
|
for _, c := range p.standaloneComments {
|
||||||
fmt.Printf("[%d] OBJECTTYPE t.Pos() = %+v\n", count, o.Pos())
|
for _, comment := range c.List {
|
||||||
firstItem := o.List.Pos()
|
fmt.Printf("[%d] OBJECTTYPE p.prev = %+v\n", count, p.prev.Pos())
|
||||||
if comment.Pos().After(p.prev.Pos()) && comment.Pos().Before(firstItem) {
|
fmt.Printf("[%d] OBJECTTYPE comment.Pos() = %+v\n", count, comment.Pos())
|
||||||
buf.Write(p.indent([]byte(comment.Text))) // TODO(arslan): indent
|
|
||||||
buf.WriteByte(newline)
|
if index != len(o.List.Items) {
|
||||||
buf.WriteByte(newline)
|
nextItem = o.List.Items[index].Pos()
|
||||||
|
} else {
|
||||||
|
nextItem = o.Rbrace
|
||||||
|
|
||||||
|
}
|
||||||
|
fmt.Printf("[%d] OBJECTTYPE nextItem = %+v\n", count, nextItem)
|
||||||
|
if comment.Pos().After(p.prev.Pos()) && comment.Pos().Before(nextItem) {
|
||||||
|
buf.Write(p.indent([]byte(comment.Text))) // TODO(arslan): indent
|
||||||
|
buf.WriteByte(newline)
|
||||||
|
buf.WriteByte(newline)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
var index int
|
if index == len(o.List.Items) {
|
||||||
for {
|
break
|
||||||
|
}
|
||||||
|
|
||||||
// check if we have adjacent one liner items. If yes we'll going to align
|
// check if we have adjacent one liner items. If yes we'll going to align
|
||||||
// the comments.
|
// the comments.
|
||||||
var aligned []*ast.ObjectItem
|
var aligned []*ast.ObjectItem
|
||||||
for i, item := range o.List.Items[index:] {
|
for _, item := range o.List.Items[index:] {
|
||||||
// we don't group one line lists
|
// we don't group one line lists
|
||||||
if len(o.List.Items) == 1 {
|
if len(o.List.Items) == 1 {
|
||||||
break
|
break
|
||||||
@ -215,31 +227,48 @@ func (p *printer) objectType(o *ast.ObjectType) []byte {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
next := 0
|
curPos := item.Pos()
|
||||||
|
|
||||||
|
nextPos := token.Pos{}
|
||||||
if index != len(o.List.Items)-1 {
|
if index != len(o.List.Items)-1 {
|
||||||
next = lines(string(p.objectItem(o.List.Items[index+1])))
|
nextPos = o.List.Items[index+1].Pos()
|
||||||
}
|
}
|
||||||
|
|
||||||
prev := 0
|
prevPos := token.Pos{}
|
||||||
if index != 0 {
|
if index != 0 {
|
||||||
prev = lines(string(p.objectItem(o.List.Items[index-1])))
|
prevPos = o.List.Items[index-1].Pos()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cur == next && next == 1) || (next == 1 && cur == 2 && i == 0) {
|
// fmt.Println("DEBUG ----------------")
|
||||||
|
// fmt.Printf("prev = %+v prevPos: %s\n", prev, prevPos)
|
||||||
|
// fmt.Printf("cur = %+v curPos: %s\n", cur, curPos)
|
||||||
|
// fmt.Printf("next = %+v nextPos: %s\n", next, nextPos)
|
||||||
|
|
||||||
|
if curPos.Line+1 == nextPos.Line {
|
||||||
aligned = append(aligned, item)
|
aligned = append(aligned, item)
|
||||||
index++
|
index++
|
||||||
} else if (cur == prev && prev == 1) || (prev == 2 && cur == 1) {
|
continue
|
||||||
aligned = append(aligned, item)
|
|
||||||
index++
|
|
||||||
} else {
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if curPos.Line-1 == prevPos.Line {
|
||||||
|
aligned = append(aligned, item)
|
||||||
|
index++
|
||||||
|
|
||||||
|
// finish if we have a new line or comment next. This happens
|
||||||
|
// if the next item is not adjacent
|
||||||
|
if curPos.Line+1 != nextPos.Line {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
// fmt.Printf("==================> len(aligned) = %+v\n", len(aligned))
|
fmt.Printf("==================> len(aligned) = %+v\n", len(aligned))
|
||||||
// for _, b := range aligned {
|
for _, b := range aligned {
|
||||||
// fmt.Printf("b = %+v\n", b)
|
fmt.Printf("b = %+v\n", b)
|
||||||
// }
|
}
|
||||||
|
|
||||||
// put newlines if the items are between other non aligned items
|
// put newlines if the items are between other non aligned items
|
||||||
if index != len(aligned) {
|
if index != len(aligned) {
|
||||||
@ -260,10 +289,6 @@ func (p *printer) objectType(o *ast.ObjectType) []byte {
|
|||||||
|
|
||||||
buf.WriteByte(newline)
|
buf.WriteByte(newline)
|
||||||
|
|
||||||
if index == len(o.List.Items) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
buf.WriteString("}")
|
buf.WriteString("}")
|
||||||
|
Loading…
Reference in New Issue
Block a user