move node at offset to aglo already

This commit is contained in:
Aleksey Kladov 2019-01-08 20:44:31 +03:00
parent 1b82084a5f
commit 702bdacb03
11 changed files with 19 additions and 22 deletions

View file

@ -2,7 +2,7 @@ pub mod visit;
use rowan::TransparentNewType;
use crate::{SyntaxNode, TextRange, TextUnit};
use crate::{SyntaxNode, TextRange, TextUnit, AstNode};
pub use rowan::LeafAtOffset;
@ -16,6 +16,10 @@ pub fn find_leaf_at_offset(node: &SyntaxNode, offset: TextUnit) -> LeafAtOffset<
}
}
pub fn find_node_at_offset<N: AstNode>(syntax: &SyntaxNode, offset: TextUnit) -> Option<&N> {
find_leaf_at_offset(syntax, offset).find_map(|leaf| leaf.ancestors().find_map(N::cast))
}
pub fn find_covering_node(root: &SyntaxNode, range: TextRange) -> &SyntaxNode {
SyntaxNode::from_repr(root.0.covering_node(range))
}