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
* Changed Comment::Associator#associate API and way of associating comments #188
Conversation
No, unfortunately this is not a solution. The proper solution would:
|
1: I might have been unclear : this PR associates all comments. 2: And what would be the index of the array? Do you really mean an integer index 0..n? This would make the caller do useless efforts to retrieve comments associated with a node. |
|
So I have been unclear, sorry for that. See my (imperfect) code sample, though, all comments are in the same map ("commentMap"). |
OK. My last comment on that still applies--there is no place for this distinction in parser's API. It is an implementation detail at most. |
Well, this is "your baby" so you decide, of course. The current implementation (or the one you proposed) is less interesting for a "client" of parser like me, and I suppose it will be true for all the clients who have to do something with the comments parsed. I still cannot exactly understand what bothers you with the idea of "decorative comments". Especially in a language like ruby where comments are on a single line. |
"Decorative comments" are a semantic distinction that is not present at source level. It is not parser's job to opine on whether the comment is "normal" or "decorative" or whatever. Its job is merely to present all comments and the nodes they're closest to. Besides, it's completely trivial to reproduce the distinction if wanted. |
Ah, good, I think we are getting to a better understanding. This is on the "closest" point that I think the current implementation can be improved. We could call them by any other name than "decorative comments", which was probably a wrong choice from me, but I do believe these comments should be associated with the previous node. Systematically associate a comment with the next node makes it wrong. But maybe I am missing something. x = f1(y,z) # call f1 on y & z
def f2(n)
... And, correct me if I am wrong, the algorithm to find that this comment was actually intended for the call of f1 is not trivial; you would have to backtrack the previous nodes - actually doing a similar tree visiting that comment associator has already done. |
Sure. In my view a proper implementation would associate a comment not on the beginning of line but at the end of line with the preceding node. Is this not what yours does? |
Yes this is what it does. So we agree on this part, great! Then the remaining issues are: (let me see if I got it correctly) 1- You would like the API to return an array of pairs [node, comment]; my PR is currently returning a hash [key=node-location, value=comment]. 2- You are against returning the extra array of boolean, one per comment, with true for a comment associated with a preceding node, and false for one associated with a following node. I used to call the former "decorative" and I agree with you this is not right. I also agree this is easy for callers to compare the comment location with the node location and figure out this way if the comment is on the same line (or after the node, for the case of comments that are after the last statement of a function or the last function of a class, etc.). If the above is more or less correct, then would you be OK if I modify 1 & 2 according to what I described? |
Ah, posted the above with my work account... This is still me ;) |
Oh! So you suggest using |
Very good! So just tell me what you want me to change in the PR and I will do it. Only removing @decorative_comment (and not return it to callers)? Anything else? |
|
Hello! |
|
||
advance_through_directives if @skip_directives | ||
|
||
process(nil, @ast) | ||
|
||
@mapping | ||
return @mapping |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just @mapping
, please
Oh, and I can add a test for associate_locations of course! |
def associate_and_advance_comment(node) | ||
@mapping[node] << current_comment | ||
def associate_and_advance_comment(prev_node, node) | ||
if prev_node and node |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use &&
Yes, please |
# node.location as a key to retrieve the comments for this node. | ||
def associate_locations | ||
@map_using_node = false | ||
return associate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto
Working on your changes... |
Sure |
@whitequark let me know how you like this one~ |
end | ||
|
||
prev_node, next_node = nil, upper_node | ||
processTrailingComments(node) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
process_trailing_comments
Sans a few minor issues, this looks very good. |
Hey hey, what about now? |
# | ||
def associate | ||
def associate(map_using_locations = false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not really what I meant... methods returning different types depending on an argument are horribly designed API.
Sigh. I'll just fix it myself
* Changed Comment::Associator#associate API and way of associating comments
Please see the final API version on master. Thanks for your work! |
This PR makes the "Comment Associator" behave differently to solve 2 issues and allow callers to make the difference between "heading" comments, which come before a piece of code, and "decorative" comments which come right after the code they are associated to.
Changes:
NB: Most of the time, a decorative comment is on the same line as the node it is associated to. The exception is for trailing comments in a file, which are after the last function of a file. The code in this PR will associate these comments correctly to the last function. This fixes issue #185.
Fixes #184
Fixes #185
Example of use: