printer: partially fixed aligned comments, still WIP

This commit is contained in:
Fatih Arslan 2015-10-28 00:59:54 +03:00
parent b93aefc3c3
commit a16955dcad
3 changed files with 67 additions and 27 deletions

View File

@ -131,15 +131,27 @@ func (p *printer) objectType(o *ast.ObjectType) []byte {
// 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 index int
for {
var oneLines []*ast.ObjectItem var oneLines []*ast.ObjectItem
for i, item := range o.List.Items { for _, item := range o.List.Items[index:] {
// protect agains slice bounds // protect agains slice bounds
if i == len(o.List.Items)-1 { 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 break
} }
if o.List.Items[i+1].Pos().Line == item.Pos().Line+1 { if o.List.Items[1+index].Pos().Line == item.Pos().Line+1 {
oneLines = append(oneLines, item) oneLines = append(oneLines, item)
index++
} else { } else {
// break in any nonadjacent items // break in any nonadjacent items
break break
@ -151,18 +163,27 @@ func (p *printer) objectType(o *ast.ObjectType) []byte {
// a := i.Keys[0] // a := i.Keys[0]
// fmt.Printf("a = %+v\n", a) // fmt.Printf("a = %+v\n", a)
// } // }
if len(oneLines) != 0 { if len(oneLines) != 0 {
items := p.alignedItems(oneLines) items := p.alignedItems(oneLines)
buf.Write(p.indent(items)) buf.Write(p.indent(items))
if index != len(o.List.Items) {
buf.WriteByte(newline) buf.WriteByte(newline)
} }
}
for _, item := range o.List.Items[len(oneLines):] { if index == len(o.List.Items) {
buf.Write(p.indent(p.objectItem(item))) break
}
buf.Write(p.indent(p.objectItem(o.List.Items[index])))
buf.WriteByte(newline) buf.WriteByte(newline)
index++
} }
buf.WriteString("}") buf.WriteString("}")
buf.WriteByte(newline)
return buf.Bytes() return buf.Bytes()
} }
@ -216,3 +237,13 @@ func (p *printer) indent(buf []byte) []byte {
} }
return res return res
} }
func lines(txt string) int {
endline := 0
for i := 0; i < len(txt); i++ {
if txt[i] == '\n' {
endline++
}
}
return endline
}

View File

@ -7,6 +7,12 @@ aligned = {
deneme = { deneme = {
bar = "fatih" bar = "fatih"
} }
projects = ["vim-go"] # yoo5 projects = ["vim-go"] # yoo5
default = "foo" # yoo6 default = "foo" # yoo6
example = {
bar = "fatih"
}
} }

View File

@ -8,4 +8,7 @@ aligned {
} }
projects = ["vim-go"] # yoo5 projects = ["vim-go"] # yoo5
default = "foo" # yoo6 default = "foo" # yoo6
example = {
bar = "fatih"
}
} }