3371: Simplify r=matklad a=matklad



bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-02-28 21:44:07 +00:00 committed by GitHub
commit cc477edc66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 3 additions and 23 deletions

View File

@ -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(),

View File

@ -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

View File

@ -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,
) {
@ -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);
}
}
@ -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()