Skip to content

Commit

Permalink
Refactor proc macro channels (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Draggu authored Dec 3, 2024
1 parent c69f3df commit deaad44
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
47 changes: 24 additions & 23 deletions src/lang/proc_macros/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,12 @@ pub struct ProcMacroClientController {
notifier: Notifier,
plugin_suite: Option<PluginSuite>,
initialization_retries: RateLimiter<NotKeyed, InMemoryState, QuantaClock>,
channels: Option<ProcMacroChannelsSenders>,
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 {
Expand All @@ -81,7 +75,7 @@ impl ProcMacroClientController {
NonZeroU32::new(RATE_LIMITER_RETRIES).unwrap(),
),
),
channels: Default::default(),
channels: ProcMacroChannels::new(),
}
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -256,19 +251,25 @@ fn parse_proc_macro_response(response: RpcResponse) -> Result<ProcMacroResult> {
}

#[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 }
}
}
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -269,7 +269,7 @@ impl Backend {
fn run(self) -> Result<JoinHandle<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.response_receiver();

let mut scheduler = Scheduler::new(&mut state, connection.make_sender());
Expand Down Expand Up @@ -344,7 +344,7 @@ impl Backend {
// +--------------------------------------------------+
fn event_loop(
connection: &Connection,
proc_macro_channels: ProcMacroChannelsReceivers,
proc_macro_channels: ProcMacroChannels,
project_updates_receiver: Receiver<ProjectUpdate>,
mut scheduler: Scheduler<'_>,
) -> Result<()> {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit deaad44

Please sign in to comment.