-
Notifications
You must be signed in to change notification settings - Fork 17.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
go/ast: doc insufficient for inserting comments #18593
Comments
@willfaught It's possible but it's complicated. In fact it's a major reason for an eventual rewrite of the AST (a more modern and simplified version is already in use in cmd/compile/internal/syntax). Not sure this helps a lot for the moment but there are two mechanisms in place:
Thus, if you are inserting comments, you need to insert them in the right place in the general comment list in the ast.File.Comments list, and you need to give them the correct position relative to the nodes where they are inserted. Unfortunately that's a bit of a trial and error process. (go/ast is one of the very earliest Go packages in existence, and the design decision made for handling comments - while it works for gofmt - is unfortunate when it comes to manipulating comments. Mea culpa.) |
Related: #14629 |
My use-case is automatically generating an AST from scratch, so there are no existing positions to work with. I found this useful StackOverflow comment which demonstrates putting comments in an existing ast (which looks rather complicated!), but no straightforward way to satisfy my need. I guess I would have to either figure out how to synthesise positions for all statements in the |
@pwaller A token.Pos is basically an offset from the beginning of the file (+ some base). A comment comes before some other token, if its pos (its offset) is less than the pos of that token. In the simplest form, you could just give tokens increasing positions (increased by say 10 each time), and provide positions between the tokens for whatever comment you may have. That should comments in the right places between tokens (but maybe not on the right lines yet). For the right lines, you also need to register lines with token.File.AddLine (or in one fell swoop with SetLines). This should get you off the ground. Again, there's no easy way at the moment to make this work. |
CL https://golang.org/cl/46370 mentions this issue. |
I don't see a way to insert documentation comments for file declarations such that they appear just above their decls after printing with go/printer.Fprint or go/format.Node (or both, as explained below). Is it impossible? If so, it should be noted in the doc; if not, it should be noted how to do it, since a straightforward mutation of the comment fields and/or creation/mutation/saving of a CommentMap for File.Comments doesn't work. Also note the related problems pointed out in the code below.
My scenario is writing a tool that inserts
// TODO
doc comments for undocumented exported decls.The text was updated successfully, but these errors were encountered: