ide_completion/completions/
keyword.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//! Completes `where` and `for` keywords.

use syntax::ast::{self, Item};

use crate::{CompletionContext, Completions};

pub(crate) fn complete_for_and_where(
    acc: &mut Completions,
    ctx: &CompletionContext<'_>,
    keyword_item: &ast::Item,
) {
    let mut add_keyword = |kw, snippet| acc.add_keyword_snippet(ctx, kw, snippet);

    match keyword_item {
        Item::Impl(it) => {
            if it.for_token().is_none() && it.trait_().is_none() && it.self_ty().is_some() {
                add_keyword("for", "for $0");
            }
            add_keyword("where", "where $0");
        }
        Item::Enum(_)
        | Item::Fn(_)
        | Item::Struct(_)
        | Item::Trait(_)
        | Item::TypeAlias(_)
        | Item::Union(_) => {
            add_keyword("where", "where $0");
        }
        _ => (),
    }
}

#[cfg(test)]
mod tests {
    use expect_test::{expect, Expect};

    use crate::tests::{check_edit, completion_list};

    fn check(ra_fixture: &str, expect: Expect) {
        let actual = completion_list(ra_fixture);
        expect.assert_eq(&actual)
    }

    #[test]
    fn test_else_edit_after_if() {
        check_edit(
            "else",
            r#"fn quux() { if true { () } $0 }"#,
            r#"fn quux() { if true { () } else {
    $0
} }"#,
        );
    }

    #[test]
    fn test_keywords_after_unsafe_in_block_expr() {
        check(
            r"fn my_fn() { unsafe $0 }",
            expect![[r#"
                kw async
                kw extern
                kw fn
                kw impl
                kw trait
            "#]],
        );
    }

    #[test]
    fn test_completion_await_impls_future() {
        check(
            r#"
//- minicore: future
use core::future::*;
struct A {}
impl Future for A {}
fn foo(a: A) { a.$0 }
"#,
            expect![[r#"
                me into_future() (as IntoFuture) fn(self) -> <Self as IntoFuture>::IntoFuture
                kw await                                                           expr.await
                sn box                                                         Box::new(expr)
                sn call                                                        function(expr)
                sn dbg                                                             dbg!(expr)
                sn dbgr                                                           dbg!(&expr)
                sn deref                                                                *expr
                sn let                                                                    let
                sn letm                                                               let mut
                sn match                                                        match expr {}
                sn ref                                                                  &expr
                sn refm                                                             &mut expr
                sn return                                                         return expr
                sn unsafe                                                           unsafe {}
            "#]],
        );

        check(
            r#"
//- minicore: future
use std::future::*;
fn foo() {
    let a = async {};
    a.$0
}
"#,
            expect![[r#"
                me into_future() (use core::future::IntoFuture) fn(self) -> <Self as IntoFuture>::IntoFuture
                kw await                                                                          expr.await
                sn box                                                                        Box::new(expr)
                sn call                                                                       function(expr)
                sn dbg                                                                            dbg!(expr)
                sn dbgr                                                                          dbg!(&expr)
                sn deref                                                                               *expr
                sn let                                                                                   let
                sn letm                                                                              let mut
                sn match                                                                       match expr {}
                sn ref                                                                                 &expr
                sn refm                                                                            &mut expr
                sn return                                                                        return expr
                sn unsafe                                                                          unsafe {}
            "#]],
        );
    }

    #[test]
    fn test_completion_await_impls_into_future() {
        check(
            r#"
//- minicore: future
use core::future::*;
struct A {}
impl IntoFuture for A {}
fn foo(a: A) { a.$0 }
"#,
            expect![[r#"
                me into_future() (as IntoFuture) fn(self) -> <Self as IntoFuture>::IntoFuture
                kw await                                                           expr.await
                sn box                                                         Box::new(expr)
                sn call                                                        function(expr)
                sn dbg                                                             dbg!(expr)
                sn dbgr                                                           dbg!(&expr)
                sn deref                                                                *expr
                sn let                                                                    let
                sn letm                                                               let mut
                sn match                                                        match expr {}
                sn ref                                                                  &expr
                sn refm                                                             &mut expr
                sn return                                                         return expr
                sn unsafe                                                           unsafe {}
            "#]],
        );
    }

    #[test]
    fn for_in_impl() {
        check_edit(
            "for",
            r#"
struct X;
impl X $0 {}
"#,
            r#"
struct X;
impl X for $0 {}
"#,
        );
        check_edit(
            "for",
            r#"
fn foo() {
    struct X;
    impl X $0 {}
}
"#,
            r#"
fn foo() {
    struct X;
    impl X for $0 {}
}
"#,
        );
        check_edit(
            "for",
            r#"
fn foo() {
    struct X;
    impl X $0
}
"#,
            r#"
fn foo() {
    struct X;
    impl X for $0
}
"#,
        );
        check_edit(
            "for",
            r#"
fn foo() {
    struct X;
    impl X { fn bar() { $0 } }
}
"#,
            r#"
fn foo() {
    struct X;
    impl X { fn bar() { for $1 in $2 {
    $0
} } }
}
"#,
        );
    }

    #[test]
    fn let_semi() {
        cov_mark::check!(let_semi);
        check_edit(
            "match",
            r#"
fn main() { let x = $0 }
"#,
            r#"
fn main() { let x = match $1 {
    $0
}; }
"#,
        );

        check_edit(
            "if",
            r#"
fn main() {
    let x = $0
    let y = 92;
}
"#,
            r#"
fn main() {
    let x = if $1 {
    $0
};
    let y = 92;
}
"#,
        );

        check_edit(
            "loop",
            r#"
fn main() {
    let x = $0
    bar();
}
"#,
            r#"
fn main() {
    let x = loop {
    $0
};
    bar();
}
"#,
        );
    }

    #[test]
    fn if_completion_in_match_guard() {
        check_edit(
            "if",
            r"
fn main() {
    match () {
        () $0
    }
}
",
            r"
fn main() {
    match () {
        () if $0
    }
}
",
        )
    }

    #[test]
    fn if_completion_in_match_arm_expr() {
        check_edit(
            "if",
            r"
fn main() {
    match () {
        () => $0
    }
}
",
            r"
fn main() {
    match () {
        () => if $1 {
    $0
}
    }
}
",
        )
    }

    #[test]
    fn if_completion_in_match_arm_expr_block() {
        check_edit(
            "if",
            r"
fn main() {
    match () {
        () => {
            $0
        }
    }
}
",
            r"
fn main() {
    match () {
        () => {
            if $1 {
    $0
}
        }
    }
}
",
        )
    }
}