I'm working on a rewriter that uses go/ast
to add some things to a file. Everything is working well, except for the comments, which go/ast
is notoriously awful at. But part of the problem seems to be that it's actively conspiring against my efforts to fix it.
I have an insertion near the end of the file that is larger than what was left in the file. There are a couple other func
s after that point, and they have comments on them. I used a visitor to calculate the appropriate Pos
for my update and then update the remaining nodes with a new Pos
so that they wouldn't lose their association with their comments, but now the comments are getting moved to above my insertion!
After tracing through the execution, what I found is that func (s *FileSet) PositionFor
in position.go
is returning 0 on the position of those remaining comments, because their new Pos
is beyond the end of the associated token.File
data. Well, then it looks like I need to update the size
on that token.File
, but it's a lower-case field so I can't do that directly, and there doesn't appear to be any method that will do it for me. size
appears to be immutable, only set at construction time, which is odd because other parts of the token.File
structs can be updated.
How in the world do I resolve this? I just want my comments to print where they're supposed to be, but every time I think I've fixed the problem, there's some other problem lurking behind it, and now I run into an immutable problem!