ide_completion/completions/attribute/
macro_use.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//! Completion for macros in `#[macro_use(...)]`
use hir::ModuleDef;
use ide_db::SymbolKind;
use syntax::ast;

use crate::{context::CompletionContext, item::CompletionItem, Completions};

pub(super) fn complete_macro_use(
    acc: &mut Completions,
    ctx: &CompletionContext<'_>,
    extern_crate: Option<&ast::ExternCrate>,
    existing_imports: &[ast::Path],
) {
    let Some(extern_crate) = extern_crate else { return };
    let Some(extern_crate) = ctx.sema.to_def(extern_crate) else { return };
    let Some(krate) = extern_crate.resolved_crate(ctx.db) else { return };

    for mod_def in krate.root_module().declarations(ctx.db) {
        if let ModuleDef::Macro(mac) = mod_def {
            let mac_name = mac.name(ctx.db);
            let mac_name = mac_name.as_str();

            let existing_import = existing_imports
                .iter()
                .filter_map(|p| p.as_single_name_ref())
                .find(|n| n.text() == mac_name);
            if existing_import.is_some() {
                continue;
            }

            let item =
                CompletionItem::new(SymbolKind::Macro, ctx.source_range(), mac_name, ctx.edition);
            item.add_to(acc, ctx.db);
        }
    }
}