Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Fix incorrect array merging in LSP initialization_options (#54950)

Benjamin Yed61edb0825ae
diff --git a/crates/languages/src/rust.rs b/crates/languages/src/rust.rs
index 16e0ff01fd5..dde5e0b6dda 100644
--- a/crates/languages/src/rust.rs
+++ b/crates/languages/src/rust.rs
@@ -692,31 +692,23 @@ impl LspAdapter for RustLspAdapter {
.get(&SERVER_NAME)
.is_some_and(|s| s.enable_lsp_tasks);
+ let mut commands = vec![
+ "rust-analyzer.showReferences",
+ "rust-analyzer.gotoLocation",
+ "rust-analyzer.triggerParameterHints",
+ "rust-analyzer.rename",
+ ];
+ if enable_lsp_tasks {
+ commands.push("rust-analyzer.runSingle");
+ }
+
let mut experimental = json!({
"commands": {
- "commands": [
- "rust-analyzer.showReferences",
- "rust-analyzer.gotoLocation",
- "rust-analyzer.triggerParameterHints",
- "rust-analyzer.rename",
- ]
+ "commands": commands,
}
});
-
if enable_lsp_tasks {
- merge_json_value_into(
- json!({
- "runnables": {
- "kinds": [ "cargo", "shell" ],
- },
- "commands": {
- "commands": [
- "rust-analyzer.runSingle",
- ]
- }
- }),
- &mut experimental,
- );
+ experimental["runnables"] = json!({ "kinds": ["cargo", "shell"] });
}
if let Some(original_experimental) = &mut original.capabilities.experimental {
diff --git a/crates/project/tests/integration/lsp_store.rs b/crates/project/tests/integration/lsp_store.rs
index 34f81fdbbe2..c43db41353e 100644
--- a/crates/project/tests/integration/lsp_store.rs
+++ b/crates/project/tests/integration/lsp_store.rs
@@ -1,5 +1,6 @@
use std::{
path::Path,
+ sync::Arc,
time::{Duration, Instant},
};
@@ -8,6 +9,7 @@ use futures::StreamExt;
use gpui::TestAppContext;
use language::{CodeLabel, FakeLspAdapter, HighlightId, rust_lang};
use lsp::Uri;
+use parking_lot::Mutex;
use project::{
Project,
lsp_store::{log_store::TestRpcRequestTracker, *},
@@ -291,3 +293,98 @@ fn test_trailing_newline_in_completion_documentation() {
CompletionDocumentation::SingleLine(s) if s == "some value"
));
}
+
+#[gpui::test]
+async fn test_user_initialization_options_override_adapter_arrays(cx: &mut TestAppContext) {
+ init_test(cx);
+
+ let user_settings = serde_json::json!({
+ "lsp": {
+ "the-fake-language-server": {
+ "initialization_options": {
+ "preview": {
+ "background": {
+ "enabled": true,
+ "args": ["--data-plane-host=127.0.0.1:23635", "--invert-colors=never"],
+ },
+ },
+ "plugins": ["user-plugin"],
+ "userOnly": ["user"],
+ },
+ },
+ },
+ });
+
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree(
+ path!("/the-root"),
+ json!({
+ ".zed": {
+ "settings.json": user_settings.to_string(),
+ },
+ "main.rs": "fn main() {}",
+ }),
+ )
+ .await;
+
+ let project = Project::test(fs, [path!("/the-root").as_ref()], cx).await;
+ let language_registry = project.read_with(cx, |project, _| project.languages().clone());
+ language_registry.add(rust_lang());
+
+ let sent_initialization_options = Arc::new(Mutex::new(None));
+ let mut fake_servers = language_registry.register_fake_lsp(
+ "Rust",
+ FakeLspAdapter {
+ name: "the-fake-language-server",
+ initialization_options: Some(json!({
+ "preview": {
+ "background": {
+ "args": ["--data-plane-host=127.0.0.1:23635", "--invert-colors=never"],
+ "partialRendering": true,
+ },
+ },
+ "plugins": ["default-plugin", "user-plugin"],
+ "adapterOnly": [1, 2],
+ })),
+ initializer: Some(Box::new({
+ let sent_initialization_options = sent_initialization_options.clone();
+ move |fake_server| {
+ let sent_initialization_options = sent_initialization_options.clone();
+ fake_server.set_request_handler::<lsp::request::Initialize, _, _>(
+ move |params, _| {
+ *sent_initialization_options.lock() = params.initialization_options;
+ async move { Ok(lsp::InitializeResult::default()) }
+ },
+ );
+ }
+ })),
+ ..FakeLspAdapter::default()
+ },
+ );
+ cx.run_until_parked();
+
+ project
+ .update(cx, |project, cx| {
+ project.open_local_buffer_with_lsp(path!("/the-root/main.rs"), cx)
+ })
+ .await
+ .unwrap();
+ fake_servers.next().await.unwrap();
+ cx.run_until_parked();
+
+ assert_eq!(
+ sent_initialization_options.lock().take(),
+ Some(json!({
+ "preview": {
+ "background": {
+ "enabled": true,
+ "args": ["--data-plane-host=127.0.0.1:23635", "--invert-colors=never"],
+ "partialRendering": true,
+ },
+ },
+ "plugins": ["user-plugin"],
+ "adapterOnly": [1, 2],
+ "userOnly": ["user"],
+ })),
+ );
+}
diff --git a/crates/util/src/util.rs b/crates/util/src/util.rs
index d4ebdfd5165..f86feca073f 100644
--- a/crates/util/src/util.rs
+++ b/crates/util/src/util.rs
@@ -452,6 +452,9 @@ pub fn merge_json_lenient_value_into(
}
}
+/// Merges `source` into `target`: objects are merged recursively, key by key;
+/// any other colliding value in `target` — including arrays — is replaced by
+/// `source`'s value wholesale.
pub fn merge_json_value_into(source: serde_json::Value, target: &mut serde_json::Value) {
use serde_json::Value;
@@ -465,13 +468,6 @@ pub fn merge_json_value_into(source: serde_json::Value, target: &mut serde_json:
}
}
}
-
- (Value::Array(source), Value::Array(target)) => {
- for value in source {
- target.push(value);
- }
- }
-
(source, target) => *target = source,
}
}
@@ -1085,4 +1081,60 @@ Line 3"#
assert_eq!(result[0], (0..6, "héllo")); // 'é' is 2 bytes
assert_eq!(result[1], (10..15, "world")); // '🦀' is 4 bytes
}
+
+ #[test]
+ fn test_merge_json_values() {
+ use serde_json::json;
+
+ let mut target = json!({
+ "unchanged": 1,
+ "replaced_scalar": "old",
+ "replaced_array": ["default-1", "default-2"],
+ "nulled": true,
+ "array_becomes_object": [1, 2],
+ "object_becomes_scalar": { "x": 1 },
+ "nested": {
+ "kept": true,
+ "overridden": 2,
+ "args": ["--default"],
+ "deeper": { "list": [1, 2], "other": "kept" },
+ },
+ });
+ let source = json!({
+ "replaced_scalar": "new",
+ "replaced_array": ["default-2", "user"],
+ "nulled": null,
+ "array_becomes_object": { "y": 2 },
+ "object_becomes_scalar": 3,
+ "inserted": ["brand-new"],
+ "nested": {
+ "overridden": 20,
+ "args": ["--user"],
+ "deeper": { "list": [3] },
+ "inserted": { "z": true },
+ },
+ });
+
+ merge_json_value_into(source, &mut target);
+
+ assert_eq!(
+ target,
+ json!({
+ "unchanged": 1,
+ "replaced_scalar": "new",
+ "replaced_array": ["default-2", "user"],
+ "nulled": null,
+ "array_becomes_object": { "y": 2 },
+ "object_becomes_scalar": 3,
+ "inserted": ["brand-new"],
+ "nested": {
+ "kept": true,
+ "overridden": 20,
+ "args": ["--user"],
+ "deeper": { "list": [3], "other": "kept" },
+ "inserted": { "z": true },
+ },
+ })
+ );
+ }
}