Auto merge of #16099 - Veykril:flyimport, r=Veykril

internal: Improve import asset perf a bit

And bump the query limit from 40 to 100
This commit is contained in:
bors 2023-12-12 14:37:38 +00:00
commit d4accf8e96
11 changed files with 197 additions and 192 deletions

View file

@ -23,6 +23,7 @@ pub struct FileSymbol {
pub loc: DeclarationLocation, pub loc: DeclarationLocation,
pub container_name: Option<SmolStr>, pub container_name: Option<SmolStr>,
pub is_alias: bool, pub is_alias: bool,
pub is_assoc: bool,
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
@ -121,34 +122,34 @@ impl<'a> SymbolCollector<'a> {
match module_def_id { match module_def_id {
ModuleDefId::ModuleId(id) => self.push_module(id), ModuleDefId::ModuleId(id) => self.push_module(id),
ModuleDefId::FunctionId(id) => { ModuleDefId::FunctionId(id) => {
self.push_decl(id); self.push_decl(id, false);
self.collect_from_body(id); self.collect_from_body(id);
} }
ModuleDefId::AdtId(AdtId::StructId(id)) => self.push_decl(id), ModuleDefId::AdtId(AdtId::StructId(id)) => self.push_decl(id, false),
ModuleDefId::AdtId(AdtId::EnumId(id)) => self.push_decl(id), ModuleDefId::AdtId(AdtId::EnumId(id)) => self.push_decl(id, false),
ModuleDefId::AdtId(AdtId::UnionId(id)) => self.push_decl(id), ModuleDefId::AdtId(AdtId::UnionId(id)) => self.push_decl(id, false),
ModuleDefId::ConstId(id) => { ModuleDefId::ConstId(id) => {
self.push_decl(id); self.push_decl(id, false);
self.collect_from_body(id); self.collect_from_body(id);
} }
ModuleDefId::StaticId(id) => { ModuleDefId::StaticId(id) => {
self.push_decl(id); self.push_decl(id, false);
self.collect_from_body(id); self.collect_from_body(id);
} }
ModuleDefId::TraitId(id) => { ModuleDefId::TraitId(id) => {
self.push_decl(id); self.push_decl(id, false);
self.collect_from_trait(id); self.collect_from_trait(id);
} }
ModuleDefId::TraitAliasId(id) => { ModuleDefId::TraitAliasId(id) => {
self.push_decl(id); self.push_decl(id, false);
} }
ModuleDefId::TypeAliasId(id) => { ModuleDefId::TypeAliasId(id) => {
self.push_decl(id); self.push_decl(id, false);
} }
ModuleDefId::MacroId(id) => match id { ModuleDefId::MacroId(id) => match id {
MacroId::Macro2Id(id) => self.push_decl(id), MacroId::Macro2Id(id) => self.push_decl(id, false),
MacroId::MacroRulesId(id) => self.push_decl(id), MacroId::MacroRulesId(id) => self.push_decl(id, false),
MacroId::ProcMacroId(id) => self.push_decl(id), MacroId::ProcMacroId(id) => self.push_decl(id, false),
}, },
// Don't index these. // Don't index these.
ModuleDefId::BuiltinType(_) => {} ModuleDefId::BuiltinType(_) => {}
@ -190,6 +191,7 @@ impl<'a> SymbolCollector<'a> {
container_name: self.current_container_name.clone(), container_name: self.current_container_name.clone(),
loc: dec_loc, loc: dec_loc,
is_alias: false, is_alias: false,
is_assoc: false,
}); });
}); });
} }
@ -202,9 +204,9 @@ impl<'a> SymbolCollector<'a> {
for &id in id { for &id in id {
if id.module(self.db.upcast()) == module_id { if id.module(self.db.upcast()) == module_id {
match id { match id {
MacroId::Macro2Id(id) => self.push_decl(id), MacroId::Macro2Id(id) => self.push_decl(id, false),
MacroId::MacroRulesId(id) => self.push_decl(id), MacroId::MacroRulesId(id) => self.push_decl(id, false),
MacroId::ProcMacroId(id) => self.push_decl(id), MacroId::ProcMacroId(id) => self.push_decl(id, false),
} }
} }
} }
@ -266,13 +268,13 @@ impl<'a> SymbolCollector<'a> {
fn push_assoc_item(&mut self, assoc_item_id: AssocItemId) { fn push_assoc_item(&mut self, assoc_item_id: AssocItemId) {
match assoc_item_id { match assoc_item_id {
AssocItemId::FunctionId(id) => self.push_decl(id), AssocItemId::FunctionId(id) => self.push_decl(id, true),
AssocItemId::ConstId(id) => self.push_decl(id), AssocItemId::ConstId(id) => self.push_decl(id, true),
AssocItemId::TypeAliasId(id) => self.push_decl(id), AssocItemId::TypeAliasId(id) => self.push_decl(id, true),
} }
} }
fn push_decl<L>(&mut self, id: L) fn push_decl<L>(&mut self, id: L, is_assoc: bool)
where where
L: Lookup + Into<ModuleDefId>, L: Lookup + Into<ModuleDefId>,
<L as Lookup>::Data: HasSource, <L as Lookup>::Data: HasSource,
@ -296,6 +298,7 @@ impl<'a> SymbolCollector<'a> {
loc: dec_loc.clone(), loc: dec_loc.clone(),
container_name: self.current_container_name.clone(), container_name: self.current_container_name.clone(),
is_alias: true, is_alias: true,
is_assoc,
}); });
} }
} }
@ -306,6 +309,7 @@ impl<'a> SymbolCollector<'a> {
container_name: self.current_container_name.clone(), container_name: self.current_container_name.clone(),
loc: dec_loc, loc: dec_loc,
is_alias: false, is_alias: false,
is_assoc,
}); });
} }
@ -331,6 +335,7 @@ impl<'a> SymbolCollector<'a> {
loc: dec_loc.clone(), loc: dec_loc.clone(),
container_name: self.current_container_name.clone(), container_name: self.current_container_name.clone(),
is_alias: true, is_alias: true,
is_assoc: false,
}); });
} }
} }
@ -341,6 +346,7 @@ impl<'a> SymbolCollector<'a> {
container_name: self.current_container_name.clone(), container_name: self.current_container_name.clone(),
loc: dec_loc, loc: dec_loc,
is_alias: false, is_alias: false,
is_assoc: false,
}); });
} }
} }

