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

View File

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

View File

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

View File

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