Some more refactoring

This commit is contained in:
Florian Diebold 2020-01-05 21:32:18 +01:00
parent def124e932
commit 12905e5b58
2 changed files with 16 additions and 8 deletions

View file

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

View file

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