Skip to content
This repository has been archived by the owner on Oct 11, 2018. It is now read-only.

Commit

Permalink
printer: implement standalone comments, still WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
fatih committed Oct 31, 2015
1 parent 792e0fe commit 9b50830
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 68 deletions.
14 changes: 14 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ type Node interface {
Pos() token.Pos
}

// NewNode returns a non usable Node interface implementer. The position is
// initalizied to zero.
func NewNode() Node {
return &zero{}
}

func (File) node() {}
func (ObjectList) node() {}
func (ObjectKey) node() {}
Expand All @@ -21,6 +27,14 @@ func (ObjectType) node() {}
func (LiteralType) node() {}
func (ListType) node() {}

type zero struct{}

func (zero) node() {}

func (z *zero) Pos() token.Pos {
return token.Pos{}
}

// File represents a single HCL file
type File struct {
Node Node // usually a *ObjectList
Expand Down
220 changes: 153 additions & 67 deletions printer/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package printer
import (
"bytes"
"fmt"
"sort"

"github.com/fatih/hcl/ast"
"github.com/fatih/hcl/token"
Expand All @@ -15,11 +16,22 @@ const (
)

type printer struct {
cfg Config
cfg Config
prev ast.Node

comments []*ast.CommentGroup // may be nil, contains all comments
standaloneComments []*ast.CommentGroup // contains all standalone comments (not assigned to any node)

enableTrace bool
indentTrace int
}

type ByPosition []*ast.CommentGroup

func (b ByPosition) Len() int { return len(b) }
func (b ByPosition) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b ByPosition) Less(i, j int) bool { return b[i].Pos().Before(b[j].Pos()) }

func (p *printer) collectComments(node ast.Node) {
// first collect all comments. This is already stored in
// ast.File.(comments)
Expand Down Expand Up @@ -64,35 +76,54 @@ func (p *printer) collectComments(node ast.Node) {
})

for _, c := range standaloneComments {
p.standaloneComments = append(p.standaloneComments, c)
}
sort.Sort(ByPosition(p.standaloneComments))

fmt.Printf("standaloneComments = %+v\n", len(p.standaloneComments))
for _, c := range p.standaloneComments {
for _, comment := range c.List {
fmt.Printf("comment = %+v\n", comment)
}
p.standaloneComments = append(p.standaloneComments, c)
}

}

// output prints creates a printable HCL output and returns it.
var count int

