printer: simplify standalone collecting
This commit is contained in:
parent
629539558b
commit
792e0fef49
@ -21,24 +21,14 @@ type printer struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *printer) collectComments(node ast.Node) {
|
func (p *printer) collectComments(node ast.Node) {
|
||||||
leadComments := make([]*ast.CommentGroup, 0)
|
// first collect all comments. This is already stored in
|
||||||
lineComments := make([]*ast.CommentGroup, 0)
|
// ast.File.(comments)
|
||||||
|
|
||||||
ast.Walk(node, func(nn ast.Node) bool {
|
ast.Walk(node, func(nn ast.Node) bool {
|
||||||
switch t := nn.(type) {
|
switch t := nn.(type) {
|
||||||
case *ast.File:
|
case *ast.File:
|
||||||
// will happen only once
|
|
||||||
p.comments = t.Comments
|
p.comments = t.Comments
|
||||||
case *ast.ObjectItem:
|
return false
|
||||||
if t.LeadComment != nil {
|
|
||||||
leadComments = append(leadComments, t.LeadComment)
|
|
||||||
}
|
|
||||||
|
|
||||||
if t.LineComment != nil {
|
|
||||||
lineComments = append(lineComments, t.LineComment)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -46,30 +36,39 @@ func (p *printer) collectComments(node ast.Node) {
|
|||||||
for _, c := range p.comments {
|
for _, c := range p.comments {
|
||||||
standaloneComments[c.Pos()] = c
|
standaloneComments[c.Pos()] = c
|
||||||
}
|
}
|
||||||
for _, lead := range leadComments {
|
|
||||||
for _, comment := range lead.List {
|
|
||||||
if _, ok := standaloneComments[comment.Pos()]; ok {
|
|
||||||
delete(standaloneComments, comment.Pos())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, line := range lineComments {
|
// next remove all lead and line comments from the overall comment map.
|
||||||
for _, comment := range line.List {
|
// This will give us comments which are standalone, comments which are not
|
||||||
if _, ok := standaloneComments[comment.Pos()]; ok {
|
// assigned to any kind of node.
|
||||||
delete(standaloneComments, comment.Pos())
|
ast.Walk(node, func(nn ast.Node) bool {
|
||||||
|
switch t := nn.(type) {
|
||||||
|
case *ast.ObjectItem:
|
||||||
|
if t.LeadComment != nil {
|
||||||
|
for _, comment := range t.LeadComment.List {
|
||||||
|
if _, ok := standaloneComments[comment.Pos()]; ok {
|
||||||
|
delete(standaloneComments, comment.Pos())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if t.LineComment != nil {
|
||||||
|
for _, comment := range t.LineComment.List {
|
||||||
|
if _, ok := standaloneComments[comment.Pos()]; ok {
|
||||||
|
delete(standaloneComments, comment.Pos())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
for _, c := range standaloneComments {
|
for _, c := range standaloneComments {
|
||||||
|
for _, comment := range c.List {
|
||||||
|
fmt.Printf("comment = %+v\n", comment)
|
||||||
|
}
|
||||||
p.standaloneComments = append(p.standaloneComments, c)
|
p.standaloneComments = append(p.standaloneComments, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("All comments len = %+v\n", len(p.comments))
|
|
||||||
fmt.Printf("Lead commetns = %+v\n", len(leadComments))
|
|
||||||
fmt.Printf("len(lineComments) = %+v\n", len(lineComments))
|
|
||||||
fmt.Printf("StandAlone Comments = %+v\n", len(p.standaloneComments))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// output prints creates a printable HCL output and returns it.
|
// output prints creates a printable HCL output and returns it.
|
||||||
|
Loading…
Reference in New Issue
Block a user