pub(crate) fn auto_import(ctx: AssistCtx) -> Option<Assist> {
    let path_under_caret: ast::Path = ctx.find_node_at_offset()?;
    if path_under_caret.syntax().ancestors().find_map(ast::UseItem::cast).is_some() {
        return None;
    }

    let module = path_under_caret.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_under_caret.syntax().ancestors().find_map(ast::SourceFile::cast)?;
            current_file.syntax().clone()
        }
    };
    let source_analyzer = ctx.source_analyzer(&position, None);
    let module_with_name_to_import = source_analyzer.module()?;

    let name_ref_to_import =
        path_under_caret.syntax().descendants().find_map(ast::NameRef::cast)?;
    if source_analyzer
        .resolve_path(ctx.db, &name_ref_to_import.syntax().ancestors().find_map(ast::Path::cast)?)
        .is_some()
    {
        return None;
    }

    let name_to_import = name_ref_to_import.syntax().to_string();
    let proposed_imports = ImportsLocator::new(ctx.db)
        .find_imports(&name_to_import)
        .into_iter()
        .filter_map(|module_def| module_with_name_to_import.find_use_path(ctx.db, module_def))
        .filter(|use_path| !use_path.segments.is_empty())
        .take(20)
        .collect::<BTreeSet<_>>();

    if proposed_imports.is_empty() {
        return None;
    }

    let mut group = ctx.add_assist_group(format!("Import {}", name_to_import));
    for import in proposed_imports {
        group.add_assist(AssistId("auto_import"), format!("Import `{}`", &import), |edit| {
            edit.target(path_under_caret.syntax().text_range());
            insert_use_statement(
                &position,
                path_under_caret.syntax(),
                &import,
                edit.text_edit_builder(),
            );
        });
    }
    group.finish()
}
// Assist: auto_import
//
// If the name is unresolved, provides all possible imports for it.
//
// ```
// fn main() {
//     let map = HashMap<|>::new();
// }
// # pub mod std { pub mod collections { pub struct HashMap { } } }
// ```
// ->
// ```
// use std::collections::HashMap;
//
// fn main() {
//     let map = HashMap::new();
// }
// # pub mod std { pub mod collections { pub struct HashMap { } } }
// ```
auto_import
If the name is unresolved, provides all possible imports for it.


auto_import If the name is unresolved provides all possible imports for it .
pub(crate) fn auto_import(ctx: AssistCtx) -> Option<Assist> {
    let path_under_caret: ast::Path = ctx.find_node_at_offset()?;
    if path_under_caret.syntax().ancestors().find_map(ast::UseItem::cast).is_some() {
        return None;
    }
    let module = path_under_caret.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_under_caret.syntax().ancestors().find_map(ast::SourceFile::cast)?;
            current_file.syntax().clone()
        }
    };
    let source_analyzer = ctx.source_analyzer(&position, None);
    let module_with_name_to_import = source_analyzer.module()?;
    let name_ref_to_import =
        path_under_caret.syntax().descendants().find_map(ast::NameRef::cast)?;
    if source_analyzer
        .resolve_path(ctx.db, &name_ref_to_import.syntax().ancestors().find_map(ast::Path::cast)?)
        .is_some()
    {
        return None;
    }
    let name_to_import = name_ref_to_import.syntax().to_string();
    let proposed_imports = ImportsLocator::new(ctx.db)
        .find_imports(&name_to_import)
        .into_iter()
        .filter_map(|module_def| module_with_name_to_import.find_use_path(ctx.db, module_def))
        .filter(|use_path| !use_path.segments.is_empty())
        .take(20)
        .collect::<BTreeSet<_>>();
    if proposed_imports.is_empty() {
        return None;
    }
    let mut group = ctx.add_assist_group(format!("Import {}", name_to_import));
    for import in proposed_imports {
        group.add_assist(AssistId("auto_import"), format!("Import `{}`", &import), |edit| {
            edit.target(path_under_caret.syntax().text_range());
            insert_use_statement(
                &position,
                path_under_caret.syntax(),
                &import,
                edit.text_edit_builder(),
            );
        });
    }
    group.finish()
}
pub ( crate ) fn auto_import ( ctx : AssistCtx ) -> Option < Assist > { let path_under_caret : ast :: Path = ctx . find_node_at_offset ( ) ? ; if path_under_caret . syntax ( ) . ancestors ( ) . find_map ( ast :: UseItem :: cast ) . is_some ( ) { return None ; } let module = path_under_caret . 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_under_caret . syntax ( ) . ancestors ( ) . find_map ( ast :: SourceFile :: cast ) ? ; current_file . syntax ( ) . clone ( ) } } ; let source_analyzer = ctx . source_analyzer ( & position , None ) ; let module_with_name_to_import = source_analyzer . module ( ) ? ; let name_ref_to_import = path_under_caret . syntax ( ) . descendants ( ) . find_map ( ast :: NameRef :: cast ) ? ; if source_analyzer . resolve_path ( ctx . db , & name_ref_to_import . syntax ( ) . ancestors ( ) . find_map ( ast :: Path :: cast ) ? ) . is_some ( ) { return None ; } let name_to_import = name_ref_to_import . syntax ( ) . to_string ( ) ; let proposed_imports = ImportsLocator :: new ( ctx . db ) . find_imports ( & name_to_import ) . into_iter ( ) . filter_map ( | module_def | module_with_name_to_import . find_use_path ( ctx . db , module_def ) ) . filter ( | use_path | ! use_path . segments . is_empty ( ) ) . take ( 20 ) . collect :: < BTreeSet < _ > > ( ) ; if proposed_imports . is_empty ( ) { return None ; } let mut group = ctx . add_assist_group ( format ! ( " " name_to_import ) ) ; for import in proposed_imports { group . add_assist ( AssistId ( " " ) , format ! ( " " import ) , | edit | { edit . target ( path_under_caret . syntax ( ) . text_range ( ) ) ; insert_use_statement ( & position , path_under_caret . syntax ( ) , & import , edit . text_edit_builder ( ) , ) ; } ) ; } group . finish ( ) }
Assist: auto_import
If the name is unresolved, provides all possible imports for it.
Assist : auto_import If the name is unresolved provides all possible imports for it .
ctx
AssistCtx
pub mod std { pub mod collections { pub struct hashmap { } } }
```
>
```
use std::collections::HashMap.

fn main() {
let map = HashMap::new();
}
` ` ` > ` ` ` use std :: collections :: HashMap . fn main () { let map = HashMap :: new () ; }
pub mod std { pub mod collections { pub struct hashmap { } } }
None