Skip to content

Commit

Permalink
Add impl constant
Browse files Browse the repository at this point in the history
  • Loading branch information
drknzz committed Jun 10, 2024
1 parent 1f52c32 commit a53646d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
1 change: 1 addition & 0 deletions extensions/scarb-doc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ fn print_module(module: &types::Module) {
for imp in module.impls.iter() {
println!(" Impl {:?}", imp.item_data.name);
print_names!(" Impl types ", imp.impl_types);
print_names!(" Impl constants ", imp.impl_constants);
print_names!(" Impl functions ", imp.impl_functions);
}
print_names!("Extern Types ", module.extern_types);
Expand Down
40 changes: 36 additions & 4 deletions extensions/scarb-doc/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_defs::db::DefsGroup;
use cairo_lang_defs::ids::{
ConstantId, EnumId, ExternFunctionId, ExternTypeId, FreeFunctionId, ImplAliasId, ImplDefId,
ImplFunctionId, ImplItemId, ImplTypeDefId, LookupItemId, MemberId, ModuleId, ModuleItemId,
ModuleTypeAliasId, StructId, TopLevelLanguageElementId, TraitConstantId, TraitFunctionId,
TraitId, TraitItemId, TraitTypeId, UseId, VariantId,
ConstantId, EnumId, ExternFunctionId, ExternTypeId, FreeFunctionId, ImplAliasId,
ImplConstantDefId, ImplDefId, ImplFunctionId, ImplItemId, ImplTypeDefId, LookupItemId,
MemberId, ModuleId, ModuleItemId, ModuleTypeAliasId, StructId, TopLevelLanguageElementId,
TraitConstantId, TraitFunctionId, TraitId, TraitItemId, TraitTypeId, UseId, VariantId,
};
use cairo_lang_filesystem::ids::CrateId;
use cairo_lang_semantic::db::SemanticGroup;
Expand Down Expand Up @@ -530,6 +530,7 @@ pub struct Impl {
pub node: ast::ItemImplPtr,

pub impl_types: Vec<ImplType>,
pub impl_constants: Vec<ImplConstant>,
pub impl_functions: Vec<ImplFunction>,

pub item_data: ItemData,
Expand All @@ -544,6 +545,12 @@ impl Impl {
.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))
.collect::<Vec<_>>();

let impl_functions = db.impl_functions(id).unwrap();
let impl_functions = impl_functions
.iter()
Expand All @@ -555,6 +562,7 @@ impl Impl {
id,
node,
impl_types,
impl_constants,
impl_functions,
item_data: ItemData::new(db, id, node),
lookup_item_data: LookupItemData::new(
Expand Down Expand Up @@ -586,6 +594,30 @@ impl ImplType {
}
}

#[derive(Clone, Debug)]
pub struct ImplConstant {
pub id: ImplConstantDefId,
pub node: ast::ItemConstantPtr,

pub item_data: ItemData,
pub lookup_item_data: LookupItemData,
}

impl ImplConstant {
pub fn new(db: &RootDatabase, id: ImplConstantDefId) -> Self {
let node = id.stable_ptr(db);
Self {
id,
node,
item_data: ItemData::new(db, id, node),
lookup_item_data: LookupItemData::new(
db,
LookupItemId::ImplItem(ImplItemId::Constant(id)),
),
}
}
}

#[derive(Clone, Debug)]
pub struct ImplFunction {
pub id: ImplFunctionId,
Expand Down
9 changes: 7 additions & 2 deletions extensions/scarb-doc/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ fn test_main() {
/// Type alias for a pair of circles
type ShapePair<Circle> = (Circle, Circle);
/// Shape constant
const SHAPE_CONST = "xyz";
/// Implementation of the area method for Circle
fn area(self: Circle) -> u32 {
3 * self.radius * self.radius
Expand All @@ -106,8 +109,6 @@ fn test_main() {
.success();
let stdout = std::str::from_utf8(&output.get_output().stdout).unwrap();

println!("{:?}", &stdout);

assert_eq!(
stdout,
indoc! {r#"
Expand All @@ -132,15 +133,19 @@ fn test_main() {
Impls : ["CircleShape", "CircleDrop", "CircleSerde", "CirclePartialEq"]
Impl "CircleShape"
Impl types : ["ShapePair"]
Impl constants : ["SHAPE_CONST"]
Impl functions : ["area"]
Impl "CircleDrop"
Impl types : []
Impl constants : []
Impl functions : []
Impl "CircleSerde"
Impl types : []
Impl constants : []
Impl functions : ["serialize", "deserialize"]
Impl "CirclePartialEq"
Impl types : []
Impl constants : []
Impl functions : ["eq"]
Extern Types : []
Extern Functions: []
Expand Down

0 comments on commit a53646d

Please sign in to comment.