Skip to content

Commit 1e20331

Browse files
authored
Merge pull request #20855 from ChayimFriedman2/improve-fixture2
feat: Improve fixture support
2 parents d135cab + 81e621a commit 1e20331

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1652
-512
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ide-completion/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ syntax.workspace = true
2828
# completions crate should depend only on the top-level `hir` package. if you need
2929
# something from some `hir-xxx` subpackage, reexport the API via `hir`.
3030
hir.workspace = true
31+
macros.workspace = true
3132

3233
[dev-dependencies]
3334
expect-test = "1.5.1"

crates/ide-completion/src/completions.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub(crate) mod lifetime;
1616
pub(crate) mod mod_;
1717
pub(crate) mod pattern;
1818
pub(crate) mod postfix;
19+
pub(crate) mod ra_fixture;
1920
pub(crate) mod record;
2021
pub(crate) mod snippet;
2122
pub(crate) mod r#type;
@@ -74,6 +75,10 @@ impl Completions {
7475
self.buf.push(item)
7576
}
7677

78+
fn add_many(&mut self, items: impl IntoIterator<Item = CompletionItem>) {
79+
self.buf.extend(items)
80+
}
81+
7782
fn add_opt(&mut self, item: Option<CompletionItem>) {
7883
if let Some(item) = item {
7984
self.buf.push(item)
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
}

crates/ide-completion/src/config.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
77
use hir::FindPathConfig;
88
use ide_db::{
9-
SnippetCap,
9+
MiniCore, SnippetCap,
1010
imports::{import_assets::ImportPathConfig, insert_use::InsertUseConfig},
1111
};
1212

1313
use crate::{CompletionFieldsToResolve, snippet::Snippet};
1414

15-
#[derive(Clone, Debug, PartialEq, Eq)]
15+
#[derive(Clone, Debug)]
1616
pub struct CompletionConfig<'a> {
1717
pub enable_postfix_completions: bool,
1818
pub enable_imports_on_the_fly: bool,
@@ -35,6 +35,7 @@ pub struct CompletionConfig<'a> {
3535
pub fields_to_resolve: CompletionFieldsToResolve,
3636
pub exclude_flyimport: Vec<(String, AutoImportExclusionType)>,
3737
pub exclude_traits: &'a [String],
38+
pub minicore: MiniCore<'a>,
3839
}
3940

4041
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]

crates/ide-completion/src/context.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ pub(crate) struct CompletionContext<'a> {
440440
pub(crate) config: &'a CompletionConfig<'a>,
441441
pub(crate) position: FilePosition,
442442

443+
pub(crate) trigger_character: Option<char>,
443444
/// The token before the cursor, in the original file.
444445
pub(crate) original_token: SyntaxToken,
445446
/// The token before the cursor, in the macro-expanded file.
@@ -703,6 +704,7 @@ impl<'db> CompletionContext<'db> {
703704
db: &'db RootDatabase,
704705
position @ FilePosition { file_id, offset }: FilePosition,
705706
config: &'db CompletionConfig<'db>,
707+
trigger_character: Option<char>,
706708
) -> Option<(CompletionContext<'db>, CompletionAnalysis<'db>)> {
707709
let _p = tracing::info_span!("CompletionContext::new").entered();
708710
let sema = Semantics::new(db);
@@ -871,6 +873,7 @@ impl<'db> CompletionContext<'db> {
871873
db,
872874
config,
873875
position,
876+
trigger_character,
874877
original_token,
875878
token,
876879
krate,

crates/ide-completion/src/context/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn check_expected_type_and_name(#[rust_analyzer::rust_fixture] ra_fixture: &str,
1010
let (db, pos) = position(ra_fixture);
1111
let config = TEST_CONFIG;
1212
let (completion_context, _analysis) =
13-
hir::attach_db(&db, || CompletionContext::new(&db, pos, &config).unwrap());
13+
hir::attach_db(&db, || CompletionContext::new(&db, pos, &config, None).unwrap());
1414

1515
let ty = completion_context
1616
.expected_type

crates/ide-completion/src/item.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use ide_db::{
99
imports::import_assets::LocatedImport,
1010
};
1111
use itertools::Itertools;
12+
use macros::UpmapFromRaFixture;
1213
use smallvec::SmallVec;
1314
use stdx::{format_to, impl_from, never};
1415
use syntax::{Edition, SmolStr, TextRange, TextSize, format_smolstr};
@@ -23,7 +24,7 @@ use crate::{
2324
///
2425
/// It is basically a POD with various properties. To construct a [`CompletionItem`],
2526
/// use [`Builder::new`] method and the [`Builder`] struct.
26-
#[derive(Clone)]
27+
#[derive(Clone, UpmapFromRaFixture)]
2728
#[non_exhaustive]
2829
pub struct CompletionItem {
2930
/// Label in the completion pop up which identifies completion.

crates/ide-completion/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ pub fn completions(
187187
position: FilePosition,
188188
trigger_character: Option<char>,
189189
) -> Option<Vec<CompletionItem>> {
190-
let (ctx, analysis) = &CompletionContext::new(db, position, config)?;
190+
let (ctx, analysis) = &CompletionContext::new(db, position, config, trigger_character)?;
191191
let mut completions = Completions::default();
192192

193193
// prevent `(` from triggering unwanted completion noise
@@ -241,6 +241,7 @@ pub fn completions(
241241
completions::extern_abi::complete_extern_abi(acc, ctx, expanded);
242242
completions::format_string::format_string(acc, ctx, original, expanded);
243243
completions::env_vars::complete_cargo_env_vars(acc, ctx, original, expanded);
244+
completions::ra_fixture::complete_ra_fixture(acc, ctx, original, expanded);
244245
}
245246
CompletionAnalysis::UnexpandedAttrTT {
246247
colon_prefix,

crates/ide-completion/src/tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use expect_test::Expect;
2929
use hir::db::HirDatabase;
3030
use hir::{PrefixKind, setup_tracing};
3131
use ide_db::{
32-
FilePosition, RootDatabase, SnippetCap,
32+
FilePosition, MiniCore, RootDatabase, SnippetCap,
3333
imports::insert_use::{ImportGranularity, InsertUseConfig},
3434
};
3535
use itertools::Itertools;
@@ -90,6 +90,7 @@ pub(crate) const TEST_CONFIG: CompletionConfig<'_> = CompletionConfig {
9090
exclude_traits: &[],
9191
enable_auto_await: true,
9292
enable_auto_iter: true,
93+
minicore: MiniCore::default(),
9394
};
9495

9596
pub(crate) fn completion_list(#[rust_analyzer::rust_fixture] ra_fixture: &str) -> String {

0 commit comments

Comments
 (0)