Add couple of utility methods

This commit is contained in:
Aleksey Kladov 2020-02-12 18:19:55 +01:00
parent 6ec982d54d
commit f2424f947c

View File

@ -323,11 +323,18 @@ pub fn cloned(&self) -> 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 @@ pub fn ancestors_with_macros<'a>(
}
}
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))