Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhuang committed Jan 4, 2024
1 parent c84352a commit 9963e3d
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 22 deletions.
2 changes: 2 additions & 0 deletions crates/ide-assists/src/handlers/auto_import.rs
Expand Up @@ -7,6 +7,7 @@ use ide_db::{
import_assets::{ImportAssets, ImportCandidate, LocatedImport},
insert_use::{insert_use, insert_use_as_alias, ImportScope},
},
items_locator::DEFAULT_QUERY_SEARCH_LIMIT,
};
use syntax::{ast, AstNode, NodeOrToken, SyntaxElement};

Expand Down Expand Up @@ -94,6 +95,7 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<
ctx.config.insert_use.prefix_kind,
ctx.config.prefer_no_std,
ctx.config.prefer_no_std,
Some(DEFAULT_QUERY_SEARCH_LIMIT.inner()),
);
if proposed_imports.is_empty() {
return None;
Expand Down
2 changes: 2 additions & 0 deletions crates/ide-assists/src/handlers/qualify_path.rs
@@ -1,6 +1,7 @@
use std::iter;

use hir::AsAssocItem;
use ide_db::items_locator::DEFAULT_QUERY_SEARCH_LIMIT;
use ide_db::RootDatabase;
use ide_db::{
helpers::mod_path_to_ast,
Expand Down Expand Up @@ -41,6 +42,7 @@ pub(crate) fn qualify_path(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
&ctx.sema,
ctx.config.prefer_no_std,
ctx.config.prefer_prelude,
Some(DEFAULT_QUERY_SEARCH_LIMIT.inner()),
);
if proposed_imports.is_empty() {
return None;
Expand Down
17 changes: 11 additions & 6 deletions crates/ide-completion/src/completions/flyimport.rs
Expand Up @@ -262,6 +262,7 @@ fn import_on_the_fly(
ctx.config.insert_use.prefix_kind,
ctx.config.prefer_no_std,
ctx.config.prefer_prelude,
ctx.config.query_search_limit,
)
.into_iter()
.filter(ns_filter)
Expand Down Expand Up @@ -309,6 +310,7 @@ fn import_on_the_fly_pat_(
ctx.config.insert_use.prefix_kind,
ctx.config.prefer_no_std,
ctx.config.prefer_prelude,
ctx.config.query_search_limit,
)
.into_iter()
.filter(ns_filter)
Expand Down Expand Up @@ -351,6 +353,7 @@ fn import_on_the_fly_method(
ctx.config.insert_use.prefix_kind,
ctx.config.prefer_no_std,
ctx.config.prefer_prelude,
ctx.config.query_search_limit,
)
.into_iter()
.filter(|import| {
Expand Down Expand Up @@ -391,12 +394,14 @@ fn import_assets_for_path(
&ctx.sema,
ctx.token.parent()?,
)?;
if fuzzy_name_length == 0 {
// nothing matches the empty string exactly, but we still compute assoc items in this case
assets_for_path.path_fuzzy_name_to_exact();
} else if fuzzy_name_length < 3 {
cov_mark::hit!(flyimport_prefix_on_short_path);
assets_for_path.path_fuzzy_name_to_prefix();
if !ctx.config.always_allow_fuzzy {
if fuzzy_name_length == 0 {
// nothing matches the empty string exactly, but we still compute assoc items in this case
assets_for_path.path_fuzzy_name_to_exact();
} else if fuzzy_name_length < 3 {
cov_mark::hit!(flyimport_prefix_on_short_path);
assets_for_path.path_fuzzy_name_to_prefix();
}
}
Some(assets_for_path)
}
Expand Down
2 changes: 2 additions & 0 deletions crates/ide-completion/src/config.rs
Expand Up @@ -22,6 +22,8 @@ pub struct CompletionConfig {
pub prefer_prelude: bool,
pub snippets: Vec<Snippet>,
pub limit: Option<usize>,
pub query_search_limit: Option<usize>,
pub always_allow_fuzzy: bool,
}

#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down
2 changes: 2 additions & 0 deletions crates/ide-completion/src/tests.rs
Expand Up @@ -79,6 +79,8 @@ pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig {
},
snippets: Vec::new(),
limit: None,
query_search_limit: Some(100),
always_allow_fuzzy: false,
};

pub(crate) fn completion_list(ra_fixture: &str) -> String {
Expand Down
27 changes: 17 additions & 10 deletions crates/ide-db/src/imports/import_assets.rs
Expand Up @@ -13,7 +13,7 @@ use syntax::{

use crate::{
helpers::item_name,
items_locator::{self, AssocSearchMode, DEFAULT_QUERY_SEARCH_LIMIT},
items_locator::{self, AssocSearchMode},
RootDatabase,
};

Expand Down Expand Up @@ -207,9 +207,10 @@ impl ImportAssets {
prefix_kind: PrefixKind,
prefer_no_std: bool,
prefer_prelude: bool,
limit: Option<usize>,
) -> Vec<LocatedImport> {
let _p = profile::span("import_assets::search_for_imports");
self.search_for(sema, Some(prefix_kind), prefer_no_std, prefer_prelude)
self.search_for(sema, Some(prefix_kind), prefer_no_std, prefer_prelude, limit)
}

/// This may return non-absolute paths if a part of the returned path is already imported into scope.
Expand All @@ -218,9 +219,10 @@ impl ImportAssets {
sema: &Semantics<'_, RootDatabase>,
prefer_no_std: bool,
prefer_prelude: bool,
limit: Option<usize>,
) -> Vec<LocatedImport> {
let _p = profile::span("import_assets::search_for_relative_paths");
self.search_for(sema, None, prefer_no_std, prefer_prelude)
self.search_for(sema, None, prefer_no_std, prefer_prelude, limit)
}

/// Requires imports to by prefix instead of fuzzily.
Expand Down Expand Up @@ -259,6 +261,7 @@ impl ImportAssets {
prefixed: Option<PrefixKind>,
prefer_no_std: bool,
prefer_prelude: bool,
limit: Option<usize>,
) -> Vec<LocatedImport> {
let _p = profile::span("import_assets::search_for");

Expand All @@ -282,13 +285,13 @@ impl ImportAssets {

match &self.import_candidate {
ImportCandidate::Path(path_candidate) => {
path_applicable_imports(sema, krate, path_candidate, mod_path)
path_applicable_imports(sema, krate, path_candidate, mod_path, limit)
}
ImportCandidate::TraitAssocItem(trait_candidate) => {
trait_applicable_items(sema, krate, &scope, trait_candidate, true, mod_path)
trait_applicable_items(sema, krate, &scope, trait_candidate, true, mod_path, limit)
}
ImportCandidate::TraitMethod(trait_candidate) => {
trait_applicable_items(sema, krate, &scope, trait_candidate, false, mod_path)
trait_applicable_items(sema, krate, &scope, trait_candidate, false, mod_path, limit)
}
}
.into_iter()
Expand All @@ -315,6 +318,7 @@ fn path_applicable_imports(
current_crate: Crate,
path_candidate: &PathImportCandidate,
mod_path: impl Fn(ItemInNs) -> Option<ModPath> + Copy,
limit: Option<usize>,
) -> FxHashSet<LocatedImport> {
let _p = profile::span("import_assets::path_applicable_imports");

Expand All @@ -338,7 +342,7 @@ fn path_applicable_imports(
let mod_path = mod_path(item)?;
Some(LocatedImport::new(mod_path, item, item))
})
.take(DEFAULT_QUERY_SEARCH_LIMIT.inner())
.take(limit.unwrap_or(usize::MAX))
.collect()
}
Some(qualifier) => items_locator::items_with_name(
Expand All @@ -348,7 +352,7 @@ fn path_applicable_imports(
AssocSearchMode::Include,
)
.filter_map(|item| import_for_item(sema.db, mod_path, &qualifier, item))
.take(DEFAULT_QUERY_SEARCH_LIMIT.inner())
.take(limit.unwrap_or(usize::MAX))
.collect(),
}
}
Expand All @@ -360,7 +364,9 @@ fn import_for_item(
original_item: ItemInNs,
) -> Option<LocatedImport> {
let _p = profile::span("import_assets::import_for_item");
let [first_segment, ..] = unresolved_qualifier else { return None };
let [first_segment, ..] = unresolved_qualifier else {
return None;
};

let item_as_assoc = item_as_assoc(db, original_item);

Expand Down Expand Up @@ -490,6 +496,7 @@ fn trait_applicable_items(
trait_candidate: &TraitImportCandidate,
trait_assoc_item: bool,
mod_path: impl Fn(ItemInNs) -> Option<ModPath>,
limit: Option<usize>,
) -> FxHashSet<LocatedImport> {
let _p = profile::span("import_assets::trait_applicable_items");

Expand All @@ -516,7 +523,7 @@ fn trait_applicable_items(
Some(assoc_item_trait.into())
}
})
.take(DEFAULT_QUERY_SEARCH_LIMIT.inner())
.take(limit.unwrap_or(usize::MAX))
.collect();

let mut located_imports = FxHashSet::default();
Expand Down
6 changes: 6 additions & 0 deletions crates/rust-analyzer/src/config.rs
Expand Up @@ -216,6 +216,8 @@ config_data! {
/// Aliased as `"checkOnSave.targets"`.
check_targets | checkOnSave_targets | checkOnSave_target: Option<CheckOnSaveTargets> = "null",

/// Always allow fuzzy matches for completions, even when the amount of potential completions is very large.
completion_alwaysAllowFuzzy: bool = "false",
/// Toggles the additional completions that automatically add imports when completed.
/// Note that your client must specify the `additionalTextEdits` LSP client capability to truly have this feature enabled.
completion_autoimport_enable: bool = "true",
Expand All @@ -232,6 +234,8 @@ config_data! {
completion_postfix_enable: bool = "true",
/// Enables completions of private items and fields that are defined in the current workspace even if they are not visible at the current position.
completion_privateEditable_enable: bool = "false",
/// Maximum of items to search when finding potential completions.
completion_querySearchLimit: Option<usize> = "100",
/// Custom completion snippets.
// NOTE: Keep this list in sync with the feature docs of user snippets.
completion_snippets_custom: FxHashMap<String, SnippetDef> = r#"{
Expand Down Expand Up @@ -1521,6 +1525,8 @@ impl Config {
)),
snippets: self.snippets.clone(),
limit: self.data.completion_limit,
query_search_limit: self.data.completion_querySearchLimit,
always_allow_fuzzy: self.data.completion_alwaysAllowFuzzy,
}
}

Expand Down
18 changes: 12 additions & 6 deletions crates/rust-analyzer/src/handlers/request.rs
Expand Up @@ -868,7 +868,11 @@ pub(crate) fn handle_completion(
let items =
to_proto::completion_items(&snap.config, &line_index, text_document_position, items);

let completion_list = lsp_types::CompletionList { is_incomplete: true, items };
let completion_list = lsp_types::CompletionList {
is_incomplete: !completion_config.always_allow_fuzzy
|| completion_config.query_search_limit.is_some(),
items,
};
Ok(Some(completion_list.into()))
}

Expand Down Expand Up @@ -1234,11 +1238,13 @@ pub(crate) fn handle_code_action_resolve(

let assist = match assists.get(assist_index) {
Some(assist) => assist,
None => return Err(invalid_params_error(format!(
"Failed to find the assist for index {} provided by the resolve request. Resolve request assist id: {}",
assist_index, params.id,
))
.into())
None => {
return Err(invalid_params_error(format!(
"Failed to find the assist for index {} provided by the resolve request. Resolve request assist id: {}",
assist_index, params.id,
))
.into())
}
};
if assist.id.0 != expected_assist_id || assist.id.1 != expected_kind {
return Err(invalid_params_error(format!(
Expand Down
6 changes: 6 additions & 0 deletions crates/rust-analyzer/src/integrated_benchmarks.rs
Expand Up @@ -146,6 +146,8 @@ fn integrated_completion_benchmark() {
prefer_no_std: false,
prefer_prelude: true,
limit: None,
query_search_limit: Some(100),
always_allow_fuzzy: false,
};
let position =
FilePosition { file_id, offset: TextSize::try_from(completion_offset).unwrap() };
Expand Down Expand Up @@ -190,6 +192,8 @@ fn integrated_completion_benchmark() {
prefer_no_std: false,
prefer_prelude: true,
limit: None,
query_search_limit: Some(100),
always_allow_fuzzy: false,
};
let position =
FilePosition { file_id, offset: TextSize::try_from(completion_offset).unwrap() };
Expand Down Expand Up @@ -231,6 +235,8 @@ fn integrated_completion_benchmark() {
prefer_no_std: false,
prefer_prelude: true,
limit: None,
query_search_limit: Some(100),
always_allow_fuzzy: false,
};
let position =
FilePosition { file_id, offset: TextSize::try_from(completion_offset).unwrap() };
Expand Down
10 changes: 10 additions & 0 deletions docs/user/generated_config.adoc
Expand Up @@ -241,6 +241,11 @@ Can be a single target, e.g. `"x86_64-unknown-linux-gnu"` or a list of targets,

Aliased as `"checkOnSave.targets"`.
--
[[rust-analyzer.completion.alwaysAllowFuzzy]]rust-analyzer.completion.alwaysAllowFuzzy (default: `false`)::
+
--
Always allow fuzzy matches for completions, even when the amount of potential completions is very large.
--
[[rust-analyzer.completion.autoimport.enable]]rust-analyzer.completion.autoimport.enable (default: `true`)::
+
--
Expand Down Expand Up @@ -278,6 +283,11 @@ Whether to show postfix snippets like `dbg`, `if`, `not`, etc.
--
Enables completions of private items and fields that are defined in the current workspace even if they are not visible at the current position.
--
[[rust-analyzer.completion.querySearchLimit]]rust-analyzer.completion.querySearchLimit (default: `100`)::
+
--
Maximum of items to search when finding potential completions.
--
[[rust-analyzer.completion.snippets.custom]]rust-analyzer.completion.snippets.custom::
+
--
Expand Down
14 changes: 14 additions & 0 deletions editors/code/package.json
Expand Up @@ -798,6 +798,11 @@
}
]
},
"rust-analyzer.completion.alwaysAllowFuzzy": {
"markdownDescription": "Always allow fuzzy matches for completions, even when the amount of potential completions is very large.",
"default": false,
"type": "boolean"
},
"rust-analyzer.completion.autoimport.enable": {
"markdownDescription": "Toggles the additional completions that automatically add imports when completed.\nNote that your client must specify the `additionalTextEdits` LSP client capability to truly have this feature enabled.",
"default": true,
Expand Down Expand Up @@ -847,6 +852,15 @@
"default": false,
"type": "boolean"
},
"rust-analyzer.completion.querySearchLimit": {
"markdownDescription": "Maximum of items to search when finding potential completions.",
"default": 100,
"type": [
"null",
"integer"
],
"minimum": 0
},
"rust-analyzer.completion.snippets.custom": {
"markdownDescription": "Custom completion snippets.",
"default": {
Expand Down

0 comments on commit 9963e3d

Please sign in to comment.