mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Avoid a couple of allocations
This commit is contained in:
parent
ef1177c5b5
commit
42c24ff25f
@ -91,7 +91,7 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext) -> Option
|
|||||||
// extra newlines in the indent block
|
// extra newlines in the indent block
|
||||||
let text = indent.text();
|
let text = indent.text();
|
||||||
if text.starts_with('\n') {
|
if text.starts_with('\n') {
|
||||||
buf.push_str("\n");
|
buf.push('\n');
|
||||||
buf.push_str(text.trim_start_matches('\n'));
|
buf.push_str(text.trim_start_matches('\n'));
|
||||||
} else {
|
} else {
|
||||||
buf.push_str(text);
|
buf.push_str(text);
|
||||||
|
@ -62,21 +62,22 @@ pub(crate) fn replace_derive_with_manual_impl(
|
|||||||
let current_module = ctx.sema.scope(annotated_name.syntax()).module()?;
|
let current_module = ctx.sema.scope(annotated_name.syntax()).module()?;
|
||||||
let current_crate = current_module.krate();
|
let current_crate = current_module.krate();
|
||||||
|
|
||||||
let found_traits =
|
let found_traits = imports_locator::find_exact_imports(
|
||||||
imports_locator::find_exact_imports(&ctx.sema, current_crate, trait_token.text())
|
&ctx.sema,
|
||||||
.filter_map(
|
current_crate,
|
||||||
|candidate: either::Either<hir::ModuleDef, hir::MacroDef>| match candidate {
|
trait_token.text().to_string(),
|
||||||
either::Either::Left(hir::ModuleDef::Trait(trait_)) => Some(trait_),
|
)
|
||||||
_ => None,
|
.filter_map(|candidate: either::Either<hir::ModuleDef, hir::MacroDef>| match candidate {
|
||||||
},
|
either::Either::Left(hir::ModuleDef::Trait(trait_)) => Some(trait_),
|
||||||
)
|
_ => None,
|
||||||
.flat_map(|trait_| {
|
})
|
||||||
current_module
|
.flat_map(|trait_| {
|
||||||
.find_use_path(ctx.sema.db, hir::ModuleDef::Trait(trait_))
|
current_module
|
||||||
.as_ref()
|
.find_use_path(ctx.sema.db, hir::ModuleDef::Trait(trait_))
|
||||||
.map(mod_path_to_ast)
|
.as_ref()
|
||||||
.zip(Some(trait_))
|
.map(mod_path_to_ast)
|
||||||
});
|
.zip(Some(trait_))
|
||||||
|
});
|
||||||
|
|
||||||
let mut no_traits_found = true;
|
let mut no_traits_found = true;
|
||||||
for (trait_path, trait_) in found_traits.inspect(|_| no_traits_found = false) {
|
for (trait_path, trait_) in found_traits.inspect(|_| no_traits_found = false) {
|
||||||
|
@ -179,25 +179,24 @@ impl ImportAssets {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut res =
|
let mut res = imports_locator::find_exact_imports(
|
||||||
imports_locator::find_exact_imports(sema, current_crate, &self.get_search_query())
|
sema,
|
||||||
.filter_map(filter)
|
current_crate,
|
||||||
.filter_map(|candidate| {
|
self.get_search_query().to_string(),
|
||||||
let item: hir::ItemInNs = candidate.either(Into::into, Into::into);
|
)
|
||||||
if let Some(prefix_kind) = prefixed {
|
.filter_map(filter)
|
||||||
self.module_with_name_to_import.find_use_path_prefixed(
|
.filter_map(|candidate| {
|
||||||
db,
|
let item: hir::ItemInNs = candidate.either(Into::into, Into::into);
|
||||||
item,
|
if let Some(prefix_kind) = prefixed {
|
||||||
prefix_kind,
|
self.module_with_name_to_import.find_use_path_prefixed(db, item, prefix_kind)
|
||||||
)
|
} else {
|
||||||
} else {
|
self.module_with_name_to_import.find_use_path(db, item)
|
||||||
self.module_with_name_to_import.find_use_path(db, item)
|
}
|
||||||
}
|
.map(|path| (path, item))
|
||||||
.map(|path| (path, item))
|
})
|
||||||
})
|
.filter(|(use_path, _)| use_path.len() > 1)
|
||||||
.filter(|(use_path, _)| use_path.len() > 1)
|
.take(20)
|
||||||
.take(20)
|
.collect::<Vec<_>>();
|
||||||
.collect::<Vec<_>>();
|
|
||||||
res.sort_by_key(|(path, _)| path.clone());
|
res.sort_by_key(|(path, _)| path.clone());
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
@ -135,11 +135,12 @@ fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<()
|
|||||||
let anchor = ctx.name_ref_syntax.as_ref()?;
|
let anchor = ctx.name_ref_syntax.as_ref()?;
|
||||||
let import_scope = ImportScope::find_insert_use_container(anchor.syntax(), &ctx.sema)?;
|
let import_scope = ImportScope::find_insert_use_container(anchor.syntax(), &ctx.sema)?;
|
||||||
|
|
||||||
|
let user_input_lowercased = potential_import_name.to_lowercase();
|
||||||
let mut all_mod_paths = imports_locator::find_similar_imports(
|
let mut all_mod_paths = imports_locator::find_similar_imports(
|
||||||
&ctx.sema,
|
&ctx.sema,
|
||||||
ctx.krate?,
|
ctx.krate?,
|
||||||
Some(40),
|
Some(40),
|
||||||
&potential_import_name,
|
potential_import_name,
|
||||||
true,
|
true,
|
||||||
)
|
)
|
||||||
.filter_map(|import_candidate| {
|
.filter_map(|import_candidate| {
|
||||||
@ -155,7 +156,6 @@ fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<()
|
|||||||
.filter(|(mod_path, _)| mod_path.len() > 1)
|
.filter(|(mod_path, _)| mod_path.len() > 1)
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let user_input_lowercased = potential_import_name.to_lowercase();
|
|
||||||
all_mod_paths.sort_by_cached_key(|(mod_path, _)| {
|
all_mod_paths.sort_by_cached_key(|(mod_path, _)| {
|
||||||
compute_fuzzy_completion_order_key(mod_path, &user_input_lowercased)
|
compute_fuzzy_completion_order_key(mod_path, &user_input_lowercased)
|
||||||
});
|
});
|
||||||
|
@ -137,7 +137,7 @@ pub fn resolve_completion_edits(
|
|||||||
config: &CompletionConfig,
|
config: &CompletionConfig,
|
||||||
position: FilePosition,
|
position: FilePosition,
|
||||||
full_import_path: &str,
|
full_import_path: &str,
|
||||||
imported_name: &str,
|
imported_name: String,
|
||||||
) -> Option<Vec<TextEdit>> {
|
) -> Option<Vec<TextEdit>> {
|
||||||
let ctx = CompletionContext::new(db, position, config)?;
|
let ctx = CompletionContext::new(db, position, config)?;
|
||||||
let anchor = ctx.name_ref_syntax.as_ref()?;
|
let anchor = ctx.name_ref_syntax.as_ref()?;
|
||||||
|
@ -262,10 +262,11 @@ pub struct Query {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Query {
|
impl Query {
|
||||||
pub fn new(query: &str) -> Self {
|
pub fn new(query: String) -> Self {
|
||||||
|
let lowercased = query.to_lowercase();
|
||||||
Self {
|
Self {
|
||||||
query: query.to_string(),
|
query,
|
||||||
lowercased: query.to_lowercase(),
|
lowercased,
|
||||||
name_only: false,
|
name_only: false,
|
||||||
search_mode: SearchMode::Contains,
|
search_mode: SearchMode::Contains,
|
||||||
case_sensitive: false,
|
case_sensitive: false,
|
||||||
@ -774,7 +775,7 @@ mod tests {
|
|||||||
check_search(
|
check_search(
|
||||||
ra_fixture,
|
ra_fixture,
|
||||||
"main",
|
"main",
|
||||||
Query::new("fmt").search_mode(SearchMode::Fuzzy),
|
Query::new("fmt".to_string()).search_mode(SearchMode::Fuzzy),
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
dep::fmt (t)
|
dep::fmt (t)
|
||||||
dep::Fmt (t)
|
dep::Fmt (t)
|
||||||
@ -789,7 +790,7 @@ mod tests {
|
|||||||
check_search(
|
check_search(
|
||||||
ra_fixture,
|
ra_fixture,
|
||||||
"main",
|
"main",
|
||||||
Query::new("fmt").search_mode(SearchMode::Equals),
|
Query::new("fmt".to_string()).search_mode(SearchMode::Equals),
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
dep::fmt (t)
|
dep::fmt (t)
|
||||||
dep::Fmt (t)
|
dep::Fmt (t)
|
||||||
@ -802,7 +803,7 @@ mod tests {
|
|||||||
check_search(
|
check_search(
|
||||||
ra_fixture,
|
ra_fixture,
|
||||||
"main",
|
"main",
|
||||||
Query::new("fmt").search_mode(SearchMode::Contains),
|
Query::new("fmt".to_string()).search_mode(SearchMode::Contains),
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
dep::fmt (t)
|
dep::fmt (t)
|
||||||
dep::Fmt (t)
|
dep::Fmt (t)
|
||||||
@ -843,7 +844,7 @@ mod tests {
|
|||||||
check_search(
|
check_search(
|
||||||
ra_fixture,
|
ra_fixture,
|
||||||
"main",
|
"main",
|
||||||
Query::new("fmt"),
|
Query::new("fmt".to_string()),
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
dep::fmt (t)
|
dep::fmt (t)
|
||||||
dep::Fmt (t)
|
dep::Fmt (t)
|
||||||
@ -857,7 +858,7 @@ mod tests {
|
|||||||
check_search(
|
check_search(
|
||||||
ra_fixture,
|
ra_fixture,
|
||||||
"main",
|
"main",
|
||||||
Query::new("fmt").name_only(),
|
Query::new("fmt".to_string()).name_only(),
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
dep::fmt (t)
|
dep::fmt (t)
|
||||||
dep::Fmt (t)
|
dep::Fmt (t)
|
||||||
@ -881,7 +882,7 @@ mod tests {
|
|||||||
check_search(
|
check_search(
|
||||||
ra_fixture,
|
ra_fixture,
|
||||||
"main",
|
"main",
|
||||||
Query::new("FMT"),
|
Query::new("FMT".to_string()),
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
dep::fmt (t)
|
dep::fmt (t)
|
||||||
dep::fmt (v)
|
dep::fmt (v)
|
||||||
@ -893,7 +894,7 @@ mod tests {
|
|||||||
check_search(
|
check_search(
|
||||||
ra_fixture,
|
ra_fixture,
|
||||||
"main",
|
"main",
|
||||||
Query::new("FMT").case_sensitive(),
|
Query::new("FMT".to_string()).case_sensitive(),
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
dep::FMT (t)
|
dep::FMT (t)
|
||||||
dep::FMT (v)
|
dep::FMT (v)
|
||||||
@ -922,7 +923,7 @@ mod tests {
|
|||||||
pub fn no() {}
|
pub fn no() {}
|
||||||
"#,
|
"#,
|
||||||
"main",
|
"main",
|
||||||
Query::new("").limit(2),
|
Query::new("".to_string()).limit(2),
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
dep::fmt (t)
|
dep::fmt (t)
|
||||||
dep::Fmt (t)
|
dep::Fmt (t)
|
||||||
@ -943,7 +944,7 @@ mod tests {
|
|||||||
check_search(
|
check_search(
|
||||||
ra_fixture,
|
ra_fixture,
|
||||||
"main",
|
"main",
|
||||||
Query::new("FMT"),
|
Query::new("FMT".to_string()),
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
dep::fmt (t)
|
dep::fmt (t)
|
||||||
dep::fmt (v)
|
dep::fmt (v)
|
||||||
@ -955,7 +956,7 @@ mod tests {
|
|||||||
check_search(
|
check_search(
|
||||||
ra_fixture,
|
ra_fixture,
|
||||||
"main",
|
"main",
|
||||||
Query::new("FMT").exclude_import_kind(ImportKind::Adt),
|
Query::new("FMT".to_string()).exclude_import_kind(ImportKind::Adt),
|
||||||
expect![[r#""#]],
|
expect![[r#""#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -249,7 +249,7 @@ impl CrateDefMap {
|
|||||||
buf.push_str(" _");
|
buf.push_str(" _");
|
||||||
}
|
}
|
||||||
|
|
||||||
buf.push_str("\n");
|
buf.push('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
for (name, child) in map.modules[module].children.iter() {
|
for (name, child) in map.modules[module].children.iter() {
|
||||||
|
@ -475,7 +475,7 @@ impl Analysis {
|
|||||||
config: &CompletionConfig,
|
config: &CompletionConfig,
|
||||||
position: FilePosition,
|
position: FilePosition,
|
||||||
full_import_path: &str,
|
full_import_path: &str,
|
||||||
imported_name: &str,
|
imported_name: String,
|
||||||
) -> Cancelable<Vec<TextEdit>> {
|
) -> Cancelable<Vec<TextEdit>> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.with_db(|db| {
|
.with_db(|db| {
|
||||||
|
@ -15,14 +15,14 @@ use rustc_hash::FxHashSet;
|
|||||||
pub fn find_exact_imports<'a>(
|
pub fn find_exact_imports<'a>(
|
||||||
sema: &Semantics<'a, RootDatabase>,
|
sema: &Semantics<'a, RootDatabase>,
|
||||||
krate: Crate,
|
krate: Crate,
|
||||||
name_to_import: &str,
|
name_to_import: String,
|
||||||
) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> {
|
) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> {
|
||||||
let _p = profile::span("find_exact_imports");
|
let _p = profile::span("find_exact_imports");
|
||||||
find_imports(
|
find_imports(
|
||||||
sema,
|
sema,
|
||||||
krate,
|
krate,
|
||||||
{
|
{
|
||||||
let mut local_query = symbol_index::Query::new(name_to_import.to_string());
|
let mut local_query = symbol_index::Query::new(name_to_import.clone());
|
||||||
local_query.exact();
|
local_query.exact();
|
||||||
local_query.limit(40);
|
local_query.limit(40);
|
||||||
local_query
|
local_query
|
||||||
@ -39,18 +39,18 @@ pub fn find_similar_imports<'a>(
|
|||||||
sema: &Semantics<'a, RootDatabase>,
|
sema: &Semantics<'a, RootDatabase>,
|
||||||
krate: Crate,
|
krate: Crate,
|
||||||
limit: Option<usize>,
|
limit: Option<usize>,
|
||||||
fuzzy_search_string: &str,
|
fuzzy_search_string: String,
|
||||||
name_only: bool,
|
name_only: bool,
|
||||||
) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> {
|
) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> {
|
||||||
let _p = profile::span("find_similar_imports");
|
let _p = profile::span("find_similar_imports");
|
||||||
|
|
||||||
let mut external_query =
|
let mut external_query = import_map::Query::new(fuzzy_search_string.clone())
|
||||||
import_map::Query::new(fuzzy_search_string).search_mode(import_map::SearchMode::Fuzzy);
|
.search_mode(import_map::SearchMode::Fuzzy);
|
||||||
if name_only {
|
if name_only {
|
||||||
external_query = external_query.name_only();
|
external_query = external_query.name_only();
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut local_query = symbol_index::Query::new(fuzzy_search_string.to_string());
|
let mut local_query = symbol_index::Query::new(fuzzy_search_string);
|
||||||
|
|
||||||
if let Some(limit) = limit {
|
if let Some(limit) = limit {
|
||||||
local_query.limit(limit);
|
local_query.limit(limit);
|
||||||
|
@ -681,7 +681,7 @@ pub(crate) fn handle_completion_resolve(
|
|||||||
&snap.config.completion,
|
&snap.config.completion,
|
||||||
FilePosition { file_id, offset },
|
FilePosition { file_id, offset },
|
||||||
&resolve_data.full_import_path,
|
&resolve_data.full_import_path,
|
||||||
&resolve_data.imported_name,
|
resolve_data.imported_name,
|
||||||
)?
|
)?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.flat_map(|edit| {
|
.flat_map(|edit| {
|
||||||
|
Loading…
Reference in New Issue
Block a user