Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DocGroup in scarb-doc #1409

Merged
merged 3 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ updates:
- "deno_task_shell"
- "gix"
- "semver"
- "salsa"

- package-ecosystem: "github-actions"
directory: "/"
Expand Down
6 changes: 4 additions & 2 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"
piotmag769 marked this conversation as resolved.
Show resolved Hide resolved
semver = { version = "1", features = ["serde"] }
serde = { version = "1", features = ["serde_derive"] }
serde-untagged = "0.1"
Expand Down
10 changes: 6 additions & 4 deletions extensions/scarb-doc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@ repository.workspace = true

[dependencies]
anyhow.workspace = true
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
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" }
126 changes: 126 additions & 0 deletions extensions/scarb-doc/src/db.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
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::cfg::{Cfg, CfgSet};
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 {
piotmag769 marked this conversation as resolved.
Show resolved Hide resolved
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);

db.set_cfg_set(Self::initial_cfg_set().into());
let plugin_suite = [get_default_plugin_suite(), starknet_plugin_suite()]
maciektr marked this conversation as resolved.
Show resolved Hide resolved
.into_iter()
.fold(PluginSuite::default(), |mut acc, suite| {
acc.add(suite);
acc
});

db.apply_plugin_suite(plugin_suite);
piotmag769 marked this conversation as resolved.
Show resolved Hide resolved

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

db
}

fn initial_cfg_set() -> CfgSet {
CfgSet::from_iter([Cfg::name("doc")])
}

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
Loading