3125: Add couple of utility methods r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-02-12 17:20:27 +00:00 committed by GitHub
commit 3da53ab3e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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))