aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/completion/complete_record_literal.rs
blob: ed40299647289fb54afe3ad0c685f64ecf6adc7d (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
use hir::Substs;

use crate::completion::{CompletionContext, Completions};

/// Complete fields in fields literals.
pub(super) fn complete_record_literal(acc: &mut Completions, ctx: &CompletionContext) {
    let (ty, variant) = match ctx.record_lit_syntax.as_ref().and_then(|it| {
        Some((
            ctx.analyzer.type_of(ctx.db, &it.clone().into())?,
            ctx.analyzer.resolve_record_literal(it)?,
        ))
    }) {
        Some(it) => it,
        _ => return,
    };
    let substs = &ty.substs().unwrap_or_else(Substs::empty);

    for field in variant.fields(ctx.db) {
        acc.add_field(ctx, field, substs);
    }
}

#[cfg(test)]
mod tests {
    use crate::completion::{do_completion, CompletionItem, CompletionKind};
    use insta::assert_debug_snapshot;

    fn complete(code: &str) -> Vec<CompletionItem> {
        do_completion(code, CompletionKind::Reference)
    }

    #[test]
    fn test_record_literal_field() {
        let completions = complete(
            r"
            struct A { the_field: u32 }
            fn foo() {
               A { the<|> }
            }
            ",
        );
        assert_debug_snapshot!(completions, @r###"
       ⋮[
       ⋮    CompletionItem {
       ⋮        label: "the_field",
       ⋮        source_range: [83; 86),
       ⋮        delete: [83; 86),
       ⋮        insert: "the_field",
       ⋮        kind: Field,
       ⋮        detail: "u32",
       ⋮    },
       ⋮]
        "###);
    }

    #[test]
    fn test_record_literal_enum_variant() {
        let completions = complete(
            r"
            enum E {
                A { a: u32 }
            }
            fn foo() {
                let _ = E::A { <|> }
            }
            ",
        );
        assert_debug_snapshot!(completions, @r###"
       ⋮[
       ⋮    CompletionItem {
       ⋮        label: "a",
       ⋮        source_range: [119; 119),
       ⋮        delete: [119; 119),
       ⋮        insert: "a",
       ⋮        kind: Field,
       ⋮        detail: "u32",
       ⋮    },
       ⋮]
        "###);
    }

    #[test]
    fn test_record_literal_two_structs() {
        let completions = complete(
            r"
            struct A { a: u32 }
            struct B { b: u32 }

            fn foo() {
               let _: A = B { <|> }
            }
            ",
        );
        assert_debug_snapshot!(completions, @r###"
       ⋮[
       ⋮    CompletionItem {
       ⋮        label: "b",
       ⋮        source_range: [119; 119),
       ⋮        delete: [119; 119),
       ⋮        insert: "b",
       ⋮        kind: Field,
       ⋮        detail: "u32",
       ⋮    },
       ⋮]
        "###);
    }

    #[test]
    fn test_record_literal_generic_struct() {
        let completions = complete(
            r"
            struct A<T> { a: T }

            fn foo() {
               let _: A<u32> = A { <|> }
            }
            ",
        );
        assert_debug_snapshot!(completions, @r###"
       ⋮[
       ⋮    CompletionItem {
       ⋮        label: "a",
       ⋮        source_range: [93; 93),
       ⋮        delete: [93; 93),
       ⋮        insert: "a",
       ⋮        kind: Field,
       ⋮        detail: "u32",
       ⋮    },
       ⋮]
        "###);
    }
}