aboutsummaryrefslogtreecommitdiff
path: root/crates/gen_lsp_server/examples/01_gen_lsp_server.rs
blob: 60c581075f72cc17e4491166194a3e4899712655 (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
use crossbeam_channel::{Sender, Receiver};
use lsp_types::{
    ServerCapabilities, InitializeParams,
    request::{GotoDefinition, GotoDefinitionResponse},
};
use gen_lsp_server::{run_server, stdio_transport, handle_shutdown, RawMessage, RawResponse};

fn main() -> Result<(), failure::Error> {
    let (receiver, sender, io_threads) = stdio_transport();
    run_server(ServerCapabilities::default(), receiver, sender, main_loop)?;
    io_threads.join()?;
    Ok(())
}

fn main_loop(
    _params: InitializeParams,
    receiver: &Receiver<RawMessage>,
    sender: &Sender<RawMessage>,
) -> Result<(), failure::Error> {
    for msg in receiver {
        match msg {
            RawMessage::Request(req) => {
                let req = match handle_shutdown(req, sender) {
                    None => return Ok(()),
                    Some(req) => req,
                };
                match req.cast::<GotoDefinition>() {
                    Ok((id, _params)) => {
                        let resp = RawResponse::ok::<GotoDefinition>(
                            id,
                            &Some(GotoDefinitionResponse::Array(Vec::new())),
                        );
                        sender.send(RawMessage::Response(resp))?;
                        continue;
                    }
                    Err(req) => req,
                };
                // ...
            }
            RawMessage::Response(_resp) => (),
            RawMessage::Notification(_not) => (),
        }
    }
    Ok(())
}