|
| 1 | +//! Injected completions for `#[rust_analyzer::rust_fixture]`. |
| 2 | +
|
| 3 | +use hir::FilePositionWrapper; |
| 4 | +use ide_db::{ |
| 5 | + impl_empty_upmap_from_ra_fixture, |
| 6 | + ra_fixture::{RaFixtureAnalysis, UpmapFromRaFixture}, |
| 7 | +}; |
| 8 | +use syntax::ast; |
| 9 | + |
| 10 | +use crate::{ |
| 11 | + CompletionItemKind, CompletionItemRefMode, CompletionRelevance, completions::Completions, |
| 12 | + context::CompletionContext, item::CompletionItemLabel, |
| 13 | +}; |
| 14 | + |
| 15 | +pub(crate) fn complete_ra_fixture( |
| 16 | + acc: &mut Completions, |
| 17 | + ctx: &CompletionContext<'_>, |
| 18 | + original: &ast::String, |
| 19 | + expanded: &ast::String, |
| 20 | +) -> Option<()> { |
| 21 | + let analysis = RaFixtureAnalysis::analyze_ra_fixture( |
| 22 | + &ctx.sema, |
| 23 | + original.clone(), |
| 24 | + expanded, |
| 25 | + ctx.config.minicore, |
| 26 | + &mut |_| {}, |
| 27 | + )?; |
| 28 | + let (virtual_file_id, virtual_offset) = analysis.map_offset_down(ctx.position.offset)?; |
| 29 | + let completions = hir::attach_db_allow_change(&analysis.db, || { |
| 30 | + crate::completions( |
| 31 | + &analysis.db, |
| 32 | + ctx.config, |
| 33 | + FilePositionWrapper { file_id: virtual_file_id, offset: virtual_offset }, |
| 34 | + ctx.trigger_character, |
| 35 | + ) |
| 36 | + })?; |
| 37 | + let completions = |
| 38 | + completions.upmap_from_ra_fixture(&analysis, virtual_file_id, ctx.position.file_id).ok()?; |
| 39 | + acc.add_many(completions); |
| 40 | + Some(()) |
| 41 | +} |
| 42 | + |
| 43 | +impl_empty_upmap_from_ra_fixture!( |
| 44 | + CompletionItemLabel, |
| 45 | + CompletionItemKind, |
| 46 | + CompletionRelevance, |
| 47 | + CompletionItemRefMode, |
| 48 | +); |
| 49 | + |
| 50 | +#[cfg(test)] |
| 51 | +mod tests { |
| 52 | + use expect_test::expect; |
| 53 | + |
| 54 | + use crate::tests::check; |
| 55 | + |
| 56 | + #[test] |
| 57 | + fn it_works() { |
| 58 | + check( |
| 59 | + r##" |
| 60 | +fn fixture(#[rust_analyzer::rust_fixture] ra_fixture: &str) {} |
| 61 | +
|
| 62 | +fn foo() { |
| 63 | + fixture(r#" |
| 64 | +fn complete_me() {} |
| 65 | +
|
| 66 | +fn baz() { |
| 67 | + let foo_bar_baz = 123; |
| 68 | + f$0 |
| 69 | +} |
| 70 | + "#); |
| 71 | +} |
| 72 | + "##, |
| 73 | + expect![[r#" |
| 74 | + fn baz() fn() |
| 75 | + fn complete_me() fn() |
| 76 | + lc foo_bar_baz i32 |
| 77 | + bt u32 u32 |
| 78 | + kw async |
| 79 | + kw const |
| 80 | + kw crate:: |
| 81 | + kw enum |
| 82 | + kw extern |
| 83 | + kw false |
| 84 | + kw fn |
| 85 | + kw for |
| 86 | + kw if |
| 87 | + kw if let |
| 88 | + kw impl |
| 89 | + kw impl for |
| 90 | + kw let |
| 91 | + kw letm |
| 92 | + kw loop |
| 93 | + kw match |
| 94 | + kw mod |
| 95 | + kw return |
| 96 | + kw self:: |
| 97 | + kw static |
| 98 | + kw struct |
| 99 | + kw trait |
| 100 | + kw true |
| 101 | + kw type |
| 102 | + kw union |
| 103 | + kw unsafe |
| 104 | + kw use |
| 105 | + kw while |
| 106 | + kw while let |
| 107 | + sn macro_rules |
| 108 | + sn pd |
| 109 | + sn ppd |
| 110 | + "#]], |
| 111 | + ); |
| 112 | + } |
| 113 | +} |
0 commit comments