From 6c1bf6f2ba9e91e71254a5ff40b9665c52ce634b Mon Sep 17 00:00:00 2001 From: Piotr Figiela <77412592+Draggu@users.noreply.github.com> Date: Thu, 28 Nov 2024 15:34:10 +0100 Subject: [PATCH] Refactor plugins handling in database commit-id:c321e209 --- src/lang/db/mod.rs | 88 +++++++++++++++++++++--------- src/lang/proc_macros/controller.rs | 6 +- 2 files changed, 67 insertions(+), 27 deletions(-) diff --git a/src/lang/db/mod.rs b/src/lang/db/mod.rs index 2662f7a..c81b699 100644 --- a/src/lang/db/mod.rs +++ b/src/lang/db/mod.rs @@ -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::{ @@ -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::*; @@ -110,37 +112,40 @@ impl AnalysisDatabase { /// considered identical based on their [`Arc`] reference. pub fn replace_plugin_suite( &mut self, - plugins_to_remove: Option, + 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>, + &mut Vec>, + &mut OrderedHashMap>, + ), ) { 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); @@ -148,6 +153,37 @@ impl AnalysisDatabase { } } +fn remove_plugin_suite( + plugins: PluginSuite, + macro_plugins: &mut Vec>, + analyzer_plugins: &mut Vec>, + inline_macro_plugins: &mut OrderedHashMap>, +) { + 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>, + analyzer_plugins: &mut Vec>, + inline_macro_plugins: &mut OrderedHashMap>, +) { + 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 { diff --git a/src/lang/proc_macros/controller.rs b/src/lang/proc_macros/controller.rs index c542233..508b7bd 100644 --- a/src/lang/proc_macros/controller.rs +++ b/src/lang/proc_macros/controller.rs @@ -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)); }