Skip to content

Commit

Permalink
Use compiler's paths directly / remove workaround code
Browse files Browse the repository at this point in the history
  • Loading branch information
wawel37 committed Sep 23, 2024
1 parent 9bcab97 commit 1faf9ce
Show file tree
Hide file tree
Showing 13 changed files with 240 additions and 278 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

120 changes: 31 additions & 89 deletions extensions/scarb-doc/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,10 @@ impl Module {
module_id: ModuleId,
include_private_items: bool,
) -> Self {
// FIXME(#1438): compiler doesn't support fetching root crate doc
let item_data = match module_id {
ModuleId::CrateRoot(crate_id) => ItemData {
name: crate_id.name(db).to_string(),
doc: None,
doc: db.get_item_documentation(DocumentableItemId::Crate(crate_id)),
signature: None,
full_path: module_id.full_path(db),
},
Expand Down Expand Up @@ -323,9 +322,7 @@ impl Struct {
include_private_items
|| is_visible_in_module(db, root_module_id, &semantic_member.id)
})
.map(|(_name, semantic_member)| {
Member::new(db, semantic_member.id, item_data.full_path.clone())
})
.map(|(_name, semantic_member)| Member::new(db, semantic_member.id))
.collect::<Vec<_>>();

let node = id.stable_ptr(db);
Expand All @@ -349,25 +346,13 @@ pub struct Member {
}

impl Member {
pub fn new(db: &ScarbDocDatabase, id: MemberId, struct_full_path: String) -> Self {
pub fn new(db: &ScarbDocDatabase, id: MemberId) -> Self {
let node = id.stable_ptr(db);
let stable_location = StableLocation::new(node.0);

let name = id.name(db).into();
// TODO(#1438): Replace with `id.full_path(db)` after it is fixed in the compiler.
let full_path = format!("{}::{}", struct_full_path, name);

let item_data = ItemData {
name,
doc: get_item_documentation(db, &stable_location),
signature: None,
full_path,
};

Self {
id,
node,
item_data,
item_data: ItemData::new(db, id, DocumentableItemId::Member(id)),
}
}
}
Expand Down Expand Up @@ -395,7 +380,7 @@ impl Enum {

let variants = variants
.iter()
.map(|(_name, variant_id)| Variant::new(db, *variant_id, item_data.full_path.clone()))
.map(|(_name, variant_id)| Variant::new(db, *variant_id))
.collect::<Vec<_>>();

let node = id.stable_ptr(db);
Expand All @@ -419,25 +404,13 @@ pub struct Variant {
}

impl Variant {
pub fn new(db: &ScarbDocDatabase, id: VariantId, enum_full_path: String) -> Self {
pub fn new(db: &ScarbDocDatabase, id: VariantId) -> Self {
let node = id.stable_ptr(db);
let stable_location = StableLocation::new(node.0);

let name = id.name(db).into();
// TODO(#1438): Replace with `id.full_path(db)` after it is fixed in the compiler.
let full_path = format!("{}::{}", enum_full_path, name);

let item_data = ItemData {
name,
doc: get_item_documentation(db, &stable_location),
signature: None,
full_path,
};

Self {
id,
node,
item_data,
item_data: ItemData::new(db, id, DocumentableItemId::Variant(id)),
}
}
}
Expand Down Expand Up @@ -513,26 +486,17 @@ impl Trait {
id,
LookupItemId::ModuleItem(ModuleItemId::Trait(id)).into(),
);
let full_path_to_trait = item_data
.full_path
.strip_suffix(item_data.name.as_str())
.unwrap()
.to_string();

let trait_constants = db.trait_constants(id).unwrap();
let trait_constants = trait_constants
.iter()
.map(|(_name, trait_constant_id)| {
TraitConstant::new(db, *trait_constant_id, full_path_to_trait.clone())
})
.map(|(_name, trait_constant_id)| TraitConstant::new(db, *trait_constant_id))
.collect::<Vec<_>>();

let trait_types = db.trait_types(id).unwrap();
let trait_types = trait_types
.iter()
.map(|(_name, trait_type_id)| {
TraitType::new(db, *trait_type_id, full_path_to_trait.clone())
})
.map(|(_name, trait_type_id)| TraitType::new(db, *trait_type_id))
.collect::<Vec<_>>();

let trait_functions = db.trait_functions(id).unwrap();
Expand Down Expand Up @@ -564,21 +528,17 @@ pub struct TraitConstant {
}

impl TraitConstant {
pub fn new(db: &ScarbDocDatabase, id: TraitConstantId, full_path_to_trait: String) -> Self {
pub fn new(db: &ScarbDocDatabase, id: TraitConstantId) -> Self {
let node = id.stable_ptr(db);

let mut item_data = ItemData::new(
db,
id,
LookupItemId::TraitItem(TraitItemId::Constant(id)).into(),
);
// TODO(#1438): introduce proper fix in compiler
item_data.full_path = full_path_to_trait + &item_data.full_path;

Self {
id,
node,
item_data,
item_data: ItemData::new(
db,
id,
LookupItemId::TraitItem(TraitItemId::Constant(id)).into(),
),
}
}
}
Expand All @@ -594,21 +554,17 @@ pub struct TraitType {
}

impl TraitType {
pub fn new(db: &ScarbDocDatabase, id: TraitTypeId, full_path_to_trait: String) -> Self {
pub fn new(db: &ScarbDocDatabase, id: TraitTypeId) -> Self {
let node = id.stable_ptr(db);

let mut item_data = ItemData::new(
db,
id,
LookupItemId::TraitItem(TraitItemId::Type(id)).into(),
);
// TODO(#1438): introduce proper fix in compiler
item_data.full_path = full_path_to_trait + &item_data.full_path;

Self {
id,
node,
item_data,
item_data: ItemData::new(
db,
id,
LookupItemId::TraitItem(TraitItemId::Type(id)).into(),
),
}
}
}
Expand Down Expand Up @@ -660,22 +616,17 @@ impl Impl {
id,
LookupItemId::ModuleItem(ModuleItemId::Impl(id)).into(),
);
let full_path_to_impl = item_data
.full_path
.strip_suffix(item_data.name.as_str())
.unwrap()
.to_string();

let impl_types = db.impl_types(id).unwrap();
let impl_types = impl_types
.iter()
.map(|(id, _)| ImplType::new(db, *id, full_path_to_impl.clone()))
.map(|(id, _)| ImplType::new(db, *id))
.collect::<Vec<_>>();

let impl_constants = db.impl_constants(id).unwrap();
let impl_constants = impl_constants
.iter()
.map(|(id, _)| ImplConstant::new(db, *id, full_path_to_impl.clone()))
.map(|(id, _)| ImplConstant::new(db, *id))
.collect::<Vec<_>>();

let impl_functions = db.impl_functions(id).unwrap();
Expand Down Expand Up @@ -707,18 +658,13 @@ pub struct ImplType {
}

impl ImplType {
pub fn new(db: &ScarbDocDatabase, id: ImplTypeDefId, full_path_to_impl: String) -> Self {
pub fn new(db: &ScarbDocDatabase, id: ImplTypeDefId) -> Self {
let node = id.stable_ptr(db);

let mut item_data =
ItemData::new(db, id, LookupItemId::ImplItem(ImplItemId::Type(id)).into());
// TODO(#1438): introduce proper fix in compiler
item_data.full_path = full_path_to_impl + &item_data.full_path;

Self {
id,
node,
item_data,
item_data: ItemData::new(db, id, LookupItemId::ImplItem(ImplItemId::Type(id)).into()),
}
}
}
Expand All @@ -734,21 +680,17 @@ pub struct ImplConstant {
}

impl ImplConstant {
pub fn new(db: &ScarbDocDatabase, id: ImplConstantDefId, full_path_to_impl: String) -> Self {
pub fn new(db: &ScarbDocDatabase, id: ImplConstantDefId) -> Self {
let node = id.stable_ptr(db);

let mut item_data = ItemData::new(
db,
id,
LookupItemId::ImplItem(ImplItemId::Constant(id)).into(),
);
// TODO(#1438): introduce proper fix in compiler
item_data.full_path = full_path_to_impl + &item_data.full_path;

Self {
id,
node,
item_data,
item_data: ItemData::new(
db,
id,
LookupItemId::ImplItem(ImplItemId::Constant(id)).into(),
),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ Radius of the circle

Fully qualified path: `hello_world::Circle::radius`

```rust
radius: u32
```


Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const SHAPE_CONST: felt252;

Calculate the area of the shape

Fully qualified path: `Shape::area`
Fully qualified path: `hello_world::Shape::area`

```rust
fn area(self: T) -> u32
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# hello_world

Fibonacci sequence calculator


Fully qualified path: `hello_world`

## Modules
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# hello_world_sub_package

Sub-package code (without feature)


Fully qualified path: `hello_world_sub_package`

## Free functions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# hello_world_sub_package

Sub-package code (with feature)


Fully qualified path: `hello_world_sub_package`

## Free functions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ Radius of the circle

Fully qualified path: `hello_world::Circle::radius`

```rust
radius: u32
```


Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const SHAPE_CONST: felt252;

Calculate the area of the shape

Fully qualified path: `Shape::area`
Fully qualified path: `hello_world::Shape::area`

```rust
fn area(self: T) -> u32
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# hello_world

Fibonacci sequence calculator


Fully qualified path: `hello_world`

## Modules
Expand Down
12 changes: 6 additions & 6 deletions extensions/scarb-doc/tests/data/json_output_test_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"root_module": {
"item_data": {
"name": "hello_world",
"doc": null,
"doc": "Fibonacci sequence calculator\n",
"signature": null,
"full_path": "hello_world"
},
Expand Down Expand Up @@ -75,7 +75,7 @@
"item_data": {
"name": "radius",
"doc": "Radius of the circle",
"signature": null,
"signature": " radius: u32",
"full_path": "hello_world::Circle::radius"
}
}
Expand All @@ -95,23 +95,23 @@
"item_data": {
"name": "Red",
"doc": "Red color",
"signature": null,
"signature": "",
"full_path": "hello_world::Color::Red"
}
},
{
"item_data": {
"name": "Green",
"doc": "Green color",
"signature": null,
"signature": "",
"full_path": "hello_world::Color::Green"
}
},
{
"item_data": {
"name": "Blue",
"doc": "Blue color",
"signature": null,
"signature": "",
"full_path": "hello_world::Color::Blue"
}
}
Expand Down Expand Up @@ -163,7 +163,7 @@
"name": "area",
"doc": "Calculate the area of the shape",
"signature": "fn area(self: T) -> u32",
"full_path": "Shape::area"
"full_path": "hello_world::Shape::area"
}
}
],
Expand Down
Loading

0 comments on commit 1faf9ce

Please sign in to comment.