-
Notifications
You must be signed in to change notification settings - Fork 204
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
Expose paren Position on DefStmt #504
Conversation
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
b71494f
to
142a13b
Compare
What's the motivation for this change? The syntax tree doesn't expose the positions of most tokens, as they are redundant given the formatting algorithm. Why are these token positions necessary, but not all the other ones? |
The L/R paren are generally exposed (see Dict, List, Tuple) |
That's not really an accurate characterization of the current design. The syntax tree records positions only for tokens that are required to accurately compute the span (start and end positions) of the tree, as in your examples of Dict, List, and Tuple. It also records the positions of interior tokens that are semantically important, such as the positions of
For what decisions does your formatter need this information? |
I would argue that the position of
I agree with you for the
We gonna go a bit deep here: Consider the following pieces of code:
When parsing that code, the |
Thanks for illuminating. I think you are pointing out a general deficiency of the current syntax tree, which is that it doesn't contain sufficient information to preserve the relative order of tokens and comments. (Go's go/ast has a similar problem, notoriously; see golang/go#20744). If we fixed it in this instance, I suspect you would still encounter similar problems elsewhere in the tree. How many other places would we need to record token positions in order to preserve token/comment order in all "reasonable" cases? (An unreasonable comment would be one before a comma, say.) |
Understood. |
But I would say that fixing those deficiencies would be pretty straightforward: add to |
Alright, let's add these two tokens. I noticed that the most popular Python formatters appear to preserve the position of comments after the last parameter. |
Is there any reason for not using Buildifier? |
Mostly wanted single tool, also it was pretty fun to write (except the part where the comments end up in random places) |
No description provided.