Hello. First off, I'm a big fan of the go/ast package. I've been playing with it for a year and its existence got me to try Go in the first place.
Now, I've been trying to do something very simple with it and it seems to me that it is *impossible* (I could be wrong). I want to ask/confirm if that's indeed the case, and what can be done.
Suppose I have the AST and a *token.FileSet of the following short Go program (acquired via go/parser.ParseFile):
package main
func main() {
println("ASTs")
println("are awesome")
}
I want to make a small modification to it (insert a newline), so that when printed with go/printer it looks like this:
The *ast.File is actually identical, the only difference is inside the *token.FileSet. However, I see no public API that would allow me to insert a newline. There's one API
http://godoc.org/go/token#File.MergeLine that lets you merge two lines.
Am I missing anything? Is the only way to make such a modification to the AST/FileSet pair by _printing_ it into text, changing the text, and then parsing it again?
Assuming that's correct, it seems the current go/ast and related things are absolutely fantastic for/at:
- parsing .go files into ast.File and token.FileSet.
- printing ast.File and token.FileSet into Go code (including formatting ala gofmt)
And not really good at:
- modifying ASTs (while preserving formatting and comments)
What can be done to address that last point, without losing the existing benefits outlined above?