[go: up one dir, main page]

tower-lsp 0.6.0

Language Server Protocol implementation based on Tower
Documentation
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! Language Server Protocol (LSP) server abstraction for [Tower].
//!
//! [Tower]: https://github.com/tower-rs/tower
//!
//! # Example
//!
//! ```rust
//! # use futures::future;
//! # use jsonrpc_core::{BoxFuture, Result};
//! # use serde_json::Value;
//! # use tower_lsp::lsp_types::request::GotoDefinitionResponse;
//! # use tower_lsp::lsp_types::*;
//! # use tower_lsp::{LanguageServer, LspService, Printer, Server};
//! #
//! #[derive(Debug, Default)]
//! struct Backend;
//!
//! impl LanguageServer for Backend {
//!     type ShutdownFuture = BoxFuture<()>;
//!     type SymbolFuture = BoxFuture<Option<Vec<SymbolInformation>>>;
//!     type ExecuteFuture = BoxFuture<Option<Value>>;
//!     type CompletionFuture = BoxFuture<Option<CompletionResponse>>;
//!     type HoverFuture = BoxFuture<Option<Hover>>;
//!     type DeclarationFuture = BoxFuture<Option<GotoDefinitionResponse>>;
//!     type DefinitionFuture = BoxFuture<Option<GotoDefinitionResponse>>;
//!     type TypeDefinitionFuture = BoxFuture<Option<GotoDefinitionResponse>>;
//!     type HighlightFuture = BoxFuture<Option<Vec<DocumentHighlight>>>;
//!
//!     fn initialize(&self, _: &Printer, _: InitializeParams) -> Result<InitializeResult> {
//!         Ok(InitializeResult::default())
//!     }
//!
//!     fn initialized(&self, printer: &Printer, _: InitializedParams) {
//!         printer.log_message(MessageType::Info, "server initialized!");
//!     }
//!
//!     fn shutdown(&self) -> Self::ShutdownFuture {
//!         Box::new(future::ok(()))
//!     }
//!
//!     fn symbol(&self, _: WorkspaceSymbolParams) -> Self::SymbolFuture {
//!         Box::new(future::ok(None))
//!     }
//!
//!     fn execute_command(&self, _: &Printer, _: ExecuteCommandParams) -> Self::ExecuteFuture {
//!         Box::new(future::ok(None))
//!     }
//!
//!     fn completion(&self, _: CompletionParams) -> Self::CompletionFuture {
//!         Box::new(future::ok(None))
//!     }
//!
//!     fn hover(&self, _: TextDocumentPositionParams) -> Self::HoverFuture {
//!         Box::new(future::ok(None))
//!     }
//!
//!     fn goto_declaration(&self, _: TextDocumentPositionParams) -> Self::DeclarationFuture {
//!         Box::new(future::ok(None))
//!     }
//!
//!     fn goto_definition(&self, _: TextDocumentPositionParams) -> Self::DefinitionFuture {
//!         Box::new(future::ok(None))
//!     }
//!
//!     fn goto_type_definition(&self, _: TextDocumentPositionParams) -> Self::TypeDefinitionFuture {
//!         Box::new(future::ok(None))
//!     }
//!
//!     fn document_highlight(&self, _: TextDocumentPositionParams) -> Self::HighlightFuture {
//!         Box::new(future::ok(None))
//!     }
//! }
//!
//! fn main() {
//!     let stdin = tokio::io::stdin();
//!     let stdout = tokio::io::stdout();
//!
//!     let (service, messages) = LspService::new(Backend::default());
//!     let handle = service.close_handle();
//!     let server = Server::new(stdin, stdout)
//!         .interleave(messages)
//!         .serve(service);
//!
//!     tokio::run(handle.run_until_exit(server));
//! }
//! ```

#![deny(missing_debug_implementations)]
#![deny(missing_docs)]
#![forbid(unsafe_code)]

pub extern crate lsp_types;

