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 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
//! Language Server Protocol (LSP) server abstraction for [Tower]. //! //! [Tower]: https://github.com/tower-rs/tower //! //! # Example //! //! ```rust //! # use std::future::Future; //! # //! # use jsonrpc_core::Result; //! # use serde_json::Value; //! # use tower_lsp::lsp_types::request::{GotoDefinitionResponse, GotoImplementationResponse}; //! # use tower_lsp::lsp_types::*; //! # use tower_lsp::{LanguageServer, LspService, Printer, Server}; //! # //! #[derive(Debug, Default)] //! struct Backend; //! //! #[tower_lsp::async_trait] //! impl LanguageServer for Backend { //! fn initialize(&self, _: &Printer, _: InitializeParams) -> Result<InitializeResult> { //! Ok(InitializeResult::default()) //! } //! //! fn initialized(&self, printer: &Printer, _: InitializedParams) { //! printer.log_message(MessageType::Info, "server initialized!"); //! } //! //! async fn shutdown(&self) -> Result<()> { //! Ok(()) //! } //! //! async fn completion(&self, _: CompletionParams) -> Result<Option<CompletionResponse>> { //! Ok(Some(CompletionResponse::Array(vec![ //! CompletionItem::new_simple("Hello".to_string(), "Some detail".to_string()), //! CompletionItem::new_simple("Bye".to_string(), "More detail".to_string()) //! ]))) //! } //! //! async fn hover(&self, _: TextDocumentPositionParams) -> Result<Option<Hover>> { //! Ok(Some(Hover { //! contents: HoverContents::Scalar( //! MarkedString::String("You're hovering!".to_string()) //! ), //! range: None //! })) //! } //! } //! //! #[tokio::main] //! async 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); //! //! handle.run_until_exit(server).await; //! } //! ``` #![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; /// A re-export of [`async-trait`](https://docs.rs/async-trait) for convenience. pub use async_trait::async_trait; use jsonrpc_core::{Error, Result}; use log::{error, warn}; use lsp_types::request::{GotoDefinitionResponse, GotoImplementationResponse}; 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/ #[async_trait] pub trait LanguageServer: Send + Sync + 'static { /// 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 async fn shutdown(&self) -> Result<()>; /// 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; warn!("Got a workspace/didChangeWorkspaceFolders notification, but it is not implemented"); } /// 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; warn!("Got a workspace/didChangeConfiguration notification, but it is not implemented"); } /// 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; warn!("Got a workspace/didChangeWatchedFiles notification, but it is not implemented"); } /// 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 async fn symbol( &self, params: WorkspaceSymbolParams, ) -> Result<Option<Vec<SymbolInformation>>> { let _ = params; error!("Got a workspace/symbol request, but it is not implemented"); Err(Error::method_not_found()) } /// 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 async fn execute_command( &self, p: &Printer, params: ExecuteCommandParams, ) -> Result<Option<Value>> { let _ = p; let _ = params; error!("Got a workspace/executeCommand request, but it is not implemented"); Err(Error::method_not_found()) } /// 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; warn!("Got a textDocument/didOpen notification, but it is not implemented"); } /// 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; warn!("Got a textDocument/didChange notification, but it is not implemented"); } /// 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; warn!("Got a textDocument/didSave notification, but it is not implemented"); } /// 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; warn!("Got a textDocument/didClose notification, but it is not implemented"); } /// 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 async fn completion(&self, params: CompletionParams) -> Result<Option<CompletionResponse>> { let _ = params; error!("Got a textDocument/completion request, but it is not implemented"); Err(Error::method_not_found()) } /// 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 async fn hover(&self, params: TextDocumentPositionParams) -> Result<Option<Hover>> { let _ = params; error!("Got a textDocument/hover request, but it is not implemented"); Err(Error::method_not_found()) } /// The [`textDocument/signatureHelp`] request is sent from the client to the server to request /// signature information at a given cursor position. /// /// [`textDocument/signatureHelp`]: https://microsoft.github.io/language-server-protocol/specifications/specification-3-15/#textDocument_signatureHelp async fn signature_help( &self, params: TextDocumentPositionParams, ) -> Result<Option<SignatureHelp>> { let _ = params; error!("Got a textDocument/signatureHelp request, but it is not implemented"); Err(Error::method_not_found()) } /// 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::declaration::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 async fn goto_declaration( &self, params: TextDocumentPositionParams, ) -> Result<Option<GotoDefinitionResponse>> { let _ = params; error!("Got a textDocument/declaration request, but it is not implemented"); Err(Error::method_not_found()) } /// 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 async fn goto_definition( &self, params: TextDocumentPositionParams, ) -> Result<Option<GotoDefinitionResponse>> { let _ = params; error!("Got a textDocument/definition request, but it is not implemented"); Err(Error::method_not_found()) } /// 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::type_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 async fn goto_type_definition( &self, params: TextDocumentPositionParams, ) -> Result<Option<GotoDefinitionResponse>> { let _ = params; error!("Got a textDocument/typeDefinition request, but it is not implemented"); Err(Error::method_not_found()) } /// The [`textDocument/implementation`] request is sent from the client to the server to resolve /// the implementation location of a symbol at a given text document position. /// /// The result type [`GotoImplementationResponse::Link`] got introduced with 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::implementation::link_support /// ``` /// /// [`textDocument/implementation`]: https://microsoft.github.io/language-server-protocol/specifications/specification-3-15/#textDocument_implementation /// [`GotoImplementationResponse::Link`]: https://docs.rs/lsp-types/0.63.1/lsp_types/request/enum.GotoDefinitionResponse.html /// [`initialize`]: #tymethod.initialize async fn goto_implementation( &self, params: TextDocumentPositionParams, ) -> Result<Option<GotoImplementationResponse>> { let _ = params; error!("Got a textDocument/implementation request, but it is not implemented"); Err(Error::method_not_found()) } /// 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 async fn document_highlight( &self, params: TextDocumentPositionParams, ) -> Result<Option<Vec<DocumentHighlight>>> { let _ = params; error!("Got a textDocument/documentHighlight request, but it is not implemented"); Err(Error::method_not_found()) } } #[async_trait] impl<S: ?Sized + LanguageServer> LanguageServer for Box<S> { fn initialize(&self, printer: &Printer, params: InitializeParams) -> Result<InitializeResult> { (**self).initialize(printer, params) } fn initialized(&self, printer: &Printer, params: InitializedParams) { (**self).initialized(printer, params); } async fn shutdown(&self) -> Result<()> { (**self).shutdown().await } 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); } async fn symbol( &self, params: WorkspaceSymbolParams, ) -> Result<Option<Vec<SymbolInformation>>> { (**self).symbol(params).await } async fn execute_command( &self, p: &Printer, params: ExecuteCommandParams, ) -> Result<Option<Value>> { (**self).execute_command(p, params).await } 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); } async fn completion(&self, params: CompletionParams) -> Result<Option<CompletionResponse>> { (**self).completion(params).await } async fn hover(&self, params: TextDocumentPositionParams) -> Result<Option<Hover>> { (**self).hover(params).await } async fn signature_help( &self, params: TextDocumentPositionParams, ) -> Result<Option<SignatureHelp>> { (**self).signature_help(params).await } async fn goto_declaration( &self, params: TextDocumentPositionParams, ) -> Result<Option<GotoDefinitionResponse>> { (**self).goto_declaration(params).await } async fn goto_definition( &self, params: TextDocumentPositionParams, ) -> Result<Option<GotoDefinitionResponse>> { (**self).goto_definition(params).await } async fn goto_type_definition( &self, params: TextDocumentPositionParams, ) -> Result<Option<GotoDefinitionResponse>> { (**self).goto_type_definition(params).await } async fn goto_implementation( &self, params: TextDocumentPositionParams, ) -> Result<Option<GotoImplementationResponse>> { (**self).goto_implementation(params).await } async fn document_highlight( &self, params: TextDocumentPositionParams, ) -> Result<Option<Vec<DocumentHighlight>>> { (**self).document_highlight(params).await } }