Skip to content

Commit

Permalink
Address comments [part-2]
Browse files Browse the repository at this point in the history
  • Loading branch information
ketkarameya committed Feb 25, 2023
1 parent 0a151da commit 796083d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 70 deletions.
70 changes: 69 additions & 1 deletion src/models/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use std::str::FromStr;

use getset::Getters;
use serde_derive::Deserialize;
use tree_sitter::{Node, Parser, Query};
use tree_sitter::{Node, Parser, Query, Range};
use tree_sitter_traversal::{traverse, Order};

use crate::utilities::parse_toml;

Expand Down Expand Up @@ -246,4 +247,71 @@ impl SourceCodeUnit {
.ignore_nodes_for_comments()
.contains(&node.kind().to_string())
}

/// 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)
pub(crate) 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());

// Search for all nodes starting/ending in the current row.
// if it is not amongst the `language.ignore_node_for_comment`, then check if it is a comment
// If the node is a comment, then return its range
for node in traverse(node.walk(), Order::Post) {
if self.should_ignore_node_for_comment_search(node) {
continue;
}

if node.start_position().row == row || node.end_position().row == row {
relevant_nodes_found = true;
let is_comment: bool = self.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);
}
None
}
}
70 changes: 1 addition & 69 deletions src/models/piranha_arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -348,73 +347,6 @@ impl SourceCodeUnit {
}
}

/// 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());

// Search for all nodes starting/ending in the current row.
// if it is not amongst the `language.ignore_node_for_comment`, then check if it is a comment
// If the node is a comment, then return its range
for node in traverse(node.walk(), Order::Post) {
if self.should_ignore_node_for_comment_search(node) {
continue;
}

if node.start_position().row == row || node.end_position().row == row {
relevant_nodes_found = true;
let is_comment: bool = self.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);
}
None
}

/// Replaces three consecutive newline characters with two
pub(crate) fn perform_delete_consecutive_new_lines(&mut self) {
if *self.piranha_arguments().delete_consecutive_new_lines() {
Expand Down

0 comments on commit 796083d

Please sign in to comment.