Skip to content

Commit 33d7e20

Browse files
committed
fix: handle case when impl{} for item A occurs after item B
Example: struct A {..} struct B {..} impl A {..} Prior to this commit, the diagram would incorrectly display the methods inside impl A as B's methods. Now it correctly displays them as A's methods.
1 parent 4cecbbb commit 33d7e20

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/core/item/mod.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,23 @@ impl <'a>Iterator for Item<'a> {
3838
self.it.next().and_then(|item| {
3939
let mut list: Vec<&'a (ptr::P<ast::Item>, Rc<ModulePath>)> = vec!(item);
4040

41-
list.extend(self.it.peeking_take_while(|&&(ref item, _): &&'a (ptr::P<ast::Item>, Rc<ModulePath>)| {
42-
if let ast::ItemKind::Impl(..) = item.kind {
43-
true
44-
} else {
45-
false
41+
// Loop over all Items to find any Impl with a name that matches our name.
42+
// This way we can handle cases like:
43+
// struct Foo{}
44+
// struct Bar{}
45+
// impl Foo {...}
46+
// impl Bar {...}
47+
// impl Foo {...}
48+
let item_name = &item.0.ident.name;
49+
list.extend(self.it.clone().filter(|&&(ref subitem, _): &&'a (ptr::P<ast::Item>, Rc<ModulePath>)| {
50+
if let &ast::ItemKind::Impl(box ast::ImplKind{self_ty: ref ty, ..}) = &subitem.kind {
51+
if let &ast::Ty {kind: ast::TyKind::Path(_, ast::Path{segments: ref seg, ..} ), .. } = &**ty {
52+
if !seg.is_empty() && &seg[0].ident.name == item_name {
53+
return true;
54+
}
55+
}
4656
}
57+
false
4758
})
4859
.collect::<Vec<&'a (ptr::P<ast::Item>, Rc<ModulePath>)>>());
4960
Some(ItemState::from(list))

0 commit comments

Comments
 (0)