@@ -5,9 +5,8 @@ use std::{
55 sync:: Arc ,
66} ;
77
8- use base_db:: SourceDatabase ;
98use paths:: AbsPath ;
10- use span:: { FileId , Span } ;
9+ use span:: Span ;
1110
1211use crate :: {
1312 Codec , ProcMacro , ProcMacroKind , ServerError ,
@@ -29,16 +28,14 @@ use crate::{
2928
3029pub mod msg;
3130
32- pub trait ClientCallbacks {
33- fn handle_sub_request ( & mut self , req : SubRequest ) -> Result < SubResponse , ServerError > ;
34- }
31+ pub type SubCallback < ' a > = & ' a mut dyn FnMut ( SubRequest ) -> Result < SubResponse , ServerError > ;
3532
3633pub fn run_conversation < C : Codec > (
3734 writer : & mut dyn Write ,
3835 reader : & mut dyn BufRead ,
3936 buf : & mut C :: Buf ,
4037 msg : BidirectionalMessage ,
41- callbacks : & mut dyn ClientCallbacks ,
38+ callback : SubCallback < ' _ > ,
4239) -> Result < BidirectionalMessage , ServerError > {
4340 let encoded = C :: encode ( & msg) . map_err ( wrap_encode) ?;
4441 C :: write ( writer, & encoded) . map_err ( wrap_io ( "failed to write initial request" ) ) ?;
@@ -59,7 +56,7 @@ pub fn run_conversation<C: Codec>(
5956 return Ok ( BidirectionalMessage :: Response ( response) ) ;
6057 }
6158 BidirectionalMessage :: SubRequest ( sr) => {
62- let resp = callbacks . handle_sub_request ( sr) ?;
59+ let resp = callback ( sr) ?;
6360 let reply = BidirectionalMessage :: SubResponse ( resp) ;
6461 let encoded = C :: encode ( & reply) . map_err ( wrap_encode) ?;
6562 C :: write ( writer, & encoded) . map_err ( wrap_io ( "failed to write sub-response" ) ) ?;
@@ -86,19 +83,13 @@ fn wrap_decode(err: io::Error) -> ServerError {
8683 ServerError { message : "failed to decode message" . into ( ) , io : Some ( Arc :: new ( err) ) }
8784}
8885
89- pub ( crate ) fn version_check ( srv : & ProcMacroServerProcess ) -> Result < u32 , ServerError > {
86+ pub ( crate ) fn version_check (
87+ srv : & ProcMacroServerProcess ,
88+ callback : SubCallback < ' _ > ,
89+ ) -> Result < u32 , ServerError > {
9090 let request = BidirectionalMessage :: Request ( Request :: ApiVersionCheck { } ) ;
9191
92- struct NoCallbacks ;
93- impl ClientCallbacks for NoCallbacks {
94- fn handle_sub_request ( & mut self , _req : SubRequest ) -> Result < SubResponse , ServerError > {
95- Err ( ServerError { message : "sub-request not supported here" . into ( ) , io : None } )
96- }
97- }
98-
99- let mut callbacks = NoCallbacks ;
100-
101- let response_payload = run_request ( srv, request, & mut callbacks) ?;
92+ let response_payload = run_request ( srv, request, callback) ?;
10293
10394 match response_payload {
10495 BidirectionalMessage :: Response ( Response :: ApiVersionCheck ( version) ) => Ok ( version) ,
@@ -111,21 +102,13 @@ pub(crate) fn version_check(srv: &ProcMacroServerProcess) -> Result<u32, ServerE
111102/// Enable support for rust-analyzer span mode if the server supports it.
112103pub ( crate ) fn enable_rust_analyzer_spans (
113104 srv : & ProcMacroServerProcess ,
105+ callback : SubCallback < ' _ > ,
114106) -> Result < SpanMode , ServerError > {
115107 let request = BidirectionalMessage :: Request ( Request :: SetConfig ( ServerConfig {
116108 span_mode : SpanMode :: RustAnalyzer ,
117109 } ) ) ;
118110
119- struct NoCallbacks ;
120- impl ClientCallbacks for NoCallbacks {
121- fn handle_sub_request ( & mut self , _req : SubRequest ) -> Result < SubResponse , ServerError > {
122- Err ( ServerError { message : "sub-request not supported here" . into ( ) , io : None } )
123- }
124- }
125-
126- let mut callbacks = NoCallbacks ;
127-
128- let response_payload = run_request ( srv, request, & mut callbacks) ?;
111+ let response_payload = run_request ( srv, request, callback) ?;
129112
130113 match response_payload {
131114 BidirectionalMessage :: Response ( Response :: SetConfig ( ServerConfig { span_mode } ) ) => {
@@ -139,21 +122,13 @@ pub(crate) fn enable_rust_analyzer_spans(
139122pub ( crate ) fn find_proc_macros (
140123 srv : & ProcMacroServerProcess ,
141124 dylib_path : & AbsPath ,
125+ callback : SubCallback < ' _ > ,
142126) -> Result < Result < Vec < ( String , ProcMacroKind ) > , String > , ServerError > {
143127 let request = BidirectionalMessage :: Request ( Request :: ListMacros {
144128 dylib_path : dylib_path. to_path_buf ( ) . into ( ) ,
145129 } ) ;
146130
147- struct NoCallbacks ;
148- impl ClientCallbacks for NoCallbacks {
149- fn handle_sub_request ( & mut self , _req : SubRequest ) -> Result < SubResponse , ServerError > {
150- Err ( ServerError { message : "sub-request not supported here" . into ( ) , io : None } )
151- }
152- }
153-
154- let mut callbacks = NoCallbacks ;
155-
156- let response_payload = run_request ( srv, request, & mut callbacks) ?;
131+ let response_payload = run_request ( srv, request, callback) ?;
157132
158133 match response_payload {
159134 BidirectionalMessage :: Response ( Response :: ListMacros ( it) ) => Ok ( it) ,
@@ -163,14 +138,14 @@ pub(crate) fn find_proc_macros(
163138
164139pub ( crate ) fn expand (
165140 proc_macro : & ProcMacro ,
166- db : & dyn SourceDatabase ,
167141 subtree : tt:: SubtreeView < ' _ , Span > ,
168142 attr : Option < tt:: SubtreeView < ' _ , Span > > ,
169143 env : Vec < ( String , String ) > ,
170144 def_site : Span ,
171145 call_site : Span ,
172146 mixed_site : Span ,
173147 current_dir : String ,
148+ callback : SubCallback < ' _ > ,
174149) -> Result < Result < tt:: TopSubtree < span:: SpanData < span:: SyntaxContext > > , String > , crate :: ServerError >
175150{
176151 let version = proc_macro. process . version ( ) ;
@@ -201,27 +176,7 @@ pub(crate) fn expand(
201176 current_dir : Some ( current_dir) ,
202177 } ) ) ) ;
203178
204- struct Callbacks < ' de > {
205- db : & ' de dyn SourceDatabase ,
206- }
207- impl < ' db > ClientCallbacks for Callbacks < ' db > {
208- fn handle_sub_request ( & mut self , req : SubRequest ) -> Result < SubResponse , ServerError > {
209- match req {
210- SubRequest :: SourceText { file_id, start, end } => {
211- let file = FileId :: from_raw ( file_id) ;
212- let text = self . db . file_text ( file) . text ( self . db ) ;
213-
214- let slice = text. get ( start as usize ..end as usize ) . map ( |s| s. to_owned ( ) ) ;
215-
216- Ok ( SubResponse :: SourceTextResult { text : slice } )
217- }
218- }
219- }
220- }
221-
222- let mut callbacks = Callbacks { db } ;
223-
224- let response_payload = run_request ( & proc_macro. process , task, & mut callbacks) ?;
179+ let response_payload = run_request ( & proc_macro. process , task, callback) ?;
225180
226181 match response_payload {
227182 BidirectionalMessage :: Response ( Response :: ExpandMacro ( it) ) => Ok ( it
@@ -253,15 +208,19 @@ pub(crate) fn expand(
253208fn run_request (
254209 srv : & ProcMacroServerProcess ,
255210 msg : BidirectionalMessage ,
256- callbacks : & mut dyn ClientCallbacks ,
211+ callback : SubCallback < ' _ > ,
257212) -> Result < BidirectionalMessage , ServerError > {
258213 if let Some ( server_error) = srv. exited ( ) {
259214 return Err ( server_error. clone ( ) ) ;
260215 }
261216
262217 if srv. use_postcard ( ) {
263- srv. run_bidirectional :: < PostcardProtocol > ( msg, callbacks )
218+ srv. run_bidirectional :: < PostcardProtocol > ( msg, callback )
264219 } else {
265- srv. run_bidirectional :: < JsonProtocol > ( msg, callbacks )
220+ srv. run_bidirectional :: < JsonProtocol > ( msg, callback )
266221 }
267222}
223+
224+ pub fn reject_subrequests ( req : SubRequest ) -> Result < SubResponse , ServerError > {
225+ Err ( ServerError { message : format ! ( "{req:?} sub-request not supported here" ) , io : None } )
226+ }
0 commit comments