class SyntaxTree::Parser
def nearest_nodes(node, comment)
Experimental RBS support (using type sampling data from the type_fusion project).
type SyntaxTree__Parser_nearest_nodes_node = SyntaxTree::BodyStmt | SyntaxTree::Statements | SyntaxTree::Program | SyntaxTree::ModuleDeclaration | SyntaxTree::ClassDeclaration | SyntaxTree::DefNode def nearest_nodes: (SyntaxTree__Parser_nearest_nodes_node node, SyntaxTree::Comment comment) -> untyped
This signature was generated using 14 samples from 1 application.
Responsible for finding the nearest nodes to the given comment within the
def nearest_nodes(node, comment) comment_start = comment.location.start_char comment_end = comment.location.end_char child_nodes = node.child_nodes.compact preceding = nil following = nil left = 0 right = child_nodes.length # This is a custom binary search that finds the nearest nodes to the given # comment. When it finds a node that completely encapsulates the comment, # it recursed downward into the tree. while left < right middle = (left + right) / 2 child = child_nodes[middle] node_start = child.location.start_char node_end = child.location.end_char if node_start <= comment_start && comment_end <= node_end # The comment is completely contained by this child node. Abandon the # binary search at this level. return nearest_nodes(child, comment) end if node_end <= comment_start # This child node falls completely before the comment. Because we will # never consider this node or any nodes before it again, this node # must be the closest preceding node we have encountered so far. preceding = child left = middle + 1 next end if comment_end <= node_start # This child node falls completely after the comment. Because we will # never consider this node or any nodes after it again, this node must # be the closest following node we have encountered so far. following = child right = middle next end # This should only happen if there is a bug in this parser. raise "Comment location overlaps with node location" end [preceding, node, following] end