use serde_json::Value;
use crate::position::Encoding;
leanfran::lean! {
pub mod proved;
r#"
structure Session where
encoding : String
additionalTextEdits : Bool
watchedFilesRegistration : Bool
serverProgress : Bool
deriving DecidableEq, Repr
def supported (name : String) : Bool :=
name == "utf-8" || name == "utf-16" || name == "utf-32"
def negotiateSession (preferences resolveProperties : List String)
(watchedFilesRegistration serverProgress : Bool) : Session :=
⟨(preferences.find? supported).getD "utf-16",
resolveProperties.contains "additionalTextEdits", watchedFilesRegistration, serverProgress⟩
theorem default_utf16 (properties : List String) (watched progress : Bool) :
(negotiateSession [] properties watched progress).encoding = "utf-16" := by
rfl
theorem first_supported_wins (name : String) (rest properties : List String)
(watched progress : Bool) (h : supported name = true) :
(negotiateSession (name :: rest) properties watched progress).encoding = name := by
simp [negotiateSession, h]
theorem unsupported_preferences_are_skipped (name : String) (rest properties : List String)
(watched progress : Bool) (h : supported name = false) :
negotiateSession (name :: rest) properties watched progress =
negotiateSession rest properties watched progress := by
simp [negotiateSession, h]
theorem supported_positions_and_client_order (tail properties : List String)
(watched progress : Bool) :
(negotiateSession ("unknown" :: "utf-8" :: "utf-32" :: tail) properties watched progress).encoding = "utf-8" ∧
(negotiateSession ("utf-32" :: "utf-16" :: "utf-8" :: tail) properties watched progress).encoding = "utf-32" ∧
(negotiateSession ("utf-16" :: "utf-8" :: tail) properties watched progress).encoding = "utf-16" := by
simp [negotiateSession, supported]
theorem edits_require_the_declared_property (encodings properties : List String)
(watched progress : Bool) :
(negotiateSession encodings properties watched progress).additionalTextEdits = true ↔
"additionalTextEdits" ∈ properties := by
simp [negotiateSession]
theorem declared_edit_property_grants_edits (encodings properties : List String)
(watched progress : Bool) :
(negotiateSession encodings ("additionalTextEdits" :: properties) watched progress).additionalTextEdits = true := by
simp [negotiateSession]
theorem unrelated_preferences_cannot_expand_permissions (encodings properties : List String) :
let registration := negotiateSession encodings properties true false
let progress := negotiateSession encodings properties false true
let neither := negotiateSession encodings properties false false
registration.watchedFilesRegistration = true ∧ registration.serverProgress = false ∧
progress.watchedFilesRegistration = false ∧ progress.serverProgress = true ∧
neither.watchedFilesRegistration = false ∧ neither.serverProgress = false ∧
registration.additionalTextEdits = neither.additionalTextEdits ∧
progress.additionalTextEdits = neither.additionalTextEdits ∧
registration.encoding = neither.encoding ∧ progress.encoding = neither.encoding := by
simp [negotiateSession]
theorem both_declarations_are_respected (encodings properties : List String) :
let session := negotiateSession encodings properties true true
session.watchedFilesRegistration = true ∧ session.serverProgress = true := by
simp [negotiateSession]
"#
}
pub use proved::Session;
pub fn negotiate(capabilities: &Value) -> Session {
let strings = |path| {
capabilities
.pointer(path)
.and_then(Value::as_array)
.into_iter()
.flatten()
.filter_map(Value::as_str)
.map(str::to_owned)
.collect::<Vec<_>>()
};
let enabled = |path| capabilities.pointer(path).and_then(Value::as_bool) == Some(true);
proved::negotiate_session(
strings("/general/positionEncodings"),
strings("/textDocument/completion/completionItem/resolveSupport/properties"),
enabled("/workspace/didChangeWatchedFiles/dynamicRegistration"),
enabled("/window/workDoneProgress"),
)
}
pub fn position_encoding(session: &Session) -> Encoding {
match session.encoding.as_str() {
"utf-8" => Encoding::Utf8,
"utf-32" => Encoding::Utf32,
_ => Encoding::Utf16,
}
}
#[cfg(test)]
mod tests {
use serde_json::json;
use super::*;
#[test]
fn client_order_and_exact_capability_paths_determine_the_session() {
let declared = json!({
"general": {"positionEncodings": ["unknown", "utf-8", "utf-32"]},
"textDocument": {"completion": {"completionItem": {"resolveSupport": {
"properties": ["documentation", "additionalTextEdits"]
}}}},
"workspace": {"didChangeWatchedFiles": {"dynamicRegistration": true}},
"window": {"workDoneProgress": true}
});
let agreed = negotiate(&declared);
assert_eq!(agreed.encoding, "utf-8");
assert!(agreed.additional_text_edits);
assert!(agreed.watched_files_registration);
assert!(agreed.server_progress);
let unsupported = json!({
"general": {"positionEncodings": ["unknown"]},
"textDocument": {"completion": {"completionItem": {"resolveSupport": {
"properties": ["AdditionalTextEdits", "documentation"]
}}}},
"workspace": {"didChangeWatchedFiles": {"dynamicRegistration": false}},
"window": {"workDoneProgress": "true"}
});
let agreed = negotiate(&unsupported);
assert_eq!(agreed.encoding, "utf-16");
assert!(!agreed.additional_text_edits);
assert!(!agreed.watched_files_registration);
assert!(!agreed.server_progress);
}
}