Skip to content

Commit 7910a8c

Browse files
committed
refactor: use Vec of Either<ast::Path, ast::IdentPat> to reduce complexity
1 parent 9d3b39f commit 7910a8c

File tree

1 file changed

+19
-27
lines changed

1 file changed

+19
-27
lines changed

crates/ide-db/src/path_transform.rs

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -315,53 +315,44 @@ impl Ctx<'_> {
315315
}
316316

317317
fn transform_path(&self, path: &SyntaxNode) -> SyntaxNode {
318-
fn find_child_paths(root_path: &SyntaxNode) -> Vec<ast::Path> {
319-
let mut result = Vec::new();
318+
fn find_child_paths_and_ident_pats(root_path: &SyntaxNode) -> Vec<Either<ast::Path, ast::IdentPat>> {
319+
let mut result: Vec<Either<ast::Path, ast::IdentPat>> = Vec::new();
320320
for child in root_path.children() {
321321
if let Some(child_path) = ast::Path::cast(child.clone()) {
322-
result.push(child_path);
322+
result.push(either::Left(child_path));
323+
} else if let Some(child_ident_pat) = ast::IdentPat::cast(child.clone()) {
324+
result.push(either::Right(child_ident_pat));
323325
} else {
324-
result.extend(find_child_paths(&child));
325-
}
326-
}
327-
result
328-
}
329-
fn find_child_ident_pats(root_path: &SyntaxNode) -> Vec<ast::IdentPat> {
330-
let mut result = Vec::new();
331-
for child in root_path.children() {
332-
if let Some(child_ident_pat) = ast::IdentPat::cast(child.clone()) {
333-
result.push(child_ident_pat);
334-
} else {
335-
result.extend(find_child_ident_pats(&child));
326+
result.extend(find_child_paths_and_ident_pats(&child));
336327
}
337328
}
338329
result
339330
}
340331

341332
let root_path = path.clone_subtree();
342333

343-
let result = find_child_paths(&root_path);
334+
let result = find_child_paths_and_ident_pats(&root_path);
344335
let mut editor = SyntaxEditor::new(root_path.clone());
345336
for sub_path in result {
346337
let new = self.transform_path(sub_path.syntax());
347338
editor.replace(sub_path.syntax(), new);
348339
}
349340

350-
let ident_result = find_child_ident_pats(&root_path);
351-
for ident_pat in ident_result {
352-
if let Some(new) = self.transform_ident_pat(&ident_pat) {
353-
editor.replace(ident_pat.syntax(), new.syntax());
354-
}
355-
}
356-
357341
let update_sub_item = editor.finish().new_root().clone().clone_subtree();
358-
let item = find_child_paths(&update_sub_item);
342+
let item = find_child_paths_and_ident_pats(&update_sub_item);
359343
let mut editor = SyntaxEditor::new(update_sub_item);
360344
for sub_path in item {
361-
self.transform_path_(&mut editor, &sub_path);
345+
self.transform_path_or_ident_pat(&mut editor, &sub_path);
362346
}
363347
editor.finish().new_root().clone()
364348
}
349+
350+
fn transform_path_or_ident_pat(&self, editor: &mut SyntaxEditor, item: &Either<ast::Path, ast::IdentPat>) -> Option<()> {
351+
match item {
352+
Either::Left(path) => self.transform_path_(editor, path),
353+
Either::Right(ident_pat) => self.transform_ident_pat(editor, ident_pat)
354+
}
355+
}
365356

366357
fn transform_path_(&self, editor: &mut SyntaxEditor, path: &ast::Path) -> Option<()> {
367358
if path.qualifier().is_some() {
@@ -537,7 +528,7 @@ impl Ctx<'_> {
537528
Some(())
538529
}
539530

540-
fn transform_ident_pat(&self, ident_pat: &ast::IdentPat) -> Option<ast::Path> {
531+
fn transform_ident_pat(&self, editor: &mut SyntaxEditor, ident_pat: &ast::IdentPat) -> Option<()> {
541532
let name = ident_pat.name()?;
542533

543534
let temp_path = make::path_from_text(&name.text());
@@ -554,7 +545,8 @@ impl Ctx<'_> {
554545
};
555546
let found_path = self.target_module.find_path(self.source_scope.db, def, cfg)?;
556547
let res = mod_path_to_ast(&found_path, self.target_edition).clone_for_update();
557-
Some(res)
548+
editor.replace(ident_pat.syntax(), res.syntax());
549+
Some(())
558550
}
559551
_ => None,
560552
}

0 commit comments

Comments
 (0)