mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 13:03:31 +00:00
Some more refactoring
This commit is contained in:
parent
def124e932
commit
12905e5b58
2 changed files with 16 additions and 8 deletions
|
@ -207,25 +207,23 @@ fn get_syntactic_substs(impl_block: ast::ImplBlock) -> Option<Vec<ast::TypeRef>>
|
|||
}
|
||||
|
||||
// FIXME: This should be a general utility (not even just for assists)
|
||||
fn substitute_type_params<N: AstNode>(
|
||||
fn substitute_type_params<N: AstNode + Clone>(
|
||||
db: &impl HirDatabase,
|
||||
node: hir::InFile<N>,
|
||||
substs: &HashMap<hir::TypeParam, ast::TypeRef>,
|
||||
) -> N {
|
||||
let type_param_replacements = node
|
||||
.value
|
||||
.syntax()
|
||||
.descendants()
|
||||
.filter_map(ast::TypeRef::cast)
|
||||
.clone()
|
||||
.descendants::<ast::TypeRef>()
|
||||
.filter_map(|n| {
|
||||
let path = match &n {
|
||||
let path = match &n.value {
|
||||
ast::TypeRef::PathType(path_type) => path_type.path()?,
|
||||
_ => return None,
|
||||
};
|
||||
let analyzer = hir::SourceAnalyzer::new(db, node.with_value(n.syntax()), None);
|
||||
let analyzer = hir::SourceAnalyzer::new(db, n.syntax(), None);
|
||||
let resolution = analyzer.resolve_path(db, &path)?;
|
||||
match resolution {
|
||||
hir::PathResolution::TypeParam(tp) => Some((n, substs.get(&tp)?.clone())),
|
||||
hir::PathResolution::TypeParam(tp) => Some((n.value, substs.get(&tp)?.clone())),
|
||||
_ => None,
|
||||
}
|
||||
})
|
||||
|
|
|
@ -322,3 +322,13 @@ impl InFile<SyntaxNode> {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
pub fn syntax(&self) -> InFile<&SyntaxNode> {
|
||||
self.with_value(self.value.syntax())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue