2021-09-13 13:43:13 +03:00
|
|
|
//! See [`import_on_the_fly`].
|
2021-01-17 00:53:15 +02:00
|
|
|
use ide_db::helpers::{
|
|
|
|
import_assets::{ImportAssets, ImportCandidate},
|
|
|
|
insert_use::ImportScope,
|
|
|
|
};
|
2021-03-03 01:26:53 +02:00
|
|
|
use itertools::Itertools;
|
2021-01-17 00:53:15 +02:00
|
|
|
use syntax::{AstNode, SyntaxNode, T};
|
2021-01-16 19:33:36 +02:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
context::CompletionContext,
|
|
|
|
render::{render_resolution_with_import, RenderContext},
|
|
|
|
ImportEdit,
|
|
|
|
};
|
|
|
|
|
|
|
|
use super::Completions;
|
|
|
|
|
2021-09-13 13:43:13 +03:00
|
|
|
// Feature: Completion With Autoimport
|
|
|
|
//
|
|
|
|
// When completing names in the current scope, proposes additional imports from other modules or crates,
|
|
|
|
// if they can be qualified in the scope, and their name contains all symbols from the completion input.
|
|
|
|
//
|
|
|
|
// To be considered applicable, the name must contain all input symbols in the given order, not necessarily adjacent.
|
|
|
|
// If any input symbol is not lowercased, the name must contain all symbols in exact case; otherwise the containing is checked case-insensitively.
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// fn main() {
|
|
|
|
// pda$0
|
|
|
|
// }
|
|
|
|
// # pub mod std { pub mod marker { pub struct PhantomData { } } }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// use std::marker::PhantomData;
|
|
|
|
//
|
|
|
|
// fn main() {
|
|
|
|
// PhantomData
|
|
|
|
// }
|
|
|
|
// # pub mod std { pub mod marker { pub struct PhantomData { } } }
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// Also completes associated items, that require trait imports.
|
|
|
|
// If any unresolved and/or partially-qualified path precedes the input, it will be taken into account.
|
|
|
|
// Currently, only the imports with their import path ending with the whole qualifier will be proposed
|
|
|
|
// (no fuzzy matching for qualifier).
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// mod foo {
|
|
|
|
// pub mod bar {
|
|
|
|
// pub struct Item;
|
|
|
|
//
|
|
|
|
// impl Item {
|
|
|
|
// pub const TEST_ASSOC: usize = 3;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// fn main() {
|
|
|
|
// bar::Item::TEST_A$0
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// use foo::bar;
|
|
|
|
//
|
|
|
|
// mod foo {
|
|
|
|
// pub mod bar {
|
|
|
|
// pub struct Item;
|
|
|
|
//
|
|
|
|
// impl Item {
|
|
|
|
// pub const TEST_ASSOC: usize = 3;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// fn main() {
|
|
|
|
// bar::Item::TEST_ASSOC
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// 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,
|
|
|
|
// no imports will be proposed.
|
|
|
|
//
|
|
|
|
// .Fuzzy search details
|
|
|
|
//
|
|
|
|
// To avoid an excessive amount of the results returned, completion input is checked for inclusion in the names only
|
|
|
|
// (i.e. in `HashMap` in the `std::collections::HashMap` path).
|
|
|
|
// For the same reasons, avoids searching for any path imports for inputs with their length less than 2 symbols
|
|
|
|
// (but shows all associated items for any input length).
|
|
|
|
//
|
|
|
|
// .Import configuration
|
|
|
|
//
|
|
|
|
// It is possible to configure how use-trees are merged with the `importMergeBehavior` setting.
|
|
|
|
// Mimics the corresponding behavior of the `Auto Import` feature.
|
|
|
|
//
|
|
|
|
// .LSP and performance implications
|
|
|
|
//
|
|
|
|
// The feature is enabled only if the LSP client supports LSP protocol version 3.16+ and reports the `additionalTextEdits`
|
|
|
|
// (case-sensitive) resolve client capability in its client capabilities.
|
|
|
|
// This way the server is able to defer the costly computations, doing them for a selected completion item only.
|
|
|
|
// For clients with no such support, all edits have to be calculated on the completion request, including the fuzzy search completion ones,
|
|
|
|
// which might be slow ergo the feature is automatically disabled.
|
|
|
|
//
|
|
|
|
// .Feature toggle
|
|
|
|
//
|
|
|
|
// The feature can be forcefully turned off in the settings with the `rust-analyzer.completion.autoimport.enable` flag.
|
|
|
|
// Note that having this flag set to `true` does not guarantee that the feature is enabled: your client needs to have the corresponding
|
|
|
|
// capability enabled.
|
2021-01-16 19:33:36 +02:00
|
|
|
pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
|
2021-01-05 10:34:03 +02:00
|
|
|
if !ctx.config.enable_imports_on_the_fly {
|
2021-01-16 19:33:36 +02:00
|
|
|
return None;
|
|
|
|
}
|
2021-06-17 13:56:55 +02:00
|
|
|
if ctx.in_use_tree()
|
2021-05-27 04:34:21 +02:00
|
|
|
|| ctx.is_path_disallowed()
|
|
|
|
|| ctx.expects_item()
|
|
|
|
|| ctx.expects_assoc_item()
|
|
|
|
{
|
2021-01-16 19:33:36 +02:00
|
|
|
return None;
|
|
|
|
}
|
2021-01-17 00:53:15 +02:00
|
|
|
let potential_import_name = {
|
|
|
|
let token_kind = ctx.token.kind();
|
2021-01-18 21:20:02 +02:00
|
|
|
if matches!(token_kind, T![.] | T![::]) {
|
2021-01-17 00:53:15 +02:00
|
|
|
String::new()
|
|
|
|
} else {
|
|
|
|
ctx.token.to_string()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-28 19:19:30 +02:00
|
|
|
let _p = profile::span("import_on_the_fly").detail(|| potential_import_name.clone());
|
2021-01-16 19:33:36 +02:00
|
|
|
|
|
|
|
let user_input_lowercased = potential_import_name.to_lowercase();
|
2021-01-17 00:53:15 +02:00
|
|
|
let import_assets = import_assets(ctx, potential_import_name)?;
|
2021-11-03 21:12:36 +01:00
|
|
|
let import_scope = ImportScope::find_insert_use_container(
|
2021-10-28 19:19:30 +02:00
|
|
|
&position_for_import(ctx, Some(import_assets.import_candidate()))?,
|
2021-01-17 00:53:15 +02:00
|
|
|
&ctx.sema,
|
|
|
|
)?;
|
2021-02-20 13:53:50 +02:00
|
|
|
|
2021-03-03 01:26:53 +02:00
|
|
|
acc.add_all(
|
|
|
|
import_assets
|
|
|
|
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind)
|
|
|
|
.into_iter()
|
2021-07-28 19:22:59 +02:00
|
|
|
.filter(|import| {
|
|
|
|
!ctx.is_item_hidden(&import.item_to_import)
|
|
|
|
&& !ctx.is_item_hidden(&import.original_item)
|
|
|
|
})
|
2021-03-03 01:26:53 +02:00
|
|
|
.sorted_by_key(|located_import| {
|
|
|
|
compute_fuzzy_completion_order_key(
|
|
|
|
&located_import.import_path,
|
|
|
|
&user_input_lowercased,
|
|
|
|
)
|
2021-03-01 13:55:47 +02:00
|
|
|
})
|
2021-03-03 01:26:53 +02:00
|
|
|
.filter_map(|import| {
|
|
|
|
render_resolution_with_import(
|
|
|
|
RenderContext::new(ctx),
|
2021-03-03 23:55:21 +02:00
|
|
|
ImportEdit { import, scope: import_scope.clone() },
|
2021-03-03 01:26:53 +02:00
|
|
|
)
|
|
|
|
}),
|
|
|
|
);
|
2021-01-16 19:33:36 +02:00
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
2021-10-28 19:19:30 +02:00
|
|
|
pub(crate) fn position_for_import(
|
|
|
|
ctx: &CompletionContext,
|
2021-01-17 02:52:36 +02:00
|
|
|
import_candidate: Option<&ImportCandidate>,
|
2021-10-28 19:19:30 +02:00
|
|
|
) -> Option<SyntaxNode> {
|
|
|
|
Some(
|
|
|
|
match import_candidate {
|
|
|
|
Some(ImportCandidate::Path(_)) => ctx.name_syntax.as_ref()?.syntax(),
|
|
|
|
Some(ImportCandidate::TraitAssocItem(_)) => ctx.path_qual()?.syntax(),
|
|
|
|
Some(ImportCandidate::TraitMethod(_)) => ctx.dot_receiver()?.syntax(),
|
|
|
|
None => return ctx.original_token.parent(),
|
|
|
|
}
|
|
|
|
.clone(),
|
|
|
|
)
|
2021-01-17 00:53:15 +02:00
|
|
|
}
|
|
|
|
|
2021-03-08 00:25:45 +02:00
|
|
|
fn import_assets(ctx: &CompletionContext, fuzzy_name: String) -> Option<ImportAssets> {
|
2021-01-05 10:34:03 +02:00
|
|
|
let current_module = ctx.scope.module()?;
|
2021-06-02 15:21:18 +02:00
|
|
|
if let Some(dot_receiver) = ctx.dot_receiver() {
|
2021-01-05 10:34:03 +02:00
|
|
|
ImportAssets::for_fuzzy_method_call(
|
|
|
|
current_module,
|
2021-08-03 17:28:51 +02:00
|
|
|
ctx.sema.type_of_expr(dot_receiver)?.original,
|
2021-01-05 10:34:03 +02:00
|
|
|
fuzzy_name,
|
2021-03-08 00:25:45 +02:00
|
|
|
dot_receiver.syntax().clone(),
|
2021-01-05 10:34:03 +02:00
|
|
|
)
|
|
|
|
} else {
|
2021-01-17 00:53:15 +02:00
|
|
|
let fuzzy_name_length = fuzzy_name.len();
|
|
|
|
let assets_for_path = ImportAssets::for_fuzzy_path(
|
|
|
|
current_module,
|
2021-06-07 12:29:46 +02:00
|
|
|
ctx.path_qual().cloned(),
|
2021-01-17 00:53:15 +02:00
|
|
|
fuzzy_name,
|
|
|
|
&ctx.sema,
|
2021-08-02 19:56:32 +02:00
|
|
|
ctx.token.parent()?,
|
2021-02-20 23:32:21 +02:00
|
|
|
)?;
|
2021-01-17 00:53:15 +02:00
|
|
|
|
2021-02-20 23:32:21 +02:00
|
|
|
if matches!(assets_for_path.import_candidate(), ImportCandidate::Path(_))
|
2021-01-17 00:53:15 +02:00
|
|
|
&& fuzzy_name_length < 2
|
|
|
|
{
|
2021-03-08 22:19:44 +02:00
|
|
|
cov_mark::hit!(ignore_short_input_for_path);
|
2021-01-17 00:53:15 +02:00
|
|
|
None
|
|
|
|
} else {
|
2021-02-20 23:32:21 +02:00
|
|
|
Some(assets_for_path)
|
2021-01-17 00:53:15 +02:00
|
|
|
}
|
2021-01-05 10:34:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-28 19:19:30 +02:00
|
|
|
pub(crate) fn compute_fuzzy_completion_order_key(
|
2021-06-08 16:50:10 +02:00
|
|
|
proposed_mod_path: &hir::ModPath,
|
2021-01-16 19:33:36 +02:00
|
|
|
user_input_lowercased: &str,
|
|
|
|
) -> usize {
|
2021-03-08 22:19:44 +02:00
|
|
|
cov_mark::hit!(certain_fuzzy_order_test);
|
2021-02-25 01:06:31 +02:00
|
|
|
let import_name = match proposed_mod_path.segments().last() {
|
2021-01-16 19:33:36 +02:00
|
|
|
Some(name) => name.to_string().to_lowercase(),
|
|
|
|
None => return usize::MAX,
|
|
|
|
};
|
2021-02-25 01:06:31 +02:00
|
|
|
match import_name.match_indices(user_input_lowercased).next() {
|
2021-01-16 19:33:36 +02:00
|
|
|
Some((first_matching_index, _)) => first_matching_index,
|
|
|
|
None => usize::MAX,
|
|
|
|
}
|
|
|
|
}
|