Skip to content

Commit

Permalink
Migrated the LS to per-crate plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
integraledelebesgue committed Dec 17, 2024
1 parent 2304720 commit e436110
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
10 changes: 9 additions & 1 deletion src/ide/hover/render/definition.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cairo_lang_defs::db::DefsGroup;
use cairo_lang_defs::plugin::InlineMacroExprPlugin;
use cairo_lang_doc::db::DocGroup;
use cairo_lang_filesystem::ids::FileId;
use cairo_lang_syntax::node::TypedSyntaxNode;
Expand Down Expand Up @@ -45,8 +46,15 @@ pub fn definition(

SymbolDef::Variable(var) => fenced_code_block(&var.signature(db)),
SymbolDef::ExprInlineMacro(macro_name) => {
let crate_id = db.file_modules(file_id).ok()?.first()?.owning_crate(db);

let mut md = fenced_code_block(macro_name);
if let Some(doc) = db.inline_macro_plugins().get(macro_name)?.documentation() {
if let Some(doc) = db
.crate_inline_macro_plugins(crate_id)
.get(macro_name)
.map(|&id| db.lookup_intern_inline_macro_plugin(id))?
.documentation()
{
md += RULE;
md += &doc;
}
Expand Down
16 changes: 11 additions & 5 deletions src/ide/macros/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::VecDeque;
use std::sync::Arc;

use cairo_lang_defs::db::DefsGroup;
use cairo_lang_defs::plugin::MacroPluginMetadata;
use cairo_lang_defs::plugin::{InlineMacroExprPlugin, MacroPlugin, MacroPluginMetadata};
use cairo_lang_diagnostics::DiagnosticsBuilder;
use cairo_lang_filesystem::db::FilesGroup;
use cairo_lang_filesystem::ids::{FileId, FileKind, FileLongId, VirtualFile};
Expand Down Expand Up @@ -39,7 +39,7 @@ pub fn expand_macro(db: &AnalysisDatabase, params: &TextDocumentPositionParams)

let metadata = MacroPluginMetadata {
cfg_set: &cfg_set,
declared_derives: &db.declared_derives(),
declared_derives: &db.declared_derives(crate_id),
allowed_features: &Default::default(),
edition,
};
Expand Down Expand Up @@ -98,11 +98,14 @@ fn expanded_macro_files(
let mut module_queue = VecDeque::from([(module_file, vec![item])]);
let mut files = VecDeque::new();

let crate_id = db.file_modules(module_file).ok()?.first()?.owning_crate(db);

while let Some((module_file, item_asts)) = module_queue.pop_front() {
files.push_back(module_file);

for item_ast in item_asts {
for plugin in db.macro_plugins() {
for &plugin_id in db.crate_macro_plugins(crate_id).iter() {
let plugin = db.lookup_intern_macro_plugin(plugin_id);
let result = plugin.generate_code(db.upcast(), item_ast.clone(), metadata);

if let Some(generated) = result.code {
Expand Down Expand Up @@ -251,7 +254,9 @@ fn expand_inline_macros_in_single_file(
output: &mut String,
mut config: FileProcessorConfig,
) -> Option<()> {
let plugins = db.inline_macro_plugins();
let crate_id = db.file_modules(file).ok()?.first()?.owning_crate(db);

let plugins = db.crate_inline_macro_plugins(crate_id);

if config.macros.is_empty() {
append_file_with_header(db, file, &config.content, output);
Expand All @@ -260,7 +265,8 @@ fn expand_inline_macros_in_single_file(
for node in config.macros.into_iter().rev() {
let inline_macro = ExprInlineMacro::from_syntax_node(db, node.clone());
let code = plugins
.get(&inline_macro.path(db).as_syntax_node().get_text_without_trivia(db))?
.get(&inline_macro.path(db).as_syntax_node().get_text_without_trivia(db))
.map(|&id| db.lookup_intern_inline_macro_plugin(id))?
.generate_code(db, &inline_macro, metadata)
.code?
.content;
Expand Down
19 changes: 9 additions & 10 deletions src/lang/db/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use cairo_lang_defs::db::{DefsDatabase, DefsGroup, try_ext_as_virtual_impl};
use cairo_lang_defs::db::{DefsDatabase, DefsGroup, init_defs_group, 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};
Expand All @@ -11,7 +11,9 @@ use cairo_lang_filesystem::ids::VirtualFile;
use cairo_lang_lowering::db::{LoweringDatabase, LoweringGroup, init_lowering_group};
use cairo_lang_lowering::utils::InliningStrategy;
use cairo_lang_parser::db::{ParserDatabase, ParserGroup};
use cairo_lang_semantic::db::{SemanticDatabase, SemanticGroup};
use cairo_lang_semantic::db::{
PluginSuiteInput, SemanticDatabase, SemanticGroup, init_semantic_group,
};
use cairo_lang_semantic::inline_macros::get_default_plugin_suite;
use cairo_lang_semantic::plugin::{AnalyzerPlugin, PluginSuite};
use cairo_lang_starknet::starknet_plugin_suite;
Expand Down Expand Up @@ -52,6 +54,8 @@ impl AnalysisDatabase {
let mut db = Self { storage: Default::default() };

init_files_group(&mut db);
init_defs_group(&mut db);
init_semantic_group(&mut db);
init_lowering_group(&mut db, InliningStrategy::Default);
// proc-macro-server can be restarted many times but we want to keep these data across
// multiple server starts, so init it once per database, not per server.
Expand All @@ -73,7 +77,9 @@ impl AnalysisDatabase {
acc.add(suite);
acc
});
db.apply_plugin_suite(plugin_suite);

let plugin_suite = db.intern_plugin_suite(plugin_suite);
db.set_default_plugins_from_suite(plugin_suite);

db
}
Expand All @@ -98,13 +104,6 @@ impl AnalysisDatabase {
self.salsa_runtime_mut().synthetic_write(Durability::LOW);
}

/// Shortcut for settings compiler plugins from a [`PluginSuite`].
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);
}

/// Updates the plugin list in the database.
///
/// This function modifies the database by removing plugins specified in `plugins_to_remove`,
Expand Down
10 changes: 7 additions & 3 deletions src/lang/db/swapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,13 @@ impl AnalysisDatabaseSwapper {

/// Copies current proc macro state into new db.
fn migrate_proc_macro_state(&self, new_db: &mut AnalysisDatabase, old_db: &AnalysisDatabase) {
new_db.set_macro_plugins(old_db.macro_plugins());
new_db.set_inline_macro_plugins(old_db.inline_macro_plugins());
new_db.set_analyzer_plugins(old_db.analyzer_plugins());
new_db.set_default_macro_plugins(old_db.default_macro_plugins());
new_db.set_default_inline_macro_plugins(old_db.default_inline_macro_plugins());
new_db.set_default_analyzer_plugins(old_db.default_analyzer_plugins());

new_db.set_macro_plugin_overrides(old_db.macro_plugin_overrides());
new_db.set_inline_macro_plugin_overrides(old_db.inline_macro_plugin_overrides());
new_db.set_analyzer_plugin_overrides(old_db.analyzer_plugin_overrides());

new_db.set_proc_macro_client_status(old_db.proc_macro_client_status());

Expand Down

0 comments on commit e436110

Please sign in to comment.