pub use self::delegate::{MessageStream, Printer};
pub use self::message::Incoming;
pub use self::service::{ExitReceiver, ExitedError, LspService};
pub use self::stdio::Server;

use futures::Future;
use jsonrpc_core::{Error, Result};
use lsp_types::request::GotoDefinitionResponse;
use lsp_types::*;
use serde_json::Value;

mod codec;
mod delegate;
mod message;
mod service;
mod stdio;

/// Trait implemented by language server backends.
///
/// This interface allows servers adhering to the [Language Server Protocol] to be implemented in a
/// safe and easily testable way without exposing the low-level implementation details.
///
/// [Language Server Protocol]: https://microsoft.github.io/language-server-protocol/
pub trait LanguageServer: Send + Sync + 'static {
    /// Response returned when a server shutdown is requested.
    type ShutdownFuture: Future<Item = (), Error = Error> + Send;
    /// Response returned when a workspace symbol action is requested.
    type SymbolFuture: Future<Item = Option<Vec<SymbolInformation>>, Error = Error> + Send;
    /// Response returned when an execute command action is requested.
    type ExecuteFuture: Future<Item = Option<Value>, Error = Error> + Send;
    /// Response returned when a completion action is requested.
    type CompletionFuture: Future<Item = Option<CompletionResponse>, Error = Error> + Send;
    /// Response returned when a hover action is requested.
    type HoverFuture: Future<Item = Option<Hover>, Error = Error> + Send;
    /// Response returned when a goto declaration action is requested.
    type DeclarationFuture: Future<Item = Option<GotoDefinitionResponse>, Error = Error> + Send;
    /// Response returned when a goto definition action is requested.
    type DefinitionFuture: Future<Item = Option<GotoDefinitionResponse>, Error = Error> + Send;
    /// Response returned when a goto type definition action is requested.
    type TypeDefinitionFuture: Future<Item = Option<GotoDefinitionResponse>, Error = Error> + Send;
    /// Response returned when a document highlight action is requested.
    type HighlightFuture: Future<Item = Option<Vec<DocumentHighlight>>, Error = Error> + Send;

    /// The [`initialize`] request is the first request sent from the client to the server.
    ///
    /// [`initialize`]: https://microsoft.github.io/language-server-protocol/specifications/specification-3-15/#initialize
    fn initialize(&self, printer: &Printer, params: InitializeParams) -> Result<InitializeResult>;

    /// The [`initialized`] notification is sent from the client to the server after the client
    /// received the result of the initialize request but before the client sends anything else.
    ///
    /// The server can use the `initialized` notification for example to dynamically register
    /// capabilities with the client.
    ///
    /// [`initialized`]: https://microsoft.github.io/language-server-protocol/specifications/specification-3-15/#initialized
    fn initialized(&self, printer: &Printer, params: InitializedParams) {
        let _ = printer;
        let _ = params;
    }

    /// The [`shutdown`] request asks the server to gracefully shut down, but to not exit.
    ///
    /// This request is often later followed by an [`exit`] notification, which will cause the
    /// server to exit immediately.
    ///
    /// [`shutdown`]: https://microsoft.github.io/language-server-protocol/specifications/specification-3-15/#shutdown
    /// [`exit`]: https://microsoft.github.io/language-server-protocol/specifications/specification-3-15/#exit
    fn shutdown(&self) -> Self::ShutdownFuture;

    /// The [`workspace/didChangeWorkspaceFolders`] notification is sent from the client to the
    /// server to inform about workspace folder configuration changes.
    ///
    /// The notification is sent by default if both of these boolean fields were set to `true` in
    /// the [`initialize`] method:
    ///
    /// * `InitializeParams::capabilities::workspace::workspace_folders`
    /// * `InitializeResult::capabilities::workspace::workspace_folders::supported`
    ///
    /// This notification is also sent if the server has registered itself to receive this
    /// notification.
    ///
    /// [`workspace/didChangeWorkspaceFolders`]: https://microsoft.github.io/language-server-protocol/specifications/specification-3-15/#workspace_didChangeWorkspaceFolders
    /// [`initialize`]: #tymethod.initialize
    fn did_change_workspace_folders(&self, p: &Printer, params: DidChangeWorkspaceFoldersParams) {
        let _ = p;
        let _ = params;
    }

    /// The [`workspace/didChangeConfiguration`] notification is sent from the client to the server
    /// to signal the change of configuration settings.
    ///
    /// [`workspace/didChangeConfiguration`]: https://microsoft.github.io/language-server-protocol/specifications/specification-3-15/#workspace_didChangeConfiguration
    fn did_change_configuration(&self, printer: &Printer, params: DidChangeConfigurationParams) {
        let _ = printer;
        let _ = params;
    }

    /// The [`workspace/didChangeWatchedFiles`] notification is sent from the client to the server
    /// when the client detects changes to files watched by the language client.
    ///
    /// It is recommended that servers register for these file events using the registration
    /// mechanism. This can be done here or in the [`initialized`] method using
    /// `Printer::register_capability()`.
    ///
    /// [`workspace/didChangeWatchedFiles`]: https://microsoft.github.io/language-server-protocol/specifications/specification-3-15/#workspace_didChangeConfiguration
    /// [`initialized`]: #tymethod.initialized
    fn did_change_watched_files(&self, printer: &Printer, params: DidChangeWatchedFilesParams) {
        let _ = printer;
        let _ = params;
    }

    /// The [`workspace/symbol`] request is sent from the client to the server to list project-wide
    /// symbols matching the given query string.
    ///
    /// [`workspace/symbol`]: https://microsoft.github.io/language-server-protocol/specifications/specification-3-15/#workspace_symbol
    fn symbol(&self, params: WorkspaceSymbolParams) -> Self::SymbolFuture;

    /// The [`workspace/executeCommand`] request is sent from the client to the server to trigger
    /// command execution on the server.
    ///
    /// In most cases, the server creates a `WorkspaceEdit` structure and applies the changes to
    /// the workspace using `Printer::apply_edit()` before returning from this function.
    ///
    /// [`workspace/executeCommand`]: https://microsoft.github.io/language-server-protocol/specifications/specification-3-15/#workspace_executeCommand
    fn execute_command(&self, p: &Printer, params: ExecuteCommandParams) -> Self::ExecuteFuture;

    /// The [`textDocument/didOpen`] notification is sent from the client to the server to signal
    /// that a new text document has been opened by the client.
    ///
    /// The document's truth is now managed by the client and the server must not try to read the
    /// document’s truth using the document's URI. "Open" in this sense means it is managed by the
    /// client. It doesn't necessarily mean that its content is presented in an editor.
    ///
    /// [`textDocument/didOpen`]: https://microsoft.github.io/language-server-protocol/specifications/specification-3-15/#textDocument_didOpen
    fn did_open(&self, printer: &Printer, params: DidOpenTextDocumentParams) {
        let _ = printer;
        let _ = params;
    }

    /// The [`textDocument/didChange`] notification is sent from the client to the server to signal
    /// changes to a text document.
    ///
    /// This notification will contain a distinct version tag and a list of edits made to the
    /// document for the server to interpret.
    ///
    /// [`textDocument/didChange`]: https://microsoft.github.io/language-server-protocol/specifications/specification-3-15/#textDocument_didChange
    fn did_change(&self, printer: &Printer, params: DidChangeTextDocumentParams) {
        let _ = printer;
        let _ = params;
    }

    /// The [`textDocument/didSave`] notification is sent from the client to the server when the
    /// document was saved in the client.
    ///
    /// [`textDocument/didSave`]: https://microsoft.github.io/language-server-protocol/specifications/specification-3-15/#textDocument_didSave
    fn did_save(&self, printer: &Printer, params: DidSaveTextDocumentParams) {
        let _ = printer;
        let _ = params;
    }

    /// The [`textDocument/didClose`] notification is sent from the client to the server when the
    /// document got closed in the client.
    ///
    /// The document's truth now exists where the document's URI points to (e.g. if the document's
    /// URI is a file URI, the truth now exists on disk).
    ///
    /// [`textDocument/didClose`]: https://microsoft.github.io/language-server-protocol/specifications/specification-3-15/#textDocument_didClose
    fn did_close(&self, printer: &Printer, params: DidCloseTextDocumentParams) {
        let _ = printer;
        let _ = params;
    }

    /// The [`textDocument/completion`] request is sent from the client to the server to compute
    /// completion items at a given cursor position.
    ///
    /// If computing full completion items is expensive, servers can additionally provide a handler
    /// for the completion item resolve request (`completionItem/resolve`). This request is sent
    /// when a completion item is selected in the user interface.
    ///
    /// [`textDocument/completion`]: https://microsoft.github.io/language-server-protocol/specifications/specification-3-15/#textDocument_completion
    fn completion(&self, params: CompletionParams) -> Self::CompletionFuture;

    /// The [`textDocument/hover`] request asks the server for hover information at a given text
    /// document position.
    ///
    /// Such hover information typically includes type signature information and inline
    /// documentation for the symbol at the given text document position.
    ///
    /// [`textDocument/hover`]: https://microsoft.github.io/language-server-protocol/specifications/specification-3-15/#textDocument_hover
    fn hover(&self, params: TextDocumentPositionParams) -> Self::HoverFuture;

    /// The [`textDocument/declaration`] request asks the server for the declaration location of a
    /// symbol at a given text document position.
    ///
    /// The [`GotoDefinitionResponse::Link`] return value was introduced in specification version
    /// 3.14.0 and requires client-side support. It can be returned if the client set the following
    /// field to `true` in the [`initialize`] method:
    ///
    /// ```text
    /// InitializeParams::capabilities::text_document::definition::link_support
    /// ```
    ///
    /// [`textDocument/declaration`]: https://microsoft.github.io/language-server-protocol/specifications/specification-3-15/#textDocument_declaration
    /// [`GotoDefinitionResponse::Link`]: https://docs.rs/lsp-types/0.63.1/lsp_types/request/enum.GotoDefinitionResponse.html#variant.Link
    /// [`initialize`]: #tymethod.initialize
    fn goto_declaration(&self, params: TextDocumentPositionParams) -> Self::DeclarationFuture;

    /// The [`textDocument/definition`] request asks the server for the definition location of a
    /// symbol at a given text document position.
    ///
    /// The [`GotoDefinitionResponse::Link`] return value was introduced in specification version
    /// 3.14.0 and requires client-side support. It can be returned if the client set the following
    /// field to `true` in the [`initialize`] method:
    ///
    /// ```text
    /// InitializeParams::capabilities::text_document::definition::link_support
    /// ```
    ///
    /// [`textDocument/definition`]: https://microsoft.github.io/language-server-protocol/specifications/specification-3-15/#textDocument_definition
    /// [`GotoDefinitionResponse::Link`]: https://docs.rs/lsp-types/0.63.1/lsp_types/request/enum.GotoDefinitionResponse.html#variant.Link
    /// [`initialize`]: #tymethod.initialize
    fn goto_definition(&self, params: TextDocumentPositionParams) -> Self::DefinitionFuture;

    /// The [`textDocument/typeDefinition`] request asks the server for the type definition location of
    /// a symbol at a given text document position.
    ///
    /// The [`GotoDefinitionResponse::Link`] return value was introduced in specification version
    /// 3.14.0 and requires client-side support. It can be returned if the client set the following
    /// field to `true` in the [`initialize`] method:
    ///
    /// ```text
    /// InitializeParams::capabilities::text_document::definition::link_support
    /// ```
    ///
    /// [`textDocument/typeDefinition`]: https://microsoft.github.io/language-server-protocol/specifications/specification-3-15/#textDocument_typeDefinition
    /// [`GotoDefinitionResponse::Link`]: https://docs.rs/lsp-types/0.63.1/lsp_types/request/enum.GotoDefinitionResponse.html#variant.Link
    /// [`initialize`]: #tymethod.initialize
    fn goto_type_definition(
        &self,
        params: TextDocumentPositionParams,
    ) -> Self::TypeDefinitionFuture;

    /// The [`textDocument/documentHighlight`] request is sent from the client to the server to
    /// resolve appropriate highlights for a given text document position.
    ///
    /// For programming languages, this usually highlights all textual references to the symbol
    /// scoped to this file.
    ///
    /// This request differs slightly from `textDocument/references` in that this one is allowed to
    /// be more fuzzy.
    ///
    /// [`textDocument/documentHighlight`]: https://microsoft.github.io/language-server-protocol/specifications/specification-3-15/#textDocument_documentHighlight
    fn document_highlight(&self, params: TextDocumentPositionParams) -> Self::HighlightFuture;
}

