M3.2: LSP client core (framing, supervision, requests, document sync)
crates/lsp is now a library: Content-Length JSON-RPC framing over stdio (32 MiB cap, malformed frames dropped and reported, fuzzed), Server::spawn with initialize/initialized (utf-16, incremental sync, completion, hover, signatureHelp, definition, references, rename, publishDiagnostics), typed requests with timeouts and $/cancelRequest, answers to server requests (configuration nulls, MethodNotFound), events on a bounded channel, supervision with backoff restarts that reopen documents and give up after 5 crashes in 2 min, shutdown/exit, and incremental document sync from lutra_text edits. Tested against a scripted fake server binary; docs: architecture, G6. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
9ccfc1bf6a
commit
81831c90e0
15 changed files with 4089 additions and 2 deletions
210
crates/lsp/src/capabilities.rs
Normal file
210
crates/lsp/src/capabilities.rs
Normal file
|
|
@ -0,0 +1,210 @@
|
|||
//! The client capabilities Lutra announces and the parts of the server's
|
||||
//! capabilities the client acts on.
|
||||
//!
|
||||
//! Spec: <https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#initialize>.
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
/// The `capabilities` member of the `initialize` request.
|
||||
///
|
||||
/// Announced: incremental document sync with `didSave`; completion (plain
|
||||
/// text insert, no snippets, markdown or plain documentation); hover;
|
||||
/// signature help; definition; references; rename with `prepareRename`;
|
||||
/// `publishDiagnostics` with versions; `workspace/configuration` (answered
|
||||
/// with `null` values) and workspace folders; work-done progress; and the
|
||||
/// `utf-16` position encoding only. No dynamic registration and no
|
||||
/// `workspace/applyEdit`: workspace edits come only as rename results.
|
||||
pub fn client_capabilities() -> Value {
|
||||
let documentation = json!(["markdown", "plaintext"]);
|
||||
json!({
|
||||
"general": {
|
||||
"positionEncodings": ["utf-16"]
|
||||
},
|
||||
"textDocument": {
|
||||
"synchronization": {
|
||||
"dynamicRegistration": false,
|
||||
"willSave": false,
|
||||
"willSaveWaitUntil": false,
|
||||
"didSave": true
|
||||
},
|
||||
"completion": {
|
||||
"dynamicRegistration": false,
|
||||
"contextSupport": true,
|
||||
"completionItem": {
|
||||
"snippetSupport": false,
|
||||
"documentationFormat": documentation,
|
||||
"deprecatedSupport": true,
|
||||
"labelDetailsSupport": true,
|
||||
"insertReplaceSupport": false
|
||||
}
|
||||
},
|
||||
"hover": {
|
||||
"dynamicRegistration": false,
|
||||
"contentFormat": documentation
|
||||
},
|
||||
"signatureHelp": {
|
||||
"dynamicRegistration": false,
|
||||
"contextSupport": true,
|
||||
"signatureInformation": {
|
||||
"documentationFormat": documentation,
|
||||
"parameterInformation": {"labelOffsetSupport": true},
|
||||
"activeParameterSupport": true
|
||||
}
|
||||
},
|
||||
"definition": {"dynamicRegistration": false, "linkSupport": true},
|
||||
"references": {"dynamicRegistration": false},
|
||||
"rename": {"dynamicRegistration": false, "prepareSupport": true},
|
||||
"publishDiagnostics": {
|
||||
"relatedInformation": false,
|
||||
"versionSupport": true,
|
||||
"codeDescriptionSupport": false,
|
||||
"dataSupport": false
|
||||
}
|
||||
},
|
||||
"workspace": {
|
||||
"applyEdit": false,
|
||||
"configuration": true,
|
||||
"workspaceFolders": true,
|
||||
"didChangeConfiguration": {"dynamicRegistration": false}
|
||||
},
|
||||
"window": {
|
||||
"workDoneProgress": true,
|
||||
"showMessage": {}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// How the server wants document changes.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum SyncKind {
|
||||
/// The server does not want `didChange` at all.
|
||||
None,
|
||||
/// Every change sends the whole text.
|
||||
Full,
|
||||
/// Changes are sent as ranges.
|
||||
Incremental,
|
||||
}
|
||||
|
||||
/// The server capabilities the client acts on, plus the raw object.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct ServerCapabilities {
|
||||
/// How `didChange` is sent.
|
||||
pub sync: SyncKind,
|
||||
/// Whether `didOpen`/`didClose` are sent. Servers that announce sync but
|
||||
/// omit `openClose` get them anyway, as most servers expect.
|
||||
pub open_close: bool,
|
||||
/// Whether `didSave` is sent.
|
||||
pub save: bool,
|
||||
/// Whether `didSave` carries the text.
|
||||
pub save_include_text: bool,
|
||||
/// The encoding the server picked (`None`: the default, `utf-16`).
|
||||
pub position_encoding: Option<String>,
|
||||
/// The whole `capabilities` object, for callers that check providers
|
||||
/// (`completionProvider`, `renameProvider`, …).
|
||||
pub raw: Value,
|
||||
}
|
||||
|
||||
impl Default for ServerCapabilities {
|
||||
fn default() -> Self {
|
||||
Self::from_json(&Value::Null)
|
||||
}
|
||||
}
|
||||
|
||||
impl ServerCapabilities {
|
||||
/// Reads the `capabilities` member of an `initialize` result.
|
||||
///
|
||||
/// `textDocumentSync` may be a number (0, 1, 2) or an object; anything
|
||||
/// else counts as [`SyncKind::Full`] with open/close, the safe choice.
|
||||
pub fn from_json(value: &Value) -> Self {
|
||||
let sync_value = value.get("textDocumentSync");
|
||||
let kind = |number: Option<u64>| match number {
|
||||
Some(0) => SyncKind::None,
|
||||
Some(2) => SyncKind::Incremental,
|
||||
_ => SyncKind::Full,
|
||||
};
|
||||
let (sync, open_close, save, save_include_text) = match sync_value {
|
||||
Some(Value::Number(number)) => (kind(number.as_u64()), true, true, false),
|
||||
Some(Value::Object(object)) => {
|
||||
let save = object.get("save");
|
||||
(
|
||||
kind(object.get("change").and_then(Value::as_u64)),
|
||||
object
|
||||
.get("openClose")
|
||||
.and_then(Value::as_bool)
|
||||
.unwrap_or(true),
|
||||
match save {
|
||||
Some(Value::Bool(enabled)) => *enabled,
|
||||
Some(Value::Object(_)) => true,
|
||||
_ => false,
|
||||
},
|
||||
save.and_then(|s| s.get("includeText"))
|
||||
.and_then(Value::as_bool)
|
||||
.unwrap_or(false),
|
||||
)
|
||||
}
|
||||
_ => (SyncKind::Full, true, true, false),
|
||||
};
|
||||
Self {
|
||||
sync,
|
||||
open_close,
|
||||
save,
|
||||
save_include_text,
|
||||
position_encoding: value
|
||||
.get("positionEncoding")
|
||||
.and_then(Value::as_str)
|
||||
.map(str::to_string),
|
||||
raw: value.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the server announces `provider` (e.g. `"hoverProvider"`):
|
||||
/// present and not `false` or `null`.
|
||||
pub fn supports(&self, provider: &str) -> bool {
|
||||
!matches!(
|
||||
self.raw.get(provider),
|
||||
None | Some(Value::Null) | Some(Value::Bool(false))
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn sync_forms() {
|
||||
assert_eq!(
|
||||
ServerCapabilities::from_json(&json!({"textDocumentSync": 2})).sync,
|
||||
SyncKind::Incremental
|
||||
);
|
||||
let object = ServerCapabilities::from_json(&json!({"textDocumentSync": {
|
||||
"change": 2, "openClose": true, "save": {"includeText": true}
|
||||
}}));
|
||||
assert_eq!(object.sync, SyncKind::Incremental);
|
||||
assert!(object.save && object.save_include_text);
|
||||
let none = ServerCapabilities::from_json(&json!({"textDocumentSync": 0}));
|
||||
assert_eq!(none.sync, SyncKind::None);
|
||||
assert_eq!(ServerCapabilities::default().sync, SyncKind::Full);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn supports_providers() {
|
||||
let caps = ServerCapabilities::from_json(
|
||||
&json!({"hoverProvider": true, "renameProvider": {"prepareProvider": true}, "x": false}),
|
||||
);
|
||||
assert!(caps.supports("hoverProvider"));
|
||||
assert!(caps.supports("renameProvider"));
|
||||
assert!(!caps.supports("x"));
|
||||
assert!(!caps.supports("definitionProvider"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn announces_utf16_and_incremental_basics() {
|
||||
let caps = client_capabilities();
|
||||
assert_eq!(caps["general"]["positionEncodings"], json!(["utf-16"]));
|
||||
assert_eq!(
|
||||
caps["textDocument"]["rename"]["prepareSupport"],
|
||||
json!(true)
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue