diff --git a/cli/lsp/language_server.rs b/cli/lsp/language_server.rs index c1e3ac8d5e..0a9d81bf39 100644 --- a/cli/lsp/language_server.rs +++ b/cli/lsp/language_server.rs @@ -84,6 +84,11 @@ impl LanguageServer { } } + fn enabled(&self) -> bool { + let config = self.config.read().unwrap(); + config.settings.enable + } + pub async fn update_import_map(&self) -> Result<(), AnyError> { let (maybe_import_map, maybe_root_uri) = { let config = self.config.read().unwrap(); @@ -217,7 +222,7 @@ impl LanguageServer { } else { vec![] }; - if settings.enable { + if self.enabled() { diagnostics.extend( diagnostics_collection .diagnostics_for(file_id, DiagnosticSource::TypeScript) @@ -570,6 +575,9 @@ impl lspower::LanguageServer for LanguageServer { } async fn hover(&self, params: HoverParams) -> LSPResult> { + if !self.enabled() { + return Ok(None); + } let specifier = utils::normalize_url( params.text_document_position_params.text_document.uri, ); @@ -598,6 +606,9 @@ impl lspower::LanguageServer for LanguageServer { &self, params: DocumentHighlightParams, ) -> LSPResult>> { + if !self.enabled() { + return Ok(None); + } let specifier = utils::normalize_url( params.text_document_position_params.text_document.uri, ); @@ -635,6 +646,9 @@ impl lspower::LanguageServer for LanguageServer { &self, params: ReferenceParams, ) -> LSPResult>> { + if !self.enabled() { + return Ok(None); + } let specifier = utils::normalize_url(params.text_document_position.text_document.uri); // TODO(lucacasonato): handle error correctly @@ -673,6 +687,9 @@ impl lspower::LanguageServer for LanguageServer { &self, params: GotoDefinitionParams, ) -> LSPResult> { + if !self.enabled() { + return Ok(None); + } let specifier = utils::normalize_url( params.text_document_position_params.text_document.uri, ); @@ -706,6 +723,9 @@ impl lspower::LanguageServer for LanguageServer { &self, params: CompletionParams, ) -> LSPResult> { + if !self.enabled() { + return Ok(None); + } let specifier = utils::normalize_url(params.text_document_position.text_document.uri); // TODO(lucacasonato): handle error correctly @@ -978,4 +998,20 @@ mod tests { ]); harness.run().await; } + + #[tokio::test] + async fn test_hover_disabled() { + let mut harness = LspTestHarness::new(vec![ + ("initialize_request_disabled.json", LspResponse::RequestAny), + ("initialized_notification.json", LspResponse::None), + ("did_open_notification.json", LspResponse::None), + ("hover_request.json", LspResponse::Request(2, json!(null))), + ( + "shutdown_request.json", + LspResponse::Request(3, json!(null)), + ), + ("exit_notification.json", LspResponse::None), + ]); + harness.run().await; + } } diff --git a/cli/tests/lsp/initialize_request.json b/cli/tests/lsp/initialize_request.json index 960420bfd3..722a3c783f 100644 --- a/cli/tests/lsp/initialize_request.json +++ b/cli/tests/lsp/initialize_request.json @@ -9,6 +9,12 @@ "version": "1.0.0" }, "rootUri": null, + "initializationOptions": { + "enable": true, + "lint": true, + "importMap": null, + "unstable": false + }, "capabilities": { "textDocument": { "synchronization": { diff --git a/cli/tests/lsp/initialize_request_disabled.json b/cli/tests/lsp/initialize_request_disabled.json new file mode 100644 index 0000000000..f763375f88 --- /dev/null +++ b/cli/tests/lsp/initialize_request_disabled.json @@ -0,0 +1,29 @@ +{ + "jsonrpc": "2.0", + "id": 1, + "method": "initialize", + "params": { + "processId": 0, + "clientInfo": { + "name": "test-harness", + "version": "1.0.0" + }, + "rootUri": null, + "initializationOptions": { + "enable": false, + "lint": true, + "importMap": null, + "unstable": false + }, + "capabilities": { + "textDocument": { + "synchronization": { + "dynamicRegistration": true, + "willSave": true, + "willSaveWaitUntil": true, + "didSave": true + } + } + } + } +}