From bbf35212784e495fa0fc1b70eb1ac42d719a7e4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Tr=C4=85tnowiecki?= Date: Sat, 29 Jun 2024 08:40:37 +0200 Subject: [PATCH] No doc --- extensions/scarb-doc/src/compilation.rs | 137 --------- extensions/scarb-doc/src/main.rs | 70 +---- extensions/scarb-doc/src/types.rs | 357 ------------------------ 3 files changed, 2 insertions(+), 562 deletions(-) delete mode 100644 extensions/scarb-doc/src/compilation.rs delete mode 100644 extensions/scarb-doc/src/types.rs diff --git a/extensions/scarb-doc/src/compilation.rs b/extensions/scarb-doc/src/compilation.rs deleted file mode 100644 index f5fa4de6c..000000000 --- a/extensions/scarb-doc/src/compilation.rs +++ /dev/null @@ -1,137 +0,0 @@ -use scarb_metadata::{ - CompilationUnitComponentMetadata, CompilationUnitMetadata, Metadata, PackageId, PackageMetadata, -}; -use smol_str::{SmolStr, ToSmolStr}; -use std::path::PathBuf; - -use cairo_lang_compiler::project::{AllCratesConfig, ProjectConfig, ProjectConfigContent}; -use cairo_lang_filesystem::cfg::{Cfg, CfgSet}; -use cairo_lang_filesystem::db::{CrateSettings, Edition, ExperimentalFeaturesConfig}; -use cairo_lang_filesystem::ids::Directory; -use cairo_lang_utils::ordered_hash_map::OrderedHashMap; - -const LIB_TARGET_KIND: &str = "lib"; -const CORELIB_CRATE_NAME: &str = "core"; - -pub fn get_project_config( - metadata: &Metadata, - package_metadata: &PackageMetadata, -) -> ProjectConfig { - let compilation_unit_metadata = - package_lib_compilation_unit(metadata, package_metadata.id.clone()) - .expect("failed to find compilation unit for package"); - let corelib = get_corelib(compilation_unit_metadata); - let dependencies = get_dependencies(compilation_unit_metadata); - let crates_config = get_crates_config(metadata, compilation_unit_metadata); - - ProjectConfig { - base_path: package_metadata.root.clone().into(), - corelib: Some(Directory::Real(corelib.source_root().into())), - content: ProjectConfigContent { - crate_roots: dependencies, - crates_config, - }, - } -} - -fn package_lib_compilation_unit( - metadata: &Metadata, - package_id: PackageId, -) -> Option<&CompilationUnitMetadata> { - metadata - .compilation_units - .iter() - .find(|m| m.package == package_id && m.target.kind == LIB_TARGET_KIND) -} - -fn get_corelib( - compilation_unit_metadata: &CompilationUnitMetadata, -) -> &CompilationUnitComponentMetadata { - compilation_unit_metadata - .components - .iter() - .find(|du| du.name == CORELIB_CRATE_NAME) - .expect("Corelib could not be found") -} - -fn get_dependencies( - compilation_unit_metadata: &CompilationUnitMetadata, -) -> OrderedHashMap { - compilation_unit_metadata - .components - .iter() - .filter(|du| du.name != CORELIB_CRATE_NAME) - .map(|cu| { - ( - cu.name.to_smolstr(), - cu.source_root().to_owned().into_std_path_buf(), - ) - }) - .collect() -} - -fn get_crates_config( - metadata: &Metadata, - compilation_unit_metadata: &CompilationUnitMetadata, -) -> AllCratesConfig { - let crates_config: OrderedHashMap = compilation_unit_metadata - .components - .iter() - .map(|component| { - let pkg = metadata.get_package(&component.package).unwrap_or_else(|| { - panic!( - "failed to find = {} package", - &component.package.to_string() - ) - }); - ( - SmolStr::from(&component.name), - get_crate_settings_for_package( - pkg, - component.cfg.as_ref().map(|cfg_vec| build_cfg_set(cfg_vec)), - ), - ) - }) - .collect(); - - AllCratesConfig { - override_map: crates_config, - ..Default::default() - } -} - -fn get_crate_settings_for_package( - package: &PackageMetadata, - cfg_set: Option, -) -> CrateSettings { - let edition = package - .edition - .clone() - .map_or(Edition::default(), |edition| { - let edition_value = serde_json::Value::String(edition); - serde_json::from_value(edition_value).unwrap() - }); - - let experimental_features = ExperimentalFeaturesConfig { - negative_impls: package - .experimental_features - .contains(&String::from("negative_impls")), - coupons: package - .experimental_features - .contains(&String::from("coupons")), - }; - - CrateSettings { - edition, - cfg_set, - experimental_features, - } -} - -fn build_cfg_set(cfg: &[scarb_metadata::Cfg]) -> CfgSet { - CfgSet::from_iter(cfg.iter().map(|cfg| { - serde_json::to_value(cfg) - .and_then(serde_json::from_value::) - .expect("Cairo's `Cfg` must serialize identically as Scarb Metadata's `Cfg`.") - })) -} diff --git a/extensions/scarb-doc/src/main.rs b/extensions/scarb-doc/src/main.rs index eb9f4abcd..c1c7bb340 100644 --- a/extensions/scarb-doc/src/main.rs +++ b/extensions/scarb-doc/src/main.rs @@ -1,19 +1,7 @@ use anyhow::Result; use clap::Parser; - -use scarb_metadata::MetadataCommand; use scarb_ui::args::PackagesFilter; -use cairo_lang_compiler::db::RootDatabase; -use cairo_lang_filesystem::db::FilesGroup; -use cairo_lang_filesystem::ids::CrateLongId; - -use compilation::get_project_config; -use types::Crate; - -mod compilation; -mod types; - #[derive(Parser, Debug)] #[command(version, about, long_about = None)] struct Args { @@ -21,62 +9,8 @@ struct Args { packages_filter: PackagesFilter, } -macro_rules! print_names { - ($label:expr, $var:expr) => { - println!( - "{}: {:?}", - $label, - $var.iter().map(|x| &x.item_data.name).collect::>() - ); - }; -} - fn main() -> Result<()> { - let args = Args::parse(); - - let metadata = MetadataCommand::new().inherit_stderr().exec()?; - let package_metadata = args.packages_filter.match_one(&metadata)?; - - let project_config = get_project_config(&metadata, &package_metadata); - - let db = &mut { - let mut b = RootDatabase::builder(); - b.with_project_config(project_config); - b.build()? - }; - - let main_crate_id = db.intern_crate(CrateLongId::Real(package_metadata.name.clone().into())); - let crate_ = Crate::new(db, main_crate_id); - - print_module(&crate_.root_module); - - Ok(()) -} - -fn print_module(module: &types::Module) { - println!("Module: {}", module.full_path); - println!( - "Submodules : {:?}", - module - .submodules - .iter() - .map(|x| &x.full_path) - .collect::>() - ); - print_names!("Constants ", module.constants); - print_names!("Uses ", module.uses); - print_names!("Free Functions ", module.free_functions); - print_names!("Structs ", module.structs); - print_names!("Enums ", module.enums); - print_names!("Type Aliases ", module.type_aliases); - print_names!("Impl Aliases ", module.impl_aliases); - print_names!("Traits ", module.traits); - print_names!("Impls ", module.impls); - print_names!("Extern Types ", module.extern_types); - print_names!("Extern Functions", module.extern_functions); + let _args = Args::parse(); - for submodule in &module.submodules { - println!(); - print_module(submodule); - } + panic!("doc is not available in this build"); } diff --git a/extensions/scarb-doc/src/types.rs b/extensions/scarb-doc/src/types.rs deleted file mode 100644 index 68c040b9a..000000000 --- a/extensions/scarb-doc/src/types.rs +++ /dev/null @@ -1,357 +0,0 @@ -// TODO(drknzz): Remove if not needed. -#![allow(dead_code)] - -use cairo_lang_defs::db::DefsGroup; -use cairo_lang_defs::ids::{ - ConstantId, EnumId, ExternFunctionId, ExternTypeId, FreeFunctionId, ImplAliasId, ImplDefId, - LookupItemId, ModuleId, ModuleItemId, ModuleTypeAliasId, NamedLanguageElementId, StructId, - TopLevelLanguageElementId, TraitId, UseId, -}; -use cairo_lang_filesystem::ids::CrateId; -use cairo_lang_syntax::node::{ast, TypedSyntaxNode}; - -#[derive(Clone, Debug)] -pub struct Crate { - pub root_module: Module, -} - -impl Crate { - pub fn new(db: &dyn DefsGroup, crate_id: CrateId) -> Self { - Self { - root_module: Module::new(db, ModuleId::CrateRoot(crate_id)), - } - } -} - -#[derive(Clone, Debug)] -pub struct Module { - pub module_id: ModuleId, - pub full_path: String, - - pub submodules: Vec, - pub constants: Vec, - pub uses: Vec, - pub free_functions: Vec, - pub structs: Vec, - pub enums: Vec, - pub type_aliases: Vec, - pub impl_aliases: Vec, - pub traits: Vec, - pub impls: Vec, - pub extern_types: Vec, - pub extern_functions: Vec, -} - -impl Module { - pub fn new(db: &dyn DefsGroup, module_id: ModuleId) -> Self { - let module_constants = db.module_constants(module_id).unwrap(); - let constants = module_constants - .iter() - .map(|(id, node)| Constant::new(db, *id, node)) - .collect(); - - let module_uses = db.module_uses(module_id).unwrap(); - let uses = module_uses - .iter() - .map(|(id, node)| Use::new(db, *id, node)) - .collect(); - - let module_free_functions = db.module_free_functions(module_id).unwrap(); - let free_functions = module_free_functions - .iter() - .map(|(id, node)| FreeFunction::new(db, *id, node)) - .collect(); - - let module_structs = db.module_structs(module_id).unwrap(); - let structs = module_structs - .iter() - .map(|(id, node)| Struct::new(db, *id, node)) - .collect(); - - let module_enums = db.module_enums(module_id).unwrap(); - let enums = module_enums - .iter() - .map(|(id, node)| Enum::new(db, *id, node)) - .collect(); - - let module_type_aliases = db.module_type_aliases(module_id).unwrap(); - let type_aliases = module_type_aliases - .iter() - .map(|(id, node)| TypeAlias::new(db, *id, node)) - .collect(); - - let module_impl_aliases = db.module_impl_aliases(module_id).unwrap(); - let impl_aliases = module_impl_aliases - .iter() - .map(|(id, node)| ImplAlias::new(db, *id, node)) - .collect(); - - let module_traits = db.module_traits(module_id).unwrap(); - let traits = module_traits - .iter() - .map(|(id, node)| Trait::new(db, *id, node)) - .collect(); - - let module_impls = db.module_impls(module_id).unwrap(); - let impls = module_impls - .iter() - .map(|(id, node)| Impl::new(db, *id, node)) - .collect(); - - let module_extern_types = db.module_extern_types(module_id).unwrap(); - let extern_types = module_extern_types - .iter() - .map(|(id, node)| ExternType::new(db, *id, node)) - .collect(); - - let module_extern_functions = db.module_extern_functions(module_id).unwrap(); - let extern_functions = module_extern_functions - .iter() - .map(|(id, node)| ExternFunction::new(db, *id, node)) - .collect(); - - let module_submodules = db.module_submodules(module_id).unwrap(); - let submodules = module_submodules - .iter() - .map(|(id, _node)| Self::new(db, ModuleId::Submodule(*id))) - .collect(); - - Self { - module_id, - full_path: module_id.full_path(db), - submodules, - constants, - uses, - free_functions, - structs, - enums, - type_aliases, - impl_aliases, - traits, - impls, - extern_types, - extern_functions, - } - } -} - -#[derive(Clone, Debug)] -pub struct ItemData { - pub name: String, - pub full_path: String, - - pub doc: Option, - pub definition: String, - - pub text: String, -} - -impl ItemData { - pub fn new(db: &dyn DefsGroup, id: ModuleItemId, node: &impl TypedSyntaxNode) -> Self { - Self { - name: id.name(db).into(), - full_path: id.full_path(db), - doc: db.get_item_documentation(LookupItemId::ModuleItem(id)), - definition: db.get_item_signature(LookupItemId::ModuleItem(id)), - text: node.as_syntax_node().get_text_without_trivia(db.upcast()), - } - } -} - -#[derive(Clone, Debug)] -pub struct Constant { - pub id: ConstantId, - pub node: ast::ItemConstantPtr, - - pub item_data: ItemData, -} - -impl Constant { - pub fn new(db: &dyn DefsGroup, id: ConstantId, node: &ast::ItemConstant) -> Self { - Self { - id, - node: node.stable_ptr(), - item_data: ItemData::new(db, ModuleItemId::Constant(id), node), - } - } -} - -#[derive(Clone, Debug)] -pub struct Use { - pub id: UseId, - pub node: ast::UsePathLeafPtr, - - pub item_data: ItemData, -} - -impl Use { - pub fn new(db: &dyn DefsGroup, id: UseId, node: &ast::UsePathLeaf) -> Self { - Self { - id, - node: node.stable_ptr(), - item_data: ItemData::new(db, ModuleItemId::Use(id), node), - } - } -} - -#[derive(Clone, Debug)] -pub struct FreeFunction { - pub id: FreeFunctionId, - pub node: ast::FunctionWithBodyPtr, - - pub item_data: ItemData, -} - -impl FreeFunction { - pub fn new(db: &dyn DefsGroup, id: FreeFunctionId, node: &ast::FunctionWithBody) -> Self { - Self { - id, - node: node.stable_ptr(), - item_data: ItemData::new(db, ModuleItemId::FreeFunction(id), node), - } - } -} - -#[derive(Clone, Debug)] -pub struct Struct { - pub id: StructId, - pub node: ast::ItemStructPtr, - - pub item_data: ItemData, -} - -impl Struct { - pub fn new(db: &dyn DefsGroup, id: StructId, node: &ast::ItemStruct) -> Self { - Self { - id, - node: node.stable_ptr(), - item_data: ItemData::new(db, ModuleItemId::Struct(id), node), - } - } -} - -#[derive(Clone, Debug)] -pub struct Enum { - pub id: EnumId, - pub node: ast::ItemEnumPtr, - - pub item_data: ItemData, -} - -impl Enum { - pub fn new(db: &dyn DefsGroup, id: EnumId, node: &ast::ItemEnum) -> Self { - Self { - id, - node: node.stable_ptr(), - item_data: ItemData::new(db, ModuleItemId::Enum(id), node), - } - } -} - -#[derive(Clone, Debug)] -pub struct TypeAlias { - pub id: ModuleTypeAliasId, - pub node: ast::ItemTypeAliasPtr, - - pub item_data: ItemData, -} - -impl TypeAlias { - pub fn new(db: &dyn DefsGroup, id: ModuleTypeAliasId, node: &ast::ItemTypeAlias) -> Self { - Self { - id, - node: node.stable_ptr(), - item_data: ItemData::new(db, ModuleItemId::TypeAlias(id), node), - } - } -} - -#[derive(Clone, Debug)] -pub struct ImplAlias { - pub id: ImplAliasId, - pub node: ast::ItemImplAliasPtr, - - pub item_data: ItemData, -} - -impl ImplAlias { - pub fn new(db: &dyn DefsGroup, id: ImplAliasId, node: &ast::ItemImplAlias) -> Self { - Self { - id, - node: node.stable_ptr(), - item_data: ItemData::new(db, ModuleItemId::ImplAlias(id), node), - } - } -} - -#[derive(Clone, Debug)] -pub struct Trait { - pub id: TraitId, - pub node: ast::ItemTraitPtr, - - pub item_data: ItemData, -} - -impl Trait { - pub fn new(db: &dyn DefsGroup, id: TraitId, node: &ast::ItemTrait) -> Self { - Self { - id, - node: node.stable_ptr(), - item_data: ItemData::new(db, ModuleItemId::Trait(id), node), - } - } -} - -#[derive(Clone, Debug)] -pub struct Impl { - pub id: ImplDefId, - pub node: ast::ItemImplPtr, - - pub item_data: ItemData, -} - -impl Impl { - pub fn new(db: &dyn DefsGroup, id: ImplDefId, node: &ast::ItemImpl) -> Self { - Self { - id, - node: node.stable_ptr(), - item_data: ItemData::new(db, ModuleItemId::Impl(id), node), - } - } -} - -#[derive(Clone, Debug)] -pub struct ExternType { - pub id: ExternTypeId, - pub node: ast::ItemExternTypePtr, - - pub item_data: ItemData, -} - -impl ExternType { - pub fn new(db: &dyn DefsGroup, id: ExternTypeId, node: &ast::ItemExternType) -> Self { - Self { - id, - node: node.stable_ptr(), - item_data: ItemData::new(db, ModuleItemId::ExternType(id), node), - } - } -} - -#[derive(Clone, Debug)] -pub struct ExternFunction { - pub id: ExternFunctionId, - pub node: ast::ItemExternFunctionPtr, - - pub item_data: ItemData, -} - -impl ExternFunction { - pub fn new(db: &dyn DefsGroup, id: ExternFunctionId, node: &ast::ItemExternFunction) -> Self { - Self { - id, - node: node.stable_ptr(), - item_data: ItemData::new(db, ModuleItemId::ExternFunction(id), node), - } - } -}