-
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
Changes from 5 commits
8f2231f
1db7fa3
c1086c9
76bce7d
14d77ae
d0893e9
f069921
3bb053c
d0037b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,8 +38,7 @@ use pyo3::{ | |
types::PyDict, | ||
}; | ||
use regex::Regex; | ||
use tree_sitter::{InputEdit, Range}; | ||
use tree_sitter_traversal::{traverse, Order}; | ||
use tree_sitter::InputEdit; | ||
|
||
use std::collections::HashMap; | ||
|
||
|
@@ -276,7 +275,7 @@ fn get_rule_graph(_arg: &PiranhaArguments) -> RuleGraph { | |
|
||
// TODO: Move to `PiranhaArgumentBuilder`'s _validate - https://github.com/uber/piranha/issues/387 | ||
// Get the user-defined rule graph (if any) via the Python/Rust API | ||
let mut user_defined_rules = _arg.rule_graph().clone(); | ||
let mut user_defined_rules: RuleGraph = _arg.rule_graph().clone(); | ||
// In the scenario when rules/edges are passed as toml files | ||
if !_arg.path_to_configurations().is_empty() { | ||
user_defined_rules = read_user_config_files(_arg.path_to_configurations()) | ||
|
@@ -332,81 +331,11 @@ mod piranha_arguments_test; | |
impl SourceCodeUnit { | ||
/// Delete the comment associated to the deleted code element | ||
pub(crate) fn _delete_associated_comment( | ||
&mut self, edit: &Edit, applied_edit: &mut InputEdit, parser: &mut tree_sitter::Parser, | ||
) { | ||
// Check if the edit kind is "DELETE something" | ||
if *self.piranha_arguments().cleanup_comments() && edit.replacement_string().is_empty() { | ||
let deleted_at = edit.p_match().range().start_point.row; | ||
if let Some(comment_range) = self._get_comment_at_line( | ||
deleted_at, | ||
*self.piranha_arguments().cleanup_comments_buffer(), | ||
edit.p_match().range().start_byte, | ||
) { | ||
debug!("Deleting an associated comment"); | ||
*applied_edit = self._apply_edit(&Edit::delete_range(self.code(), comment_range), parser); | ||
} | ||
} | ||
} | ||
|
||
/// This function reports the range of the comment associated to the deleted element. | ||
/// | ||
/// # Arguments: | ||
/// * row : The row number where the deleted element started | ||
/// * buffer: Number of lines that we want to look up to find associated comment | ||
/// | ||
/// # Algorithm : | ||
/// Get all the nodes that either start and end at [row] | ||
/// If **all** nodes are comments | ||
/// * return the range of the comment | ||
/// If the [row] has no node that either starts/ends there: | ||
/// * recursively call this method for [row] -1 (until buffer is positive) | ||
/// This function reports the range of the comment associated to the deleted element. | ||
/// | ||
/// # Arguments: | ||
/// * row : The row number where the deleted element started | ||
/// * buffer: Number of lines that we want to look up to find associated comment | ||
/// | ||
/// # Algorithm : | ||
/// Get all the nodes that either start and end at [row] | ||
/// If **all** nodes are comments | ||
/// * return the range of the comment | ||
/// If the [row] has no node that either starts/ends there: | ||
/// * recursively call this method for [row] -1 (until buffer is positive) | ||
fn _get_comment_at_line( | ||
&mut self, row: usize, buffer: usize, start_byte: usize, | ||
) -> Option<Range> { | ||
// Get all nodes that start or end on `updated_row`. | ||
let mut relevant_nodes_found = false; | ||
let mut relevant_nodes_are_comments = true; | ||
let mut comment_range = None; | ||
// Since the previous edit was a delete, the start and end of the replacement range is [start_byte]. | ||
let node = self | ||
.root_node() | ||
.descendant_for_byte_range(start_byte, start_byte) | ||
.unwrap_or_else(|| self.root_node()); | ||
|
||
for node in traverse(node.walk(), Order::Post) { | ||
if node.start_position().row == row || node.end_position().row == row { | ||
relevant_nodes_found = true; | ||
let is_comment: bool = self | ||
.piranha_arguments() | ||
.language() | ||
.is_comment(node.kind().to_string()); | ||
relevant_nodes_are_comments = relevant_nodes_are_comments && is_comment; | ||
if is_comment { | ||
comment_range = Some(node.range()); | ||
} | ||
} | ||
} | ||
|
||
if relevant_nodes_found { | ||
if relevant_nodes_are_comments { | ||
return comment_range; | ||
} | ||
} else if buffer > 0 { | ||
// We pass [start_byte] itself, because we know that parent of the current row is the parent of the above row too. | ||
// If that's not the case, its okay, because we will not find any comments in these scenarios. | ||
return self._get_comment_at_line(row - 1, buffer - 1, start_byte); | ||
&mut self, edit: &Edit, parser: &mut tree_sitter::Parser, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. from Let's say
becomes
now
Since
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
) -> Option<InputEdit> { | ||
if let Some(comment_range) = self._get_nearest_comment_range(edit, 0) { | ||
debug!("Deleting an associated comment"); | ||
return Some(self.apply_edit(&Edit::delete_range(self.code(), comment_range), parser)); | ||
} | ||
None | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. inlined Earlier we had a function called I just inlined |
||
// Get the tree_sitter's input edit representation | ||
let mut applied_edit = self._apply_edit(edit, parser); | ||
self._delete_associated_comment(edit, &mut applied_edit, parser); | ||
applied_edit | ||
} | ||
|
||
/// Applies an edit to the source code unit | ||
/// # Arguments | ||
/// * `replace_range` - the range of code to be replaced | ||
|
@@ -330,25 +323,31 @@ impl SourceCodeUnit { | |
/// The `edit:InputEdit` performed. | ||
/// | ||
/// Note - Causes side effect. - Updates `self.ast` and `self.code` | ||
pub(crate) fn _apply_edit(&mut self, edit: &Edit, parser: &mut Parser) -> InputEdit { | ||
let mut edit_to_apply = edit.clone(); | ||
pub(crate) fn apply_edit(&mut self, edit: &Edit, parser: &mut Parser) -> InputEdit { | ||
let mut edit: Edit = edit.clone(); | ||
// Check if the edit is a `Delete` operation then delete trailing comma | ||
if edit.replacement_string().trim().is_empty() { | ||
edit_to_apply = self.delete_trailing_comma(edit); | ||
if edit.is_delete() { | ||
edit = self.delete_trailing_comma(&edit); | ||
} | ||
// Get the tree_sitter's input edit representation | ||
let (new_source_code, ts_edit) = get_tree_sitter_edit(self.code.clone(), &edit_to_apply); | ||
let (new_source_code, ts_edit) = get_tree_sitter_edit(self.code.clone(), &edit); | ||
// Apply edit to the tree | ||
self.ast.edit(&ts_edit); | ||
self._replace_file_contents_and_re_parse(&new_source_code, parser, true); | ||
if self.ast.root_node().has_error() { | ||
if self.root_node().has_error() { | ||
let msg = format!( | ||
"Produced syntactically incorrect source code {}", | ||
self.code() | ||
); | ||
error!("{}", msg); | ||
panic!("{}", msg); | ||
} | ||
// Check if the edit is a `Delete` operation then delete associated comment | ||
if edit.is_delete() && *self.piranha_arguments().cleanup_comments() { | ||
if let Some(deleted_comment) = self._delete_associated_comment(&edit, parser) { | ||
return deleted_comment; | ||
} | ||
} | ||
ts_edit | ||
} | ||
|
||
|
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.