How to find comments on same line as node? (python) #2890
-
I'm working on some rust code that uses tree-sitter to parse python code (with https://github.com/tree-sitter/tree-sitter-python). Are there any convenience methods to facilitate searching for comments on a given row? I have a Here is the rust code I'm working on: impl Visitor for ImportCollector<'_> {
// ... snip other functions
fn visit_string(&mut self, node: tree_sitter::Node) -> ChildBehavior {
let range = node.range();
let text: &str = self.string_at(range);
// do something with the text here if the node does not have the comment.
ChildBehavior::Ignore
} This function is used for checking for the pragma comment in several other places, but it requires me to pass in the node whose sibling could be the comment node. That's problematic because I don't know how many parents I'll have to walk to find the comment node. impl ImportCollector<'_> {
// ... snip other functions
fn is_pragma_ignored(&self, node: tree_sitter::Node) -> bool {
if let Some(sibling) = node.next_named_sibling() {
let next_node_range = sibling.range();
if sibling.kind_id() == KindID::COMMENT
&& node.range().end_point.row == next_node_range.start_point.row
&& self
.code_at(next_node_range)
.contains("# pants: no-infer-dep")
{
return true;
}
}
false
} Any suggestions? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
This is what I came up with: pantsbuild/pants@36f4b3a Is there a better way to do this? Some utility in tree-stitter, accessible from rust, that facilitates a search like this? |
Beta Was this translation helpful? Give feedback.
This is what I came up with: pantsbuild/pants@36f4b3a
Is there a better way to do this? Some utility in tree-stitter, accessible from rust, that facilitates a search like this?