Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: convert unmerge_use to SyntaxFactory SyntaxEditor abstraction #18565

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions crates/ide-assists/src/handlers/unmerge_use.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use syntax::{
ast::{self, edit_in_place::Removable, make, HasVisibility},
ted::{self, Position},
ast::{self, edit_in_place::Removable, make, HasVisibility, syntax_factory::SyntaxFactory}, syntax_editor::SyntaxEditor,
syntax_editor::Position,
AstNode, SyntaxKind,
};

Expand All @@ -22,7 +22,7 @@ use crate::{
// use std::fmt::Display;
// ```
pub(crate) fn unmerge_use(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
let tree: ast::UseTree = ctx.find_node_at_offset::<ast::UseTree>()?.clone_for_update();
let tree: ast::UseTree = ctx.find_node_at_offset::<ast::UseTree>()?;

let tree_list = tree.syntax().parent().and_then(ast::UseTreeList::cast)?;
if tree_list.use_trees().count() < 2 {
Expand All @@ -33,7 +33,6 @@ pub(crate) fn unmerge_use(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<
let use_: ast::Use = tree_list.syntax().ancestors().find_map(ast::Use::cast)?;
let path = resolve_full_path(&tree)?;

let old_parent_range = use_.syntax().parent()?.text_range();
let new_parent = use_.syntax().parent()?;

// If possible, explain what is going to be done.
Expand All @@ -44,16 +43,23 @@ pub(crate) fn unmerge_use(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<

let target = tree.syntax().text_range();
acc.add(AssistId("unmerge_use", AssistKind::RefactorRewrite), label, target, |builder| {
let new_use = make::use_(

let make = SyntaxFactory::new();

let mut editor = builder.make_editor(&new_parent);

let new_use = make.use_(
use_.visibility(),
make::use_tree(path, tree.use_tree_list(), tree.rename(), tree.star_token().is_some()),
)
.clone_for_update();
make.use_tree(path, tree.use_tree_list(), tree.rename(), tree.star_token().is_some()),
);

editor.delete(tree.syntax());
editor.insert(Position::after(use_.syntax()), new_use.syntax());

tree.remove();
ted::insert(Position::after(use_.syntax()), new_use.syntax());
// editor.replace(use_.syntax().parent().unwrap(), new_parent);

builder.replace(old_parent_range, new_parent.to_string());
editor.add_mappings(make.finish_with_mappings());
builder.add_file_edits(ctx.file_id(), editor);
})
}

Expand Down
10 changes: 9 additions & 1 deletion crates/syntax/src/ast/syntax_factory/constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use itertools::Itertools;

use crate::{
ast::{self, make, HasName},
ast::{self, make, HasName, HasVisibility, Use, UseTree},

Check failure on line 5 in crates/syntax/src/ast/syntax_factory/constructors.rs

View workflow job for this annotation

GitHub Actions / Rust (ubuntu-latest)

unused imports: `HasVisibility`, `UseTree`, and `Use`
syntax_editor::SyntaxMappingBuilder,
AstNode,
};
Expand Down Expand Up @@ -107,4 +107,12 @@

ast
}

pub fn use_(&self, visibility: Option<ast::Visibility>, use_tree: ast::UseTree) -> ast::Use {
make::use_(visibility, use_tree)
}

pub fn use_tree(&self, path: ast::Path, use_tree_list: Option<ast::UseTreeList>, alias: Option<ast::Rename>, add_star: bool) -> ast::UseTree {
make::use_tree(path, use_tree_list, alias, add_star)
}
}
Loading