From 12905e5b58f22df026ef30afa6f0bdf7319cbddd Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 5 Jan 2020 21:32:18 +0100 Subject: [PATCH] Some more refactoring --- .../src/assists/add_missing_impl_members.rs | 14 ++++++-------- crates/ra_hir_expand/src/lib.rs | 10 ++++++++++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/crates/ra_assists/src/assists/add_missing_impl_members.rs b/crates/ra_assists/src/assists/add_missing_impl_members.rs index 22f1157ccd..942b34dc16 100644 --- a/crates/ra_assists/src/assists/add_missing_impl_members.rs +++ b/crates/ra_assists/src/assists/add_missing_impl_members.rs @@ -207,25 +207,23 @@ fn get_syntactic_substs(impl_block: ast::ImplBlock) -> Option> } // FIXME: This should be a general utility (not even just for assists) -fn substitute_type_params( +fn substitute_type_params( db: &impl HirDatabase, node: hir::InFile, substs: &HashMap, ) -> N { let type_param_replacements = node - .value - .syntax() - .descendants() - .filter_map(ast::TypeRef::cast) + .clone() + .descendants::() .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, } }) diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 2fa5d51402..51c5f9623c 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -322,3 +322,13 @@ impl InFile { }) } } + +impl InFile { + pub fn descendants(self) -> impl Iterator> { + 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()) + } +}