diff --git a/scarb/src/ops/proc_macro_server/methods/expand_inline.rs b/scarb/src/ops/proc_macro_server/methods/expand_inline.rs new file mode 100644 index 000000000..90e0cc3e3 --- /dev/null +++ b/scarb/src/ops/proc_macro_server/methods/expand_inline.rs @@ -0,0 +1,30 @@ +use crate::{ + compiler::plugin::proc_macro::{ExpansionKind, ProcMacroHost}, + ops::proc_macro_server::json_rpc::Handler, +}; +use anyhow::Result; +use cairo_lang_macro::TokenStream; +use scarb_proc_macro_server_types::methods::{expand::ExpandInline, ProcMacroResult}; +use std::sync::Arc; + +impl Handler for ExpandInline { + fn handle(proc_macros: Arc, params: Self::Params) -> Result { + let instance = proc_macros + .macros() + .into_iter() + .find(|e| { + e.get_expansions() + .iter() + .filter(|expansion| expansion.kind == ExpansionKind::Inline) + .any(|expansion| expansion.name == params.name) + }) + .unwrap(); + + let result = instance.generate_code(params.name.into(), TokenStream::empty(), params.args); + + Ok(ProcMacroResult { + token_stream: result.token_stream, + diagnostics: result.diagnostics, + }) + } +} diff --git a/scarb/src/ops/proc_macro_server/methods/mod.rs b/scarb/src/ops/proc_macro_server/methods/mod.rs index 1a3926524..1f322d5f8 100644 --- a/scarb/src/ops/proc_macro_server/methods/mod.rs +++ b/scarb/src/ops/proc_macro_server/methods/mod.rs @@ -1,3 +1,4 @@ pub mod defined_macros; pub mod expand_attribute; pub mod expand_derive; +pub mod expand_inline; diff --git a/scarb/src/ops/proc_macro_server/mod.rs b/scarb/src/ops/proc_macro_server/mod.rs index dc8ee6603..9510045da 100644 --- a/scarb/src/ops/proc_macro_server/mod.rs +++ b/scarb/src/ops/proc_macro_server/mod.rs @@ -8,7 +8,7 @@ use crossbeam_channel::{Receiver, Sender}; use json_rpc::Handler; use scarb_proc_macro_server_types::jsonrpc::{ResponseError, RpcRequest, RpcResponse}; use scarb_proc_macro_server_types::methods::defined_macros::DefinedMacros; -use scarb_proc_macro_server_types::methods::expand::{ExpandAttribute, ExpandDerive}; +use scarb_proc_macro_server_types::methods::expand::{ExpandAttribute, ExpandDerive, ExpandInline}; use scarb_proc_macro_server_types::methods::Method; use serde_json::Value; @@ -76,6 +76,7 @@ fn route_request(proc_macros: Arc, request: RpcRequest) -> Result run_handler::(proc_macros.clone(), request.value) } ExpandDerive::METHOD => run_handler::(proc_macros.clone(), request.value), + ExpandInline::METHOD => run_handler::(proc_macros.clone(), request.value), _ => Err(anyhow!("method not found")), } }