Skip to content

Commit 12905e5

Browse files
committed
Some more refactoring
1 parent def124e commit 12905e5

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

crates/ra_assists/src/assists/add_missing_impl_members.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,25 +207,23 @@ fn get_syntactic_substs(impl_block: ast::ImplBlock) -> Option<Vec<ast::TypeRef>>
207207
}
208208

209209
// FIXME: This should be a general utility (not even just for assists)
210-
fn substitute_type_params<N: AstNode>(
210+
fn substitute_type_params<N: AstNode + Clone>(
211211
db: &impl HirDatabase,
212212
node: hir::InFile<N>,
213213
substs: &HashMap<hir::TypeParam, ast::TypeRef>,
214214
) -> N {
215215
let type_param_replacements = node
216-
.value
217-
.syntax()
218-
.descendants()
219-
.filter_map(ast::TypeRef::cast)
216+
.clone()
217+
.descendants::<ast::TypeRef>()
220218
.filter_map(|n| {
221-
let path = match &n {
219+
let path = match &n.value {
222220
ast::TypeRef::PathType(path_type) => path_type.path()?,
223221
_ => return None,
224222
};
225-
let analyzer = hir::SourceAnalyzer::new(db, node.with_value(n.syntax()), None);
223+
let analyzer = hir::SourceAnalyzer::new(db, n.syntax(), None);
226224
let resolution = analyzer.resolve_path(db, &path)?;
227225
match resolution {
228-
hir::PathResolution::TypeParam(tp) => Some((n, substs.get(&tp)?.clone())),
226+
hir::PathResolution::TypeParam(tp) => Some((n.value, substs.get(&tp)?.clone())),
229227
_ => None,
230228
}
231229
})

crates/ra_hir_expand/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,3 +322,13 @@ impl InFile<SyntaxNode> {
322322
})
323323
}
324324
}
325+
326+
impl<N: AstNode> InFile<N> {
327+
pub fn descendants<T: AstNode>(self) -> impl Iterator<Item = InFile<T>> {
328+
self.value.syntax().descendants().filter_map(T::cast).map(move |n| self.with_value(n))
329+
}
330+
331+
pub fn syntax(&self) -> InFile<&SyntaxNode> {
332+
self.with_value(self.value.syntax())
333+
}
334+
}

0 commit comments

Comments
 (0)