From 06ae1c907a88bcfaa7d71e78fae897da6e3a04cd Mon Sep 17 00:00:00 2001 From: Piotr Figiela <77412592+Draggu@users.noreply.github.com> Date: Wed, 23 Oct 2024 18:43:06 +0200 Subject: [PATCH] Add `ExpandInline` handler commit-id:e383d343 --- .../methods/expand_inline.rs | 30 +++++++++++++++++++ .../src/ops/proc_macro_server/methods/mod.rs | 1 + scarb/src/ops/proc_macro_server/mod.rs | 3 +- 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 scarb/src/ops/proc_macro_server/methods/expand_inline.rs 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..87a6622fe --- /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.item); + + 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")), } }