diff --git a/crates/lsp/src/lsp.rs b/crates/lsp/src/lsp.rs index ddb87f806f..5c51a5c120 100644 --- a/crates/lsp/src/lsp.rs +++ b/crates/lsp/src/lsp.rs @@ -658,6 +658,10 @@ impl LanguageServer { }), ..SignatureHelpClientCapabilities::default() }), + synchronization: Some(TextDocumentSyncClientCapabilities { + did_save: Some(true), + ..TextDocumentSyncClientCapabilities::default() + }), ..TextDocumentClientCapabilities::default() }), experimental: Some(json!({ diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs index c3d30b9ecf..241dc1cc43 100644 --- a/crates/project/src/project.rs +++ b/crates/project/src/project.rs @@ -2547,15 +2547,21 @@ impl Project { }; for (_, _, server) in self.language_servers_for_worktree(worktree_id) { - let text = include_text(server.as_ref()).then(|| buffer.read(cx).text()); - server - .notify::( - lsp::DidSaveTextDocumentParams { - text_document: text_document.clone(), - text, - }, - ) - .log_err(); + if let Some(include_text) = include_text(server.as_ref()) { + let text = if include_text { + Some(buffer.read(cx).text()) + } else { + None + }; + server + .notify::( + lsp::DidSaveTextDocumentParams { + text_document: text_document.clone(), + text, + }, + ) + .log_err(); + } } for language_server_id in self.language_server_ids_for_buffer(buffer.read(cx), cx) { @@ -11179,20 +11185,27 @@ impl Completion { } } -fn include_text(server: &lsp::LanguageServer) -> bool { - server - .capabilities() - .text_document_sync - .as_ref() - .and_then(|sync| match sync { - lsp::TextDocumentSyncCapability::Kind(_) => None, - lsp::TextDocumentSyncCapability::Options(options) => options.save.as_ref(), - }) - .and_then(|save_options| match save_options { - lsp::TextDocumentSyncSaveOptions::Supported(_) => None, - lsp::TextDocumentSyncSaveOptions::SaveOptions(options) => options.include_text, - }) - .unwrap_or(false) +fn include_text(server: &lsp::LanguageServer) -> Option { + match server.capabilities().text_document_sync.as_ref()? { + lsp::TextDocumentSyncCapability::Kind(kind) => match kind { + &lsp::TextDocumentSyncKind::NONE => None, + &lsp::TextDocumentSyncKind::FULL => Some(true), + &lsp::TextDocumentSyncKind::INCREMENTAL => Some(false), + _ => None, + }, + lsp::TextDocumentSyncCapability::Options(options) => match options.save.as_ref()? { + lsp::TextDocumentSyncSaveOptions::Supported(supported) => { + if *supported { + Some(true) + } else { + None + } + } + lsp::TextDocumentSyncSaveOptions::SaveOptions(save_options) => { + Some(save_options.include_text.unwrap_or(false)) + } + }, + } } async fn load_direnv_environment(dir: &Path) -> Result>> { diff --git a/crates/project/src/project_tests.rs b/crates/project/src/project_tests.rs index 1c64c08345..d0961609d3 100644 --- a/crates/project/src/project_tests.rs +++ b/crates/project/src/project_tests.rs @@ -322,6 +322,12 @@ async fn test_managing_language_servers(cx: &mut gpui::TestAppContext) { trigger_characters: Some(vec![".".to_string(), "::".to_string()]), ..Default::default() }), + text_document_sync: Some(lsp::TextDocumentSyncCapability::Options( + lsp::TextDocumentSyncOptions { + save: Some(lsp::TextDocumentSyncSaveOptions::Supported(true)), + ..Default::default() + }, + )), ..Default::default() }, ..Default::default() @@ -336,6 +342,12 @@ async fn test_managing_language_servers(cx: &mut gpui::TestAppContext) { trigger_characters: Some(vec![":".to_string()]), ..Default::default() }), + text_document_sync: Some(lsp::TextDocumentSyncCapability::Options( + lsp::TextDocumentSyncOptions { + save: Some(lsp::TextDocumentSyncSaveOptions::Supported(true)), + ..Default::default() + }, + )), ..Default::default() }, ..Default::default()