Skip to content

Commit

Permalink
Use compiler's paths directly / remove workaround code (#1609)
Browse files Browse the repository at this point in the history
[This](starkware-libs/cairo#6409) needs to be
merged inside the compiler before we merge it.
  • Loading branch information
wawel37 authored Sep 30, 2024
1 parent 05c34eb commit 0366ff0
Show file tree
Hide file tree
Showing 14 changed files with 248 additions and 313 deletions.
153 changes: 34 additions & 119 deletions extensions/scarb-doc/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use cairo_lang_semantic::items::visibility;
use cairo_lang_syntax::node::helpers::QueryAttrs;
use cairo_lang_utils::Upcast;
use itertools::Itertools;
use serde::Serialize;

use cairo_lang_defs::db::DefsGroup;
use cairo_lang_defs::diagnostic_utils::StableLocation;
use cairo_lang_defs::ids::{
ConstantId, EnumId, ExternFunctionId, ExternTypeId, FreeFunctionId, ImplAliasId,
ImplConstantDefId, ImplDefId, ImplFunctionId, ImplItemId, ImplTypeDefId, LanguageElementId,
LookupItemId, MemberId, ModuleId, ModuleItemId, ModuleTypeAliasId, NamedLanguageElementId,
StructId, TopLevelLanguageElementId, TraitConstantId, TraitFunctionId, TraitId, TraitItemId,
TraitTypeId, VariantId,
LookupItemId, MemberId, ModuleId, ModuleItemId, ModuleTypeAliasId, StructId,
TopLevelLanguageElementId, TraitConstantId, TraitFunctionId, TraitId, TraitItemId, TraitTypeId,
VariantId,
};
use cairo_lang_doc::db::DocGroup;
use cairo_lang_doc::documentable_item::DocumentableItemId;
Expand Down Expand Up @@ -81,11 +79,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 @@ -332,9 +329,7 @@ impl Struct {
|| is_visible_in_module(db, root_module_id, &semantic_member.id))
&& !is_doc_hidden_attr(db, syntax_node)
})
.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 @@ -358,25 +353,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 @@ -404,7 +387,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 @@ -428,25 +411,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 @@ -522,26 +493,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 @@ -573,21 +535,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 @@ -603,21 +561,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 @@ -669,22 +623,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 @@ -716,18 +665,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 @@ -743,21 +687,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 Expand Up @@ -837,28 +777,3 @@ impl ExternFunction {
}
}
}

// TODO(#1428): This function is temporarily copied until further modifications in cairo compiler are done.
fn get_item_documentation(db: &dyn DefsGroup, stable_location: &StableLocation) -> Option<String> {
let doc = stable_location.syntax_node(db).get_text(db.upcast());
let doc = doc
.lines()
.take_while_ref(|line| {
!line
.trim_start()
.chars()
.next()
.map_or(false, |c| c.is_alphabetic())
})
.filter_map(|line| {
let dedent = line.trim_start();
for prefix in ["///", "//!"] {
if let Some(content) = dedent.strip_prefix(prefix) {
return Some(content.strip_prefix(' ').unwrap_or(content));
}
}
None
})
.collect::<Vec<&str>>();
(!doc.is_empty()).then(|| doc.join("\n"))
}
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 @@ -14,7 +14,7 @@ impl CircleShape of Shape<Circle>

Shape constant

Fully qualified path: `hello_world::hello_world::CircleShape::SHAPE_CONST`
Fully qualified path: `hello_world::CircleShape::SHAPE_CONST`

```rust
const SHAPE_CONST: felt252 = 'xyz';
Expand All @@ -40,7 +40,7 @@ fn area(self: Circle) -> u32

Type alias for a pair of circles

Fully qualified path: `hello_world::hello_world::CircleShape::ShapePair`
Fully qualified path: `hello_world::CircleShape::ShapePair`

```rust
type ShapePair = (Circle, Circle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ trait Shape<T>

Constant for the shape type

Fully qualified path: `hello_world::hello_world::Shape::SHAPE_CONST`
Fully qualified path: `hello_world::Shape::SHAPE_CONST`

```rust
const SHAPE_CONST: felt252;
Expand All @@ -40,7 +40,7 @@ fn area(self: T) -> u32

Type alias for a pair of shapes

Fully qualified path: `hello_world::hello_world::Shape::ShapePair`
Fully qualified path: `hello_world::Shape::ShapePair`

```rust
type ShapePair;
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 @@ -14,7 +14,7 @@ impl CircleShape of Shape<Circle>

Shape constant

Fully qualified path: `hello_world::hello_world::CircleShape::SHAPE_CONST`
Fully qualified path: `hello_world::CircleShape::SHAPE_CONST`

```rust
const SHAPE_CONST: felt252 = 'xyz';
Expand All @@ -40,7 +40,7 @@ fn area(self: Circle) -> u32

Type alias for a pair of circles

Fully qualified path: `hello_world::hello_world::CircleShape::ShapePair`
Fully qualified path: `hello_world::CircleShape::ShapePair`

```rust
type ShapePair = (Circle, Circle);
Expand Down
Loading

0 comments on commit 0366ff0

Please sign in to comment.