ide_completion/completions/
extern_abi.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
//! Completes function abi strings.
use syntax::{
    ast::{self, IsString},
    AstNode, AstToken, SmolStr,
};

use crate::{
    completions::Completions, context::CompletionContext, CompletionItem, CompletionItemKind,
};

// Most of these are feature gated, we should filter/add feature gate completions once we have them.
const SUPPORTED_CALLING_CONVENTIONS: &[&str] = &[
    "Rust",
    "C",
    "C-unwind",
    "cdecl",
    "stdcall",
    "stdcall-unwind",
    "fastcall",
    "vectorcall",
    "thiscall",
    "thiscall-unwind",
    "aapcs",
    "win64",
    "sysv64",
    "ptx-kernel",
    "msp430-interrupt",
    "x86-interrupt",
    "efiapi",
    "avr-interrupt",
    "avr-non-blocking-interrupt",
    "riscv-interrupt-m",
    "riscv-interrupt-s",
    "C-cmse-nonsecure-call",
    "C-cmse-nonsecure-entry",
    "wasm",
    "system",
    "system-unwind",
    "rust-intrinsic",
    "rust-call",
    "unadjusted",
];

pub(crate) fn complete_extern_abi(
    acc: &mut Completions,
    ctx: &CompletionContext<'_>,
    expanded: &ast::String,
) -> Option<()> {
    if !expanded.syntax().parent().map_or(false, |it| ast::Abi::can_cast(it.kind())) {
        return None;
    }
    let abi_str = expanded;
    let source_range = abi_str.text_range_between_quotes()?;
    for &abi in SUPPORTED_CALLING_CONVENTIONS {
        CompletionItem::new(
            CompletionItemKind::Keyword,
            source_range,
            SmolStr::new_static(abi),
            ctx.edition,
        )
        .add_to(acc, ctx.db);
    }
    Some(())
}

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

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

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

    #[test]
    fn only_completes_in_string_literals() {
        check(
            r#"
$0 fn foo {}
"#,
            expect![[]],
        );
    }

    #[test]
    fn requires_extern_prefix() {
        check(
            r#"
"$0" fn foo {}
"#,
            expect![[]],
        );
    }

    #[test]
    fn works() {
        check(
            r#"
extern "$0" fn foo {}
"#,
            expect![[]],
        );
        check_edit(
            "Rust",
            r#"
extern "$0" fn foo {}
"#,
            r#"
extern "Rust" fn foo {}
"#,
        );
    }
}