Skip to content
Merged
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
1 change: 0 additions & 1 deletion crates/ra_assists/src/handlers/auto_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ pub(crate) fn auto_import(ctx: AssistCtx) -> Option<Assist> {
group.add_assist(AssistId("auto_import"), format!("Import `{}`", &import), |edit| {
edit.target(auto_import_assets.syntax_under_caret.text_range());
insert_use_statement(
&auto_import_assets.syntax_under_caret,
&auto_import_assets.syntax_under_caret,
&import,
edit.text_edit_builder(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,12 @@ pub(crate) fn replace_qualified_name_with_use(ctx: AssistCtx) -> Option<Assist>
return None;
}

let module = path.syntax().ancestors().find_map(ast::Module::cast);
let position = match module.and_then(|it| it.item_list()) {
Some(item_list) => item_list.syntax().clone(),
None => {
let current_file = path.syntax().ancestors().find_map(ast::SourceFile::cast)?;
current_file.syntax().clone()
}
};

ctx.add_assist(
AssistId("replace_qualified_name_with_use"),
"Replace qualified path with use",
|edit| {
let path_to_import = hir_path.mod_path().clone();
insert_use_statement(
&position,
&path.syntax(),
&path_to_import,
edit.text_edit_builder(),
);
insert_use_statement(path.syntax(), &path_to_import, edit.text_edit_builder());

if let Some(last) = path.segment() {
// Here we are assuming the assist will provide a correct use statement
Expand Down
9 changes: 2 additions & 7 deletions crates/ra_assists/src/utils/insert_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ use ra_text_edit::TextEditBuilder;
pub fn insert_use_statement(
// Ideally the position of the cursor, used to
position: &SyntaxNode,
// The statement to use as anchor (last resort)
anchor: &SyntaxNode,
path_to_import: &ModPath,
edit: &mut TextEditBuilder,
) {
Expand All @@ -29,7 +27,7 @@ pub fn insert_use_statement(
});

if let Some(container) = container {
let action = best_action_for_target(container, anchor.clone(), &target);
let action = best_action_for_target(container, position.clone(), &target);
make_assist(&action, &target, edit);
}
}
Expand Down Expand Up @@ -379,10 +377,7 @@ fn best_action_for_target(
// another item and we use it as anchor.
// If there are no items above, we choose the target path itself as anchor.
// todo: we should include even whitespace blocks as anchor candidates
let anchor = container
.children()
.find(|n| n.text_range().start() < anchor.text_range().start())
.or_else(|| Some(anchor));
let anchor = container.children().next().or_else(|| Some(anchor));

let add_after_anchor = anchor
.clone()
Expand Down