impl<S: ?Sized + LanguageServer> LanguageServer for Box<S> {
    type ShutdownFuture = S::ShutdownFuture;
    type SymbolFuture = S::SymbolFuture;
    type ExecuteFuture = S::ExecuteFuture;
    type CompletionFuture = S::CompletionFuture;
    type HoverFuture = S::HoverFuture;
    type DeclarationFuture = S::DeclarationFuture;
    type DefinitionFuture = S::DefinitionFuture;
    type TypeDefinitionFuture = S::TypeDefinitionFuture;
    type HighlightFuture = S::HighlightFuture;

    fn initialize(&self, printer: &Printer, params: InitializeParams) -> Result<InitializeResult> {
        (**self).initialize(printer, params)
    }

    fn initialized(&self, printer: &Printer, params: InitializedParams) {
        (**self).initialized(printer, params);
    }

    fn shutdown(&self) -> Self::ShutdownFuture {
        (**self).shutdown()
    }

    fn did_change_workspace_folders(&self, p: &Printer, params: DidChangeWorkspaceFoldersParams) {
        (**self).did_change_workspace_folders(p, params);
    }

    fn did_change_configuration(&self, printer: &Printer, params: DidChangeConfigurationParams) {
        (**self).did_change_configuration(printer, params);
    }

