From 702bdacb03b498f36ed9ccf4f37ca923affb1e9c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 8 Jan 2019 20:44:31 +0300 Subject: move node at offset to aglo already --- crates/ra_syntax/src/algo.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs index 13f50d2ef..2826ec7f2 100644 --- a/crates/ra_syntax/src/algo.rs +++ b/crates/ra_syntax/src/algo.rs @@ -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(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)) } -- cgit v1.2.3 From 0c88360eb46c44935421c5d4bc36910ce3e96782 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 8 Jan 2019 20:47:37 +0300 Subject: add comment --- crates/ra_syntax/src/algo.rs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs index 2826ec7f2..45747e21d 100644 --- a/crates/ra_syntax/src/algo.rs +++ b/crates/ra_syntax/src/algo.rs @@ -16,6 +16,15 @@ pub fn find_leaf_at_offset(node: &SyntaxNode, offset: TextUnit) -> LeafAtOffset< } } +/// Finds a node of specific Ast type at offset. Note that this is slightly +/// impercise: if the cursor is strictly betwen two nodes of the desired type, +/// as in +/// +/// ```no-run +/// struct Foo {}|struct Bar; +/// ``` +/// +/// then the left node will be silently prefered. pub fn find_node_at_offset(syntax: &SyntaxNode, offset: TextUnit) -> Option<&N> { find_leaf_at_offset(syntax, offset).find_map(|leaf| leaf.ancestors().find_map(N::cast)) } -- cgit v1.2.3