mylang_lsp_server/
responder.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
mod analyzer;
mod diagnostic;
mod handler;
mod range;

use std::sync::{atomic::AtomicBool, Arc};

use actix::{Actor, Addr, Context, ContextFutureSpawner, Handler, WrapFuture};

use self::handler::handle;
use crate::{message::LspMessage, sender::Sender};

/// 言語クライアントから受信したメッセージをもとに応答を決定し、[`Sender`] に送信を依頼するアクター
pub struct Responder {
    sender: Addr<Sender>,
    diagnostics_supported: Arc<AtomicBool>,
}

impl Responder {
    pub fn new(sender: Addr<Sender>) -> Self {
        Self {
            sender,
            diagnostics_supported: Arc::new(AtomicBool::new(false)),
        }
    }
}

impl Actor for Responder {
    type Context = Context<Self>;
}

impl Handler<LspMessage> for Responder {
    type Result = ();

    fn handle(&mut self, msg: LspMessage, ctx: &mut Self::Context) {
        let sender = self.sender.clone();

        let diagnostics_supported = Arc::clone(&self.diagnostics_supported);

        async move { handle(sender, diagnostics_supported, msg).await }
            .into_actor(self)
            .wait(ctx);
    }
}