    fn did_change_watched_files(&self, printer: &Printer, params: DidChangeWatchedFilesParams) {
        (**self).did_change_watched_files(printer, params);
    }

    fn symbol(&self, params: WorkspaceSymbolParams) -> Self::SymbolFuture {
        (**self).symbol(params)
    }

    fn execute_command(&self, p: &Printer, params: ExecuteCommandParams) -> Self::ExecuteFuture {
        (**self).execute_command(p, params)
    }

    fn completion(&self, params: CompletionParams) -> Self::CompletionFuture {
        (**self).completion(params)
    }

    fn did_open(&self, printer: &Printer, params: DidOpenTextDocumentParams) {
        (**self).did_open(printer, params);
    }

    fn did_change(&self, printer: &Printer, params: DidChangeTextDocumentParams) {
        (**self).did_change(printer, params);
    }

    fn did_save(&self, printer: &Printer, params: DidSaveTextDocumentParams) {
        (**self).did_save(printer, params);
    }

    fn did_close(&self, printer: &Printer, params: DidCloseTextDocumentParams) {
        (**self).did_close(printer, params);
    }

    fn hover(&self, params: TextDocumentPositionParams) -> Self::HoverFuture {
        (**self).hover(params)
    }

    fn goto_declaration(&self, params: TextDocumentPositionParams) -> Self::DeclarationFuture {
        (**self).goto_declaration(params)
    }

    fn goto_definition(&self, params: TextDocumentPositionParams) -> Self::DefinitionFuture {
        (**self).goto_definition(params)
    }

    fn goto_type_definition(
        &self,
        params: TextDocumentPositionParams,
    ) -> Self::TypeDefinitionFuture {
        (**self).goto_type_definition(params)
    }

    fn document_highlight(&self, params: TextDocumentPositionParams) -> Self::HighlightFuture {
        (**self).document_highlight(params)
    }
}