Skip to content

Commit 872951d

Browse files
committed
Replace 'postorder' with 'reverse of preorder' to traverse the AST in path_transform
1 parent e53792b commit 872951d

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

crates/ide-db/src/path_transform.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use crate::helpers::mod_path_to_ast;
44
use either::Either;
55
use hir::{AsAssocItem, HirDisplay, ModuleDef, SemanticsScope};
6+
use itertools::Itertools;
67
use rustc_hash::FxHashMap;
78
use syntax::{
89
ast::{self, make, AstNode},
@@ -227,24 +228,28 @@ struct Ctx<'a> {
227228
same_self_type: bool,
228229
}
229230

230-
fn postorder(item: &SyntaxNode) -> impl Iterator<Item = SyntaxNode> {
231-
item.preorder().filter_map(|event| match event {
232-
syntax::WalkEvent::Enter(_) => None,
233-
syntax::WalkEvent::Leave(node) => Some(node),
234-
})
231+
fn preorder_rev(item: &SyntaxNode) -> impl Iterator<Item = SyntaxNode> {
232+
let x = item
233+
.preorder()
234+
.filter_map(|event| match event {
235+
syntax::WalkEvent::Enter(node) => Some(node),
236+
syntax::WalkEvent::Leave(_) => None,
237+
})
238+
.collect_vec();
239+
x.into_iter().rev()
235240
}
236241

237242
impl Ctx<'_> {
238243
fn apply(&self, item: &SyntaxNode) {
239244
// `transform_path` may update a node's parent and that would break the
240245
// tree traversal. Thus all paths in the tree are collected into a vec
241246
// so that such operation is safe.
242-
let paths = postorder(item).filter_map(ast::Path::cast).collect::<Vec<_>>();
247+
let paths = preorder_rev(item).filter_map(ast::Path::cast).collect::<Vec<_>>();
243248
for path in paths {
244249
self.transform_path(path);
245250
}
246251

247-
postorder(item).filter_map(ast::Lifetime::cast).for_each(|lifetime| {
252+
preorder_rev(item).filter_map(ast::Lifetime::cast).for_each(|lifetime| {
248253
if let Some(subst) = self.lifetime_substs.get(&lifetime.syntax().text().to_string()) {
249254
ted::replace(lifetime.syntax(), subst.clone_subtree().clone_for_update().syntax());
250255
}
@@ -263,7 +268,7 @@ impl Ctx<'_> {
263268
// `transform_path` may update a node's parent and that would break the
264269
// tree traversal. Thus all paths in the tree are collected into a vec
265270
// so that such operation is safe.
266-
let paths = postorder(value).filter_map(ast::Path::cast).collect::<Vec<_>>();
271+
let paths = preorder_rev(value).filter_map(ast::Path::cast).collect::<Vec<_>>();
267272
for path in paths {
268273
self.transform_path(path);
269274
}

0 commit comments

Comments
 (0)