ide_completion/completions/
flyimport.rs

1//! See [`import_on_the_fly`].
2use hir::{ItemInNs, ModuleDef};
3use ide_db::imports::{
4    import_assets::{ImportAssets, LocatedImport},
5    insert_use::ImportScope,
6};
7use itertools::Itertools;
8use syntax::{AstNode, SyntaxNode, ast};
9
10use crate::{
11    Completions,
12    config::AutoImportExclusionType,
13    context::{
14        CompletionContext, DotAccess, PathCompletionCtx, PathKind, PatternContext, Qualified,
15        TypeLocation,
16    },
17    render::{RenderContext, render_resolution_with_import, render_resolution_with_import_pat},
18};
19
20// Feature: Completion With Autoimport
21//
22// When completing names in the current scope, proposes additional imports from other modules or crates,
23// if they can be qualified in the scope, and their name contains all symbols from the completion input.
24//
25// To be considered applicable, the name must contain all input symbols in the given order, not necessarily adjacent.
26// If any input symbol is not lowercased, the name must contain all symbols in exact case; otherwise the containing is checked case-insensitively.
27//
28// ```
29// fn main() {
30//     pda$0
31// }
32// # pub mod std { pub mod marker { pub struct PhantomData { } } }
33// ```
34// ->
35// ```
36// use std::marker::PhantomData;
37//
38// fn main() {
39//     PhantomData
40// }
41// # pub mod std { pub mod marker { pub struct PhantomData { } } }
42// ```
43//
44// Also completes associated items, that require trait imports.
45// If any unresolved and/or partially-qualified path precedes the input, it will be taken into account.
46// Currently, only the imports with their import path ending with the whole qualifier will be proposed
47// (no fuzzy matching for qualifier).
48//
49// ```
50// mod foo {
51//     pub mod bar {
52//         pub struct Item;
53//
54//         impl Item {
55//             pub const TEST_ASSOC: usize = 3;
56//         }
57//     }
58// }
59//
60// fn main() {
61//     bar::Item::TEST_A$0
62// }
63// ```
64// ->
65// ```
66// use foo::bar;
67//
68// mod foo {
69//     pub mod bar {
70//         pub struct Item;
71//
72//         impl Item {
73//             pub const TEST_ASSOC: usize = 3;
74//         }
75//     }
76// }
77//
78// fn main() {
79//     bar::Item::TEST_ASSOC
80// }
81// ```
82//
83// NOTE: currently, if an assoc item comes from a trait that's not currently imported, and it also has an unresolved and/or partially-qualified path,
84// no imports will be proposed.
85//
86// #### Fuzzy search details
87//
88// To avoid an excessive amount of the results returned, completion input is checked for inclusion in the names only
89// (i.e. in `HashMap` in the `std::collections::HashMap` path).
90// For the same reasons, avoids searching for any path imports for inputs with their length less than 2 symbols
91// (but shows all associated items for any input length).
92//
93// #### Import configuration
94//
95// It is possible to configure how use-trees are merged with the `imports.granularity.group` setting.
96// Mimics the corresponding behavior of the `Auto Import` feature.
97//
98// #### LSP and performance implications
99//
100// The feature is enabled only if the LSP client supports LSP protocol version 3.16+ and reports the `additionalTextEdits`
101// (case-sensitive) resolve client capability in its client capabilities.
102// This way the server is able to defer the costly computations, doing them for a selected completion item only.
103// For clients with no such support, all edits have to be calculated on the completion request, including the fuzzy search completion ones,
104// which might be slow ergo the feature is automatically disabled.
105//
106// #### Feature toggle
107//
108// The feature can be forcefully turned off in the settings with the `rust-analyzer.completion.autoimport.enable` flag.
109// Note that having this flag set to `true` does not guarantee that the feature is enabled: your client needs to have the corresponding
110// capability enabled.
111pub(crate) fn import_on_the_fly_path(
112    acc: &mut Completions,
113    ctx: &CompletionContext<'_>,
114    path_ctx: &PathCompletionCtx<'_>,
115) -> Option<()> {
116    if !ctx.config.enable_imports_on_the_fly {
117        return None;
118    }
119    let qualified = match path_ctx {
120        PathCompletionCtx {
121            kind:
122                PathKind::Expr { .. }
123                | PathKind::Type { .. }
124                | PathKind::Attr { .. }
125                | PathKind::Derive { .. }
126                | PathKind::Item { .. }
127                | PathKind::Pat { .. },
128            qualified,
129            ..
130        } => qualified,
131        _ => return None,
132    };
133    let potential_import_name = import_name(ctx);
134    let qualifier = match qualified {
135        Qualified::With { path, .. } => Some(path.clone()),
136        _ => None,
137    };
138    let import_assets = import_assets_for_path(ctx, &potential_import_name, qualifier.clone())?;
139
140    import_on_the_fly(
141        acc,
142        ctx,
143        path_ctx,
144        import_assets,
145        qualifier.map(|it| it.syntax().clone()).or_else(|| ctx.original_token.parent())?,
146        potential_import_name,
147    )
148}
149
150pub(crate) fn import_on_the_fly_pat(
151    acc: &mut Completions,
152    ctx: &CompletionContext<'_>,
153    pattern_ctx: &PatternContext,
154) -> Option<()> {
155    if !ctx.config.enable_imports_on_the_fly {
156        return None;
157    }
158    if let PatternContext { record_pat: Some(_), .. } = pattern_ctx {
159        return None;
160    }
161
162    let potential_import_name = import_name(ctx);
163    let import_assets = import_assets_for_path(ctx, &potential_import_name, None)?;
164
165    import_on_the_fly_pat_(
166        acc,
167        ctx,
168        pattern_ctx,
169        import_assets,
170        ctx.original_token.parent()?,
171        potential_import_name,
172    )
173}
174
175pub(crate) fn import_on_the_fly_dot(
176    acc: &mut Completions,
177    ctx: &CompletionContext<'_>,
178    dot_access: &DotAccess<'_>,
179) -> Option<()> {
180    if !ctx.config.enable_imports_on_the_fly {
181        return None;
182    }
183    let receiver = dot_access.receiver.as_ref()?;
184    let ty = dot_access.receiver_ty.as_ref()?;
185    let potential_import_name = import_name(ctx);
186    let import_assets = ImportAssets::for_fuzzy_method_call(
187        ctx.module,
188        ty.original.clone(),
189        potential_import_name.clone(),
190        receiver.syntax().clone(),
191    )?;
192
193    import_on_the_fly_method(
194        acc,
195        ctx,
196        dot_access,
197        import_assets,
198        receiver.syntax().clone(),
199        potential_import_name,
200    )
201}
202
203fn import_on_the_fly(
204    acc: &mut Completions,
205    ctx: &CompletionContext<'_>,
206    path_ctx @ PathCompletionCtx { kind, .. }: &PathCompletionCtx<'_>,
207    import_assets: ImportAssets<'_>,
208    position: SyntaxNode,
209    potential_import_name: String,
210) -> Option<()> {
211    let _p = tracing::info_span!("import_on_the_fly", ?potential_import_name).entered();
212
213    ImportScope::find_insert_use_container(&position, &ctx.sema)?;
214
215    let ns_filter = |import: &LocatedImport| {
216        match (kind, import.original_item) {
217            // Aren't handled in flyimport
218            (PathKind::Vis { .. } | PathKind::Use, _) => false,
219            // modules are always fair game
220            (_, ItemInNs::Types(hir::ModuleDef::Module(_))) => true,
221            // and so are macros(except for attributes)
222            (
223                PathKind::Expr { .. }
224                | PathKind::Type { .. }
225                | PathKind::Item { .. }
226                | PathKind::Pat { .. },
227                ItemInNs::Macros(mac),
228            ) => mac.is_fn_like(ctx.db),
229            (PathKind::Item { .. }, ..) => false,
230
231            (PathKind::Expr { .. }, ItemInNs::Types(_) | ItemInNs::Values(_)) => true,
232
233            (PathKind::Pat { .. }, ItemInNs::Types(_)) => true,
234            (PathKind::Pat { .. }, ItemInNs::Values(def)) => {
235                matches!(def, hir::ModuleDef::Const(_))
236            }
237
238            (PathKind::Type { location }, ItemInNs::Types(ty)) => {
239                if matches!(location, TypeLocation::TypeBound) {
240                    matches!(ty, ModuleDef::Trait(_))
241                } else if matches!(location, TypeLocation::ImplTrait) {
242                    matches!(ty, ModuleDef::Trait(_) | ModuleDef::Module(_))
243                } else {
244                    true
245                }
246            }
247            (PathKind::Type { .. }, ItemInNs::Values(_)) => false,
248
249            (PathKind::Attr { .. }, ItemInNs::Macros(mac)) => mac.is_attr(ctx.db),
250            (PathKind::Attr { .. }, _) => false,
251
252            (PathKind::Derive { existing_derives }, ItemInNs::Macros(mac)) => {
253                mac.is_derive(ctx.db) && !existing_derives.contains(&mac)
254            }
255            (PathKind::Derive { .. }, _) => false,
256        }
257    };
258    let user_input_lowercased = potential_import_name.to_lowercase();
259
260    let import_cfg = ctx.config.import_path_config();
261
262    import_assets
263        .search_for_imports(&ctx.sema, import_cfg, ctx.config.insert_use.prefix_kind)
264        .filter(ns_filter)
265        .filter(|import| {
266            let original_item = &import.original_item;
267            !ctx.is_item_hidden(&import.item_to_import)
268                && !ctx.is_item_hidden(original_item)
269                && ctx.check_stability(original_item.attrs(ctx.db).as_ref())
270        })
271        .filter(|import| filter_excluded_flyimport(ctx, import))
272        .sorted_by(|a, b| {
273            let key = |import_path| {
274                (
275                    compute_fuzzy_completion_order_key(import_path, &user_input_lowercased),
276                    import_path,
277                )
278            };
279            key(&a.import_path).cmp(&key(&b.import_path))
280        })
281        .filter_map(|import| {
282            render_resolution_with_import(RenderContext::new(ctx), path_ctx, import)
283        })
284        .map(|builder| builder.build(ctx.db))
285        .for_each(|item| acc.add(item));
286    Some(())
287}
288
289fn import_on_the_fly_pat_(
290    acc: &mut Completions,
291    ctx: &CompletionContext<'_>,
292    pattern_ctx: &PatternContext,
293    import_assets: ImportAssets<'_>,
294    position: SyntaxNode,
295    potential_import_name: String,
296) -> Option<()> {
297    let _p = tracing::info_span!("import_on_the_fly_pat_", ?potential_import_name).entered();
298
299    ImportScope::find_insert_use_container(&position, &ctx.sema)?;
300
301    let ns_filter = |import: &LocatedImport| match import.original_item {
302        ItemInNs::Macros(mac) => mac.is_fn_like(ctx.db),
303        ItemInNs::Types(_) => true,
304        ItemInNs::Values(def) => matches!(def, hir::ModuleDef::Const(_)),
305    };
306    let user_input_lowercased = potential_import_name.to_lowercase();
307    let cfg = ctx.config.import_path_config();
308
309    import_assets
310        .search_for_imports(&ctx.sema, cfg, ctx.config.insert_use.prefix_kind)
311        .filter(ns_filter)
312        .filter(|import| {
313            let original_item = &import.original_item;
314            !ctx.is_item_hidden(&import.item_to_import)
315                && !ctx.is_item_hidden(original_item)
316                && ctx.check_stability(original_item.attrs(ctx.db).as_ref())
317        })
318        .sorted_by(|a, b| {
319            let key = |import_path| {
320                (
321                    compute_fuzzy_completion_order_key(import_path, &user_input_lowercased),
322                    import_path,
323                )
324            };
325            key(&a.import_path).cmp(&key(&b.import_path))
326        })
327        .filter_map(|import| {
328            render_resolution_with_import_pat(RenderContext::new(ctx), pattern_ctx, import)
329        })
330        .map(|builder| builder.build(ctx.db))
331        .for_each(|item| acc.add(item));
332    Some(())
333}
334
335fn import_on_the_fly_method(
336    acc: &mut Completions,
337    ctx: &CompletionContext<'_>,
338    dot_access: &DotAccess<'_>,
339    import_assets: ImportAssets<'_>,
340    position: SyntaxNode,
341    potential_import_name: String,
342) -> Option<()> {
343    let _p = tracing::info_span!("import_on_the_fly_method", ?potential_import_name).entered();
344
345    ImportScope::find_insert_use_container(&position, &ctx.sema)?;
346
347    let user_input_lowercased = potential_import_name.to_lowercase();
348
349    let cfg = ctx.config.import_path_config();
350
351    import_assets
352        .search_for_imports(&ctx.sema, cfg, ctx.config.insert_use.prefix_kind)
353        .filter(|import| {
354            !ctx.is_item_hidden(&import.item_to_import)
355                && !ctx.is_item_hidden(&import.original_item)
356        })
357        .filter(|import| filter_excluded_flyimport(ctx, import))
358        .sorted_by(|a, b| {
359            let key = |import_path| {
360                (
361                    compute_fuzzy_completion_order_key(import_path, &user_input_lowercased),
362                    import_path,
363                )
364            };
365            key(&a.import_path).cmp(&key(&b.import_path))
366        })
367        .for_each(|import| {
368            if let ItemInNs::Values(hir::ModuleDef::Function(f)) = import.original_item {
369                acc.add_method_with_import(ctx, dot_access, f, import);
370            }
371        });
372    Some(())
373}
374
375fn filter_excluded_flyimport(ctx: &CompletionContext<'_>, import: &LocatedImport) -> bool {
376    let def = import.item_to_import.into_module_def();
377    let is_exclude_flyimport = ctx.exclude_flyimport.get(&def).copied();
378
379    if matches!(is_exclude_flyimport, Some(AutoImportExclusionType::Always))
380        || !import.complete_in_flyimport.0
381    {
382        return false;
383    }
384    let method_imported = import.item_to_import != import.original_item;
385    if method_imported
386        && (is_exclude_flyimport.is_some()
387            || ctx.exclude_flyimport.contains_key(&import.original_item.into_module_def()))
388    {
389        // If this is a method, exclude it either if it was excluded itself (which may not be caught above,
390        // because `item_to_import` is the trait), or if its trait was excluded. We don't need to check
391        // the attributes here, since they pass from trait to methods on import map construction.
392        return false;
393    }
394    true
395}
396
397fn import_name(ctx: &CompletionContext<'_>) -> String {
398    let token_kind = ctx.token.kind();
399
400    if token_kind.is_any_identifier() { ctx.token.to_string() } else { String::new() }
401}
402
403fn import_assets_for_path<'db>(
404    ctx: &CompletionContext<'db>,
405    potential_import_name: &str,
406    qualifier: Option<ast::Path>,
407) -> Option<ImportAssets<'db>> {
408    let _p =
409        tracing::info_span!("import_assets_for_path", ?potential_import_name, ?qualifier).entered();
410
411    let fuzzy_name_length = potential_import_name.len();
412    let mut assets_for_path = ImportAssets::for_fuzzy_path(
413        ctx.module,
414        qualifier,
415        potential_import_name.to_owned(),
416        &ctx.sema,
417        ctx.token.parent()?,
418    )?;
419    if fuzzy_name_length == 0 {
420        // nothing matches the empty string exactly, but we still compute assoc items in this case
421        assets_for_path.path_fuzzy_name_to_exact();
422    } else if fuzzy_name_length < 3 {
423        cov_mark::hit!(flyimport_prefix_on_short_path);
424        assets_for_path.path_fuzzy_name_to_prefix();
425    }
426    Some(assets_for_path)
427}
428
429fn compute_fuzzy_completion_order_key(
430    proposed_mod_path: &hir::ModPath,
431    user_input_lowercased: &str,
432) -> usize {
433    cov_mark::hit!(certain_fuzzy_order_test);
434    let import_name = match proposed_mod_path.segments().last() {
435        // FIXME: nasty alloc, this is a hot path!
436        Some(name) => name.as_str().to_ascii_lowercase(),
437        None => return usize::MAX,
438    };
439    match import_name.match_indices(user_input_lowercased).next() {
440        Some((first_matching_index, _)) => first_matching_index,
441        None => usize::MAX,
442    }
443}