Skip to content

Commit

Permalink
DocGroup in scarb-doc
Browse files Browse the repository at this point in the history
  • Loading branch information
piotmag769 committed Jul 3, 2024
1 parent 4b75f43 commit 40926ae
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 53 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ cairo-lang-compiler = { git = "https://github.com/starkware-libs/cairo", rev = "
cairo-lang-debug = { git = "https://github.com/starkware-libs/cairo", rev = "43cf361d9b6b26ec8cd5ee076dd15341bde7577c" }
cairo-lang-defs = { git = "https://github.com/starkware-libs/cairo", rev = "43cf361d9b6b26ec8cd5ee076dd15341bde7577c" }
cairo-lang-diagnostics = { git = "https://github.com/starkware-libs/cairo", rev = "43cf361d9b6b26ec8cd5ee076dd15341bde7577c" }
cairo-lang-doc = { git = "https://github.com/starkware-libs/cairo", rev = "43cf361d9b6b26ec8cd5ee076dd15341bde7577c" }
cairo-lang-filesystem = { git = "https://github.com/starkware-libs/cairo", rev = "43cf361d9b6b26ec8cd5ee076dd15341bde7577c" }
cairo-lang-formatter = { git = "https://github.com/starkware-libs/cairo", rev = "43cf361d9b6b26ec8cd5ee076dd15341bde7577c" }
cairo-lang-language-server = { git = "https://github.com/starkware-libs/cairo", rev = "43cf361d9b6b26ec8cd5ee076dd15341bde7577c" }
Expand Down Expand Up @@ -99,6 +100,7 @@ ra_ap_toolchain = "0.0.218"
rayon = "1.10"
redb = "2.1.1"
reqwest = { version = "0.11", features = ["gzip", "brotli", "deflate", "json", "stream"], default-features = false }
salsa = "0.16.1"
semver = { version = "1", features = ["serde"] }
serde = { version = "1", features = ["serde_derive"] }
serde-untagged = "0.1"
Expand Down
10 changes: 7 additions & 3 deletions extensions/scarb-doc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,21 @@ camino.workspace = true
clap.workspace = true
cairo-lang-compiler.workspace = true
cairo-lang-defs.workspace = true
cairo-lang-doc.workspace = true
cairo-lang-filesystem.workspace = true
cairo-lang-parser.workspace = true
cairo-lang-semantic.workspace = true
cairo-lang-starknet.workspace = true
cairo-lang-syntax.workspace = true
cairo-lang-filesystem.workspace = true
cairo-lang-utils.workspace = true
itertools.workspace = true
scarb-metadata = { path = "../../scarb-metadata" }
scarb-ui = { path = "../../utils/scarb-ui" }
serde_json.workspace = true
itertools.workspace = true
salsa.workspace = true
smol_str.workspace = true

[dev-dependencies]
scarb-test-support = { path = "../../utils/scarb-test-support" }
assert_fs.workspace = true
indoc.workspace = true
scarb-test-support = { path = "../../utils/scarb-test-support" }
120 changes: 120 additions & 0 deletions extensions/scarb-doc/src/db.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
use cairo_lang_compiler::project::{
update_crate_root, update_crate_roots_from_project_config, ProjectConfig,
};
use cairo_lang_defs::db::{DefsDatabase, DefsGroup};
use cairo_lang_doc::db::{DocDatabase, DocGroup};
use cairo_lang_filesystem::db::{
init_files_group, AsFilesGroupMut, FilesDatabase, FilesGroup, CORELIB_CRATE_NAME,
};
use cairo_lang_parser::db::{ParserDatabase, ParserGroup};
use cairo_lang_semantic::db::{SemanticDatabase, SemanticGroup};
use cairo_lang_semantic::inline_macros::get_default_plugin_suite;
use cairo_lang_semantic::plugin::PluginSuite;
use cairo_lang_starknet::starknet_plugin_suite;
use cairo_lang_syntax::node::db::{SyntaxDatabase, SyntaxGroup};
use cairo_lang_utils::Upcast;

use salsa;

/// The Cairo compiler Salsa database tailored for scarb-doc usage.
#[salsa::database(
FilesDatabase,
ParserDatabase,
SyntaxDatabase,
DefsDatabase,
SemanticDatabase,
DocDatabase
)]
pub struct ScarbDocDatabase {
storage: salsa::Storage<Self>,
}

