mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 13:48:50 +00:00
Merge #3125
3125: Add couple of utility methods r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
3da53ab3e7
1 changed files with 19 additions and 3 deletions
|
@ -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))
|
||||
|
|
Loading…
Reference in a new issue