Skip to content

Commit

Permalink
Refactor plugins handling in database
Browse files Browse the repository at this point in the history
commit-id:c321e209
  • Loading branch information
Draggu committed Dec 2, 2024
1 parent 3013a6d commit 6c1bf6f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 27 deletions.
88 changes: 62 additions & 26 deletions src/lang/db/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::Arc;

use cairo_lang_defs::db::{DefsDatabase, DefsGroup, try_ext_as_virtual_impl};
use cairo_lang_defs::plugin::{InlineMacroExprPlugin, MacroPlugin};
use cairo_lang_doc::db::DocDatabase;
use cairo_lang_filesystem::cfg::{Cfg, CfgSet};
use cairo_lang_filesystem::db::{
Expand All @@ -12,11 +13,12 @@ use cairo_lang_lowering::utils::InliningStrategy;
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_semantic::plugin::{AnalyzerPlugin, PluginSuite};
use cairo_lang_starknet::starknet_plugin_suite;
use cairo_lang_syntax::node::db::{SyntaxDatabase, SyntaxGroup};
use cairo_lang_test_plugin::test_plugin_suite;
use cairo_lang_utils::Upcast;
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use salsa::{Database, Durability};

pub use self::semantic::*;
Expand Down Expand Up @@ -110,44 +112,78 @@ impl AnalysisDatabase {
/// considered identical based on their [`Arc`] reference.
pub fn replace_plugin_suite(
&mut self,
plugins_to_remove: Option<PluginSuite>,
plugins_to_remove: PluginSuite,
plugins_to_add: PluginSuite,
) {
self.with_plugins_mut(move |macro_plugins, analyzer_plugins, inline_macro_plugins| {
remove_plugin_suite(
plugins_to_remove,
macro_plugins,
analyzer_plugins,
inline_macro_plugins,
);
add_plugin_suite(plugins_to_add, macro_plugins, analyzer_plugins, inline_macro_plugins);
})
}

/// Adds plugin suit to database.
pub fn add_plugin_suite(&mut self, plugins: PluginSuite) {
self.with_plugins_mut(move |macro_plugins, analyzer_plugins, inline_macro_plugins| {
add_plugin_suite(plugins, macro_plugins, analyzer_plugins, inline_macro_plugins)
})
}

fn with_plugins_mut(
&mut self,
action: impl FnOnce(
&mut Vec<Arc<dyn MacroPlugin>>,
&mut Vec<Arc<dyn AnalyzerPlugin>>,
&mut OrderedHashMap<String, Arc<dyn InlineMacroExprPlugin>>,
),
) {
let mut macro_plugins = self.macro_plugins();
let mut analyzer_plugins = self.analyzer_plugins();
let mut inline_macro_plugins = Arc::unwrap_or_clone(self.inline_macro_plugins());

if let Some(plugins_to_remove) = plugins_to_remove {
macro_plugins.retain(|plugin| {
plugins_to_remove
.plugins
.iter()
.all(|previous_plugin| !Arc::ptr_eq(previous_plugin, plugin))
});
analyzer_plugins.retain(|plugin| {
plugins_to_remove
.analyzer_plugins
.iter()
.all(|previous_plugin| !Arc::ptr_eq(previous_plugin, plugin))
});
inline_macro_plugins.retain(|_, plugin| {
plugins_to_remove
.inline_macro_plugins
.iter()
.all(|(_, previous_plugin)| !Arc::ptr_eq(previous_plugin, plugin))
});
}

macro_plugins.extend(plugins_to_add.plugins);
analyzer_plugins.extend(plugins_to_add.analyzer_plugins);
inline_macro_plugins.extend(plugins_to_add.inline_macro_plugins);
action(&mut macro_plugins, &mut analyzer_plugins, &mut inline_macro_plugins);

self.set_macro_plugins(macro_plugins);
self.set_analyzer_plugins(analyzer_plugins);
self.set_inline_macro_plugins(Arc::new(inline_macro_plugins));
}
}

fn remove_plugin_suite(
plugins: PluginSuite,
macro_plugins: &mut Vec<Arc<dyn MacroPlugin>>,
analyzer_plugins: &mut Vec<Arc<dyn AnalyzerPlugin>>,
inline_macro_plugins: &mut OrderedHashMap<String, Arc<dyn InlineMacroExprPlugin>>,
) {
macro_plugins.retain(|plugin| {
plugins.plugins.iter().all(|previous_plugin| !Arc::ptr_eq(previous_plugin, plugin))
});
analyzer_plugins.retain(|plugin| {
plugins.analyzer_plugins.iter().all(|previous_plugin| !Arc::ptr_eq(previous_plugin, plugin))
});
inline_macro_plugins.retain(|_, plugin| {
plugins
.inline_macro_plugins
.iter()
.all(|(_, previous_plugin)| !Arc::ptr_eq(previous_plugin, plugin))
});
}

fn add_plugin_suite(
plugins: PluginSuite,
macro_plugins: &mut Vec<Arc<dyn MacroPlugin>>,
analyzer_plugins: &mut Vec<Arc<dyn AnalyzerPlugin>>,
inline_macro_plugins: &mut OrderedHashMap<String, Arc<dyn InlineMacroExprPlugin>>,
) {
macro_plugins.extend(plugins.plugins);
analyzer_plugins.extend(plugins.analyzer_plugins);
inline_macro_plugins.extend(plugins.inline_macro_plugins);
}

impl salsa::Database for AnalysisDatabase {}
impl ExternalFiles for AnalysisDatabase {
fn try_ext_as_virtual(&self, external_id: salsa::InternId) -> Option<VirtualFile> {
Expand Down
6 changes: 5 additions & 1 deletion src/lang/proc_macros/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ impl ProcMacroClientController {
// restart proc-macro-server.
let previous_plugin_suite = self.plugin_suite.replace(new_plugin_suite.clone());

db.replace_plugin_suite(previous_plugin_suite, new_plugin_suite);
if let Some(previous_plugin_suite) = previous_plugin_suite {
db.replace_plugin_suite(previous_plugin_suite, new_plugin_suite);
} else {
db.add_plugin_suite(new_plugin_suite);
}

db.set_proc_macro_client_status(ClientStatus::Ready(client));
}
Expand Down

0 comments on commit 6c1bf6f

Please sign in to comment.