impl ScarbDocDatabase {
pub fn new(project_config: Option<ProjectConfig>) -> Self {
let mut db = Self {
storage: Default::default(),
};

init_files_group(&mut db);

let plugin_suite = [get_default_plugin_suite(), starknet_plugin_suite()]
.into_iter()
.fold(PluginSuite::default(), |mut acc, suite| {
acc.add(suite);
acc
});

db.apply_plugin_suite(plugin_suite);

if let Some(config) = project_config {
db.apply_project_config(config);
}

db
}

fn apply_plugin_suite(&mut self, plugin_suite: PluginSuite) {
self.set_macro_plugins(plugin_suite.plugins);
self.set_inline_macro_plugins(plugin_suite.inline_macro_plugins.into());
self.set_analyzer_plugins(plugin_suite.analyzer_plugins);
}

fn apply_project_config(&mut self, config: ProjectConfig) {
update_crate_roots_from_project_config(self, &config);
if let Some(corelib) = &config.corelib {
update_crate_root(self, &config, CORELIB_CRATE_NAME.into(), corelib.clone());
}
}
}

impl salsa::Database for ScarbDocDatabase {}

impl salsa::ParallelDatabase for ScarbDocDatabase {
fn snapshot(&self) -> salsa::Snapshot<Self> {
salsa::Snapshot::new(ScarbDocDatabase {
storage: self.storage.snapshot(),
})
}
}

impl AsFilesGroupMut for ScarbDocDatabase {
fn as_files_group_mut(&mut self) -> &mut (dyn FilesGroup + 'static) {
self
}
}

impl Upcast<dyn FilesGroup> for ScarbDocDatabase {
fn upcast(&self) -> &(dyn FilesGroup + 'static) {
self
}
}

impl Upcast<dyn ParserGroup> for ScarbDocDatabase {
fn upcast(&self) -> &(dyn ParserGroup + 'static) {
self
}
}

impl Upcast<dyn SyntaxGroup> for ScarbDocDatabase {
fn upcast(&self) -> &(dyn SyntaxGroup + 'static) {
self
}
}

impl Upcast<dyn DefsGroup> for ScarbDocDatabase {
fn upcast(&self) -> &(dyn DefsGroup + 'static) {
self
}
}

impl Upcast<dyn SemanticGroup> for ScarbDocDatabase {
fn upcast(&self) -> &(dyn SemanticGroup + 'static) {
self
}
}

impl Upcast<dyn DocGroup> for ScarbDocDatabase {
fn upcast(&self) -> &(dyn DocGroup + 'static) {
self
}
}
3 changes: 3 additions & 0 deletions extensions/scarb-doc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod compilation;
pub mod db;
pub mod types;
18 changes: 6 additions & 12 deletions extensions/scarb-doc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ 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;
use scarb_doc::compilation::get_project_config;
use scarb_doc::db::ScarbDocDatabase;
use scarb_doc::types;
use scarb_doc::types::Crate;

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
Expand All @@ -39,14 +37,10 @@ fn main() -> Result<()> {

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 db = ScarbDocDatabase::new(Some(project_config));

let main_crate_id = db.intern_crate(CrateLongId::Real(package_metadata.name.clone().into()));
let crate_ = Crate::new(db, main_crate_id);
let crate_ = Crate::new(&db, main_crate_id);

print_module(&crate_.root_module);

Expand Down
Loading

0 comments on commit 40926ae

Please sign in to comment.