15 // NewEmptyFile constructs a new file with no content, ready to be mutated
16 // by other calls that append to its body.
17 func NewEmptyFile() *File {
22 f.body = f.children.Append(body)
26 // Body returns the root body of the file, which contains the top-level
27 // attributes and blocks.
28 func (f *File) Body() *Body {
29 return f.body.content.(*Body)
32 // WriteTo writes the tokens underlying the receiving file to the given writer.
34 // The tokens first have a simple formatting pass applied that adjusts only
35 // the spaces between them.
36 func (f *File) WriteTo(wr io.Writer) (int64, error) {
37 tokens := f.inTree.children.BuildTokens(nil)
39 return tokens.WriteTo(wr)
42 // Bytes returns a buffer containing the source code resulting from the
43 // tokens underlying the receiving file. If any updates have been made via
44 // the AST API, these will be reflected in the result.
45 func (f *File) Bytes() []byte {
46 buf := &bytes.Buffer{}
51 type comments struct {
58 func newComments(tokens Tokens) *comments {
64 func (c *comments) BuildTokens(to Tokens) Tokens {
65 return c.tokens.BuildTokens(to)
68 type identifier struct {
75 func newIdentifier(token *Token) *identifier {
81 func (i *identifier) BuildTokens(to Tokens) Tokens {
82 return append(to, i.token)
85 func (i *identifier) hasName(name string) bool {
86 return name == string(i.token.Bytes)
96 func newNumber(token *Token) *number {
102 func (n *number) BuildTokens(to Tokens) Tokens {
103 return append(to, n.token)
113 func newQuoted(tokens Tokens) *quoted {
119 func (q *quoted) BuildTokens(to Tokens) Tokens {
120 return q.tokens.BuildTokens(to)