Unnamed repository; edit this file 'description' to name the repository.
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
//! Injected completions for `#[rust_analyzer::rust_fixture]`.

use hir::FilePositionWrapper;
use ide_db::{
    impl_empty_upmap_from_ra_fixture,
    ra_fixture::{RaFixtureAnalysis, UpmapFromRaFixture},
};
use syntax::ast;

use crate::{
    CompletionItemKind, CompletionItemRefMode, CompletionRelevance, completions::Completions,
    context::CompletionContext, item::CompletionItemLabel,
};

pub(crate) fn complete_ra_fixture(
    acc: &mut Completions,
    ctx: &CompletionContext<'_>,
    original: &ast::String,
    expanded: &ast::String,
) -> Option<()> {
    let analysis = RaFixtureAnalysis::analyze_ra_fixture(
        &ctx.sema,
        original.clone(),
        expanded,
        ctx.config.minicore,
        &mut |_| {},
    )?;
    let (virtual_file_id, virtual_offset) = analysis.map_offset_down(ctx.position.offset)?;
    let completions = hir::attach_db_allow_change(&analysis.db, || {
        crate::completions(
            &analysis.db,
            ctx.config,
            FilePositionWrapper { file_id: virtual_file_id, offset: virtual_offset },
            ctx.trigger_character,
        )
    })?;
    let completions =
        completions.upmap_from_ra_fixture(&analysis, virtual_file_id, ctx.position.file_id).ok()?;
    acc.add_many(completions);
    Some(())
}

impl_empty_upmap_from_ra_fixture!(
    CompletionItemLabel,
    CompletionItemKind,
    CompletionRelevance,
    CompletionItemRefMode,
);

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

    use crate::tests::check;

    #[test]
    fn it_works() {
        check(
            r##"
fn fixture(#[rust_analyzer::rust_fixture] ra_fixture: &str) {}

fn foo() {
    fixture(r#"
fn complete_me() {}

fn baz() {
    let foo_bar_baz = 123;
    f$0
}
    "#);
}
        "##,
            expect![[r#"
                fn baz()         fn()
                fn complete_me() fn()
                lc foo_bar_baz    i32
                bt u32            u32
                kw async
                kw const
                kw crate::
                kw enum
                kw extern
                kw false
                kw fn
                kw for
                kw if
                kw if let
                kw impl
                kw impl for
                kw let
                kw letm
                kw loop
                kw match
                kw mod
                kw return
                kw self::
                kw static
                kw struct
                kw trait
                kw true
                kw type
                kw union
                kw unsafe
                kw use
                kw while
                kw while let
                sn macro_rules
                sn pd
                sn ppd
            "#]],
        );
    }
}