Skip to content

Commit 38c11be

Browse files
committed
Move convert_to_def_in_trait into ide-db
1 parent dcb4837 commit 38c11be

File tree

4 files changed

+30
-47
lines changed

4 files changed

+30
-47
lines changed

crates/ide-db/src/imports/import_assets.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ fn import_for_item(
401401
})
402402
}
403403

404-
fn item_for_path_search(db: &RootDatabase, item: ItemInNs) -> Option<ItemInNs> {
404+
pub fn item_for_path_search(db: &RootDatabase, item: ItemInNs) -> Option<ItemInNs> {
405405
Some(match item {
406406
ItemInNs::Types(_) | ItemInNs::Values(_) => match item_as_assoc(db, item) {
407407
Some(assoc_item) => match assoc_item.container(db) {

crates/ide-db/src/rename.rs

+3-43
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use std::fmt;
2424

2525
use base_db::{AnchoredPathBuf, FileId, FileRange};
2626
use either::Either;
27-
use hir::{AsAssocItem, FieldSource, HasSource, InFile, ModuleSource, Semantics};
27+
use hir::{FieldSource, HasSource, InFile, ModuleSource, Semantics};
2828
use stdx::never;
2929
use syntax::{
3030
ast::{self, HasName},
@@ -37,6 +37,7 @@ use crate::{
3737
search::FileReference,
3838
source_change::{FileSystemEdit, SourceChange},
3939
syntax_helpers::node_ext::expr_as_name_ref,
40+
traits::convert_to_def_in_trait,
4041
RootDatabase,
4142
};
4243

@@ -271,7 +272,7 @@ fn rename_reference(
271272
}
272273
}
273274

274-
let def = convert_to_def_in_trait(def, sema);
275+
let def = convert_to_def_in_trait(sema.db, def);
275276
let usages = def.usages(sema).all();
276277

277278
if !usages.is_empty() && ident_kind == IdentifierKind::Underscore {
@@ -298,47 +299,6 @@ fn rename_reference(
298299
Ok(source_change)
299300
}
300301

301-
pub(crate) fn convert_to_def_in_trait(
302-
def: Definition,
303-
sema: &Semantics<RootDatabase>,
304-
) -> Definition {
305-
// HACK: resolve trait impl items to the item def of the trait definition
306-
// so that we properly resolve all trait item references
307-
let assoc_item = match def {
308-
Definition::Function(it) => it.as_assoc_item(sema.db),
309-
Definition::TypeAlias(it) => it.as_assoc_item(sema.db),
310-
Definition::Const(it) => it.as_assoc_item(sema.db),
311-
_ => None,
312-
};
313-
match assoc_item {
314-
Some(assoc) => assoc
315-
.containing_trait_impl(sema.db)
316-
.and_then(|trait_| {
317-
trait_.items(sema.db).into_iter().find_map(|it| match (it, assoc) {
318-
(hir::AssocItem::Function(trait_func), hir::AssocItem::Function(func))
319-
if trait_func.name(sema.db) == func.name(sema.db) =>
320-
{
321-
Some(Definition::Function(trait_func))
322-
}
323-
(hir::AssocItem::Const(trait_konst), hir::AssocItem::Const(konst))
324-
if trait_konst.name(sema.db) == konst.name(sema.db) =>
325-
{
326-
Some(Definition::Const(trait_konst))
327-
}
328-
(
329-
hir::AssocItem::TypeAlias(trait_type_alias),
330-
hir::AssocItem::TypeAlias(type_alias),
331-
) if trait_type_alias.name(sema.db) == type_alias.name(sema.db) => {
332-
Some(Definition::TypeAlias(trait_type_alias))
333-
}
334-
_ => None,
335-
})
336-
})
337-
.unwrap_or(def),
338-
None => def,
339-
}
340-
}
341-
342302
pub fn source_edit_from_references(
343303
references: &[FileReference],
344304
def: Definition,

crates/ide-db/src/search.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use syntax::{ast, match_ast, AstNode, TextRange, TextSize};
1616

1717
use crate::{
1818
defs::{Definition, NameClass, NameRefClass},
19+
traits::convert_to_def_in_trait,
1920
RootDatabase,
2021
};
2122

@@ -620,7 +621,7 @@ impl<'a> FindUsages<'a> {
620621
sink(file_id, reference)
621622
}
622623
Some(NameRefClass::Definition(def))
623-
if crate::rename::convert_to_def_in_trait(def, self.sema) == self.def =>
624+
if convert_to_def_in_trait(self.sema.db, def) == self.def =>
624625
{
625626
let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax());
626627
let reference = FileReference {

crates/ide-db/src/traits.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Functionality for obtaining data related to traits from the DB.
22
3-
use crate::RootDatabase;
4-
use hir::Semantics;
3+
use crate::{defs::Definition, RootDatabase};
4+
use hir::{db::HirDatabase, AsAssocItem, Semantics};
55
use rustc_hash::FxHashSet;
66
use syntax::{ast, AstNode};
77

@@ -69,6 +69,28 @@ pub fn get_missing_assoc_items(
6969
})
7070
}
7171

72+
/// Converts associated trait impl items to their trait definition counterpart
73+
pub(crate) fn convert_to_def_in_trait(db: &dyn HirDatabase, def: Definition) -> Definition {
74+
use hir::AssocItem::*;
75+
(|| {
76+
let assoc = def.as_assoc_item(db)?;
77+
let trait_ = assoc.containing_trait_impl(db)?;
78+
let name = match assoc {
79+
Function(it) => it.name(db),
80+
Const(it) => it.name(db)?,
81+
TypeAlias(it) => it.name(db),
82+
};
83+
let item = trait_.items(db).into_iter().find(|it| match (it, assoc) {
84+
(Function(trait_func), Function(_)) => trait_func.name(db) == name,
85+
(Const(trait_konst), Const(_)) => trait_konst.name(db).map_or(false, |it| it == name),
86+
(TypeAlias(trait_type_alias), TypeAlias(_)) => trait_type_alias.name(db) == name,
87+
_ => false,
88+
})?;
89+
Some(Definition::from(item))
90+
})()
91+
.unwrap_or(def)
92+
}
93+
7294
#[cfg(test)]
7395
mod tests {
7496
use base_db::{fixture::ChangeFixture, FilePosition};

0 commit comments

Comments
 (0)