From 9999862016e5fdc345e6abd8ac967e58ec172022 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 3 Mar 2022 14:16:56 -0800 Subject: [PATCH] Enable formatting feature of JSON language server The feature doesn't work yet because the JSON language server only supports *range* formatting, not document formatting. We need to adjust our code to inspect the server's capabilities and send range formatting requests instead when needed. We're going to hold off on doing this right now, because it will create merge conflicts with the `preserve-worktrees` branch (#525) Co-Authored-By: Nathan Sobo --- crates/language/src/language.rs | 16 ++++++++++++++-- crates/lsp/src/lsp.rs | 15 +++++++++++---- crates/zed/src/language.rs | 7 +++++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/crates/language/src/language.rs b/crates/language/src/language.rs index f3068b7792..ec525d1e4d 100644 --- a/crates/language/src/language.rs +++ b/crates/language/src/language.rs @@ -18,6 +18,7 @@ use highlight_map::HighlightMap; use lazy_static::lazy_static; use parking_lot::{Mutex, RwLock}; use serde::Deserialize; +use serde_json::Value; use std::{ cell::RefCell, ops::Range, @@ -78,9 +79,11 @@ pub trait LspAdapter: 'static + Send + Sync { ) -> BoxFuture<'static, Result>; fn cached_server_binary(&self, container_dir: PathBuf) -> BoxFuture<'static, Option>; fn process_diagnostics(&self, diagnostics: &mut lsp::PublishDiagnosticsParams); + fn label_for_completion(&self, _: &lsp::CompletionItem, _: &Language) -> Option { None } + fn label_for_symbol(&self, _: &str, _: lsp::SymbolKind, _: &Language) -> Option { None } @@ -88,6 +91,10 @@ pub trait LspAdapter: 'static + Send + Sync { fn server_args(&self) -> &[&str] { &[] } + + fn initialization_options(&self) -> Option { + None + } } #[derive(Clone, Debug, PartialEq, Eq)] @@ -291,8 +298,13 @@ impl LanguageRegistry { Some(cx.background().spawn(async move { let server_binary_path = server_binary_path.await?; let server_args = adapter.server_args(); - let server = - lsp::LanguageServer::new(&server_binary_path, server_args, &root_path, background)?; + let server = lsp::LanguageServer::new( + &server_binary_path, + server_args, + adapter.initialization_options(), + &root_path, + background, + )?; Ok(server) })) } diff --git a/crates/lsp/src/lsp.rs b/crates/lsp/src/lsp.rs index 890b6d2fb0..1a66d2bb5a 100644 --- a/crates/lsp/src/lsp.rs +++ b/crates/lsp/src/lsp.rs @@ -103,6 +103,7 @@ impl LanguageServer { pub fn new( binary_path: &Path, args: &[&str], + options: Option, root_path: &Path, background: Arc, ) -> Result> { @@ -115,13 +116,14 @@ impl LanguageServer { .spawn()?; let stdin = server.stdin.take().unwrap(); let stdout = server.stdout.take().unwrap(); - Self::new_internal(stdin, stdout, root_path, background) + Self::new_internal(stdin, stdout, root_path, options, background) } fn new_internal( stdin: Stdin, stdout: Stdout, root_path: &Path, + options: Option, executor: Arc, ) -> Result> where @@ -232,7 +234,7 @@ impl LanguageServer { .spawn({ let this = this.clone(); async move { - if let Some(capabilities) = this.init(root_uri).log_err().await { + if let Some(capabilities) = this.init(root_uri, options).log_err().await { *capabilities_tx.borrow_mut() = Some(capabilities); } @@ -244,13 +246,17 @@ impl LanguageServer { Ok(this) } - async fn init(self: Arc, root_uri: Url) -> Result { + async fn init( + self: Arc, + root_uri: Url, + options: Option, + ) -> Result { #[allow(deprecated)] let params = InitializeParams { process_id: Default::default(), root_path: Default::default(), root_uri: Some(root_uri), - initialization_options: Default::default(), + initialization_options: options, capabilities: ClientCapabilities { text_document: Some(TextDocumentClientCapabilities { definition: Some(GotoCapability { @@ -530,6 +536,7 @@ impl LanguageServer { stdin_writer, stdout_reader, Path::new("/"), + None, cx.background().clone(), ) .unwrap(); diff --git a/crates/zed/src/language.rs b/crates/zed/src/language.rs index 0e5adb78fc..7b5d0de8b4 100644 --- a/crates/zed/src/language.rs +++ b/crates/zed/src/language.rs @@ -7,6 +7,7 @@ use lazy_static::lazy_static; use regex::Regex; use rust_embed::RustEmbed; use serde::Deserialize; +use serde_json::json; use smol::fs::{self, File}; use std::{borrow::Cow, env::consts, path::PathBuf, str, sync::Arc}; use util::{ResultExt, TryFutureExt}; @@ -522,6 +523,12 @@ impl LspAdapter for JsonLspAdapter { } fn process_diagnostics(&self, _: &mut lsp::PublishDiagnosticsParams) {} + + fn initialization_options(&self) -> Option { + Some(json!({ + "provideFormatter": true + })) + } } pub fn build_language_registry() -> LanguageRegistry {