aboutsummaryrefslogtreecommitdiff
path: root/crates/gen_lsp_server/src/lib.rs
blob: baca921dfc88420776102dc2e80d5be7f05453c2 (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#[macro_use]
extern crate failure;
#[macro_use]
extern crate log;
extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
extern crate crossbeam_channel;
extern crate languageserver_types;

mod msg;
mod stdio;

use crossbeam_channel::{Sender, Receiver};
use languageserver_types::{
    ServerCapabilities, InitializeResult, InitializeParams,
    request::{Initialize, Shutdown},
    notification::{Initialized, Exit},
};

pub type Result<T> = ::std::result::Result<T, failure::Error>;
pub use {
    msg::{RawMessage, RawRequest, RawResponse, RawResponseError, RawNotification, ErrorCode},
    stdio::{stdio_transport, Threads},
};

pub fn run_server(
    caps: ServerCapabilities,
    server: impl FnOnce(
        InitializeParams,
        &mut Receiver<RawMessage>,
        &mut Sender<RawMessage>,
    ) -> Result<()>,
    mut receiver: Receiver<RawMessage>,
    mut sender: Sender<RawMessage>,
) -> Result<()> {
    info!("lsp server initializes");
    let params = initialize(&mut receiver, &mut sender, caps)?;
    info!("lsp server initialized, serving requests");
    server(params, &mut receiver, &mut sender)?;
    info!("lsp server waiting for exit notification");
    match receiver.recv() {
        Some(RawMessage::Notification(n)) => {
            n.cast::<Exit>().map_err(|n| format_err!(
                "unexpected notification during shutdown: {:?}", n
            ))?
        }
        m => bail!("unexpected message during shutdown: {:?}", m)
    }
    info!("lsp server shutdown complete");
    Ok(())
}

pub fn handle_shutdown(req: RawRequest, sender: &Sender<RawMessage>) -> Option<RawRequest> {
    match req.cast::<Shutdown>() {
        Ok((id, ())) => {
            let resp = RawResponse::ok::<Shutdown>(id, &());
            sender.send(RawMessage::Response(resp));
            None
        }
        Err(req) => Some(req),
    }
}

fn initialize(
    receiver: &mut Receiver<RawMessage>,
    sender: &mut Sender<RawMessage>,
    caps: ServerCapabilities,
) -> Result<InitializeParams> {
    let (id, params) = match receiver.recv() {
        Some(RawMessage::Request(req)) => match req.cast::<Initialize>() {
            Err(req) => bail!("expected initialize request, got {:?}", req),
            Ok(req) => req,
        }
        msg =>
            bail!("expected initialize request, got {:?}", msg),
    };
    let resp = RawResponse::ok::<Initialize>(id, &InitializeResult { capabilities: caps });
    sender.send(RawMessage::Response(resp));
    match receiver.recv() {
        Some(RawMessage::Notification(n)) => {
            n.cast::<Initialized>().map_err(|_| format_err!(
                "expected initialized notification"
            ))?;
        }
        _ => bail!("expected initialized notification"),
    }
    Ok(params)
}