From b5b1c527567a1f0b7fc8299ebb8fea9f28610ed6 Mon Sep 17 00:00:00 2001 From: Piotr Figiela <77412592+Draggu@users.noreply.github.com> Date: Thu, 28 Nov 2024 17:45:18 +0100 Subject: [PATCH] Refactor proc macro channels commit-id:561e7087 --- src/lang/proc_macros/controller.rs | 47 +++++++++++++++--------------- src/lib.rs | 10 +++---- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/lang/proc_macros/controller.rs b/src/lang/proc_macros/controller.rs index 508b7bd..535f37a 100644 --- a/src/lang/proc_macros/controller.rs +++ b/src/lang/proc_macros/controller.rs @@ -52,18 +52,12 @@ pub struct ProcMacroClientController { notifier: Notifier, plugin_suite: Option, initialization_retries: RateLimiter, - channels: Option, + channels: ProcMacroChannels, } impl ProcMacroClientController { - pub fn init_channels(&mut self) -> ProcMacroChannelsReceivers { - let (response_sender, response_receiver) = crossbeam::channel::bounded(1); - let (error_sender, error_receiver) = crossbeam::channel::bounded(1); - - self.channels = - Some(ProcMacroChannelsSenders { error: error_sender, response: response_sender }); - - ProcMacroChannelsReceivers { error: error_receiver, response: response_receiver } + pub fn channels(&mut self) -> ProcMacroChannels { + self.channels.clone() } pub fn new(scarb: ScarbToolchain, notifier: Notifier) -> Self { @@ -81,7 +75,7 @@ impl ProcMacroClientController { NonZeroU32::new(RATE_LIMITER_RETRIES).unwrap(), ), ), - channels: Default::default(), + channels: ProcMacroChannels::new(), } } @@ -154,11 +148,12 @@ impl ProcMacroClientController { fn spawn_server(&mut self, db: &mut AnalysisDatabase) { match self.scarb.proc_macro_server() { Ok(proc_macro_server) => { - let channels = self.channels.clone().unwrap(); - let client = ProcMacroClient::new( - ProcMacroServerConnection::stdio(proc_macro_server, channels.response), - channels.error, + ProcMacroServerConnection::stdio( + proc_macro_server, + self.channels.response_sender.clone(), + ), + self.channels.error_sender.clone(), ); client.start_initialize(); @@ -256,19 +251,25 @@ fn parse_proc_macro_response(response: RpcResponse) -> Result { } #[derive(Clone)] -pub struct ProcMacroChannelsReceivers { +pub struct ProcMacroChannels { + // A single element queue is used to notify when client occurred an error. + error_sender: Sender<()>, + // A single element queue is used to notify when the response queue is pushed. - pub response: Receiver<()>, + pub response_receiver: Receiver<()>, + + // A single element queue is used to notify when the response queue is pushed. + response_sender: Sender<()>, // A single element queue is used to notify when client occurred an error. - pub error: Receiver<()>, + pub error_receiver: Receiver<()>, } -#[derive(Clone)] -struct ProcMacroChannelsSenders { - // A single element queue is used to notify when the response queue is pushed. - response: Sender<()>, +impl ProcMacroChannels { + fn new() -> Self { + let (response_sender, response_receiver) = crossbeam::channel::bounded(1); + let (error_sender, error_receiver) = crossbeam::channel::bounded(1); - // A single element queue is used to notify when client occurred an error. - error: Sender<()>, + Self { response_sender, response_receiver, error_sender, error_receiver } + } } diff --git a/src/lib.rs b/src/lib.rs index 5e19d41..eba0759 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,7 +54,7 @@ use lsp_types::RegistrationParams; use tracing::{debug, error, info}; use crate::lang::lsp::LsProtoGroup; -use crate::lang::proc_macros::controller::ProcMacroChannelsReceivers; +use crate::lang::proc_macros::controller::ProcMacroChannels; use crate::lsp::capabilities::server::{ collect_dynamic_registrations, collect_server_capabilities, }; @@ -269,7 +269,7 @@ impl Backend { fn run(self) -> Result>> { event_loop_thread(move || { let Self { mut state, connection } = self; - let proc_macro_channels = state.proc_macro_controller.init_channels(); + let proc_macro_channels = state.proc_macro_controller.channels(); let project_updates_receiver = state.project_controller.init_channel(); let mut scheduler = Scheduler::new(&mut state, connection.make_sender()); @@ -344,7 +344,7 @@ impl Backend { // +--------------------------------------------------+ fn event_loop( connection: &Connection, - proc_macro_channels: ProcMacroChannelsReceivers, + proc_macro_channels: ProcMacroChannels, project_updates_receiver: Receiver, mut scheduler: Scheduler<'_>, ) -> Result<()> { @@ -373,12 +373,12 @@ impl Backend { }; scheduler.dispatch(task); } - recv(proc_macro_channels.response) -> response => { + recv(proc_macro_channels.response_receiver) -> response => { let Ok(()) = response else { break }; scheduler.local(Self::on_proc_macro_response); } - recv(proc_macro_channels.error) -> error => { + recv(proc_macro_channels.error_receiver) -> error => { let Ok(()) = error else { break }; scheduler.local(Self::on_proc_macro_error);