use std::{collections::BTreeMap, fmt};
use diag_lang::Diagnostic;
use incremental_lang::Grammar;
use rpc_io::{Call, Json, Peer};
use crate::{
diagnostics::{Scratch, write_clear, write_publish},
document::Document,
error::Error,
json::{trusted_raw, write_i64, write_str},
outbox::Outbox,
};
pub(crate) type Workspace<G> = BTreeMap<Box<str>, Document<G>>;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum MessageType {
Error = 1,
Warning = 2,
Info = 3,
Log = 4,
}
impl MessageType {
#[must_use]
pub const fn as_u32(self) -> u32 {
self as u32
}
}
pub(crate) struct Wire {
pub(crate) peer: Peer,
pub(crate) calls: Vec<Call>,
pub(crate) failure: Option<Error>,
scratch: Scratch,
text: String,
outbox: Outbox,
direct: bool,
}
impl Wire {
pub(crate) fn new(peer: Peer, outbox: Outbox) -> Self {
Self {
peer,
calls: Vec::new(),
failure: None,
scratch: Scratch::default(),
text: String::new(),
outbox,
direct: false,
}
}
pub(crate) fn go_direct(&mut self, direct: bool) {
self.direct = direct;
}
pub(crate) fn log(&mut self, kind: MessageType, message: &str) {
self.text.clear();
self.text.push_str("{\"type\":");
write_i64(&mut self.text, i64::from(kind.as_u32()));
self.text.push_str(",\"message\":");
write_str(&mut self.text, message);
self.text.push('}');
self.send_text("window/logMessage");
}
fn send_text(&mut self, method: &str) {
if self.direct && (!cfg!(debug_assertions) || trusted_raw(&self.text).is_ok()) {
self.outbox.push_notification(method, &self.text);
return;
}
let result = trusted_raw(&self.text)
.map_err(|e| Error::Io(std::io::Error::other(e.to_string())))
.and_then(|raw| self.peer.notify(method, raw).map_err(Error::from));
if let Err(error) = result {
self.remember_failure(error);
}
}
pub(crate) fn remember_failure(&mut self, error: Error) {
if self.failure.is_none() {
self.failure = Some(error);
}
}
}
pub struct Context<'a, G: Grammar> {
workspace: &'a Workspace<G>,
wire: &'a mut Wire,
}
impl<'a, G: Grammar> Context<'a, G> {
pub(crate) fn new(workspace: &'a Workspace<G>, wire: &'a mut Wire) -> Self {
Self { workspace, wire }
}
#[inline]
#[must_use]
pub fn document(&self, uri: &str) -> Option<&'a Document<G>> {
self.workspace.get(uri)
}
pub fn documents(&self) -> impl ExactSizeIterator<Item = &'a Document<G>> + 'a {
self.workspace.values()
}
pub fn notify<'p>(&mut self, method: &str, params: impl Into<Json<'p>>) -> Result<(), Error> {
self.wire.peer.notify(method, params).map_err(Error::from)
}
pub fn request<'p>(&mut self, method: &str, params: impl Into<Json<'p>>) -> Result<u64, Error> {
let call = self.wire.peer.request(method, params)?;
let id = call.id();
self.wire.calls.push(call);
Ok(id)
}
pub fn log(&mut self, kind: MessageType, message: &str) {
self.wire.log(kind, message);
}
pub fn publish_diagnostics(&mut self, document: &Document<G>, diagnostics: &[Diagnostic]) {
let wire = &mut *self.wire;
wire.text.clear();
write_publish(&mut wire.text, &mut wire.scratch, document, diagnostics);
wire.send_text("textDocument/publishDiagnostics");
}
pub fn clear_diagnostics(&mut self, uri: &str) {
let wire = &mut *self.wire;
wire.text.clear();
write_clear(&mut wire.text, uri);
wire.send_text("textDocument/publishDiagnostics");
}
}
impl<G: Grammar> fmt::Debug for Context<'_, G> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Context")
.field("documents", &self.workspace.len())
.field("outstanding_requests", &self.wire.calls.len())
.finish_non_exhaustive()
}
}