aboutsummaryrefslogtreecommitdiff
path: root/crates/completion/src/render/pattern.rs
blob: cfe2116bd2c29d2bf4d84b705525c626685b45a6 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//! Renderer for patterns.

use hir::{db::HirDatabase, HasVisibility, Name, StructKind};
use itertools::Itertools;

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

pub(crate) fn render_struct_pat(
    ctx: RenderContext<'_>,
    strukt: hir::Struct,
    local_name: Option<Name>,
) -> Option<CompletionItem> {
    let _p = profile::span("render_struct_pat");

    let module = ctx.completion.scope.module()?;
    let fields = strukt.fields(ctx.db());
    let n_fields = fields.len();
    let fields = fields
        .into_iter()
        .filter(|field| field.is_visible_from(ctx.db(), module))
        .collect::<Vec<_>>();

    if fields.is_empty() {
        // Matching a struct without matching its fields is pointless, unlike matching a Variant without its fields
        return None;
    }
    let fields_omitted = n_fields - fields.len() > 0;

    let name = local_name.unwrap_or_else(|| strukt.name(ctx.db())).to_string();
    let pat = render_pat(&ctx, &name, strukt.kind(ctx.db()), &fields, fields_omitted)?;

    let mut completion = CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), name)
        .kind(CompletionItemKind::Binding)
        .set_documentation(ctx.docs(strukt))
        .set_deprecated(ctx.is_deprecated(strukt))
        .detail(&pat);
    if let Some(snippet_cap) = ctx.snippet_cap() {
        completion = completion.insert_snippet(snippet_cap, pat);
    } else {
        completion = completion.insert_text(pat);
    }
    Some(completion.build())
}

pub(crate) fn render_variant_pat(
    ctx: RenderContext<'_>,
    variant: hir::Variant,
    local_name: Option<Name>,
) -> Option<CompletionItem> {
    let _p = profile::span("render_variant_pat");

    let module = ctx.completion.scope.module()?;
    let fields = variant.fields(ctx.db());
    let n_fields = fields.len();
    let fields = fields
        .into_iter()
        .filter(|field| field.is_visible_from(ctx.db(), module))
        .collect::<Vec<_>>();

    let fields_omitted = n_fields - fields.len() > 0;

    let name = local_name.unwrap_or_else(|| variant.name(ctx.db())).to_string();
    let pat = render_pat(&ctx, &name, variant.kind(ctx.db()), &fields, fields_omitted)?;

    let mut completion = CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), name)
        .kind(CompletionItemKind::Binding)
        .set_documentation(ctx.docs(variant))
        .set_deprecated(ctx.is_deprecated(variant))
        .detail(&pat);
    if let Some(snippet_cap) = ctx.snippet_cap() {
        completion = completion.insert_snippet(snippet_cap, pat);
    } else {
        completion = completion.insert_text(pat);
    }
    Some(completion.build())
}

fn render_pat(
    ctx: &RenderContext<'_>,
    name: &str,
    kind: StructKind,
    fields: &[hir::Field],
    fields_omitted: bool,
) -> Option<String> {
    let mut pat = match kind {
        StructKind::Tuple if ctx.snippet_cap().is_some() => {
            render_tuple_as_pat(&fields, &name, fields_omitted)
        }
        StructKind::Record => {
            render_record_as_pat(ctx.db(), ctx.snippet_cap(), &fields, &name, fields_omitted)
        }
        _ => return None,
    };

    if ctx.completion.is_param {
        pat.push(':');
        pat.push(' ');
        pat.push_str(&name);
    }
    if ctx.snippet_cap().is_some() {
        pat.push_str("$0");
    }
    Some(pat)
}

fn render_record_as_pat(
    db: &dyn HirDatabase,
    snippet_cap: Option<SnippetCap>,
    fields: &[hir::Field],
    name: &str,
    fields_omitted: bool,
) -> String {
    let fields = fields.iter();
    if snippet_cap.is_some() {
        format!(
            "{name} {{ {}{} }}",
            fields
                .enumerate()
                .map(|(idx, field)| format!("${{{}:{}}}", idx + 1, field.name(db)))
                .format(", "),
            if fields_omitted { ", .." } else { "" },
            name = name
        )
    } else {
        format!(
            "{name} {{ {}{} }}",
            fields.map(|field| field.name(db)).format(", "),
            if fields_omitted { ", .." } else { "" },
            name = name
        )
    }
}

fn render_tuple_as_pat(fields: &[hir::Field], name: &str, fields_omitted: bool) -> String {
    format!(
        "{name}({}{})",
        fields.iter().enumerate().map(|(idx, _)| format!("${}", idx + 1)).format(", "),
        if fields_omitted { ", .." } else { "" },
        name = name
    )
}