|
| 1 | +//! The main loop of the proc-macro server. |
| 2 | +use std::io; |
| 3 | + |
| 4 | +use proc_macro_api::{ |
| 5 | + json::{read_json, write_json}, |
| 6 | + msg::{ |
| 7 | + self, deserialize_span_data_index_map, serialize_span_data_index_map, ExpandMacroData, |
| 8 | + ExpnGlobals, Message, SpanMode, TokenId, CURRENT_API_VERSION, |
| 9 | + }, |
| 10 | +}; |
| 11 | +use proc_macro_srv::EnvSnapshot; |
| 12 | + |
| 13 | +pub(crate) fn run() -> io::Result<()> { |
| 14 | + fn macro_kind_to_api(kind: proc_macro_srv::ProcMacroKind) -> proc_macro_api::ProcMacroKind { |
| 15 | + match kind { |
| 16 | + proc_macro_srv::ProcMacroKind::CustomDerive => { |
| 17 | + proc_macro_api::ProcMacroKind::CustomDerive |
| 18 | + } |
| 19 | + proc_macro_srv::ProcMacroKind::Bang => proc_macro_api::ProcMacroKind::Bang, |
| 20 | + proc_macro_srv::ProcMacroKind::Attr => proc_macro_api::ProcMacroKind::Attr, |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + let read_request = |
| 25 | + |buf: &mut String| msg::Request::read(read_json, &mut io::stdin().lock(), buf); |
| 26 | + |
| 27 | + let write_response = |msg: msg::Response| msg.write(write_json, &mut io::stdout().lock()); |
| 28 | + |
| 29 | + let env = EnvSnapshot::default(); |
| 30 | + let mut srv = proc_macro_srv::ProcMacroSrv::new(&env); |
| 31 | + let mut buf = String::new(); |
| 32 | + |
| 33 | + let mut span_mode = SpanMode::Id; |
| 34 | + |
| 35 | + while let Some(req) = read_request(&mut buf)? { |
| 36 | + let res = match req { |
| 37 | + msg::Request::ListMacros { dylib_path } => { |
| 38 | + msg::Response::ListMacros(srv.list_macros(&dylib_path).map(|macros| { |
| 39 | + macros.into_iter().map(|(name, kind)| (name, macro_kind_to_api(kind))).collect() |
| 40 | + })) |
| 41 | + } |
| 42 | + msg::Request::ExpandMacro(task) => { |
| 43 | + let msg::ExpandMacro { |
| 44 | + lib, |
| 45 | + env, |
| 46 | + current_dir, |
| 47 | + data: |
| 48 | + ExpandMacroData { |
| 49 | + macro_body, |
| 50 | + macro_name, |
| 51 | + attributes, |
| 52 | + has_global_spans: |
| 53 | + ExpnGlobals { serialize: _, def_site, call_site, mixed_site }, |
| 54 | + span_data_table, |
| 55 | + }, |
| 56 | + } = *task; |
| 57 | + match span_mode { |
| 58 | + SpanMode::Id => msg::Response::ExpandMacro({ |
| 59 | + let def_site = TokenId(def_site as u32); |
| 60 | + let call_site = TokenId(call_site as u32); |
| 61 | + let mixed_site = TokenId(mixed_site as u32); |
| 62 | + |
| 63 | + let macro_body = macro_body.to_subtree_unresolved(CURRENT_API_VERSION); |
| 64 | + let attributes = |
| 65 | + attributes.map(|it| it.to_subtree_unresolved(CURRENT_API_VERSION)); |
| 66 | + |
| 67 | + srv.expand( |
| 68 | + lib, |
| 69 | + env, |
| 70 | + current_dir, |
| 71 | + macro_name, |
| 72 | + macro_body, |
| 73 | + attributes, |
| 74 | + def_site, |
| 75 | + call_site, |
| 76 | + mixed_site, |
| 77 | + ) |
| 78 | + .map(|it| msg::FlatTree::new_raw(&it, CURRENT_API_VERSION)) |
| 79 | + .map_err(msg::PanicMessage) |
| 80 | + }), |
| 81 | + SpanMode::RustAnalyzer => msg::Response::ExpandMacroExtended({ |
| 82 | + let mut span_data_table = deserialize_span_data_index_map(&span_data_table); |
| 83 | + |
| 84 | + let def_site = span_data_table[def_site]; |
| 85 | + let call_site = span_data_table[call_site]; |
| 86 | + let mixed_site = span_data_table[mixed_site]; |
| 87 | + |
| 88 | + let macro_body = |
| 89 | + macro_body.to_subtree_resolved(CURRENT_API_VERSION, &span_data_table); |
| 90 | + let attributes = attributes.map(|it| { |
| 91 | + it.to_subtree_resolved(CURRENT_API_VERSION, &span_data_table) |
| 92 | + }); |
| 93 | + srv.expand( |
| 94 | + lib, |
| 95 | + env, |
| 96 | + current_dir, |
| 97 | + macro_name, |
| 98 | + macro_body, |
| 99 | + attributes, |
| 100 | + def_site, |
| 101 | + call_site, |
| 102 | + mixed_site, |
| 103 | + ) |
| 104 | + .map(|it| { |
| 105 | + ( |
| 106 | + msg::FlatTree::new(&it, CURRENT_API_VERSION, &mut span_data_table), |
| 107 | + serialize_span_data_index_map(&span_data_table), |
| 108 | + ) |
| 109 | + }) |
| 110 | + .map(|(tree, span_data_table)| msg::ExpandMacroExtended { |
| 111 | + tree, |
| 112 | + span_data_table, |
| 113 | + }) |
| 114 | + .map_err(msg::PanicMessage) |
| 115 | + }), |
| 116 | + } |
| 117 | + } |
| 118 | + msg::Request::ApiVersionCheck {} => msg::Response::ApiVersionCheck(CURRENT_API_VERSION), |
| 119 | + msg::Request::SetConfig(config) => { |
| 120 | + span_mode = config.span_mode; |
| 121 | + msg::Response::SetConfig(config) |
| 122 | + } |
| 123 | + }; |
| 124 | + write_response(res)? |
| 125 | + } |
| 126 | + |
| 127 | + Ok(()) |
| 128 | +} |
0 commit comments