aboutsummaryrefslogtreecommitdiff
path: root/crates/completion/src/render/macro_.rs
blob: bcf94f47eae6854cc58f8398c73692974f37872b (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use hir::{Documentation, HasAttrs, HasSource};
use syntax::display::macro_label;
use test_utils::mark;

use crate::{
    item::{CompletionItem, CompletionItemKind, CompletionKind},
    render::RenderContext,
};

#[derive(Debug)]
pub(crate) struct MacroRender<'a> {
    ctx: RenderContext<'a>,
    name: String,
    macro_: hir::MacroDef,
    docs: Option<Documentation>,
    bra: &'static str,
    ket: &'static str,
}

impl<'a> MacroRender<'a> {
    pub(crate) fn new(
        ctx: RenderContext<'a>,
        name: String,
        macro_: hir::MacroDef,
    ) -> MacroRender<'a> {
        let docs = macro_.docs(ctx.db());
        let docs_str = docs.as_ref().map_or("", |s| s.as_str());
        let (bra, ket) = guess_macro_braces(&name, docs_str);

        MacroRender { ctx, name, macro_, docs, bra, ket }
    }

    pub(crate) fn render(&self) -> Option<CompletionItem> {
        // FIXME: Currently proc-macro do not have ast-node,
        // such that it does not have source
        if self.macro_.is_proc_macro() {
            return None;
        }

        let mut builder =
            CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), &self.label())
                .kind(CompletionItemKind::Macro)
                .set_documentation(self.docs.clone())
                .set_deprecated(self.ctx.is_deprecated(self.macro_))
                .detail(self.detail());

        let needs_bang = self.needs_bang();
        builder = match self.ctx.snippet_cap() {
            Some(cap) if needs_bang => {
                let snippet = self.snippet();
                let lookup = self.lookup();
                builder.insert_snippet(cap, snippet).lookup_by(lookup)
            }
            None if needs_bang => builder.insert_text(self.banged_name()),
            _ => {
                mark::hit!(dont_insert_macro_call_parens_unncessary);
                builder.insert_text(&self.name)
            }
        };

        Some(builder.build())
    }

    fn needs_bang(&self) -> bool {
        self.ctx.completion.use_item_syntax.is_none() && !self.ctx.completion.is_macro_call
    }

    fn label(&self) -> String {
        format!("{}!{}…{}", self.name, self.bra, self.ket)
    }

    fn snippet(&self) -> String {
        format!("{}!{}$0{}", self.name, self.bra, self.ket)
    }

    fn lookup(&self) -> String {
        self.banged_name()
    }

    fn banged_name(&self) -> String {
        format!("{}!", self.name)
    }

    fn detail(&self) -> String {
        let ast_node = self.macro_.source(self.ctx.db()).value;
        macro_label(&ast_node)
    }
}

fn guess_macro_braces(macro_name: &str, docs: &str) -> (&'static str, &'static str) {
    let mut votes = [0, 0, 0];
    for (idx, s) in docs.match_indices(&macro_name) {
        let (before, after) = (&docs[..idx], &docs[idx + s.len()..]);
        // Ensure to match the full word
        if after.starts_with('!')
            && !before.ends_with(|c: char| c == '_' || c.is_ascii_alphanumeric())
        {
            // It may have spaces before the braces like `foo! {}`
            match after[1..].chars().find(|&c| !c.is_whitespace()) {
                Some('{') => votes[0] += 1,
                Some('[') => votes[1] += 1,
                Some('(') => votes[2] += 1,
                _ => {}
            }
        }
    }

    // Insert a space before `{}`.
    // We prefer the last one when some votes equal.
    let (_vote, (bra, ket)) = votes
        .iter()
        .zip(&[(" {", "}"), ("[", "]"), ("(", ")")])
        .max_by_key(|&(&vote, _)| vote)
        .unwrap();
    (*bra, *ket)
}