use std::collections::HashMap;
use crate::text_document::{DocumentUri, TextEdit};
use serde::{Serialize, Deserialize};
use crate::connection::{RpcConnection, Callback};
use crate::{Server, Connection, TypeProvider};
#[derive(Serialize, Debug, Default)]
pub struct WorkspaceEdit {
pub changes: HashMap<DocumentUri, Vec<TextEdit>>
}
pub(super) struct ApplyEdit<T: TypeProvider> {
callback: Callback<Server<T>>
}
#[derive(Serialize)]
struct ApplyWorkspaceEditParams {
edit: WorkspaceEdit
}
#[derive(Deserialize, Debug, Default)]
pub struct ApplyWorkspaceEditResponse {
pub applied: bool
}
impl<T: TypeProvider> Connection<T> {
pub fn apply_edit(&mut self, tag: T::ApplyEditData, edit: WorkspaceEdit) -> bool {
self.request(
ApplyEdit::<T>::METHOD,
tag,
ApplyWorkspaceEditParams { edit }
)
}
}
impl<T: TypeProvider> Server<T> {
pub fn on_apply_edit_response(&mut self, callback: fn(&mut Server<T>, T::ApplyEditData, ApplyWorkspaceEditResponse)) {
self.workspace.apply_edit.callback = Callback::response(callback);
}
}
impl<T: TypeProvider> Default for ApplyEdit<T> {
fn default() -> Self {
Self {
callback: Callback::response(|_, _: T::ApplyEditData, _: ApplyWorkspaceEditResponse| ())
}
}
}
impl<T: TypeProvider> ApplyEdit<T> {
pub(super) const METHOD: &'static str = "workspace/applyEdit";
pub(crate) fn callback(&self) -> Callback<Server<T>> {
self.callback.clone()
}
}