diff options
author | Aleksey Kladov <[email protected]> | 2019-08-30 15:24:11 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-08-30 15:24:11 +0100 |
commit | 72a3722470e5297c72dcaccaf2f113e7b758606d (patch) | |
tree | 3f0e4056ba2e4b3799b72d71d709783aa6dffc49 /crates/gen_lsp_server/examples | |
parent | 7d72ca80003b7915ed7fc64907b5b6dc5c88dacd (diff) |
move lsp-server to a separate repository
Diffstat (limited to 'crates/gen_lsp_server/examples')
-rw-r--r-- | crates/gen_lsp_server/examples/01_gen_lsp_server.rs | 47 | ||||
-rw-r--r-- | crates/gen_lsp_server/examples/02_gen_lsp_server_with_logging.rs | 120 |
2 files changed, 0 insertions, 167 deletions
diff --git a/crates/gen_lsp_server/examples/01_gen_lsp_server.rs b/crates/gen_lsp_server/examples/01_gen_lsp_server.rs deleted file mode 100644 index f49965064..000000000 --- a/crates/gen_lsp_server/examples/01_gen_lsp_server.rs +++ /dev/null | |||
@@ -1,47 +0,0 @@ | |||
1 | use std::error::Error; | ||
2 | |||
3 | use crossbeam_channel::{Receiver, Sender}; | ||
4 | use gen_lsp_server::{handle_shutdown, run_server, stdio_transport, RawMessage, RawResponse}; | ||
5 | use lsp_types::{ | ||
6 | request::{GotoDefinition, GotoDefinitionResponse}, | ||
7 | InitializeParams, ServerCapabilities, | ||
8 | }; | ||
9 | |||
10 | fn main() -> Result<(), Box<dyn Error + Sync + Send>> { | ||
11 | let (receiver, sender, io_threads) = stdio_transport(); | ||
12 | run_server(ServerCapabilities::default(), receiver, sender, main_loop)?; | ||
13 | io_threads.join()?; | ||
14 | Ok(()) | ||
15 | } | ||
16 | |||
17 | fn main_loop( | ||
18 | _params: InitializeParams, | ||
19 | receiver: &Receiver<RawMessage>, | ||
20 | sender: &Sender<RawMessage>, | ||
21 | ) -> Result<(), Box<dyn Error + Sync + Send>> { | ||
22 | for msg in receiver { | ||
23 | match msg { | ||
24 | RawMessage::Request(req) => { | ||
25 | let req = match handle_shutdown(req, sender) { | ||
26 | None => return Ok(()), | ||
27 | Some(req) => req, | ||
28 | }; | ||
29 | match req.cast::<GotoDefinition>() { | ||
30 | Ok((id, _params)) => { | ||
31 | let resp = RawResponse::ok::<GotoDefinition>( | ||
32 | id, | ||
33 | &Some(GotoDefinitionResponse::Array(Vec::new())), | ||
34 | ); | ||
35 | sender.send(RawMessage::Response(resp))?; | ||
36 | continue; | ||
37 | } | ||
38 | Err(req) => req, | ||
39 | }; | ||
40 | // ... | ||
41 | } | ||
42 | RawMessage::Response(_resp) => (), | ||
43 | RawMessage::Notification(_not) => (), | ||
44 | } | ||
45 | } | ||
46 | Ok(()) | ||
47 | } | ||
diff --git a/crates/gen_lsp_server/examples/02_gen_lsp_server_with_logging.rs b/crates/gen_lsp_server/examples/02_gen_lsp_server_with_logging.rs deleted file mode 100644 index 3c48106c5..000000000 --- a/crates/gen_lsp_server/examples/02_gen_lsp_server_with_logging.rs +++ /dev/null | |||
@@ -1,120 +0,0 @@ | |||
1 | //! A minimal example LSP server that can only respond to the `gotoDefinition` request. To use | ||
2 | //! this example, execute it and then send an `initialize` request. | ||
3 | //! | ||
4 | //! ```no_run | ||
5 | //! Content-Length: 85 | ||
6 | //! | ||
7 | //! {"jsonrpc": "2.0", "method": "initialize", "id": 1, "params": {"capabilities": {}}} | ||
8 | //! ``` | ||
9 | //! | ||
10 | //! This will respond with a server respose. Then send it a `initialized` notification which will | ||
11 | //! have no response. | ||
12 | //! | ||
13 | //! ```no_run | ||
14 | //! Content-Length: 59 | ||
15 | //! | ||
16 | //! {"jsonrpc": "2.0", "method": "initialized", "params": {}} | ||
17 | //! ``` | ||
18 | //! | ||
19 | //! Once these two are sent, then we enter the main loop of the server. The only request this | ||
20 | //! example can handle is `gotoDefinition`: | ||
21 | //! | ||
22 | //! ```no_run | ||
23 | //! Content-Length: 159 | ||
24 | //! | ||
25 | //! {"jsonrpc": "2.0", "method": "textDocument/definition", "id": 2, "params": {"textDocument": {"uri": "file://temp"}, "position": {"line": 1, "character": 1}}} | ||
26 | //! ``` | ||
27 | //! | ||
28 | //! To finish up without errors, send a shutdown request: | ||
29 | //! | ||
30 | //! ```no_run | ||
31 | //! Content-Length: 67 | ||
32 | //! | ||
33 | //! {"jsonrpc": "2.0", "method": "shutdown", "id": 3, "params": null} | ||
34 | //! ``` | ||
35 | //! | ||
36 | //! The server will exit the main loop and finally we send a `shutdown` notification to stop | ||
37 | //! the server. | ||
38 | //! | ||
39 | //! ``` | ||
40 | //! Content-Length: 54 | ||
41 | //! | ||
42 | //! {"jsonrpc": "2.0", "method": "exit", "params": null} | ||
43 | //! ``` | ||
44 | |||
45 | use std::error::Error; | ||
46 | |||
47 | use crossbeam_channel::{Receiver, Sender}; | ||
48 | use gen_lsp_server::{ | ||
49 | handle_shutdown, run_server, stdio_transport, RawMessage, RawRequest, RawResponse, | ||
50 | }; | ||
51 | use log::info; | ||
52 | use lsp_types::{ | ||
53 | request::{GotoDefinition, GotoDefinitionResponse}, | ||
54 | InitializeParams, ServerCapabilities, | ||
55 | }; | ||
56 | |||
57 | fn main() -> Result<(), Box<dyn Error + Sync + Send>> { | ||
58 | // Set up logging. Because `stdio_transport` gets a lock on stdout and stdin, we must have | ||
59 | // our logging only write out to stderr. | ||
60 | flexi_logger::Logger::with_str("info").start().unwrap(); | ||
61 | info!("starting generic LSP server"); | ||
62 | |||
63 | // Create the transport. Includes the stdio (stdin and stdout) versions but this could | ||
64 | // also be implemented to use sockets or HTTP. | ||
65 | let (receiver, sender, io_threads) = stdio_transport(); | ||
66 | |||
67 | // Run the server and wait for the two threads to end (typically by trigger LSP Exit event). | ||
68 | run_server(ServerCapabilities::default(), receiver, sender, main_loop)?; | ||
69 | io_threads.join()?; | ||
70 | |||
71 | // Shut down gracefully. | ||
72 | info!("shutting down server"); | ||
73 | Ok(()) | ||
74 | } | ||
75 | |||
76 | fn main_loop( | ||
77 | _params: InitializeParams, | ||
78 | receiver: &Receiver<RawMessage>, | ||
79 | sender: &Sender<RawMessage>, | ||
80 | ) -> Result<(), Box<dyn Error + Sync + Send>> { | ||
81 | info!("starting example main loop"); | ||
82 | for msg in receiver { | ||
83 | info!("got msg: {:?}", msg); | ||
84 | match msg { | ||
85 | RawMessage::Request(req) => { | ||
86 | let req = match log_handle_shutdown(req, sender) { | ||
87 | None => return Ok(()), | ||
88 | Some(req) => req, | ||
89 | }; | ||
90 | info!("got request: {:?}", req); | ||
91 | match req.cast::<GotoDefinition>() { | ||
92 | Ok((id, params)) => { | ||
93 | info!("got gotoDefinition request #{}: {:?}", id, params); | ||
94 | let resp = RawResponse::ok::<GotoDefinition>( | ||
95 | id, | ||
96 | &Some(GotoDefinitionResponse::Array(Vec::new())), | ||
97 | ); | ||
98 | info!("sending gotoDefinition response: {:?}", resp); | ||
99 | sender.send(RawMessage::Response(resp))?; | ||
100 | continue; | ||
101 | } | ||
102 | Err(req) => req, | ||
103 | }; | ||
104 | // ... | ||
105 | } | ||
106 | RawMessage::Response(resp) => { | ||
107 | info!("got response: {:?}", resp); | ||
108 | } | ||
109 | RawMessage::Notification(not) => { | ||
110 | info!("got notification: {:?}", not); | ||
111 | } | ||
112 | } | ||
113 | } | ||
114 | Ok(()) | ||
115 | } | ||
116 | |||
117 | pub fn log_handle_shutdown(req: RawRequest, sender: &Sender<RawMessage>) -> Option<RawRequest> { | ||
118 | info!("handle_shutdown: {:?}", req); | ||
119 | handle_shutdown(req, sender) | ||
120 | } | ||