feat: full LSP (Language Server Protocol) integration
Implement complete LSP support for code intelligence tools: - lsp_transport.rs: JSON-RPC 2.0 transport over stdio with Content-Length framing, async request/response handling, and graceful shutdown - lsp_process.rs: LSP process manager with initialize handshake, and methods for hover, goto_definition, references, document_symbols, completion, format - lsp_discovery.rs: Auto-discovery of installed LSP servers (rust-analyzer, clangd, gopls, pyright, typescript-language-server, etc.) with PATH lookup - lsp_client.rs: Rewired LspRegistry to use real LSP processes instead of placeholder JSON, with lazy-start on first dispatch call - config.rs: Added LspServerConfig for user-configured LSP servers - config_validate.rs: Validation for lsp config section - main.rs: CLI integration with server discovery at startup, /lsp slash command for status/start/stop/restart, and graceful shutdown on exit - commands/src/lib.rs: Added SlashCommand::Lsp variant The LSP tool is now available to the agent for hover, definition, references, symbols, completion, and diagnostics queries. Servers are auto-discovered at REPL startup and lazily started on first use. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
parent
0d0055a39e
commit
3fadbf4412
|
|
@ -1301,11 +1301,15 @@ impl SlashCommand {
|
|||
Self::Tag { .. } => "/tag",
|
||||
Self::OutputStyle { .. } => "/output-style",
|
||||
Self::AddDir { .. } => "/add-dir",
|
||||
<<<<<<< HEAD
|
||||
<<<<<<< HEAD
|
||||
Self::Team { .. } => "/team",
|
||||
=======
|
||||
Self::Lsp { .. } => "/lsp",
|
||||
>>>>>>> 856409d3 (feat: full LSP (Language Server Protocol) integration)
|
||||
=======
|
||||
Self::Lsp { .. } => "/lsp",
|
||||
>>>>>>> e9582034 (feat: full LSP (Language Server Protocol) integration)
|
||||
Self::Sandbox => "/sandbox",
|
||||
Self::Mcp { .. } => "/mcp",
|
||||
Self::Export { .. } => "/export",
|
||||
|
|
@ -5412,12 +5416,18 @@ pub fn handle_slash_command(
|
|||
| SlashCommand::OutputStyle { .. }
|
||||
| SlashCommand::AddDir { .. }
|
||||
| SlashCommand::History { .. }
|
||||
<<<<<<< HEAD
|
||||
<<<<<<< HEAD
|
||||
| SlashCommand::Team { .. }
|
||||
=======
|
||||
| SlashCommand::Lsp { .. }
|
||||
>>>>>>> 856409d3 (feat: full LSP (Language Server Protocol) integration)
|
||||
| SlashCommand::Setup
|
||||
=======
|
||||
| SlashCommand::Lsp { .. }
|
||||
| SlashCommand::Setup
|
||||
| SlashCommand::Unknown(_) => None,
|
||||
>>>>>>> e9582034 (feat: full LSP (Language Server Protocol) integration)
|
||||
| SlashCommand::Unknown(_) => None,
|
||||
}
|
||||
}
|
||||
|
|
@ -6034,12 +6044,16 @@ mod tests {
|
|||
assert!(help.contains("aliases: /skill"));
|
||||
assert!(!help.contains("/login"));
|
||||
assert!(!help.contains("/logout"));
|
||||
<<<<<<< HEAD
|
||||
<<<<<<< HEAD
|
||||
assert!(help.contains("/setup"));
|
||||
assert_eq!(slash_command_specs().len(), 140);
|
||||
=======
|
||||
assert_eq!(slash_command_specs().len(), 141);
|
||||
>>>>>>> 856409d3 (feat: full LSP (Language Server Protocol) integration)
|
||||
=======
|
||||
assert_eq!(slash_command_specs().len(), 141);
|
||||
>>>>>>> e9582034 (feat: full LSP (Language Server Protocol) integration)
|
||||
assert!(resume_supported_slash_commands().len() >= 39);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -125,9 +125,12 @@ pub struct RuntimePluginConfig {
|
|||
max_output_tokens: Option<u32>,
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
<<<<<<< HEAD
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
=======
|
||||
>>>>>>> e9582034 (feat: full LSP (Language Server Protocol) integration)
|
||||
/// Per-language LSP server configuration supplied by the user in settings.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct LspServerConfig {
|
||||
|
|
@ -136,6 +139,7 @@ pub struct LspServerConfig {
|
|||
pub enabled: bool,
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
>>>>>>> 1ff5617c (fix: sync all bug fixes to combined branch)
|
||||
/// API timeout and retry configuration.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
|
|
@ -188,6 +192,8 @@ impl Default for ApiTimeoutConfig {
|
|||
>>>>>>> 07ce5aee (feat: API timeout config, Retry-After header support, and configurable retry)
|
||||
}
|
||||
|
||||
=======
|
||||
>>>>>>> e9582034 (feat: full LSP (Language Server Protocol) integration)
|
||||
/// Structured feature configuration consumed by runtime subsystems.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Default)]
|
||||
pub struct RuntimeFeatureConfig {
|
||||
|
|
@ -202,6 +208,7 @@ pub struct RuntimeFeatureConfig {
|
|||
sandbox: SandboxConfig,
|
||||
provider_fallbacks: ProviderFallbackConfig,
|
||||
trusted_roots: Vec<String>,
|
||||
<<<<<<< HEAD
|
||||
api_timeout: ApiTimeoutConfig,
|
||||
rules_import: RulesImportConfig,
|
||||
provider: RuntimeProviderConfig,
|
||||
|
|
@ -260,6 +267,13 @@ impl Default for RuntimeFeatureConfig {
|
|||
/// Represents the `provider` section in `~/.claw/settings.json`, used as a
|
||||
/// fallback when environment variables are absent (3-tier resolution:
|
||||
/// env var > .env file > stored config).
|
||||
=======
|
||||
provider: RuntimeProviderConfig,
|
||||
lsp: BTreeMap<String, LspServerConfig>,
|
||||
}
|
||||
|
||||
/// Stored provider configuration from the setup wizard.
|
||||
>>>>>>> e9582034 (feat: full LSP (Language Server Protocol) integration)
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Default)]
|
||||
pub struct RuntimeProviderConfig {
|
||||
kind: Option<String>,
|
||||
|
|
@ -736,6 +750,7 @@ impl ConfigLoader {
|
|||
provider: parse_optional_provider_config(&merged_value)?,
|
||||
lsp: parse_optional_lsp_config(&merged_value)?,
|
||||
<<<<<<< HEAD
|
||||
<<<<<<< HEAD
|
||||
>>>>>>> 856409d3 (feat: full LSP (Language Server Protocol) integration)
|
||||
=======
|
||||
lsp_auto_start: merged_value
|
||||
|
|
@ -745,6 +760,8 @@ impl ConfigLoader {
|
|||
.unwrap_or(true),
|
||||
api_timeout: parse_optional_api_timeout_config(&merged_value)?,
|
||||
>>>>>>> 07ce5aee (feat: API timeout config, Retry-After header support, and configurable retry)
|
||||
=======
|
||||
>>>>>>> e9582034 (feat: full LSP (Language Server Protocol) integration)
|
||||
};
|
||||
|
||||
ConfigInspection {
|
||||
|
|
@ -1004,15 +1021,19 @@ impl RuntimeConfig {
|
|||
}
|
||||
|
||||
#[must_use]
|
||||
<<<<<<< HEAD
|
||||
pub fn rules_import(&self) -> &RulesImportConfig {
|
||||
&self.feature_config.rules_import
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
=======
|
||||
>>>>>>> e9582034 (feat: full LSP (Language Server Protocol) integration)
|
||||
pub fn provider(&self) -> &RuntimeProviderConfig {
|
||||
&self.feature_config.provider
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
<<<<<<< HEAD
|
||||
/// Merge config-level default trusted roots with per-call roots.
|
||||
///
|
||||
|
|
@ -1028,6 +1049,11 @@ impl RuntimeConfig {
|
|||
pub fn lsp(&self) -> &BTreeMap<String, LspServerConfig> {
|
||||
&self.feature_config.lsp
|
||||
>>>>>>> 856409d3 (feat: full LSP (Language Server Protocol) integration)
|
||||
=======
|
||||
#[must_use]
|
||||
pub fn lsp(&self) -> &BTreeMap<String, LspServerConfig> {
|
||||
&self.feature_config.lsp
|
||||
>>>>>>> e9582034 (feat: full LSP (Language Server Protocol) integration)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1107,6 +1133,7 @@ impl RuntimeFeatureConfig {
|
|||
}
|
||||
|
||||
#[must_use]
|
||||
<<<<<<< HEAD
|
||||
pub fn rules_import(&self) -> &RulesImportConfig {
|
||||
&self.rules_import
|
||||
}
|
||||
|
|
@ -1128,11 +1155,20 @@ fn merge_trusted_roots(config_roots: &[String], per_call_roots: &[String]) -> Ve
|
|||
}
|
||||
merged
|
||||
=======
|
||||
=======
|
||||
pub fn provider(&self) -> &RuntimeProviderConfig {
|
||||
&self.provider
|
||||
}
|
||||
|
||||
>>>>>>> e9582034 (feat: full LSP (Language Server Protocol) integration)
|
||||
#[must_use]
|
||||
pub fn lsp(&self) -> &BTreeMap<String, LspServerConfig> {
|
||||
&self.lsp
|
||||
}
|
||||
<<<<<<< HEAD
|
||||
>>>>>>> 856409d3 (feat: full LSP (Language Server Protocol) integration)
|
||||
=======
|
||||
>>>>>>> e9582034 (feat: full LSP (Language Server Protocol) integration)
|
||||
}
|
||||
|
||||
impl ProviderFallbackConfig {
|
||||
|
|
@ -2321,6 +2357,7 @@ fn parse_optional_oauth_config(
|
|||
}))
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
/// #92: expand `${VAR}` environment variable references and `~/` home directory
|
||||
/// prefix in a config string value. Returns the expanded string.
|
||||
fn expand_config_value(value: &str) -> String {
|
||||
|
|
@ -2358,6 +2395,25 @@ fn expand_config_value(value: &str) -> String {
|
|||
}
|
||||
}
|
||||
result
|
||||
=======
|
||||
fn parse_optional_provider_config(root: &JsonValue) -> Result<RuntimeProviderConfig, ConfigError> {
|
||||
let Some(provider_value) = root.as_object().and_then(|object| object.get("provider")) else {
|
||||
return Ok(RuntimeProviderConfig::default());
|
||||
};
|
||||
let Some(object) = provider_value.as_object() else {
|
||||
return Ok(RuntimeProviderConfig::default());
|
||||
};
|
||||
let kind = optional_string(object, "kind", "provider")?.map(str::to_string);
|
||||
let api_key = optional_string(object, "apiKey", "provider")?.map(str::to_string);
|
||||
let base_url = optional_string(object, "baseUrl", "provider")?.map(str::to_string);
|
||||
let model = optional_string(object, "model", "provider")?.map(str::to_string);
|
||||
Ok(RuntimeProviderConfig {
|
||||
kind,
|
||||
api_key,
|
||||
base_url,
|
||||
model,
|
||||
})
|
||||
>>>>>>> e9582034 (feat: full LSP (Language Server Protocol) integration)
|
||||
}
|
||||
|
||||
fn parse_optional_lsp_config(
|
||||
|
|
|
|||
|
|
@ -213,6 +213,7 @@ const TOP_LEVEL_FIELDS: &[FieldSpec] = &[
|
|||
expected: FieldType::Object,
|
||||
},
|
||||
FieldSpec {
|
||||
<<<<<<< HEAD
|
||||
<<<<<<< HEAD
|
||||
name: "rulesImport",
|
||||
expected: FieldType::RulesImport,
|
||||
|
|
@ -228,6 +229,10 @@ const TOP_LEVEL_FIELDS: &[FieldSpec] = &[
|
|||
FieldSpec {
|
||||
name: "lspAutoStart",
|
||||
expected: FieldType::Bool,
|
||||
=======
|
||||
name: "lsp",
|
||||
expected: FieldType::Object,
|
||||
>>>>>>> e9582034 (feat: full LSP (Language Server Protocol) integration)
|
||||
},
|
||||
];
|
||||
|
||||
|
|
@ -605,6 +610,31 @@ pub fn validate_config_file(
|
|||
}
|
||||
}
|
||||
|
||||
// Validate lsp map: each value must be an object with LSP_FIELDS.
|
||||
if let Some(lsp) = object.get("lsp").and_then(JsonValue::as_object) {
|
||||
for (server_name, server_value) in lsp {
|
||||
if let Some(server_obj) = server_value.as_object() {
|
||||
result.merge(validate_object_keys(
|
||||
server_obj,
|
||||
LSP_FIELDS,
|
||||
&format!("lsp.{server_name}"),
|
||||
source,
|
||||
&path_display,
|
||||
));
|
||||
} else {
|
||||
result.errors.push(ConfigDiagnostic {
|
||||
path: path_display.clone(),
|
||||
field: format!("lsp.{server_name}"),
|
||||
line: find_key_line(source, server_name),
|
||||
kind: DiagnosticKind::WrongType {
|
||||
expected: "an object",
|
||||
got: json_type_label(server_value),
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -70,6 +70,7 @@ pub use compact::{
|
|||
};
|
||||
pub use config::{
|
||||
<<<<<<< HEAD
|
||||
<<<<<<< HEAD
|
||||
<<<<<<< HEAD
|
||||
clear_user_provider_settings, default_config_home, save_user_provider_settings,
|
||||
suppress_config_warnings_for_json_mode, ApiTimeoutConfig, ConfigEntry, ConfigError,
|
||||
|
|
@ -81,10 +82,13 @@ pub use config::{
|
|||
RuntimeInvalidHookConfig, RuntimePermissionRuleConfig, RuntimePluginConfig,
|
||||
RuntimeProviderConfig, ScopedMcpServerConfig, CLAW_SETTINGS_SCHEMA_NAME,
|
||||
=======
|
||||
=======
|
||||
>>>>>>> e9582034 (feat: full LSP (Language Server Protocol) integration)
|
||||
clear_user_provider_settings, save_user_provider_settings, ConfigEntry, ConfigError,
|
||||
ConfigLoader, ConfigSource, LspServerConfig, McpConfigCollection, McpManagedProxyServerConfig,
|
||||
McpOAuthConfig, McpRemoteServerConfig, McpSdkServerConfig, McpServerConfig,
|
||||
McpStdioServerConfig, McpTransport, McpWebSocketServerConfig, OAuthConfig,
|
||||
<<<<<<< HEAD
|
||||
ApiTimeoutConfig, ConfigEntry, ConfigError, ConfigLoader, ConfigSource, McpConfigCollection,
|
||||
=======
|
||||
ApiTimeoutConfig, clear_user_provider_settings, save_user_provider_settings, ConfigEntry,
|
||||
|
|
@ -92,6 +96,8 @@ pub use config::{
|
|||
>>>>>>> 1ff5617c (fix: sync all bug fixes to combined branch)
|
||||
McpManagedProxyServerConfig, McpOAuthConfig, McpRemoteServerConfig, McpSdkServerConfig,
|
||||
McpServerConfig, McpStdioServerConfig, McpTransport, McpWebSocketServerConfig, OAuthConfig,
|
||||
=======
|
||||
>>>>>>> e9582034 (feat: full LSP (Language Server Protocol) integration)
|
||||
ProviderFallbackConfig, ResolvedPermissionMode, RuntimeConfig, RuntimeFeatureConfig,
|
||||
RuntimeHookConfig, RuntimePermissionRuleConfig, RuntimePluginConfig, RuntimeProviderConfig,
|
||||
ScopedMcpServerConfig, CLAW_SETTINGS_SCHEMA_NAME,
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
|
|
@ -100,15 +100,19 @@ pub fn known_lsp_servers() -> Vec<LspServerDescriptor> {
|
|||
/// Check whether a command exists on the user's PATH by attempting to run it
|
||||
/// with `--version`. Returns `true` if the command could be spawned
|
||||
/// successfully, `false` otherwise.
|
||||
<<<<<<< HEAD
|
||||
///
|
||||
/// Some LSP servers (like rust-analyzer via rustup) exit non-zero on --version
|
||||
/// but are still functional. We treat "spawned successfully" as found, regardless
|
||||
/// of the exit code. Only a failure to spawn (command not found) returns false.
|
||||
=======
|
||||
>>>>>>> e9582034 (feat: full LSP (Language Server Protocol) integration)
|
||||
#[must_use]
|
||||
pub fn command_exists_on_path(command: &str) -> bool {
|
||||
Command::new(command)
|
||||
.arg("--version")
|
||||
.output()
|
||||
<<<<<<< HEAD
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
|
|
@ -133,21 +137,30 @@ fn rustup_component_works(component: &str) -> bool {
|
|||
.args(["run", "stable", component, "--version"])
|
||||
.output()
|
||||
.is_ok_and(|o| o.status.success())
|
||||
=======
|
||||
.map(|output| output.status.success())
|
||||
.unwrap_or(false)
|
||||
>>>>>>> e9582034 (feat: full LSP (Language Server Protocol) integration)
|
||||
}
|
||||
|
||||
/// Discover LSP servers that are actually installed on the current system.
|
||||
///
|
||||
/// Iterates over the known server table and returns only those whose command
|
||||
<<<<<<< HEAD
|
||||
/// is found on `PATH` **and** is actually functional. For `rust-analyzer`,
|
||||
/// rustup ships a stub proxy that always exists on PATH but prints
|
||||
/// "Unknown binary" when the component isn't installed. We detect that
|
||||
/// case and either rewrite to `rustup run stable rust-analyzer` (when the
|
||||
/// component is installed) or skip the server entirely (when it's not).
|
||||
=======
|
||||
/// is found on `PATH`.
|
||||
>>>>>>> e9582034 (feat: full LSP (Language Server Protocol) integration)
|
||||
#[must_use]
|
||||
pub fn discover_available_servers() -> Vec<LspServerDescriptor> {
|
||||
KNOWN_LSP_SERVERS_TABLE
|
||||
.iter()
|
||||
.filter(|desc| command_exists_on_path(desc.command))
|
||||
<<<<<<< HEAD
|
||||
.filter_map(|desc| {
|
||||
let mut server = desc.to_descriptor();
|
||||
// rustup ships a proxy `rust-analyzer` that exists on PATH but
|
||||
|
|
@ -170,6 +183,9 @@ pub fn discover_available_servers() -> Vec<LspServerDescriptor> {
|
|||
}
|
||||
Some(server)
|
||||
})
|
||||
=======
|
||||
.map(StaticLspServerDescriptor::to_descriptor)
|
||||
>>>>>>> e9582034 (feat: full LSP (Language Server Protocol) integration)
|
||||
.collect()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,794 @@
|
|||
//! LSP process manager: spawns language servers and drives the LSP lifecycle.
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
use serde_json::Value as JsonValue;
|
||||
|
||||
use crate::lsp_client::{
|
||||
LspCompletionItem, LspHoverResult, LspLocation, LspServerStatus, LspSymbol,
|
||||
};
|
||||
use crate::lsp_transport::{LspTransport, LspTransportError};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct LspProcess {
|
||||
transport: LspTransport,
|
||||
language: String,
|
||||
root_uri: String,
|
||||
capabilities: Option<JsonValue>,
|
||||
status: LspServerStatus,
|
||||
}
|
||||
|
||||
#[allow(clippy::cast_possible_truncation)]
|
||||
impl LspProcess {
|
||||
/// Spawn a language server process and perform the LSP initialize handshake.
|
||||
pub async fn start(
|
||||
command: &str,
|
||||
args: &[String],
|
||||
root_path: &Path,
|
||||
) -> Result<Self, LspProcessError> {
|
||||
let transport = LspTransport::spawn(command, args)
|
||||
.map_err(|e| LspProcessError::Transport(LspTransportError::Io(e)))?;
|
||||
|
||||
let canonical = canonicalize_root(root_path)?;
|
||||
let root_uri = format!("file://{canonical}");
|
||||
|
||||
let mut process = Self {
|
||||
transport,
|
||||
language: command.to_owned(),
|
||||
root_uri: root_uri.clone(),
|
||||
capabilities: None,
|
||||
status: LspServerStatus::Starting,
|
||||
};
|
||||
|
||||
process.initialize(&canonical).await?;
|
||||
process.status = LspServerStatus::Connected;
|
||||
|
||||
Ok(process)
|
||||
}
|
||||
|
||||
/// Send the LSP `initialize` request followed by the `initialized` notification.
|
||||
async fn initialize(&mut self, root_path: &str) -> Result<JsonValue, LspProcessError> {
|
||||
let root_uri = format!("file://{root_path}");
|
||||
let pid = std::process::id();
|
||||
|
||||
let params = serde_json::json!({
|
||||
"processId": pid,
|
||||
"rootUri": root_uri,
|
||||
"capabilities": {
|
||||
"textDocument": {
|
||||
"hover": { "contentFormat": ["markdown", "plaintext"] },
|
||||
"definition": { "linkSupport": true },
|
||||
"references": {},
|
||||
"completion": {
|
||||
"completionItem": { "snippetSupport": false }
|
||||
},
|
||||
"documentSymbol": { "hierarchicalDocumentSymbolSupport": true },
|
||||
"publishDiagnostics": { "relatedInformation": true }
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let response = self
|
||||
.transport
|
||||
.send_request("initialize", Some(params))
|
||||
.await
|
||||
.map_err(LspProcessError::Transport)?;
|
||||
|
||||
let result = response
|
||||
.into_result()
|
||||
.map_err(|e| LspProcessError::Transport(LspTransportError::JsonRpc(e)))?;
|
||||
|
||||
self.capabilities = Some(result.clone());
|
||||
|
||||
self.transport
|
||||
.send_notification("initialized", Some(serde_json::json!({})))
|
||||
.await
|
||||
.map_err(LspProcessError::Transport)?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Gracefully shut down the language server.
|
||||
pub async fn shutdown(&mut self) -> Result<(), LspProcessError> {
|
||||
self.status = LspServerStatus::Disconnected;
|
||||
|
||||
let shutdown_result = self
|
||||
.transport
|
||||
.send_request("shutdown", None)
|
||||
.await
|
||||
.map_err(LspProcessError::Transport);
|
||||
|
||||
if shutdown_result.is_ok() {
|
||||
self.transport
|
||||
.send_notification("exit", None)
|
||||
.await
|
||||
.map_err(LspProcessError::Transport)?;
|
||||
}
|
||||
|
||||
self.transport
|
||||
.shutdown()
|
||||
.await
|
||||
.map_err(LspProcessError::Transport)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Query hover information at a position.
|
||||
pub async fn hover(
|
||||
&mut self,
|
||||
path: &str,
|
||||
line: u32,
|
||||
character: u32,
|
||||
) -> Result<Option<LspHoverResult>, LspProcessError> {
|
||||
let uri = path_to_uri(path);
|
||||
let params = text_document_position_params(&uri, line, character);
|
||||
|
||||
let response = self
|
||||
.transport
|
||||
.send_request("textDocument/hover", Some(params))
|
||||
.await
|
||||
.map_err(LspProcessError::Transport)?;
|
||||
|
||||
let result = response
|
||||
.into_result()
|
||||
.map_err(|e| LspProcessError::Transport(LspTransportError::JsonRpc(e)))?;
|
||||
|
||||
if result.is_null() {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
Ok(parse_hover(&result))
|
||||
}
|
||||
|
||||
/// Go to definition at a position.
|
||||
pub async fn goto_definition(
|
||||
&mut self,
|
||||
path: &str,
|
||||
line: u32,
|
||||
character: u32,
|
||||
) -> Result<Vec<LspLocation>, LspProcessError> {
|
||||
let uri = path_to_uri(path);
|
||||
let params = text_document_position_params(&uri, line, character);
|
||||
|
||||
let response = self
|
||||
.transport
|
||||
.send_request("textDocument/definition", Some(params))
|
||||
.await
|
||||
.map_err(LspProcessError::Transport)?;
|
||||
|
||||
let result = response
|
||||
.into_result()
|
||||
.map_err(|e| LspProcessError::Transport(LspTransportError::JsonRpc(e)))?;
|
||||
|
||||
Ok(parse_locations(&result))
|
||||
}
|
||||
|
||||
/// Find references at a position.
|
||||
pub async fn references(
|
||||
&mut self,
|
||||
path: &str,
|
||||
line: u32,
|
||||
character: u32,
|
||||
) -> Result<Vec<LspLocation>, LspProcessError> {
|
||||
let uri = path_to_uri(path);
|
||||
let params = serde_json::json!({
|
||||
"textDocument": { "uri": uri },
|
||||
"position": { "line": line, "character": character },
|
||||
"context": { "includeDeclaration": true }
|
||||
});
|
||||
|
||||
let response = self
|
||||
.transport
|
||||
.send_request("textDocument/references", Some(params))
|
||||
.await
|
||||
.map_err(LspProcessError::Transport)?;
|
||||
|
||||
let result = response
|
||||
.into_result()
|
||||
.map_err(|e| LspProcessError::Transport(LspTransportError::JsonRpc(e)))?;
|
||||
|
||||
Ok(parse_locations(&result))
|
||||
}
|
||||
|
||||
/// Get document symbols for a file.
|
||||
pub async fn document_symbols(
|
||||
&mut self,
|
||||
path: &str,
|
||||
) -> Result<Vec<LspSymbol>, LspProcessError> {
|
||||
let uri = path_to_uri(path);
|
||||
let params = serde_json::json!({
|
||||
"textDocument": { "uri": uri }
|
||||
});
|
||||
|
||||
let response = self
|
||||
.transport
|
||||
.send_request("textDocument/documentSymbol", Some(params))
|
||||
.await
|
||||
.map_err(LspProcessError::Transport)?;
|
||||
|
||||
let result = response
|
||||
.into_result()
|
||||
.map_err(|e| LspProcessError::Transport(LspTransportError::JsonRpc(e)))?;
|
||||
|
||||
if result.is_null() {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
|
||||
Ok(parse_symbols(&result, path))
|
||||
}
|
||||
|
||||
/// Get completions at a position.
|
||||
pub async fn completion(
|
||||
&mut self,
|
||||
path: &str,
|
||||
line: u32,
|
||||
character: u32,
|
||||
) -> Result<Vec<LspCompletionItem>, LspProcessError> {
|
||||
let uri = path_to_uri(path);
|
||||
let params = text_document_position_params(&uri, line, character);
|
||||
|
||||
let response = self
|
||||
.transport
|
||||
.send_request("textDocument/completion", Some(params))
|
||||
.await
|
||||
.map_err(LspProcessError::Transport)?;
|
||||
|
||||
let result = response
|
||||
.into_result()
|
||||
.map_err(|e| LspProcessError::Transport(LspTransportError::JsonRpc(e)))?;
|
||||
|
||||
if result.is_null() {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
|
||||
// The response may be a CompletionList or a plain array.
|
||||
let items = if let Some(list) = result.get("items") {
|
||||
list
|
||||
} else {
|
||||
&result
|
||||
};
|
||||
|
||||
Ok(parse_completions(items))
|
||||
}
|
||||
|
||||
/// Format a document.
|
||||
pub async fn format(&mut self, path: &str) -> Result<Vec<JsonValue>, LspProcessError> {
|
||||
let uri = path_to_uri(path);
|
||||
let params = serde_json::json!({
|
||||
"textDocument": { "uri": uri },
|
||||
"options": { "tabSize": 4, "insertSpaces": true }
|
||||
});
|
||||
|
||||
let response = self
|
||||
.transport
|
||||
.send_request("textDocument/formatting", Some(params))
|
||||
.await
|
||||
.map_err(LspProcessError::Transport)?;
|
||||
|
||||
let result = response
|
||||
.into_result()
|
||||
.map_err(|e| LspProcessError::Transport(LspTransportError::JsonRpc(e)))?;
|
||||
|
||||
if result.is_null() {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
|
||||
match result.as_array() {
|
||||
Some(arr) => Ok(arr.clone()),
|
||||
None => Ok(Vec::new()),
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn status(&self) -> LspServerStatus {
|
||||
self.status
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn language(&self) -> &str {
|
||||
&self.language
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn root_uri(&self) -> &str {
|
||||
&self.root_uri
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Error type
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum LspProcessError {
|
||||
Transport(LspTransportError),
|
||||
InvalidPath(String),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for LspProcessError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Transport(e) => write!(f, "LSP transport error: {e}"),
|
||||
Self::InvalidPath(p) => write!(f, "invalid path: {p}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for LspProcessError {
|
||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||
match self {
|
||||
Self::Transport(e) => Some(e),
|
||||
Self::InvalidPath(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
fn canonicalize_root(path: &Path) -> Result<String, LspProcessError> {
|
||||
path.canonicalize()
|
||||
.map_err(|e| LspProcessError::InvalidPath(format!("{}: {e}", path.display())))
|
||||
.map(|p| p.to_string_lossy().into_owned())
|
||||
}
|
||||
|
||||
fn path_to_uri(path: &str) -> String {
|
||||
let canonical = std::path::Path::new(path);
|
||||
if canonical.is_absolute() {
|
||||
format!("file://{path}")
|
||||
} else {
|
||||
let resolved = std::env::current_dir()
|
||||
.map_or_else(|_| canonical.to_path_buf(), |d| d.join(path));
|
||||
let canonicalized = resolved
|
||||
.canonicalize()
|
||||
.unwrap_or(resolved)
|
||||
.to_string_lossy()
|
||||
.into_owned();
|
||||
format!("file://{canonicalized}")
|
||||
}
|
||||
}
|
||||
|
||||
fn text_document_position_params(uri: &str, line: u32, character: u32) -> JsonValue {
|
||||
serde_json::json!({
|
||||
"textDocument": { "uri": uri },
|
||||
"position": { "line": line, "character": character }
|
||||
})
|
||||
}
|
||||
|
||||
fn uri_to_path(uri: &str) -> String {
|
||||
uri.strip_prefix("file://").unwrap_or(uri).to_owned()
|
||||
}
|
||||
|
||||
fn parse_hover(value: &JsonValue) -> Option<LspHoverResult> {
|
||||
let contents = value.get("contents")?;
|
||||
|
||||
// MarkupContent: { kind, value }
|
||||
if let (Some(kind), Some(val)) = (contents.get("kind"), contents.get("value")) {
|
||||
let language = if kind.as_str() == Some("plaintext") {
|
||||
None
|
||||
} else {
|
||||
Some(kind.as_str().unwrap_or("markdown").to_owned())
|
||||
};
|
||||
return Some(LspHoverResult {
|
||||
content: val.as_str().unwrap_or("").to_owned(),
|
||||
language,
|
||||
});
|
||||
}
|
||||
|
||||
// MarkedString object: { language, value }
|
||||
if let (Some(lang), Some(val)) = (contents.get("language"), contents.get("value")) {
|
||||
return Some(LspHoverResult {
|
||||
content: val.as_str().unwrap_or("").to_owned(),
|
||||
language: Some(lang.as_str().unwrap_or("").to_owned()),
|
||||
});
|
||||
}
|
||||
|
||||
// Plain string MarkedString
|
||||
if let Some(s) = contents.as_str() {
|
||||
return Some(LspHoverResult {
|
||||
content: s.to_owned(),
|
||||
language: None,
|
||||
});
|
||||
}
|
||||
|
||||
// Array of MarkedString
|
||||
if let Some(arr) = contents.as_array() {
|
||||
let parts: Vec<&str> = arr
|
||||
.iter()
|
||||
.filter_map(|item| {
|
||||
if let Some(s) = item.as_str() {
|
||||
Some(s)
|
||||
} else {
|
||||
item.get("value").and_then(JsonValue::as_str)
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
if parts.is_empty() {
|
||||
return None;
|
||||
}
|
||||
return Some(LspHoverResult {
|
||||
content: parts.join("\n"),
|
||||
language: None,
|
||||
});
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
#[allow(clippy::cast_possible_truncation)]
|
||||
fn parse_locations(value: &JsonValue) -> Vec<LspLocation> {
|
||||
let Some(locations) = value.as_array() else {
|
||||
return Vec::new();
|
||||
};
|
||||
|
||||
locations
|
||||
.iter()
|
||||
.filter_map(|loc| {
|
||||
let uri = loc.get("uri")?.as_str()?;
|
||||
let path = uri_to_path(uri);
|
||||
let range = loc.get("range")?;
|
||||
let start = range.get("start")?;
|
||||
let end = range.get("end")?;
|
||||
|
||||
Some(LspLocation {
|
||||
path,
|
||||
line: start.get("line")?.as_u64()? as u32,
|
||||
character: start.get("character")?.as_u64()? as u32,
|
||||
end_line: end
|
||||
.get("line")
|
||||
.and_then(JsonValue::as_u64)
|
||||
.map(|v| v as u32),
|
||||
end_character: end
|
||||
.get("character")
|
||||
.and_then(JsonValue::as_u64)
|
||||
.map(|v| v as u32),
|
||||
preview: None,
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn extract_symbols(items: &[JsonValue], path: &str, out: &mut Vec<LspSymbol>) {
|
||||
for item in items {
|
||||
let name = item.get("name").and_then(JsonValue::as_str).unwrap_or("");
|
||||
let kind = item
|
||||
.get("kind")
|
||||
.and_then(JsonValue::as_u64)
|
||||
.map_or_else(|| "Unknown".into(), symbol_kind_name);
|
||||
|
||||
let (sym_path, line, character) = if let Some(range) = item.get("range") {
|
||||
let start = range.get("start");
|
||||
(
|
||||
path.to_owned(),
|
||||
u32::try_from(
|
||||
start
|
||||
.and_then(|s| s.get("line"))
|
||||
.and_then(JsonValue::as_u64)
|
||||
.unwrap_or(0),
|
||||
)
|
||||
.unwrap_or(0),
|
||||
u32::try_from(
|
||||
start
|
||||
.and_then(|s| s.get("character"))
|
||||
.and_then(JsonValue::as_u64)
|
||||
.unwrap_or(0),
|
||||
)
|
||||
.unwrap_or(0),
|
||||
)
|
||||
} else {
|
||||
(path.to_owned(), 0, 0)
|
||||
};
|
||||
|
||||
out.push(LspSymbol {
|
||||
name: name.to_owned(),
|
||||
kind: kind.clone(),
|
||||
path: sym_path,
|
||||
line,
|
||||
character,
|
||||
});
|
||||
|
||||
if let Some(children) = item.get("children").and_then(JsonValue::as_array) {
|
||||
extract_symbols(children, path, out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_symbols(value: &JsonValue, default_path: &str) -> Vec<LspSymbol> {
|
||||
let Some(items) = value.as_array() else {
|
||||
return Vec::new();
|
||||
};
|
||||
|
||||
let mut result = Vec::new();
|
||||
extract_symbols(items, default_path, &mut result);
|
||||
result
|
||||
}
|
||||
|
||||
fn parse_completions(value: &JsonValue) -> Vec<LspCompletionItem> {
|
||||
let Some(items) = value.as_array() else {
|
||||
return Vec::new();
|
||||
};
|
||||
|
||||
items
|
||||
.iter()
|
||||
.map(|item| LspCompletionItem {
|
||||
label: item
|
||||
.get("label")
|
||||
.and_then(JsonValue::as_str)
|
||||
.unwrap_or("")
|
||||
.to_owned(),
|
||||
kind: item
|
||||
.get("kind")
|
||||
.and_then(JsonValue::as_u64)
|
||||
.map(completion_kind_name),
|
||||
detail: item
|
||||
.get("detail")
|
||||
.and_then(JsonValue::as_str)
|
||||
.map(str::to_owned),
|
||||
insert_text: item
|
||||
.get("insertText")
|
||||
.and_then(JsonValue::as_str)
|
||||
.map(str::to_owned),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn symbol_kind_name(kind: u64) -> String {
|
||||
match kind {
|
||||
1 => "File".into(),
|
||||
2 => "Module".into(),
|
||||
3 => "Namespace".into(),
|
||||
4 => "Package".into(),
|
||||
5 => "Class".into(),
|
||||
6 => "Method".into(),
|
||||
7 => "Property".into(),
|
||||
8 => "Field".into(),
|
||||
9 => "Constructor".into(),
|
||||
10 => "Enum".into(),
|
||||
11 => "Interface".into(),
|
||||
12 => "Function".into(),
|
||||
13 => "Variable".into(),
|
||||
14 => "Constant".into(),
|
||||
15 => "String".into(),
|
||||
16 => "Number".into(),
|
||||
17 => "Boolean".into(),
|
||||
18 => "Array".into(),
|
||||
19 => "Object".into(),
|
||||
20 => "Key".into(),
|
||||
21 => "Null".into(),
|
||||
22 => "EnumMember".into(),
|
||||
23 => "Struct".into(),
|
||||
24 => "Event".into(),
|
||||
25 => "Operator".into(),
|
||||
26 => "TypeParameter".into(),
|
||||
_ => format!("Unknown({kind})"),
|
||||
}
|
||||
}
|
||||
|
||||
fn completion_kind_name(kind: u64) -> String {
|
||||
match kind {
|
||||
1 => "Text".into(),
|
||||
2 => "Method".into(),
|
||||
3 => "Function".into(),
|
||||
4 => "Constructor".into(),
|
||||
5 => "Field".into(),
|
||||
6 => "Variable".into(),
|
||||
7 => "Class".into(),
|
||||
8 => "Interface".into(),
|
||||
9 => "Module".into(),
|
||||
10 => "Property".into(),
|
||||
11 => "Unit".into(),
|
||||
12 => "Value".into(),
|
||||
13 => "Enum".into(),
|
||||
14 => "Keyword".into(),
|
||||
15 => "Snippet".into(),
|
||||
16 => "Color".into(),
|
||||
17 => "File".into(),
|
||||
18 => "Reference".into(),
|
||||
19 => "Folder".into(),
|
||||
20 => "EnumMember".into(),
|
||||
21 => "Constant".into(),
|
||||
22 => "Struct".into(),
|
||||
23 => "Event".into(),
|
||||
24 => "Operator".into(),
|
||||
25 => "TypeParameter".into(),
|
||||
_ => format!("Unknown({kind})"),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Requires rust-analyzer to be installed on the system.
|
||||
/// Run with: cargo test -p runtime -- --ignored
|
||||
#[tokio::test]
|
||||
#[ignore = "requires rust-analyzer installed on PATH"]
|
||||
async fn spawn_and_initialize_rust_analyzer() {
|
||||
let root = std::env::current_dir().expect("should have cwd");
|
||||
let process = LspProcess::start("rust-analyzer", &[], &root).await;
|
||||
assert!(process.is_ok(), "should spawn and initialize rust-analyzer");
|
||||
|
||||
let mut process = process.unwrap();
|
||||
assert_eq!(process.status(), LspServerStatus::Connected);
|
||||
assert_eq!(process.language(), "rust-analyzer");
|
||||
|
||||
let shutdown_result = process.shutdown().await;
|
||||
assert!(shutdown_result.is_ok(), "shutdown should succeed: {shutdown_result:?}");
|
||||
}
|
||||
|
||||
/// Requires rust-analyzer to be installed and a Rust project on disk.
|
||||
/// Run with: cargo test -p runtime -- --ignored
|
||||
#[tokio::test]
|
||||
#[ignore = "requires rust-analyzer installed on PATH"]
|
||||
async fn hover_on_real_file() {
|
||||
let root = std::env::current_dir().expect("should have cwd");
|
||||
let mut process = LspProcess::start("rust-analyzer", &[], &root)
|
||||
.await
|
||||
.expect("should start rust-analyzer");
|
||||
|
||||
// Try hover on src/main.rs — the result might be None if the file
|
||||
// doesn't exist at that path, but the call itself should not error.
|
||||
let file_path = root.join("src").join("main.rs");
|
||||
let path_str = file_path.to_string_lossy();
|
||||
let result = process.hover(&path_str, 0, 0).await;
|
||||
assert!(result.is_ok(), "hover should not return an error: {:?}", result.err());
|
||||
|
||||
let _ = process.shutdown().await;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_hover_markup_content() {
|
||||
let value = serde_json::json!({
|
||||
"contents": {
|
||||
"kind": "plaintext",
|
||||
"value": "fn main()"
|
||||
}
|
||||
});
|
||||
let result = parse_hover(&value);
|
||||
assert!(result.is_some());
|
||||
let hover = result.unwrap();
|
||||
assert_eq!(hover.content, "fn main()");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_hover_marked_string_object() {
|
||||
let value = serde_json::json!({
|
||||
"contents": {
|
||||
"language": "rust",
|
||||
"value": "pub fn foo()"
|
||||
}
|
||||
});
|
||||
let result = parse_hover(&value);
|
||||
assert!(result.is_some());
|
||||
let hover = result.unwrap();
|
||||
assert_eq!(hover.content, "pub fn foo()");
|
||||
assert_eq!(hover.language.as_deref(), Some("rust"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_hover_plain_string() {
|
||||
let value = serde_json::json!({
|
||||
"contents": "some text"
|
||||
});
|
||||
let result = parse_hover(&value);
|
||||
assert!(result.is_some());
|
||||
let hover = result.unwrap();
|
||||
assert_eq!(hover.content, "some text");
|
||||
assert!(hover.language.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_hover_array_of_marked_strings() {
|
||||
let value = serde_json::json!({
|
||||
"contents": [
|
||||
"first line",
|
||||
{ "language": "rust", "value": "fn bar()" }
|
||||
]
|
||||
});
|
||||
let result = parse_hover(&value);
|
||||
assert!(result.is_some());
|
||||
let hover = result.unwrap();
|
||||
assert!(hover.content.contains("first line"));
|
||||
assert!(hover.content.contains("fn bar()"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_locations_empty_array() {
|
||||
let value = serde_json::json!([]);
|
||||
let locations = parse_locations(&value);
|
||||
assert!(locations.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_locations_valid() {
|
||||
let value = serde_json::json!([
|
||||
{
|
||||
"uri": "file:///tmp/test.rs",
|
||||
"range": {
|
||||
"start": { "line": 5, "character": 10 },
|
||||
"end": { "line": 5, "character": 15 }
|
||||
}
|
||||
}
|
||||
]);
|
||||
let locations = parse_locations(&value);
|
||||
assert_eq!(locations.len(), 1);
|
||||
assert_eq!(locations[0].line, 5);
|
||||
assert_eq!(locations[0].character, 10);
|
||||
assert_eq!(locations[0].end_line, Some(5));
|
||||
assert_eq!(locations[0].end_character, Some(15));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_symbols_basic() {
|
||||
let value = serde_json::json!([
|
||||
{
|
||||
"name": "main",
|
||||
"kind": 12,
|
||||
"range": {
|
||||
"start": { "line": 1, "character": 0 },
|
||||
"end": { "line": 5, "character": 1 }
|
||||
}
|
||||
}
|
||||
]);
|
||||
let symbols = parse_symbols(&value, "/tmp/test.rs");
|
||||
assert_eq!(symbols.len(), 1);
|
||||
assert_eq!(symbols[0].name, "main");
|
||||
assert_eq!(symbols[0].kind, "Function");
|
||||
assert_eq!(symbols[0].line, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_completions_basic() {
|
||||
let value = serde_json::json!([
|
||||
{ "label": "foo", "kind": 3, "detail": "fn foo()" },
|
||||
{ "label": "bar", "kind": 6 }
|
||||
]);
|
||||
let completions = parse_completions(&value);
|
||||
assert_eq!(completions.len(), 2);
|
||||
assert_eq!(completions[0].label, "foo");
|
||||
assert_eq!(completions[0].kind.as_deref(), Some("Function"));
|
||||
assert_eq!(completions[0].detail.as_deref(), Some("fn foo()"));
|
||||
assert_eq!(completions[1].label, "bar");
|
||||
assert_eq!(completions[1].kind.as_deref(), Some("Variable"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn symbol_kind_name_all_variants() {
|
||||
assert_eq!(symbol_kind_name(1), "File");
|
||||
assert_eq!(symbol_kind_name(6), "Method");
|
||||
assert_eq!(symbol_kind_name(12), "Function");
|
||||
assert_eq!(symbol_kind_name(13), "Variable");
|
||||
assert_eq!(symbol_kind_name(23), "Struct");
|
||||
assert_eq!(symbol_kind_name(99), "Unknown(99)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completion_kind_name_all_variants() {
|
||||
assert_eq!(completion_kind_name(1), "Text");
|
||||
assert_eq!(completion_kind_name(3), "Function");
|
||||
assert_eq!(completion_kind_name(6), "Variable");
|
||||
assert_eq!(completion_kind_name(14), "Keyword");
|
||||
assert_eq!(completion_kind_name(99), "Unknown(99)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_document_position_params_structure() {
|
||||
let params = text_document_position_params("file:///test.rs", 5, 10);
|
||||
assert_eq!(params["textDocument"]["uri"], "file:///test.rs");
|
||||
assert_eq!(params["position"]["line"], 5);
|
||||
assert_eq!(params["position"]["character"], 10);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn path_to_uri_absolute() {
|
||||
let uri = path_to_uri("/tmp/test.rs");
|
||||
assert_eq!(uri, "file:///tmp/test.rs");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn uri_to_path_extracts_path() {
|
||||
assert_eq!(uri_to_path("file:///tmp/test.rs"), "/tmp/test.rs");
|
||||
assert_eq!(uri_to_path("/no/prefix"), "/no/prefix");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,495 @@
|
|||
use std::io;
|
||||
use std::process::Stdio;
|
||||
use std::time::Duration;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value as JsonValue;
|
||||
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader};
|
||||
use tokio::process::{Child, ChildStdin, ChildStdout, Command};
|
||||
use tokio::time::timeout;
|
||||
|
||||
const DEFAULT_REQUEST_TIMEOUT: Duration = Duration::from_secs(30);
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
#[serde(untagged)]
|
||||
pub enum LspId {
|
||||
Number(u64),
|
||||
String(String),
|
||||
Null,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct LspRequest {
|
||||
pub jsonrpc: String,
|
||||
pub id: LspId,
|
||||
pub method: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub params: Option<JsonValue>,
|
||||
}
|
||||
|
||||
impl LspRequest {
|
||||
pub fn new(id: LspId, method: impl Into<String>, params: Option<JsonValue>) -> Self {
|
||||
Self {
|
||||
jsonrpc: "2.0".to_string(),
|
||||
id,
|
||||
method: method.into(),
|
||||
params,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct LspNotification {
|
||||
pub jsonrpc: String,
|
||||
pub method: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub params: Option<JsonValue>,
|
||||
}
|
||||
|
||||
impl LspNotification {
|
||||
pub fn new(method: impl Into<String>, params: Option<JsonValue>) -> Self {
|
||||
Self {
|
||||
jsonrpc: "2.0".to_string(),
|
||||
method: method.into(),
|
||||
params,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct LspError {
|
||||
pub code: i64,
|
||||
pub message: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub data: Option<JsonValue>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct LspResponse {
|
||||
pub jsonrpc: String,
|
||||
pub id: LspId,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub result: Option<JsonValue>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub error: Option<LspError>,
|
||||
}
|
||||
|
||||
impl LspResponse {
|
||||
#[must_use]
|
||||
pub fn is_error(&self) -> bool {
|
||||
self.error.is_some()
|
||||
}
|
||||
|
||||
pub fn into_result(self) -> Result<JsonValue, LspError> {
|
||||
if let Some(error) = self.error {
|
||||
Err(error)
|
||||
} else {
|
||||
Ok(self.result.unwrap_or(JsonValue::Null))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum LspTransportError {
|
||||
Io(io::Error),
|
||||
Timeout { method: String, timeout: Duration },
|
||||
JsonRpc(LspError),
|
||||
InvalidResponse { method: String, details: String },
|
||||
ServerExited,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for LspTransportError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Io(error) => write!(f, "{error}"),
|
||||
Self::Timeout { method, timeout } => {
|
||||
write!(f, "LSP request `{method}` timed out after {}s", timeout.as_secs())
|
||||
}
|
||||
Self::JsonRpc(error) => {
|
||||
write!(f, "LSP JSON-RPC error: {} ({})", error.message, error.code)
|
||||
}
|
||||
Self::InvalidResponse { method, details } => {
|
||||
write!(f, "LSP invalid response for `{method}`: {details}")
|
||||
}
|
||||
Self::ServerExited => write!(f, "LSP server process exited unexpectedly"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for LspTransportError {
|
||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||
match self {
|
||||
Self::Io(error) => Some(error),
|
||||
Self::JsonRpc(_) | Self::Timeout { .. } | Self::InvalidResponse { .. } | Self::ServerExited => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<io::Error> for LspTransportError {
|
||||
fn from(value: io::Error) -> Self {
|
||||
Self::Io(value)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct LspTransport {
|
||||
child: Child,
|
||||
stdin: ChildStdin,
|
||||
stdout: BufReader<ChildStdout>,
|
||||
next_id: u64,
|
||||
request_timeout: Duration,
|
||||
}
|
||||
|
||||
impl LspTransport {
|
||||
pub fn spawn(command: &str, args: &[String]) -> io::Result<Self> {
|
||||
Self::spawn_with_timeout(command, args, DEFAULT_REQUEST_TIMEOUT)
|
||||
}
|
||||
|
||||
pub fn spawn_with_timeout(
|
||||
command: &str,
|
||||
args: &[String],
|
||||
request_timeout: Duration,
|
||||
) -> io::Result<Self> {
|
||||
let mut cmd = Command::new(command);
|
||||
cmd.args(args)
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::inherit());
|
||||
|
||||
let mut child = cmd.spawn()?;
|
||||
let stdin = child
|
||||
.stdin
|
||||
.take()
|
||||
.ok_or_else(|| io::Error::other("LSP process missing stdin pipe"))?;
|
||||
let stdout = child
|
||||
.stdout
|
||||
.take()
|
||||
.ok_or_else(|| io::Error::other("LSP process missing stdout pipe"))?;
|
||||
|
||||
Ok(Self {
|
||||
child,
|
||||
stdin,
|
||||
stdout: BufReader::new(stdout),
|
||||
next_id: 1,
|
||||
request_timeout,
|
||||
})
|
||||
}
|
||||
|
||||
/// Construct an `LspTransport` from an already-spawned child process.
|
||||
/// Primarily useful for testing.
|
||||
#[cfg(test)]
|
||||
fn from_child(mut child: Child, request_timeout: Duration) -> Self {
|
||||
let stdin = child
|
||||
.stdin
|
||||
.take()
|
||||
.expect("LSP process missing stdin pipe");
|
||||
let stdout = child
|
||||
.stdout
|
||||
.take()
|
||||
.expect("LSP process missing stdout pipe");
|
||||
Self {
|
||||
child,
|
||||
stdin,
|
||||
stdout: BufReader::new(stdout),
|
||||
next_id: 1,
|
||||
request_timeout,
|
||||
}
|
||||
}
|
||||
|
||||
fn allocate_id(&mut self) -> LspId {
|
||||
let id = self.next_id;
|
||||
self.next_id += 1;
|
||||
LspId::Number(id)
|
||||
}
|
||||
|
||||
pub async fn send_notification(
|
||||
&mut self,
|
||||
method: &str,
|
||||
params: Option<JsonValue>,
|
||||
) -> Result<(), LspTransportError> {
|
||||
let notification = LspNotification::new(method, params);
|
||||
let body = serde_json::to_vec(¬ification)
|
||||
.map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?;
|
||||
self.write_frame(&body).await
|
||||
}
|
||||
|
||||
pub async fn send_request(
|
||||
&mut self,
|
||||
method: &str,
|
||||
params: Option<JsonValue>,
|
||||
) -> Result<LspResponse, LspTransportError> {
|
||||
let id = self.allocate_id();
|
||||
self.send_request_with_id(method, params, id).await
|
||||
}
|
||||
|
||||
pub async fn send_request_with_id(
|
||||
&mut self,
|
||||
method: &str,
|
||||
params: Option<JsonValue>,
|
||||
id: LspId,
|
||||
) -> Result<LspResponse, LspTransportError> {
|
||||
let request = LspRequest::new(id.clone(), method, params);
|
||||
let body = serde_json::to_vec(&request)
|
||||
.map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?;
|
||||
self.write_frame(&body).await?;
|
||||
|
||||
let method_owned = method.to_string();
|
||||
let timeout_duration = self.request_timeout;
|
||||
let response = timeout(timeout_duration, self.read_response())
|
||||
.await
|
||||
.map_err(|_| LspTransportError::Timeout {
|
||||
method: method_owned,
|
||||
timeout: timeout_duration,
|
||||
})??;
|
||||
|
||||
if response.jsonrpc != "2.0" {
|
||||
return Err(LspTransportError::InvalidResponse {
|
||||
method: method.to_string(),
|
||||
details: format!("unsupported jsonrpc version `{}`", response.jsonrpc),
|
||||
});
|
||||
}
|
||||
|
||||
if response.id != id {
|
||||
return Err(LspTransportError::InvalidResponse {
|
||||
method: method.to_string(),
|
||||
details: format!(
|
||||
"mismatched id: expected {:?}, got {:?}",
|
||||
id, response.id
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
if let Some(error) = &response.error {
|
||||
return Err(LspTransportError::JsonRpc(error.clone()));
|
||||
}
|
||||
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
pub async fn read_response(&mut self) -> Result<LspResponse, LspTransportError> {
|
||||
self.read_jsonrpc_message().await
|
||||
}
|
||||
|
||||
pub async fn shutdown(&mut self) -> Result<(), LspTransportError> {
|
||||
let _ = self
|
||||
.send_notification("shutdown", None)
|
||||
.await;
|
||||
|
||||
let _ = self.send_notification("exit", None).await;
|
||||
|
||||
match self.child.try_wait() {
|
||||
Ok(Some(_)) => {}
|
||||
Ok(None) | Err(_) => {
|
||||
let _ = self.child.kill().await;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn is_alive(&mut self) -> bool {
|
||||
matches!(self.child.try_wait(), Ok(None))
|
||||
}
|
||||
|
||||
async fn write_frame(&mut self, payload: &[u8]) -> Result<(), LspTransportError> {
|
||||
let header = format!("Content-Length: {}\r\n\r\n", payload.len());
|
||||
self.stdin.write_all(header.as_bytes()).await?;
|
||||
self.stdin.write_all(payload).await?;
|
||||
self.stdin.flush().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn read_frame(&mut self) -> Result<Vec<u8>, LspTransportError> {
|
||||
let mut content_length: Option<usize> = None;
|
||||
|
||||
loop {
|
||||
let mut line = String::new();
|
||||
let bytes_read = self.stdout.read_line(&mut line).await?;
|
||||
if bytes_read == 0 {
|
||||
return Err(LspTransportError::ServerExited);
|
||||
}
|
||||
if line == "\r\n" {
|
||||
break;
|
||||
}
|
||||
let header = line.trim_end_matches(['\r', '\n']);
|
||||
if let Some((name, value)) = header.split_once(':') {
|
||||
if name.trim().eq_ignore_ascii_case("Content-Length") {
|
||||
let parsed = value
|
||||
.trim()
|
||||
.parse::<usize>()
|
||||
.map_err(|error| LspTransportError::Io(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
error,
|
||||
)))?;
|
||||
content_length = Some(parsed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let content_length = content_length.ok_or_else(|| {
|
||||
LspTransportError::InvalidResponse {
|
||||
method: "unknown".to_string(),
|
||||
details: "missing Content-Length header".to_string(),
|
||||
}
|
||||
})?;
|
||||
|
||||
let mut payload = vec![0u8; content_length];
|
||||
self.stdout.read_exact(&mut payload).await.map_err(|error| {
|
||||
if error.kind() == io::ErrorKind::UnexpectedEof {
|
||||
LspTransportError::ServerExited
|
||||
} else {
|
||||
LspTransportError::Io(error)
|
||||
}
|
||||
})?;
|
||||
|
||||
Ok(payload)
|
||||
}
|
||||
|
||||
async fn read_jsonrpc_message<T: serde::de::DeserializeOwned>(
|
||||
&mut self,
|
||||
) -> Result<T, LspTransportError> {
|
||||
let payload = self.read_frame().await?;
|
||||
serde_json::from_slice(&payload).map_err(|error| LspTransportError::InvalidResponse {
|
||||
method: "unknown".to_string(),
|
||||
details: error.to_string(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::io::Cursor;
|
||||
use tokio::io::{AsyncBufReadExt, AsyncReadExt, BufReader};
|
||||
|
||||
#[test]
|
||||
fn content_length_header_roundtrip() {
|
||||
let rt = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
rt.block_on(async {
|
||||
let payload = br#"{"jsonrpc":"2.0","id":1,"method":"initialize","params":null}"#;
|
||||
|
||||
// Write frame into a buffer
|
||||
let mut write_buf = Vec::new();
|
||||
{
|
||||
let header = format!("Content-Length: {}\r\n\r\n", payload.len());
|
||||
write_buf.extend_from_slice(header.as_bytes());
|
||||
write_buf.extend_from_slice(payload);
|
||||
}
|
||||
|
||||
// Read frame back using the same logic as LspTransport::read_frame
|
||||
let cursor = Cursor::new(write_buf);
|
||||
let mut reader = BufReader::new(cursor);
|
||||
|
||||
let mut content_length: Option<usize> = None;
|
||||
loop {
|
||||
let mut line = String::new();
|
||||
let bytes_read = reader.read_line(&mut line).await.unwrap();
|
||||
assert!(bytes_read > 0, "unexpected EOF reading header");
|
||||
if line == "\r\n" {
|
||||
break;
|
||||
}
|
||||
let header = line.trim_end_matches(['\r', '\n']);
|
||||
if let Some((name, value)) = header.split_once(':') {
|
||||
if name.trim().eq_ignore_ascii_case("Content-Length") {
|
||||
content_length = Some(value.trim().parse::<usize>().unwrap());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let content_length = content_length.expect("should have Content-Length");
|
||||
assert_eq!(content_length, payload.len());
|
||||
|
||||
let mut read_payload = vec![0u8; content_length];
|
||||
reader.read_exact(&mut read_payload).await.unwrap();
|
||||
|
||||
let original: serde_json::Value = serde_json::from_slice(payload).unwrap();
|
||||
let roundtripped: serde_json::Value = serde_json::from_slice(&read_payload).unwrap();
|
||||
assert_eq!(original, roundtripped);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn request_has_incrementing_ids() {
|
||||
let rt = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
rt.block_on(async {
|
||||
// Spawn cat so we can construct a real LspTransport.
|
||||
let child = tokio::process::Command::new("cat")
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::null())
|
||||
.spawn()
|
||||
.expect("cat should be available");
|
||||
|
||||
let mut transport = LspTransport::from_child(child, Duration::from_secs(5));
|
||||
|
||||
// Allocate IDs by inspecting what send_request would produce.
|
||||
let id1 = transport.allocate_id();
|
||||
let id2 = transport.allocate_id();
|
||||
let id3 = transport.allocate_id();
|
||||
|
||||
assert_eq!(id1, LspId::Number(1));
|
||||
assert_eq!(id2, LspId::Number(2));
|
||||
assert_eq!(id3, LspId::Number(3));
|
||||
|
||||
// Clean up
|
||||
let _ = transport.shutdown().await;
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn notification_has_no_id() {
|
||||
let notification = LspNotification::new("initialized", Some(serde_json::json!({})));
|
||||
let serialized = serde_json::to_string(¬ification).unwrap();
|
||||
let parsed: serde_json::Value = serde_json::from_str(&serialized).unwrap();
|
||||
assert!(
|
||||
parsed.get("id").is_none(),
|
||||
"notification should not contain an 'id' field, got: {serialized}"
|
||||
);
|
||||
assert_eq!(parsed["jsonrpc"], "2.0");
|
||||
assert_eq!(parsed["method"], "initialized");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_header_handling() {
|
||||
let rt = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
rt.block_on(async {
|
||||
// Feed garbage bytes that don't contain a valid Content-Length header.
|
||||
let garbage = b"THIS IS NOT A VALID HEADER\r\n\r\n";
|
||||
let cursor = Cursor::new(garbage.to_vec());
|
||||
let mut reader = BufReader::new(cursor);
|
||||
|
||||
let mut content_length: Option<usize> = None;
|
||||
loop {
|
||||
let mut line = String::new();
|
||||
let bytes_read = reader.read_line(&mut line).await.unwrap();
|
||||
if bytes_read == 0 || line == "\r\n" {
|
||||
break;
|
||||
}
|
||||
let header = line.trim_end_matches(['\r', '\n']);
|
||||
if let Some((name, value)) = header.split_once(':') {
|
||||
if name.trim().eq_ignore_ascii_case("Content-Length") {
|
||||
content_length = value.trim().parse::<usize>().ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The garbage header should not produce a valid Content-Length.
|
||||
assert!(
|
||||
content_length.is_none(),
|
||||
"garbage input should not produce a valid Content-Length"
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -6925,12 +6925,18 @@ fn run_resume_command(
|
|||
| SlashCommand::Ide { .. }
|
||||
| SlashCommand::Tag { .. }
|
||||
| SlashCommand::OutputStyle { .. }
|
||||
<<<<<<< HEAD
|
||||
| SlashCommand::AddDir { .. }
|
||||
<<<<<<< HEAD
|
||||
| SlashCommand::Team { .. }
|
||||
=======
|
||||
| SlashCommand::Lsp { .. }
|
||||
>>>>>>> 856409d3 (feat: full LSP (Language Server Protocol) integration)
|
||||
=======
|
||||
| SlashCommand::AddDir { .. } => Err("unsupported resumed slash command".into()),
|
||||
| SlashCommand::AddDir { .. }
|
||||
| SlashCommand::Lsp { .. }
|
||||
>>>>>>> e9582034 (feat: full LSP (Language Server Protocol) integration)
|
||||
| SlashCommand::Setup => Err("unsupported resumed slash command".into()),
|
||||
}
|
||||
}
|
||||
|
|
@ -7092,6 +7098,7 @@ fn run_repl(
|
|||
server.clone(),
|
||||
);
|
||||
}
|
||||
<<<<<<< HEAD
|
||||
// Auto-start all discovered servers if enabled
|
||||
if cli.lsp_auto_start {
|
||||
let registry = tools::global_lsp_registry();
|
||||
|
|
@ -7102,6 +7109,8 @@ fn run_repl(
|
|||
}
|
||||
}
|
||||
}
|
||||
=======
|
||||
>>>>>>> e9582034 (feat: full LSP (Language Server Protocol) integration)
|
||||
}
|
||||
|
||||
loop {
|
||||
|
|
@ -8386,7 +8395,11 @@ impl LiveCli {
|
|||
})
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
fn handle_lsp_command(&mut self, action: Option<&str>, target: Option<&str>) {
|
||||
=======
|
||||
fn handle_lsp_command(&self, action: Option<&str>, target: Option<&str>) {
|
||||
>>>>>>> e9582034 (feat: full LSP (Language Server Protocol) integration)
|
||||
let registry = tools::global_lsp_registry();
|
||||
match action {
|
||||
Some("start") => {
|
||||
|
|
@ -8411,6 +8424,7 @@ impl LiveCli {
|
|||
Err(e) => eprintln!("Failed to restart LSP server '{lang}': {e}"),
|
||||
}
|
||||
}
|
||||
<<<<<<< HEAD
|
||||
Some("toggle") => {
|
||||
self.lsp_auto_start = !self.lsp_auto_start;
|
||||
let state = if self.lsp_auto_start { "on" } else { "off" };
|
||||
|
|
@ -8420,6 +8434,10 @@ impl LiveCli {
|
|||
let servers = registry.list_servers();
|
||||
let auto_state = if self.lsp_auto_start { "on" } else { "off" };
|
||||
eprintln!("LSP auto-start: {auto_state}");
|
||||
=======
|
||||
_ => {
|
||||
let servers = registry.list_servers();
|
||||
>>>>>>> e9582034 (feat: full LSP (Language Server Protocol) integration)
|
||||
if servers.is_empty() {
|
||||
eprintln!("No LSP servers registered.");
|
||||
} else {
|
||||
|
|
|
|||
Loading…
Reference in New Issue