mirror of
https://github.com/zed-industries/zed.git
synced 2026-12-04 02:33:21 +01:00
Add autocomplete for initialization_options (#43104)
Closes #18287 Release Notes: - Added autocomplete for lsp initialization_options ## Description This MR adds the following code-changes: - `initialization_options_schema` to the `LspAdapter` to get JSON Schema's from the language server - Adds a post-processing step to inject schema request paths into the settings schema in `SettingsStore::json_schema` - Adds an implementation for fetching the schema for rust-analyzer which fetches it from the binary it is provided with - Similarly for ruff <img width="857" height="836" alt="image" src="https://github.com/user-attachments/assets/3cc10883-364f-4f04-b3b9-3c3881f64252" /> ## Open Questions(Would be nice to get some advice here) - Binary Fetching: - I'm pretty sure the binary fetching is suboptimal. The main problem here was getting access to the delegate but i figured that out eventually in a way that i _hope_ should be fine. - The toolchain and binary options can differ from what the user has configured potentially leading to mismatches in the autocomplete values returned(these are probably rarely changed though). I could not really find a way to fetch these in this context so the provided ones are for now just `default` values. - For the trait API it is just provided a binary, since i wanted to use the potentially cached binary from the CachedLspAdapter. Is that fine our should the arguments be passed to the LspAdapter such that it can potentially download the LSP? - As for those LSPs with JSON schema files in their repositories i can add the files to zed manually e.g. in languages/language/initialization_options_schema.json, which could cause mismatches with the actual binary. Is there a preferred approach for Zed here also with regards to updating them?
This commit is contained in:
1 parent
213cb30445
commit
83449293b6
14 files changed
+707
-53
No files matched your search
@@ -26,6 +26,7 @@ use settings::Settings;
|
||||
use smol::lock::OnceCell;
|
||||
use std::cmp::{Ordering, Reverse};
|
||||
use std::env::consts;
|
||||
use std::process::Stdio;
|
||||
use terminal::terminal_settings::TerminalSettings;
|
||||
use util::command::new_smol_command;
|
||||
use util::fs::{make_file_executable, remove_matching};
|
||||
@@ -2173,6 +2174,119 @@ pub(crate) struct RuffLspAdapter {
|
||||
fs: Arc<dyn Fs>,
|
||||
}
|
||||
|
||||
impl RuffLspAdapter {
|
||||
fn convert_ruff_schema(raw_schema: &serde_json::Value) -> serde_json::Value {
|
||||
let Some(schema_object) = raw_schema.as_object() else {
|
||||
return raw_schema.clone();
|
||||
};
|
||||
|
||||
let mut root_properties = serde_json::Map::new();
|
||||
|
||||
for (key, value) in schema_object {
|
||||
let parts: Vec<&str> = key.split('.').collect();
|
||||
|
||||
if parts.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut current = &mut root_properties;
|
||||
|
||||
for (i, part) in parts.iter().enumerate() {
|
||||
let is_last = i == parts.len() - 1;
|
||||
|
||||
if is_last {
|
||||
let mut schema_entry = serde_json::Map::new();
|
||||
|
||||
if let Some(doc) = value.get("doc").and_then(|d| d.as_str()) {
|
||||
schema_entry.insert(
|
||||
"markdownDescription".to_string(),
|
||||
serde_json::Value::String(doc.to_string()),
|
||||
);
|
||||
}
|
||||
|
||||
if let Some(default_val) = value.get("default") {
|
||||
schema_entry.insert("default".to_string(), default_val.clone());
|
||||
}
|
||||
|
||||
if let Some(value_type) = value.get("value_type").and_then(|v| v.as_str()) {
|
||||
if value_type.contains('|') {
|
||||
let enum_values: Vec<serde_json::Value> = value_type
|
||||
.split('|')
|
||||
.map(|s| s.trim().trim_matches('"'))
|
||||
.filter(|s| !s.is_empty())
|
||||
.map(|s| serde_json::Value::String(s.to_string()))
|
||||
.collect();
|
||||
|
||||
if !enum_values.is_empty() {
|
||||
schema_entry
|
||||
.insert("type".to_string(), serde_json::json!("string"));
|
||||
schema_entry.insert(
|
||||
"enum".to_string(),
|
||||
serde_json::Value::Array(enum_values),
|
||||
);
|
||||
}
|
||||
} else if value_type.starts_with("list[") {
|
||||
schema_entry.insert("type".to_string(), serde_json::json!("array"));
|
||||
if let Some(item_type) = value_type
|
||||
.strip_prefix("list[")
|
||||
.and_then(|s| s.strip_suffix(']'))
|
||||
{
|
||||
let json_type = match item_type {
|
||||
"str" => "string",
|
||||
"int" => "integer",
|
||||
"bool" => "boolean",
|
||||
_ => "string",
|
||||
};
|
||||
schema_entry.insert(
|
||||
"items".to_string(),
|
||||
serde_json::json!({"type": json_type}),
|
||||
);
|
||||
}
|
||||
} else if value_type.starts_with("dict[") {
|
||||
schema_entry.insert("type".to_string(), serde_json::json!("object"));
|
||||
} else {
|
||||
let json_type = match value_type {
|
||||
"bool" => "boolean",
|
||||
"int" | "usize" => "integer",
|
||||
"str" => "string",
|
||||
_ => "string",
|
||||
};
|
||||
schema_entry.insert(
|
||||
"type".to_string(),
|
||||
serde_json::Value::String(json_type.to_string()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
current.insert(part.to_string(), serde_json::Value::Object(schema_entry));
|
||||
} else {
|
||||
let next_current = current
|
||||
.entry(part.to_string())
|
||||
.or_insert_with(|| {
|
||||
serde_json::json!({
|
||||
"type": "object",
|
||||
"properties": {}
|
||||
})
|
||||
})
|
||||
.as_object_mut()
|
||||
.expect("should be an object")
|
||||
.entry("properties")
|
||||
.or_insert_with(|| serde_json::json!({}))
|
||||
.as_object_mut()
|
||||
.expect("properties should be an object");
|
||||
|
||||
current = next_current;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
serde_json::json!({
|
||||
"type": "object",
|
||||
"properties": root_properties
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
impl RuffLspAdapter {
|
||||
const GITHUB_ASSET_KIND: AssetKind = AssetKind::TarGz;
|
||||
@@ -2225,6 +2339,36 @@ impl LspAdapter for RuffLspAdapter {
|
||||
fn name(&self) -> LanguageServerName {
|
||||
Self::SERVER_NAME
|
||||
}
|
||||
|
||||
async fn initialization_options_schema(
|
||||
self: Arc<Self>,
|
||||
language_server_binary: &LanguageServerBinary,
|
||||
) -> Option<serde_json::Value> {
|
||||
let mut command = util::command::new_smol_command(&language_server_binary.path);
|
||||
command
|
||||
.args(&["config", "--output-format", "json"])
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped());
|
||||
let cmd = command
|
||||
.spawn()
|
||||
.map_err(|e| log::debug!("failed to spawn command {command:?}: {e}"))
|
||||
.ok()?;
|
||||
let output = cmd
|
||||
.output()
|
||||
.await
|
||||
.map_err(|e| log::debug!("failed to execute command {command:?}: {e}"))
|
||||
.ok()?;
|
||||
if !output.status.success() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let raw_schema: serde_json::Value = serde_json::from_slice(output.stdout.as_slice())
|
||||
.map_err(|e| log::debug!("failed to parse ruff's JSON schema output: {e}"))
|
||||
.ok()?;
|
||||
|
||||
let converted_schema = Self::convert_ruff_schema(&raw_schema);
|
||||
Some(converted_schema)
|
||||
}
|
||||
}
|
||||
|
||||
impl LspInstaller for RuffLspAdapter {
|
||||
@@ -2568,4 +2712,149 @@ mod tests {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_convert_ruff_schema() {
|
||||
use super::RuffLspAdapter;
|
||||
|
||||
let raw_schema = serde_json::json!({
|
||||
"line-length": {
|
||||
"doc": "The line length to use when enforcing long-lines violations",
|
||||
"default": "88",
|
||||
"value_type": "int",
|
||||
"scope": null,
|
||||
"example": "line-length = 120",
|
||||
"deprecated": null
|
||||
},
|
||||
"lint.select": {
|
||||
"doc": "A list of rule codes or prefixes to enable",
|
||||
"default": "[\"E4\", \"E7\", \"E9\", \"F\"]",
|
||||
"value_type": "list[RuleSelector]",
|
||||
"scope": null,
|
||||
"example": "select = [\"E4\", \"E7\", \"E9\", \"F\", \"B\", \"Q\"]",
|
||||
"deprecated": null
|
||||
},
|
||||
"lint.isort.case-sensitive": {
|
||||
"doc": "Sort imports taking into account case sensitivity.",
|
||||
"default": "false",
|
||||
"value_type": "bool",
|
||||
"scope": null,
|
||||
"example": "case-sensitive = true",
|
||||
"deprecated": null
|
||||
},
|
||||
"format.quote-style": {
|
||||
"doc": "Configures the preferred quote character for strings.",
|
||||
"default": "\"double\"",
|
||||
"value_type": "\"double\" | \"single\" | \"preserve\"",
|
||||
"scope": null,
|
||||
"example": "quote-style = \"single\"",
|
||||
"deprecated": null
|
||||
}
|
||||
});
|
||||
|
||||
let converted = RuffLspAdapter::convert_ruff_schema(&raw_schema);
|
||||
|
||||
assert!(converted.is_object());
|
||||
assert_eq!(
|
||||
converted.get("type").and_then(|v| v.as_str()),
|
||||
Some("object")
|
||||
);
|
||||
|
||||
let properties = converted
|
||||
.get("properties")
|
||||
.expect("should have properties")
|
||||
.as_object()
|
||||
.expect("properties should be an object");
|
||||
|
||||
assert!(properties.contains_key("line-length"));
|
||||
assert!(properties.contains_key("lint"));
|
||||
assert!(properties.contains_key("format"));
|
||||
|
||||
let line_length = properties
|
||||
.get("line-length")
|
||||
.expect("should have line-length")
|
||||
.as_object()
|
||||
.expect("line-length should be an object");
|
||||
|
||||
assert_eq!(
|
||||
line_length.get("type").and_then(|v| v.as_str()),
|
||||
Some("integer")
|
||||
);
|
||||
assert_eq!(
|
||||
line_length.get("default").and_then(|v| v.as_str()),
|
||||
Some("88")
|
||||
);
|
||||
|
||||
let lint = properties
|
||||
.get("lint")
|
||||
.expect("should have lint")
|
||||
.as_object()
|
||||
.expect("lint should be an object");
|
||||
|
||||
let lint_props = lint
|
||||
.get("properties")
|
||||
.expect("lint should have properties")
|
||||
.as_object()
|
||||
.expect("lint properties should be an object");
|
||||
|
||||
assert!(lint_props.contains_key("select"));
|
||||
assert!(lint_props.contains_key("isort"));
|
||||
|
||||
let select = lint_props.get("select").expect("should have select");
|
||||
assert_eq!(select.get("type").and_then(|v| v.as_str()), Some("array"));
|
||||
|
||||
let isort = lint_props
|
||||
.get("isort")
|
||||
.expect("should have isort")
|
||||
.as_object()
|
||||
.expect("isort should be an object");
|
||||
|
||||
let isort_props = isort
|
||||
.get("properties")
|
||||
.expect("isort should have properties")
|
||||
.as_object()
|
||||
.expect("isort properties should be an object");
|
||||
|
||||
let case_sensitive = isort_props
|
||||
.get("case-sensitive")
|
||||
.expect("should have case-sensitive");
|
||||
|
||||
assert_eq!(
|
||||
case_sensitive.get("type").and_then(|v| v.as_str()),
|
||||
Some("boolean")
|
||||
);
|
||||
assert!(case_sensitive.get("markdownDescription").is_some());
|
||||
|
||||
let format = properties
|
||||
.get("format")
|
||||
.expect("should have format")
|
||||
.as_object()
|
||||
.expect("format should be an object");
|
||||
|
||||
let format_props = format
|
||||
.get("properties")
|
||||
.expect("format should have properties")
|
||||
.as_object()
|
||||
.expect("format properties should be an object");
|
||||
|
||||
let quote_style = format_props
|
||||
.get("quote-style")
|
||||
.expect("should have quote-style");
|
||||
|
||||
assert_eq!(
|
||||
quote_style.get("type").and_then(|v| v.as_str()),
|
||||
Some("string")
|
||||
);
|
||||
|
||||
let enum_values = quote_style
|
||||
.get("enum")
|
||||
.expect("should have enum")
|
||||
.as_array()
|
||||
.expect("enum should be an array");
|
||||
|
||||
assert_eq!(enum_values.len(), 3);
|
||||
assert!(enum_values.contains(&serde_json::json!("double")));
|
||||
assert!(enum_values.contains(&serde_json::json!("single")));
|
||||
assert!(enum_values.contains(&serde_json::json!("preserve")));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user