diff options
| author | Aleksey Kladov <aleksey.kladov@gmail.com> | 2020-02-12 18:19:55 +0100 |
|---|---|---|
| committer | Aleksey Kladov <aleksey.kladov@gmail.com> | 2020-02-12 18:19:55 +0100 |
| commit | f2424f947cb10fa5bdcd33f34b4444b7ab78b07d (patch) | |
| tree | 684845bc3e9b38ffc8e61c354aba66fdd308fc48 | |
| parent | 6ec982d54dcb28a56e5f13337d045e187949888b (diff) | |
| download | rust-f2424f947cb10fa5bdcd33f34b4444b7ab78b07d.tar.gz rust-f2424f947cb10fa5bdcd33f34b4444b7ab78b07d.zip | |
Add couple of utility methods
| -rw-r--r-- | crates/ra_hir_expand/src/lib.rs | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 7cf3b59a79d..9506f2e1cd4 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -323,11 +323,18 @@ impl<T: Clone> InFile<&T> { } } +impl<T> InFile<Option<T>> { + pub fn transpose(self) -> Option<InFile<T>> { + let value = self.value?; + Some(InFile::new(self.file_id, value)) + } +} + impl InFile<SyntaxNode> { - pub fn ancestors_with_macros<'a>( + pub fn ancestors_with_macros( self, - db: &'a impl crate::db::AstDatabase, - ) -> impl Iterator<Item = InFile<SyntaxNode>> + 'a { + db: &impl crate::db::AstDatabase, + ) -> impl Iterator<Item = InFile<SyntaxNode>> + '_ { std::iter::successors(Some(self), move |node| match node.value.parent() { Some(parent) => Some(node.with_value(parent)), None => { @@ -338,6 +345,15 @@ impl InFile<SyntaxNode> { } } +impl InFile<SyntaxToken> { + pub fn ancestors_with_macros( + self, + db: &impl crate::db::AstDatabase, + ) -> impl Iterator<Item = InFile<SyntaxNode>> + '_ { + self.map(|it| it.parent()).ancestors_with_macros(db) + } +} + impl<N: AstNode> InFile<N> { pub fn descendants<T: AstNode>(self) -> impl Iterator<Item = InFile<T>> { self.value.syntax().descendants().filter_map(T::cast).map(move |n| self.with_value(n)) |