View file

@ -458,13 +458,11 @@ impl Builder {
} }
if let [import_edit] = &*self.imports_to_add { if let [import_edit] = &*self.imports_to_add {
// snippets can have multiple imports, but normal completions only have up to one // snippets can have multiple imports, but normal completions only have up to one
if let Some(original_path) = import_edit.original_path.as_ref() { label_detail.replace(SmolStr::from(format!(
label_detail.replace(SmolStr::from(format!( "{} (use {})",
"{} (use {})", label_detail.as_deref().unwrap_or_default(),
label_detail.as_deref().unwrap_or_default(), import_edit.import_path.display(db)
original_path.display(db) )));
)));
}
} else if let Some(trait_name) = self.trait_name { } else if let Some(trait_name) = self.trait_name {
label_detail.replace(SmolStr::from(format!( label_detail.replace(SmolStr::from(format!(
"{} (as {trait_name})", "{} (as {trait_name})",

View file

@ -181,7 +181,7 @@ fn import_edits(ctx: &CompletionContext<'_>, requires: &[GreenNode]) -> Option<V
ctx.config.prefer_no_std, ctx.config.prefer_no_std,
ctx.config.prefer_prelude, ctx.config.prefer_prelude,
)?; )?;
Some((path.len() > 1).then(|| LocatedImport::new(path.clone(), item, item, None))) Some((path.len() > 1).then(|| LocatedImport::new(path.clone(), item, item)))
}; };
let mut res = Vec::with_capacity(requires.len()); let mut res = Vec::with_capacity(requires.len());
for import in requires { for import in requires {

View file

@ -597,8 +597,8 @@ fn main() {
} }
"#, "#,
expect![[r#" expect![[r#"
ct SPECIAL_CONST (use dep::test_mod::TestTrait) u8 DEPRECATED
fn weird_function() (use dep::test_mod::TestTrait) fn() DEPRECATED fn weird_function() (use dep::test_mod::TestTrait) fn() DEPRECATED
ct SPECIAL_CONST (use dep::test_mod::TestTrait) u8 DEPRECATED
"#]], "#]],
); );
} }
@ -717,7 +717,7 @@ fn main() {
check( check(
fixture, fixture,
expect![[r#" expect![[r#"
st Item (use foo::bar::baz::Item) Item st Item (use foo::bar) Item
"#]], "#]],
); );
@ -725,19 +725,19 @@ fn main() {
"Item", "Item",
fixture, fixture,
r#" r#"
use foo::bar; use foo::bar;
mod foo { mod foo {
pub mod bar { pub mod bar {
pub mod baz { pub mod baz {
pub struct Item; pub struct Item;
}
}
} }
}
}
fn main() { fn main() {
bar::baz::Item bar::baz::Item
}"#, }"#,
); );
} }
@ -803,7 +803,7 @@ fn main() {
check( check(
fixture, fixture,
expect![[r#" expect![[r#"
ct TEST_ASSOC (use foo::bar::Item) usize ct TEST_ASSOC (use foo::bar) usize
"#]], "#]],
); );

View file

@ -1,14 +1,14 @@
//! Look up accessible paths for items. //! Look up accessible paths for items.
use hir::{ use hir::{
AsAssocItem, AssocItem, AssocItemContainer, Crate, ItemInNs, ModPath, Module, ModuleDef, AsAssocItem, AssocItem, AssocItemContainer, Crate, ItemInNs, ModPath, Module, ModuleDef, Name,
PathResolution, PrefixKind, ScopeDef, Semantics, SemanticsScope, Type, PathResolution, PrefixKind, ScopeDef, Semantics, SemanticsScope, Type,
}; };
use itertools::Itertools; use itertools::{EitherOrBoth, Itertools};
use rustc_hash::FxHashSet; use rustc_hash::{FxHashMap, FxHashSet};
use syntax::{ use syntax::{
ast::{self, make, HasName}, ast::{self, make, HasName},
utils::path_to_string_stripping_turbo_fish, AstNode, SmolStr, SyntaxNode,
AstNode, SyntaxNode,
}; };
use crate::{ use crate::{
@ -51,18 +51,11 @@ pub struct TraitImportCandidate {
#[derive(Debug)] #[derive(Debug)]
pub struct PathImportCandidate { pub struct PathImportCandidate {
/// Optional qualifier before name. /// Optional qualifier before name.
pub qualifier: Option<FirstSegmentUnresolved>, pub qualifier: Option<Vec<SmolStr>>,
/// The name the item (struct, trait, enum, etc.) should have. /// The name the item (struct, trait, enum, etc.) should have.
pub name: NameToImport, pub name: NameToImport,
} }
/// A qualifier that has a first segment and it's unresolved.
#[derive(Debug)]
pub struct FirstSegmentUnresolved {
fist_segment: ast::NameRef,
full_qualifier: ast::Path,
}
/// A name that will be used during item lookups. /// A name that will be used during item lookups.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum NameToImport { pub enum NameToImport {
@ -195,18 +188,11 @@ pub struct LocatedImport {
/// the original item is the associated constant, but the import has to be a trait that /// the original item is the associated constant, but the import has to be a trait that
/// defines this constant. /// defines this constant.
pub original_item: ItemInNs, pub original_item: ItemInNs,
/// A path of the original item.
pub original_path: Option<ModPath>,
} }
impl LocatedImport { impl LocatedImport {
pub fn new( pub fn new(import_path: ModPath, item_to_import: ItemInNs, original_item: ItemInNs) -> Self {
import_path: ModPath, Self { import_path, item_to_import, original_item }
item_to_import: ItemInNs,
original_item: ItemInNs,
original_path: Option<ModPath>,
) -> Self {
Self { import_path, item_to_import, original_item, original_path }
} }
} }
@ -351,64 +337,75 @@ fn path_applicable_imports(
) )
.filter_map(|item| { .filter_map(|item| {
let mod_path = mod_path(item)?; let mod_path = mod_path(item)?;
Some(LocatedImport::new(mod_path.clone(), item, item, Some(mod_path))) Some(LocatedImport::new(mod_path, item, item))
})
.collect()
}
Some(first_segment_unresolved) => {
let unresolved_qualifier =
path_to_string_stripping_turbo_fish(&first_segment_unresolved.full_qualifier);
let unresolved_first_segment = first_segment_unresolved.fist_segment.text();
items_locator::items_with_name(
sema,
current_crate,
path_candidate.name.clone(),
AssocSearchMode::Include,
Some(DEFAULT_QUERY_SEARCH_LIMIT.inner()),
)
.filter_map(|item| {
import_for_item(
sema.db,
mod_path,
&unresolved_first_segment,
&unresolved_qualifier,
item,
)
}) })
.collect() .collect()
} }
Some(qualifier) => items_locator::items_with_name(
sema,
current_crate,
path_candidate.name.clone(),
AssocSearchMode::Include,
Some(DEFAULT_QUERY_SEARCH_LIMIT.inner()),
)
.filter_map(|item| import_for_item(sema.db, mod_path, &qualifier, item))
.collect(),
} }
} }
fn import_for_item( fn import_for_item(
db: &RootDatabase, db: &RootDatabase,
mod_path: impl Fn(ItemInNs) -> Option<ModPath>, mod_path: impl Fn(ItemInNs) -> Option<ModPath>,
unresolved_first_segment: &str, unresolved_qualifier: &[SmolStr],
unresolved_qualifier: &str,
original_item: ItemInNs, original_item: ItemInNs,
) -> Option<LocatedImport> { ) -> Option<LocatedImport> {
let _p = profile::span("import_assets::import_for_item"); let _p = profile::span("import_assets::import_for_item");
let [first_segment, ..] = unresolved_qualifier else { return None };
let original_item_candidate = item_for_path_search(db, original_item)?; let item_as_assoc = item_as_assoc(db, original_item);
let import_path_candidate = mod_path(original_item_candidate)?;
let import_path_string = import_path_candidate.display(db).to_string();
let expected_import_end = if item_as_assoc(db, original_item).is_some() { let (original_item_candidate, trait_item_to_import) = match item_as_assoc {
unresolved_qualifier.to_string() Some(assoc_item) => match assoc_item.container(db) {
} else { AssocItemContainer::Trait(trait_) => {
format!("{unresolved_qualifier}::{}", item_name(db, original_item)?.display(db)) let trait_ = ItemInNs::from(ModuleDef::from(trait_));
(trait_, Some(trait_))
}
AssocItemContainer::Impl(impl_) => {
(ItemInNs::from(ModuleDef::from(impl_.self_ty(db).as_adt()?)), None)
}
},
None => (original_item, None),
}; };
if !import_path_string.contains(unresolved_first_segment) let import_path_candidate = mod_path(original_item_candidate)?;
|| !import_path_string.ends_with(&expected_import_end)
{ let mut import_path_candidate_segments = import_path_candidate.segments().iter().rev();
let predicate = |it: EitherOrBoth<&SmolStr, &Name>| match it {
// segments match, check next one
EitherOrBoth::Both(a, b) if b.as_str() == Some(&**a) => None,
// segments mismatch / qualifier is longer than the path, bail out
EitherOrBoth::Both(..) | EitherOrBoth::Left(_) => Some(false),
// all segments match and we have exhausted the qualifier, proceed
EitherOrBoth::Right(_) => Some(true),
};
if item_as_assoc.is_none() {
let item_name = item_name(db, original_item)?.as_text()?;
let last_segment = import_path_candidate_segments.next()?;
if last_segment.as_str() != Some(&*item_name) {
return None;
}
}
let ends_with = unresolved_qualifier
.iter()
.rev()
.zip_longest(import_path_candidate_segments)
.find_map(predicate)
.unwrap_or(true);
if !ends_with {
return None; return None;
} }
let segment_import = let segment_import = find_import_for_segment(db, original_item_candidate, first_segment)?;
find_import_for_segment(db, original_item_candidate, unresolved_first_segment)?;
let trait_item_to_import = item_as_assoc(db, original_item)
.and_then(|assoc| assoc.containing_trait(db))
.map(|trait_| ItemInNs::from(ModuleDef::from(trait_)));
Some(match (segment_import == original_item_candidate, trait_item_to_import) { Some(match (segment_import == original_item_candidate, trait_item_to_import) {
(true, Some(_)) => { (true, Some(_)) => {
// FIXME we should be able to import both the trait and the segment, // FIXME we should be able to import both the trait and the segment,
@ -416,42 +413,37 @@ fn import_for_item(
// especially in case of lazy completion edit resolutions. // especially in case of lazy completion edit resolutions.
return None; return None;
} }
(false, Some(trait_to_import)) => LocatedImport::new( (false, Some(trait_to_import)) => {
mod_path(trait_to_import)?, LocatedImport::new(mod_path(trait_to_import)?, trait_to_import, original_item)
trait_to_import, }
original_item, (true, None) => {
mod_path(original_item), LocatedImport::new(import_path_candidate, original_item_candidate, original_item)
), }
(true, None) => LocatedImport::new( (false, None) => {
import_path_candidate, LocatedImport::new(mod_path(segment_import)?, segment_import, original_item)
original_item_candidate, }
original_item,
mod_path(original_item),
),
(false, None) => LocatedImport::new(
mod_path(segment_import)?,
segment_import,
original_item,
mod_path(original_item),
),
}) })
} }
pub fn item_for_path_search(db: &RootDatabase, item: ItemInNs) -> Option<ItemInNs> { pub fn item_for_path_search(db: &RootDatabase, item: ItemInNs) -> Option<ItemInNs> {
Some(match item { Some(match item {
ItemInNs::Types(_) | ItemInNs::Values(_) => match item_as_assoc(db, item) { ItemInNs::Types(_) | ItemInNs::Values(_) => match item_as_assoc(db, item) {
Some(assoc_item) => match assoc_item.container(db) { Some(assoc_item) => item_for_path_search_assoc(db, assoc_item)?,
AssocItemContainer::Trait(trait_) => ItemInNs::from(ModuleDef::from(trait_)),
AssocItemContainer::Impl(impl_) => {
ItemInNs::from(ModuleDef::from(impl_.self_ty(db).as_adt()?))
}
},
None => item, None => item,
}, },
ItemInNs::Macros(_) => item, ItemInNs::Macros(_) => item,
}) })
} }
fn item_for_path_search_assoc(db: &RootDatabase, assoc_item: AssocItem) -> Option<ItemInNs> {
Some(match assoc_item.container(db) {
AssocItemContainer::Trait(trait_) => ItemInNs::from(ModuleDef::from(trait_)),
AssocItemContainer::Impl(impl_) => {
ItemInNs::from(ModuleDef::from(impl_.self_ty(db).as_adt()?))
}
})
}
fn find_import_for_segment( fn find_import_for_segment(
db: &RootDatabase, db: &RootDatabase,
original_item: ItemInNs, original_item: ItemInNs,
@ -528,6 +520,7 @@ fn trait_applicable_items(
.collect(); .collect();
let mut located_imports = FxHashSet::default(); let mut located_imports = FxHashSet::default();
let mut trait_import_paths = FxHashMap::default();
if trait_assoc_item { if trait_assoc_item {
trait_candidate.receiver_ty.iterate_path_candidates( trait_candidate.receiver_ty.iterate_path_candidates(
@ -545,12 +538,14 @@ fn trait_applicable_items(
} }
let located_trait = assoc.containing_trait(db)?; let located_trait = assoc.containing_trait(db)?;
let trait_item = ItemInNs::from(ModuleDef::from(located_trait)); let trait_item = ItemInNs::from(ModuleDef::from(located_trait));
let original_item = assoc_to_item(assoc); let import_path = trait_import_paths
.entry(trait_item)
.or_insert_with(|| mod_path(trait_item))
.clone()?;
located_imports.insert(LocatedImport::new( located_imports.insert(LocatedImport::new(
mod_path(trait_item)?, import_path,
trait_item, trait_item,
original_item, assoc_to_item(assoc),
mod_path(original_item),
)); ));
} }
None::<()> None::<()>
@ -568,12 +563,14 @@ fn trait_applicable_items(
if required_assoc_items.contains(&assoc) { if required_assoc_items.contains(&assoc) {
let located_trait = assoc.containing_trait(db)?; let located_trait = assoc.containing_trait(db)?;
let trait_item = ItemInNs::from(ModuleDef::from(located_trait)); let trait_item = ItemInNs::from(ModuleDef::from(located_trait));
let original_item = assoc_to_item(assoc); let import_path = trait_import_paths
.entry(trait_item)
.or_insert_with(|| mod_path(trait_item))
.clone()?;
located_imports.insert(LocatedImport::new( located_imports.insert(LocatedImport::new(
mod_path(trait_item)?, import_path,
trait_item, trait_item,
original_item, assoc_to_item(assoc),
mod_path(original_item),
)); ));
} }
None::<()> None::<()>
@ -671,18 +668,13 @@ fn path_import_candidate(
Some(match qualifier { Some(match qualifier {
Some(qualifier) => match sema.resolve_path(&qualifier) { Some(qualifier) => match sema.resolve_path(&qualifier) {
None => { None => {
let qualifier_start = if qualifier.first_qualifier().map_or(true, |it| sema.resolve_path(&it).is_none()) {
qualifier.syntax().descendants().find_map(ast::NameRef::cast)?; let mut qualifier = qualifier
let qualifier_start_path = .segments_of_this_path_only_rev()
qualifier_start.syntax().ancestors().find_map(ast::Path::cast)?; .map(|seg| seg.name_ref().map(|name| SmolStr::new(name.text())))
if sema.resolve_path(&qualifier_start_path).is_none() { .collect::<Option<Vec<_>>>()?;
ImportCandidate::Path(PathImportCandidate { qualifier.reverse();
qualifier: Some(FirstSegmentUnresolved { ImportCandidate::Path(PathImportCandidate { qualifier: Some(qualifier), name })
fist_segment: qualifier_start,
full_qualifier: qualifier,
}),
name,
})
} else { } else {
return None; return None;
} }

View file

@ -3,13 +3,13 @@
//! The main reason for this module to exist is the fact that project's items and dependencies' items //! The main reason for this module to exist is the fact that project's items and dependencies' items
//! are located in different caches, with different APIs. //! are located in different caches, with different APIs.
use either::Either; use either::Either;
use hir::{import_map, AsAssocItem, Crate, ItemInNs, Semantics}; use hir::{import_map, Crate, ItemInNs, Semantics};
use limit::Limit; use limit::Limit;
use crate::{imports::import_assets::NameToImport, symbol_index, RootDatabase}; use crate::{imports::import_assets::NameToImport, symbol_index, RootDatabase};
/// A value to use, when uncertain which limit to pick. /// A value to use, when uncertain which limit to pick.
pub static DEFAULT_QUERY_SEARCH_LIMIT: Limit = Limit::new(40); pub static DEFAULT_QUERY_SEARCH_LIMIT: Limit = Limit::new(100);
pub use import_map::AssocSearchMode; pub use import_map::AssocSearchMode;
@ -101,8 +101,8 @@ fn find_items<'a>(
.into_iter() .into_iter()
.filter(move |candidate| match assoc_item_search { .filter(move |candidate| match assoc_item_search {
AssocSearchMode::Include => true, AssocSearchMode::Include => true,
AssocSearchMode::Exclude => candidate.def.as_assoc_item(db).is_none(), AssocSearchMode::Exclude => !candidate.is_assoc,
AssocSearchMode::AssocItemsOnly => candidate.def.as_assoc_item(db).is_some(), AssocSearchMode::AssocItemsOnly => candidate.is_assoc,
}) })
.map(|local_candidate| match local_candidate.def { .map(|local_candidate| match local_candidate.def {
hir::ModuleDef::Macro(macro_def) => ItemInNs::Macros(macro_def), hir::ModuleDef::Macro(macro_def) => ItemInNs::Macros(macro_def),

View file

@ -50,7 +50,7 @@ enum SearchMode {
Prefix, Prefix,
} }
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct Query { pub struct Query {
query: String, query: String,
lowercased: String, lowercased: String,

View file

@ -36,6 +36,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "Struct", name: "Struct",
@ -65,6 +66,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "mul1", name: "mul1",
@ -94,6 +96,7 @@
}, },
container_name: None, container_name: None,
is_alias: true, is_alias: true,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "mul2", name: "mul2",
@ -123,6 +126,7 @@
}, },
container_name: None, container_name: None,
is_alias: true, is_alias: true,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "s1", name: "s1",
@ -152,6 +156,7 @@
}, },
container_name: None, container_name: None,
is_alias: true, is_alias: true,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "s1", name: "s1",
@ -181,6 +186,7 @@
}, },
container_name: None, container_name: None,
is_alias: true, is_alias: true,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "s2", name: "s2",
@ -210,6 +216,7 @@
}, },
container_name: None, container_name: None,
is_alias: true, is_alias: true,
is_assoc: false,
}, },
], ],
), ),

View file

@ -34,6 +34,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "CONST", name: "CONST",
@ -61,6 +62,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "CONST_WITH_INNER", name: "CONST_WITH_INNER",
@ -88,6 +90,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "Enum", name: "Enum",
@ -117,6 +120,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "ItemLikeMacro", name: "ItemLikeMacro",
@ -146,6 +150,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "Macro", name: "Macro",
@ -175,6 +180,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "STATIC", name: "STATIC",
@ -202,6 +208,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "Struct", name: "Struct",
@ -231,6 +238,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "StructFromMacro", name: "StructFromMacro",
@ -260,6 +268,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "StructInFn", name: "StructInFn",
@ -291,6 +300,7 @@
"main", "main",
), ),
is_alias: false, is_alias: false,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "StructInNamedConst", name: "StructInNamedConst",
@ -322,6 +332,7 @@
"CONST_WITH_INNER", "CONST_WITH_INNER",
), ),
is_alias: false, is_alias: false,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "StructInUnnamedConst", name: "StructInUnnamedConst",
@ -351,6 +362,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "Trait", name: "Trait",
@ -378,6 +390,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "Trait", name: "Trait",
@ -407,6 +420,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "Union", name: "Union",
@ -436,6 +450,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "a_mod", name: "a_mod",
@ -465,6 +480,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "b_mod", name: "b_mod",
@ -494,6 +510,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "define_struct", name: "define_struct",
@ -523,6 +540,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "impl_fn", name: "impl_fn",
@ -550,6 +568,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: true,
}, },
FileSymbol { FileSymbol {
name: "macro_rules_macro", name: "macro_rules_macro",
@ -579,6 +598,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "main", name: "main",
@ -606,6 +626,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "really_define_struct", name: "really_define_struct",
@ -635,6 +656,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "trait_fn", name: "trait_fn",
@ -664,6 +686,7 @@
"Trait", "Trait",
), ),
is_alias: false, is_alias: false,
is_assoc: true,
}, },
], ],
), ),
@ -704,6 +727,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: false,
}, },
], ],
), ),
@ -744,6 +768,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "StructInModB", name: "StructInModB",
@ -773,6 +798,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "SuperItemLikeMacro", name: "SuperItemLikeMacro",
@ -802,6 +828,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "ThisStruct", name: "ThisStruct",
@ -831,6 +858,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: false,
}, },
FileSymbol { FileSymbol {
name: "ThisStruct", name: "ThisStruct",
@ -860,6 +888,7 @@
}, },
container_name: None, container_name: None,
is_alias: false, is_alias: false,
is_assoc: false,
}, },
], ],
), ),

