Skip to content

Commit

Permalink
Add ExpandInline handler
Browse files Browse the repository at this point in the history
commit-id:e383d343
  • Loading branch information
Draggu committed Nov 5, 2024
1 parent b01f475 commit 9b62a3f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
30 changes: 30 additions & 0 deletions scarb/src/ops/proc_macro_server/methods/expand_inline.rs
Original file line number Diff line number Diff line change
@@ -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<ProcMacroHost>, params: Self::Params) -> Result<Self::Response> {
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,
})
}
}
1 change: 1 addition & 0 deletions scarb/src/ops/proc_macro_server/methods/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod defined_macros;
pub mod expand_attribute;
pub mod expand_derive;
pub mod expand_inline;
3 changes: 2 additions & 1 deletion scarb/src/ops/proc_macro_server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -76,6 +76,7 @@ fn route_request(proc_macros: Arc<ProcMacroHost>, request: RpcRequest) -> Result
run_handler::<ExpandAttribute>(proc_macros.clone(), request.value)
}
ExpandDerive::METHOD => run_handler::<ExpandDerive>(proc_macros.clone(), request.value),
ExpandInline::METHOD => run_handler::<ExpandInline>(proc_macros.clone(), request.value),
_ => Err(anyhow!("method not found")),
}
}
Expand Down

0 comments on commit 9b62a3f

Please sign in to comment.