-
Notifications
You must be signed in to change notification settings - Fork 188
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
Fix go comments bug #392
Fix go comments bug #392
Conversation
815a0f5
to
1db7fa3
Compare
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.
Self review and annotations
#[get = "pub"] | ||
comment_nodes: Vec<String>, | ||
/// The node kinds to be ignored when reasoning about comments | ||
#[get = "pub"] | ||
ignore_nodes_for_comments: Vec<String>, |
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.
The property i mentioned in the PR description.
src/models/piranha_arguments.rs
Outdated
@@ -386,6 +386,13 @@ impl SourceCodeUnit { | |||
.unwrap_or_else(|| self.root_node()); | |||
|
|||
for node in traverse(node.walk(), Order::Post) { | |||
if self |
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.
The bug fix.
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.
Change looks great (assuming I understood it correctly). Some minor requests regarding documentation and what seems to be a few stale comments 🙂
} else { | ||
fmt.Println("remain") | ||
} | ||
// this comment doesnt get removed but it should |
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.
No longer true, no? Shouldn't the comment be updated to saying it will get removed (same with all the cases below)
src/models/language.rs
Outdated
#[get = "pub"] | ||
comment_nodes: Vec<String>, | ||
/// The node kinds to be ignored when reasoning about comments |
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.
/// The node kinds to be ignored when reasoning about comments | |
/// The node kinds to be ignored when searching for comments |
Just to be clear, the algorithm will find the nearest node to the deleted position that is not one of these. Then, if that node is a comment, it will delete it. Correct? Am I understanding the logic?
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.
Yes. You have got it correct. I have made it more explicit now.
src/models/language.rs
Outdated
@@ -66,6 +76,13 @@ impl PiranhaLanguage { | |||
self.comment_nodes().contains(&kind) | |||
} | |||
|
|||
pub fn should_ignore_node_for_comment(&self, node: &Node) -> bool { |
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.
Documentation on this one, explaining what it does and how it's used.
Like, is the following correct?
[Piranha] will find the nearest node to the deleted position that is not one of these. Then, if that node is a comment, it will delete it.
(Even if so, please rewrite the doc comment a bit with some more info about how this function is used to implement the algorithm above)
796083d
to
084fab3
Compare
084fab3
to
76bce7d
Compare
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.
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.
Aaah! I missed a corner case in this bug fix. :|
@@ -72,7 +72,6 @@ func simplify_if_statement_false_comment_demo_single_comment() { | |||
|
|||
func simplify_if_statement_false_comment_demo_double_comment() { | |||
fmt.Println("remain") | |||
// this comment doesnt get removed |
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.
The bug was not fully solved :| . Now its solved.
debug!("Deleting an associated comment"); | ||
*applied_edit = self._apply_edit(&Edit::delete_range(self.code(), comment_range), parser); | ||
} | ||
&mut self, edit: &Edit, parser: &mut tree_sitter::Parser, |
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.
from apply_edit
we call _delete_associated_comment
and from _delete_associated_comment
we recursively call apply_edit
. This ensures that can delete a series of comments.
Let's say apply_edit
deletes int foo
in the below example.
// some
// comment
int foo;
becomes
// some
// comment
now _delete_associated_comment
is triggered, and now we produce
// some
Since _delete_associated_comment
is effectively called recursively
<all comments are deleted>
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.
Can we have this documented in a comment in the code in addition to in the PR review comments? Possibly in the documentation for this function.
@@ -313,13 +313,6 @@ impl SourceCodeUnit { | |||
self.perform_delete_consecutive_new_lines(); | |||
} | |||
|
|||
pub(crate) fn apply_edit(&mut self, edit: &Edit, parser: &mut Parser) -> InputEdit { |
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.
inlined apply_edit
, this simplifies the code and also kinda solves this bug further.
Earlier we had a function called apply_edit
which called _apply_edit
and then called _delete_associated_comment
. Now _delete_associated_comment
called _apply_edit
(not apply_edit
), therefore _delete_associated_comment
was not getting invoked recursively, and therefore we were not deleting a series of comments.
I just inlined apply_edit
into _apply_edit
and renamed _apply_edit
to apply_edit
. Now when apply_edit
is called from _delete_associated_comment
, it ensures that _delete_associated_comment
is called recursively. (I adjusted all the signatures and simplified the code)
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.
LGTM. One small request documentation wise, though:
debug!("Deleting an associated comment"); | ||
*applied_edit = self._apply_edit(&Edit::delete_range(self.code(), comment_range), parser); | ||
} | ||
&mut self, edit: &Edit, parser: &mut tree_sitter::Parser, |
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.
Can we have this documented in a comment in the code in addition to in the PR review comments? Possibly in the documentation for this function.
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.
LGTM!
Bug Diagnosis (#376): This problem is only in
Go
(because of its grammar, in my observation). The problem is caused when the comment is associated to the last statement of a method body inGo
. Diagnosis:Our comment fetching algorithm looks for comments on lines on the same line as deleted element and some lines above it (determined by the
cleanup_comment_buffer
flag). There is a caveat in this algorithm - (i) Get all the nodes that either start and end at [row] (ii) If all nodes are comments.Now lets take the
Go
case where the comment is associated to the last statement of a method body inGo
.When we delete the last statement of the method body, now the last "statement" of the method body is a comment.
For some reason, tree sitter reports that this last row is where the comment and the block end. :|
Fix: Add a field
ignore_node_for_comment
property toPiranhaLanguage
, now our algorithm will ignore the node kinds when reasoning about all nodes that start/end at last row.Test Plan Added the test cases as reported in the issue. I added similar test cases for Java and Kotlin too, but I did not find this inconsistency for them (so did not have set the
ignore_node_for_comment
property for Java and Kotlin)