// output prints creates b printable HCL output and returns it.
func (p *printer) output(n interface{}) []byte {
var buf bytes.Buffer
count++

switch t := n.(type) {
case *ast.File:
// for i, group := range t.Comments {
// for _, comment := range group.List {
// fmt.Printf("[%d] comment = %+v\n", i, comment)
// }
// }
return p.output(t.Node)
case *ast.ObjectList:
for i, item := range t.Items {
buf.Write(p.objectItem(item))
fmt.Printf("[%d] item: %s\n", i, item.Keys[0].Token.Text)
buf.Write(p.output(item))
if i != len(t.Items)-1 {
buf.Write([]byte{newline, newline})
}
}
case *ast.ObjectKey:
buf.WriteString(t.Token.Text)
case *ast.ObjectItem:
for _, c := range p.standaloneComments {
for _, comment := range c.List {
fmt.Printf("[%d] OBJECTITEM p.prev = %+v\n", count, p.prev.Pos())
fmt.Printf("[%d] OBJECTITEM comment.Pos() = %+v\n", count, comment.Pos())
fmt.Printf("[%d] OBJECTTYPE t.Pos() = %+v\n", count, t.Pos())
if comment.Pos().After(p.prev.Pos()) && comment.Pos().Before(t.Pos()) {
buf.WriteString(comment.Text)
buf.WriteByte(newline)
buf.WriteByte(newline)
}
}
}

p.prev = t
buf.Write(p.objectItem(t))
case *ast.LiteralType:
buf.WriteString(t.Token.Text)
Expand All @@ -104,10 +135,15 @@ func (p *printer) output(n interface{}) []byte {
fmt.Printf(" unknown type: %T\n", n)
}

// if item, ok := n.(ast.Node); ok {
// p.prev = item
// }

return buf.Bytes()
}

func (p *printer) objectItem(o *ast.ObjectItem) []byte {
defer un(trace(p, fmt.Sprintf("ObjectItem: %s", o.Keys[0].Token.Text)))
var buf bytes.Buffer

if o.LeadComment != nil {
Expand Down Expand Up @@ -140,70 +176,26 @@ func (p *printer) objectItem(o *ast.ObjectItem) []byte {
return buf.Bytes()
}

func (p *printer) alignedItems(items []*ast.ObjectItem) []byte {
func (p *printer) objectType(o *ast.ObjectType) []byte {
defer un(trace(p, "ObjectType"))
var buf bytes.Buffer
buf.WriteString("{")
buf.WriteByte(newline)

var longestLine int
for _, item := range items {
lineLen := len(item.Keys[0].Token.Text) + len(p.output(item.Val))
if lineLen > longestLine {
longestLine = lineLen
}
}

for i, item := range items {
if item.LeadComment != nil {
for _, comment := range item.LeadComment.List {
buf.WriteString(comment.Text)
for _, c := range p.standaloneComments {
for _, comment := range c.List {
fmt.Printf("[%d] OBJECTTYPE p.prev = %+v\n", count, p.prev.Pos())
fmt.Printf("[%d] OBJECTTYPE comment.Pos() = %+v\n", count, comment.Pos())
fmt.Printf("[%d] OBJECTTYPE t.Pos() = %+v\n", count, o.Pos())
firstItem := o.List.Pos()
if comment.Pos().After(p.prev.Pos()) && comment.Pos().Before(firstItem) {
buf.Write(p.indent([]byte(comment.Text))) // TODO(arslan): indent
buf.WriteByte(newline)
buf.WriteByte(newline)
}
}

curLen := 0
for i, k := range item.Keys {
buf.WriteString(k.Token.Text)
buf.WriteByte(blank)

// reach end of key
if i == len(item.Keys)-1 && len(item.Keys) == 1 {
buf.WriteString("=")
buf.WriteByte(blank)
}

curLen = len(k.Token.Text) // two blanks and one assign
}
val := p.output(item.Val)
buf.Write(val)
curLen += len(val)

if item.Val.Pos().Line == item.Keys[0].Pos().Line && item.LineComment != nil {
for i := 0; i < longestLine-curLen+1; i++ {
buf.WriteByte(blank)
}

for _, comment := range item.LineComment.List {
buf.WriteString(comment.Text)
}
}

// do not print for the last item
if i != len(items)-1 {
buf.WriteByte(newline)
}
}

return buf.Bytes()
}

func (p *printer) literal(l *ast.LiteralType) []byte {
return []byte(l.Token.Text)
}

func (p *printer) objectType(o *ast.ObjectType) []byte {
var buf bytes.Buffer
buf.WriteString("{")
buf.WriteByte(newline)

var index int
for {
// check if we have adjacent one liner items. If yes we'll going to align
Expand Down Expand Up @@ -255,10 +247,13 @@ func (p *printer) objectType(o *ast.ObjectType) []byte {
}

if len(aligned) >= 1 {
items := p.alignedItems(aligned)
p.prev = aligned[len(aligned)-1]

items := p.alignedItems(aligned)
buf.Write(p.indent(items))
} else {
p.prev = o.List.Items[index]

buf.Write(p.indent(p.objectItem(o.List.Items[index])))
index++
}
Expand All @@ -275,6 +270,65 @@ func (p *printer) objectType(o *ast.ObjectType) []byte {
return buf.Bytes()
}

func (p *printer) alignedItems(items []*ast.ObjectItem) []byte {
var buf bytes.Buffer

var longestLine int
for _, item := range items {
lineLen := len(item.Keys[0].Token.Text) + len(p.output(item.Val))
if lineLen > longestLine {
longestLine = lineLen
}
}

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)
buf.WriteByte(blank)

// reach end of key
if i == len(item.Keys)-1 && len(item.Keys) == 1 {
buf.WriteString("=")
buf.WriteByte(blank)
}

curLen = len(k.Token.Text) // two blanks and one assign
}
val := p.output(item.Val)
buf.Write(val)
curLen += len(val)

if item.Val.Pos().Line == item.Keys[0].Pos().Line && item.LineComment != nil {
for i := 0; i < longestLine-curLen+1; i++ {
buf.WriteByte(blank)
}

for _, comment := range item.LineComment.List {
buf.WriteString(comment.Text)
}
}

// do not print for the last item
if i != len(items)-1 {
buf.WriteByte(newline)
}
}

return buf.Bytes()
}

func (p *printer) literal(l *ast.LiteralType) []byte {
return []byte(l.Token.Text)
}

// printList prints a HCL list
func (p *printer) list(l *ast.ListType) []byte {
var buf bytes.Buffer
Expand Down Expand Up @@ -335,3 +389,35 @@ func lines(txt string) int {
}
return endline
}

// ----------------------------------------------------------------------------
// Tracing support

func (p *printer) printTrace(a ...interface{}) {
if !p.enableTrace {
return
}

const dots = ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . "
const n = len(dots)
i := 2 * p.indentTrace
for i > n {
fmt.Print(dots)
i -= n
}
// i <= n
fmt.Print(dots[0:i])
fmt.Println(a...)
}

func trace(p *printer, msg string) *printer {
p.printTrace(msg, "(")
p.indentTrace++
return p
}

// Usage pattern: defer un(trace(p, "..."))
func un(p *printer) {
p.indentTrace--
p.printTrace(")")
}
2 changes: 2 additions & 0 deletions printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func (c *Config) Fprint(output io.Writer, node ast.Node) error {
cfg: *c,
comments: make([]*ast.CommentGroup, 0),
standaloneComments: make([]*ast.CommentGroup, 0),
prev: ast.NewNode(),
// enableTrace: true,
}

p.collectComments(node)
Expand Down
13 changes: 13 additions & 0 deletions printer/testdata/comment.golden
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
// A standalone comment is a comment which is not attached to any kind of node

// This comes from Terraform, as a test
variable "foo" {
# Standalone comment should be still here

default = "bar"
description = "bar" # yooo
}

/* This is a multi line standalone
comment*/

// fatih arslan
/* This is a developer test
account and a multine comment */
Expand All @@ -15,6 +22,12 @@ numbers = [1, 2] // another line here
# Another comment
variable = {
description = "bar" # another yooo

foo = {
# Nested standalone

bar = "fatih"
}
}

// lead comment
Expand Down
Loading

0 comments on commit 9b50830

Please sign in to comment.