Skip to content

Commit

Permalink
Handle plugins per each crate
Browse files Browse the repository at this point in the history
commit-id:22479572
  • Loading branch information
integraledelebesgue committed Jan 23, 2025
1 parent 67150c9 commit 1ee8c6f
Show file tree
Hide file tree
Showing 10 changed files with 381 additions and 190 deletions.
112 changes: 74 additions & 38 deletions Cargo.lock

Large diffs are not rendered by default.

63 changes: 31 additions & 32 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ lsp-server = "0.7.8"
lsp-types = "=0.95.0"
memchr = "2.7.4"
salsa = { package = "rust-analyzer-salsa", version = "0.17.0-pre.6" }
scarb-metadata = { git = "https://github.com/software-mansion/scarb", branch = "spr/main/ac416d0e" } # TODO: Change back to crates-io as soon as changes from that branch are released there
scarb-proc-macro-server-types = "0.1"
scarb-metadata = { git = "https://github.com/software-mansion/scarb", branch = "spr/main/ac416d0e" } # TODO: Change back to crates-io as soon as changes from that branch are released there
scarb-proc-macro-server-types = { git = "https://github.com/software-mansion/scarb", branch = "spr/main/2adeb9c2" } # TODO: Change back to crates-io as soon as changes from that branch are released there
scarb-stable-hash = "1"
serde = { version = "1", default-features = false, features = ["derive"] }
serde_json = "1.0.135"
Expand Down Expand Up @@ -90,36 +90,35 @@ test-log = "0.2.16"
# on some of them directly.
# This ensures no duplicate instances of Cairo crates are pulled in by mistake.
[patch.crates-io]
cairo-lang-casm = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-compiler = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-debug = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-defs = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-diagnostics = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-doc = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-eq-solver = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-executable = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-filesystem = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-formatter = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-lowering = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-parser = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-plugins = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-proc-macros = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-project = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-runnable-utils = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-semantic = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-sierra = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-sierra-ap-change = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-sierra-gas = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-sierra-generator = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-sierra-to-casm = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-sierra-type-size = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-starknet = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-starknet-classes = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-syntax = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-syntax-codegen = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-test-plugin = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-test-utils = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-utils = { git = "https://github.com/starkware-libs/cairo", rev = "cdd317675179bf6849a526479dc91e41608d684b" }
cairo-lang-casm = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-compiler = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-debug = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-defs = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-diagnostics = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-doc = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-eq-solver = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-executable = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-filesystem = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-formatter = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-lowering = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-parser = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-plugins = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-proc-macros = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-project = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-semantic = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-sierra = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-sierra-ap-change = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-sierra-gas = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-sierra-generator = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-sierra-to-casm = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-sierra-type-size = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-starknet = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-starknet-classes = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-syntax = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-syntax-codegen = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-test-plugin = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-test-utils = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-utils = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }

# The profile used for CI in pull requests.
# External dependencies are built with optimisation enabled,
Expand Down
15 changes: 13 additions & 2 deletions src/ide/completion/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use cairo_lang_defs::ids::{
LanguageElementId, LookupItemId, ModuleFileId, ModuleId, NamedLanguageElementId,
TopLevelLanguageElementId, TraitFunctionId,
};
use cairo_lang_defs::plugin::MacroPlugin;
use cairo_lang_filesystem::db::FilesGroup;
use cairo_lang_filesystem::ids::FileId;
use cairo_lang_filesystem::span::TextOffset;
Expand Down Expand Up @@ -47,7 +48,12 @@ pub fn attribute_completions(

let attribute = Attribute::from_syntax_node(db, node.parent().unwrap());

let plugins = db.macro_plugins();
// TODO: Will add per-crate logic later in this commit.
let plugins = db
.default_macro_plugins()
.iter()
.map(|&id| db.lookup_intern_macro_plugin(id))
.collect::<Vec<_>>();

let attr = attribute.attr(db);
let attr_node = attr.as_syntax_node();
Expand Down Expand Up @@ -161,7 +167,12 @@ pub fn generic_completions(
return completions;
};

let inline_plugins = db.inline_macro_plugins();
// TODO: Will add per-crate logic later in this commit.
let inline_plugins = db
.default_inline_macro_plugins()
.iter()
.map(|(name, &id)| (name.clone(), db.lookup_intern_inline_macro_plugin(id)))
.collect::<Vec<_>>();

let inline_macros = inline_plugins.iter().map(|plugin| CompletionItem {
label: format!("{}!", plugin.0),
Expand Down
11 changes: 10 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,16 @@ 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.as_str())?.documentation() {

if let Some(doc) = db
.crate_inline_macro_plugins(crate_id)
.get(macro_name.as_str())
.map(|&id| db.lookup_intern_inline_macro_plugin(id))?
.documentation()
{
md += RULE;
md += &doc;
}
Expand Down
22 changes: 15 additions & 7 deletions src/ide/macros/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ 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};
use cairo_lang_filesystem::ids::{CrateId, FileId, FileKind, FileLongId, VirtualFile};
use cairo_lang_filesystem::span::{TextSpan, TextWidth};
use cairo_lang_formatter::FormatterConfig;
use cairo_lang_parser::db::ParserGroup;
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 @@ -72,7 +72,7 @@ pub fn expand_macro(db: &AnalysisDatabase, params: &TextDocumentPositionParams)
};

// Expand inline macros in generated files.
expand_inline_macros(db, node_to_expand, files, &metadata, top_level_macro_kind)
expand_inline_macros(db, crate_id, node_to_expand, files, &metadata, top_level_macro_kind)
}

#[derive(Copy, Clone)]
Expand All @@ -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 @@ -141,6 +144,7 @@ fn node_depth(node: SyntaxNode) -> usize {
/// Expands inline macros for each file.
fn expand_inline_macros(
db: &AnalysisDatabase,
crate_id: CrateId,
node_to_expand: SyntaxNode,
mut files: VecDeque<FileId>,
metadata: &MacroPluginMetadata<'_>,
Expand All @@ -150,6 +154,7 @@ fn expand_inline_macros(

expand_inline_macros_in_single_file(
db,
crate_id,
metadata,
files.pop_front().unwrap(),
&mut files,
Expand All @@ -160,6 +165,7 @@ fn expand_inline_macros(
while let Some(file) = files.pop_front() {
expand_inline_macros_in_single_file(
db,
crate_id,
metadata,
file,
&mut files,
Expand Down Expand Up @@ -245,13 +251,14 @@ impl FileProcessorConfig {
/// pushes output string.
fn expand_inline_macros_in_single_file(
db: &AnalysisDatabase,
crate_id: CrateId,
metadata: &MacroPluginMetadata<'_>,
file: FileId,
files: &mut VecDeque<FileId>,
output: &mut String,
mut config: FileProcessorConfig,
) -> Option<()> {
let plugins = db.inline_macro_plugins();
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 +267,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
Loading

0 comments on commit 1ee8c6f

Please sign in to comment.