View file

@ -275,10 +275,19 @@ impl ast::Path {
successors(Some(self.clone()), ast::Path::qualifier).last().unwrap() successors(Some(self.clone()), ast::Path::qualifier).last().unwrap()
} }
pub fn first_qualifier(&self) -> Option<ast::Path> {
successors(self.qualifier(), ast::Path::qualifier).last()
}
pub fn first_segment(&self) -> Option<ast::PathSegment> { pub fn first_segment(&self) -> Option<ast::PathSegment> {
self.first_qualifier_or_self().segment() self.first_qualifier_or_self().segment()
} }
// FIXME: Check usages of Self::segments, they might be wrong because of the logic of the bloew function
pub fn segments_of_this_path_only_rev(&self) -> impl Iterator<Item = ast::PathSegment> + Clone {
self.qualifiers_and_self().filter_map(|it| it.segment())
}
pub fn segments(&self) -> impl Iterator<Item = ast::PathSegment> + Clone { pub fn segments(&self) -> impl Iterator<Item = ast::PathSegment> + Clone {
successors(self.first_segment(), |p| { successors(self.first_segment(), |p| {
p.parent_path().parent_path().and_then(|p| p.segment()) p.parent_path().parent_path().and_then(|p| p.segment())
@ -289,6 +298,10 @@ impl ast::Path {
successors(self.qualifier(), |p| p.qualifier()) successors(self.qualifier(), |p| p.qualifier())
} }
pub fn qualifiers_and_self(&self) -> impl Iterator<Item = ast::Path> + Clone {
successors(Some(self.clone()), |p| p.qualifier())
}
pub fn top_path(&self) -> ast::Path { pub fn top_path(&self) -> ast::Path {
let mut this = self.clone(); let mut this = self.clone();
while let Some(path) = this.parent_path() { while let Some(path) = this.parent_path() {

View file

@ -1,48 +1,8 @@
//! A set of utils methods to reuse on other abstraction levels //! A set of utils methods to reuse on other abstraction levels
use itertools::Itertools; use crate::SyntaxKind;
use crate::{ast, match_ast, AstNode, SyntaxKind};
pub fn path_to_string_stripping_turbo_fish(path: &ast::Path) -> String {
path.syntax()
.children()
.filter_map(|node| {
match_ast! {
match node {
ast::PathSegment(it) => {
Some(it.name_ref()?.to_string())
},
ast::Path(it) => {
Some(path_to_string_stripping_turbo_fish(&it))
},
_ => None,
}
}
})
.join("::")
}
pub fn is_raw_identifier(name: &str) -> bool { pub fn is_raw_identifier(name: &str) -> bool {
let is_keyword = SyntaxKind::from_keyword(name).is_some(); let is_keyword = SyntaxKind::from_keyword(name).is_some();
is_keyword && !matches!(name, "self" | "crate" | "super" | "Self") is_keyword && !matches!(name, "self" | "crate" | "super" | "Self")
} }
#[cfg(test)]
mod tests {
use super::path_to_string_stripping_turbo_fish;
use crate::ast::make;
#[test]
fn turbofishes_are_stripped() {
assert_eq!("Vec", path_to_string_stripping_turbo_fish(&make::path_from_text("Vec::<i32>")),);
assert_eq!(
"Vec::new",
path_to_string_stripping_turbo_fish(&make::path_from_text("Vec::<i32>::new")),
);
assert_eq!(
"Vec::new",
path_to_string_stripping_turbo_fish(&make::path_from_text("Vec::new()")),
);
}
}