printer: support lead comment on aligned items

This commit is contained in:
Fatih Arslan 2015-10-30 21:49:10 +03:00
parent b4756273da
commit 877d63151c
5 changed files with 68 additions and 49 deletions

View File

@ -86,7 +86,14 @@ func (p *printer) alignedItems(items []*ast.ObjectItem) []byte {
} }
} }
for _, item := range items { for i, item := range items {
if item.LeadComment != nil {
for _, comment := range item.LeadComment.List {
buf.WriteString(comment.Text)
buf.WriteByte(newline)
}
}
curLen := 0 curLen := 0
for i, k := range item.Keys { for i, k := range item.Keys {
buf.WriteString(k.Token.Text) buf.WriteString(k.Token.Text)
@ -114,7 +121,10 @@ func (p *printer) alignedItems(items []*ast.ObjectItem) []byte {
} }
} }
buf.WriteByte(newline) // do not print for the last item
if i != len(items)-1 {
buf.WriteByte(newline)
}
} }
return buf.Bytes() return buf.Bytes()
@ -133,21 +143,24 @@ func (p *printer) objectType(o *ast.ObjectType) []byte {
for { for {
// 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 oneLines []*ast.ObjectItem var aligned []*ast.ObjectItem
for _, item := range o.List.Items[index:] { for i, 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
} }
// one means a oneliner with out any lead comment
// two means a oneliner with lead comment
// anything else might be something else
cur := lines(string(p.objectItem(item))) cur := lines(string(p.objectItem(item)))
if cur != 1 { if cur > 2 {
break break
} }
next := 0 next := 0
if index != len(o.List.Items)-1 { if index != len(o.List.Items)-1 {
next = lines(string(p.objectItem(o.List.Items[index+0]))) next = lines(string(p.objectItem(o.List.Items[index+1])))
} }
prev := 0 prev := 0
@ -155,37 +168,42 @@ func (p *printer) objectType(o *ast.ObjectType) []byte {
prev = lines(string(p.objectItem(o.List.Items[index-1]))) prev = lines(string(p.objectItem(o.List.Items[index-1])))
} }
if cur == next { if (cur == next && next == 1) || (next == 1 && cur == 2 && i == 0) {
oneLines = append(oneLines, item) aligned = append(aligned, item)
index++ index++
} else if cur == prev { } else if (cur == prev && prev == 1) || (prev == 2 && cur == 1) {
oneLines = append(oneLines, item) aligned = append(aligned, item)
index++ index++
} else { } else {
break break
} }
} }
if len(oneLines) != 0 { // fmt.Printf("==================> len(aligned) = %+v\n", len(aligned))
items := p.alignedItems(oneLines) // for _, b := range aligned {
// 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(oneLines) { if index != len(aligned) {
buf.WriteByte(newline) buf.WriteByte(newline)
}
buf.Write(p.indent(items))
if index != len(o.List.Items) && len(oneLines) > 1 {
buf.WriteByte(newline)
}
} }
if len(aligned) >= 1 {
items := p.alignedItems(aligned)
buf.Write(p.indent(items))
} else {
buf.Write(p.indent(p.objectItem(o.List.Items[index])))
index++
}
buf.WriteByte(newline)
if index == len(o.List.Items) { if index == len(o.List.Items) {
break break
} }
buf.Write(p.indent(p.objectItem(o.List.Items[index])))
buf.WriteByte(newline)
index++
} }
buf.WriteString("}") buf.WriteString("}")
@ -244,17 +262,11 @@ func (p *printer) indent(buf []byte) []byte {
} }
func lines(txt string) int { func lines(txt string) int {
endline := 0 endline := 1
for i := 0; i < len(txt); i++ { for i := 0; i < len(txt); i++ {
if txt[i] == '\n' { if txt[i] == '\n' {
endline++ endline++
} }
} }
// some txt do not have any kinde of newlines, treat them also as a one
// liner
if endline == 0 {
endline = 1
}
return endline return endline
} }

View File

@ -19,7 +19,7 @@ variable = {
// lead comment // lead comment
foo { foo {
bar = "fatih" // line comment 2 bar = "fatih" // line comment 2
} // line comment 3 } // line comment 3

View File

@ -1,17 +1,20 @@
aligned = { aligned = {
a = "bar" # yoo1 # We have some aligned items below
default = "bar" # yoo2 foo = "bar" # yoo1
bar = "bar" # yoo3 default = "bar" # yoo2
fatih = ["fatih", "arslan"] // yoo4 bar = "bar" # yoo3
deneme = { default = {
bar = "fatih" bar = "example"
} }
bar = "bar" # yoo5 #deneme arslan
fatih = ["fatih", "arslan"] // yoo6 fatih = ["fatih"] # yoo4
deneme = { #fatih arslan
bar = "fatih" fatiharslan = ["arslan"] // yoo5
default = {
bar = "example"
} }
} }

View File

@ -1,14 +1,16 @@
aligned { aligned {
a = "bar" # yoo1 # We have some aligned items below
foo = "bar" # yoo1
default = "bar" # yoo2 default = "bar" # yoo2
bar = "bar" # yoo3 bar = "bar" # yoo3
fatih = ["fatih", "arslan"] // yoo4 default = {
deneme = { bar = "example"
bar = "fatih"
} }
bar = "bar" # yoo5 #deneme arslan
fatih = ["fatih", "arslan"] // yoo6 fatih = ["fatih"] # yoo4
deneme = { #fatih arslan
bar = "fatih" fatiharslan = ["arslan"] // yoo5
default = {
bar = "example"
} }
} }

View File

@ -20,10 +20,12 @@ resource "aws_security_group" "firewall" {
resource aws_instance "web" { resource aws_instance "web" {
ami = "${var.foo}" ami = "${var.foo}"
security_groups = [ security_groups = [
"foo", "foo",
"${aws_security_group.firewall.foo}", "${aws_security_group.firewall.foo}",
] ]
network_interface = { network_interface = {
device_index = 0 device_index = 0
description = "Main network interface" description = "Main network interface"