//! The client capabilities Lutra announces and the parts of the server's //! capabilities the client acts on. //! //! Spec: . 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, /// 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| 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) ); } }