mirror of https://github.com/lapce/lapce.git
Remove unused code (#1336)
* Delete unused code * Simplify ProxyCallback * Remove unnecessary block
This commit is contained in:
parent
285d422b26
commit
df46a646da
|
@ -2,7 +2,6 @@
|
|||
pub mod core;
|
||||
pub mod counter;
|
||||
pub mod file;
|
||||
pub mod lsp;
|
||||
mod parse;
|
||||
pub mod plugin;
|
||||
pub mod proxy;
|
||||
|
@ -11,22 +10,11 @@
|
|||
pub mod style;
|
||||
pub mod terminal;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::atomic::AtomicU64;
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::Result;
|
||||
use crossbeam_channel::{Receiver, Sender};
|
||||
use parking_lot::Mutex;
|
||||
pub use parse::Call;
|
||||
pub use parse::RequestId;
|
||||
pub use parse::RpcObject;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use serde_json::json;
|
||||
use serde_json::Value;
|
||||
|
||||
pub use stdio::stdio_transport;
|
||||
|
||||
|
@ -37,246 +25,8 @@ pub enum RpcMessage<Req, Notif, Resp> {
|
|||
Error(RequestId, RpcError),
|
||||
}
|
||||
|
||||
impl<Req: Serialize, Notif: Serialize, Resp: Serialize>
|
||||
RpcMessage<Req, Notif, Resp>
|
||||
{
|
||||
pub fn to_value(&self) -> Result<Value> {
|
||||
let value = match self {
|
||||
RpcMessage::Request(id, req) => {
|
||||
let mut msg = serde_json::to_value(req)?;
|
||||
msg.as_object_mut()
|
||||
.ok_or_else(|| anyhow::anyhow!(""))?
|
||||
.insert("id".into(), (*id).into());
|
||||
msg
|
||||
}
|
||||
RpcMessage::Response(_, _) => todo!(),
|
||||
RpcMessage::Notification(_) => todo!(),
|
||||
RpcMessage::Error(_, _) => todo!(),
|
||||
};
|
||||
Ok(value)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct RpcError {
|
||||
pub code: i64,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
pub trait Callback: Send {
|
||||
fn call(self: Box<Self>, result: Result<Value, Value>);
|
||||
}
|
||||
|
||||
impl<F: Send + FnOnce(Result<Value, Value>)> Callback for F {
|
||||
fn call(self: Box<F>, result: Result<Value, Value>) {
|
||||
(*self)(result)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait NewCallback<Resp>: Send {
|
||||
fn call(self: Box<Self>, result: Result<Resp, RpcError>);
|
||||
}
|
||||
|
||||
impl<Resp, F: Send + FnOnce(Result<Resp, RpcError>)> NewCallback<Resp> for F {
|
||||
fn call(self: Box<F>, result: Result<Resp, RpcError>) {
|
||||
(*self)(result)
|
||||
}
|
||||
}
|
||||
|
||||
enum ResponseHandler {
|
||||
Chan(Sender<Result<Value, Value>>),
|
||||
Callback(Box<dyn Callback>),
|
||||
}
|
||||
|
||||
impl ResponseHandler {
|
||||
fn invoke(self, result: Result<Value, Value>) {
|
||||
match self {
|
||||
ResponseHandler::Chan(tx) => {
|
||||
let _ = tx.send(result);
|
||||
}
|
||||
ResponseHandler::Callback(f) => f.call(result),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
pub enum ControlFlow {
|
||||
Continue,
|
||||
Exit,
|
||||
}
|
||||
|
||||
pub trait Handler {
|
||||
type Notification: DeserializeOwned;
|
||||
type Request: DeserializeOwned;
|
||||
|
||||
fn handle_notification(&mut self, rpc: Self::Notification) -> ControlFlow;
|
||||
fn handle_request(&mut self, rpc: Self::Request) -> Result<Value, Value>;
|
||||
}
|
||||
|
||||
pub trait NewHandler<Req, Notif, Resp> {
|
||||
fn handle_notification(&mut self, rpc: Notif);
|
||||
fn handle_request(&mut self, rpc: Req);
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RpcHandler {
|
||||
sender: Sender<Value>,
|
||||
id: Arc<AtomicU64>,
|
||||
pending: Arc<Mutex<HashMap<u64, ResponseHandler>>>,
|
||||
}
|
||||
|
||||
impl RpcHandler {
|
||||
pub fn new(sender: Sender<Value>) -> Self {
|
||||
Self {
|
||||
sender,
|
||||
id: Arc::new(AtomicU64::new(0)),
|
||||
pending: Arc::new(Mutex::new(HashMap::new())),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mainloop<H>(&mut self, receiver: Receiver<Value>, handler: &mut H)
|
||||
where
|
||||
H: Handler,
|
||||
{
|
||||
for msg in receiver {
|
||||
let rpc: RpcObject = msg.into();
|
||||
if rpc.is_response() {
|
||||
let id = rpc.get_id().unwrap();
|
||||
match rpc.into_response() {
|
||||
Ok(resp) => {
|
||||
self.handle_response(id, resp);
|
||||
}
|
||||
Err(msg) => {
|
||||
self.handle_response(id, Err(json!(msg)));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
match rpc.into_rpc::<H::Notification, H::Request>() {
|
||||
Ok(Call::Request(id, request)) => {
|
||||
let result = handler.handle_request(request);
|
||||
self.respond(id, result);
|
||||
}
|
||||
Ok(Call::Notification(notification)) => {
|
||||
if handler.handle_notification(notification)
|
||||
== ControlFlow::Exit
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
Err(_e) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send_rpc_notification(&self, method: &str, params: &Value) {
|
||||
if let Err(_e) = self.sender.send(json!({
|
||||
"method": method,
|
||||
"params": params,
|
||||
})) {}
|
||||
}
|
||||
|
||||
fn send_rpc_request_common(
|
||||
&self,
|
||||
method: &str,
|
||||
params: &Value,
|
||||
rh: ResponseHandler,
|
||||
) {
|
||||
let id = self.id.fetch_add(1, Ordering::Relaxed);
|
||||
{
|
||||
let mut pending = self.pending.lock();
|
||||
pending.insert(id, rh);
|
||||
}
|
||||
if let Err(_e) = self.sender.send(json!({
|
||||
"id": id,
|
||||
"method": method,
|
||||
"params": params,
|
||||
})) {
|
||||
let mut pending = self.pending.lock();
|
||||
if let Some(rh) = pending.remove(&id) {
|
||||
rh.invoke(Err(json!("io error")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn send_rpc_request_value_common<T: serde::Serialize>(
|
||||
&self,
|
||||
request: T,
|
||||
rh: ResponseHandler,
|
||||
) {
|
||||
let id = self.id.fetch_add(1, Ordering::Relaxed);
|
||||
{
|
||||
let mut pending = self.pending.lock();
|
||||
pending.insert(id, rh);
|
||||
}
|
||||
let mut request = serde_json::to_value(request).unwrap();
|
||||
request
|
||||
.as_object_mut()
|
||||
.unwrap()
|
||||
.insert("id".to_string(), json!(id));
|
||||
|
||||
if let Err(_e) = self.sender.send(request) {
|
||||
let mut pending = self.pending.lock();
|
||||
if let Some(rh) = pending.remove(&id) {
|
||||
rh.invoke(Err(json!("io error")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send_rpc_request_value<T: serde::Serialize>(
|
||||
&self,
|
||||
request: T,
|
||||
) -> Result<Value, Value> {
|
||||
let (tx, rx) = crossbeam_channel::bounded(1);
|
||||
self.send_rpc_request_value_common(request, ResponseHandler::Chan(tx));
|
||||
rx.recv().unwrap_or_else(|_| Err(json!("io error")))
|
||||
}
|
||||
|
||||
pub fn send_rpc_request_value_async<T: serde::Serialize>(
|
||||
&self,
|
||||
request: T,
|
||||
f: Box<dyn Callback>,
|
||||
) {
|
||||
self.send_rpc_request_value_common(request, ResponseHandler::Callback(f));
|
||||
}
|
||||
|
||||
pub fn send_rpc_request(
|
||||
&self,
|
||||
method: &str,
|
||||
params: &Value,
|
||||
) -> Result<Value, Value> {
|
||||
let (tx, rx) = crossbeam_channel::bounded(1);
|
||||
self.send_rpc_request_common(method, params, ResponseHandler::Chan(tx));
|
||||
rx.recv().unwrap_or_else(|_| Err(json!("io error")))
|
||||
}
|
||||
|
||||
pub fn send_rpc_request_async(
|
||||
&self,
|
||||
method: &str,
|
||||
params: &Value,
|
||||
f: Box<dyn Callback>,
|
||||
) {
|
||||
self.send_rpc_request_common(method, params, ResponseHandler::Callback(f));
|
||||
}
|
||||
|
||||
fn handle_response(&self, id: u64, resp: Result<Value, Value>) {
|
||||
let handler = {
|
||||
let mut pending = self.pending.lock();
|
||||
pending.remove(&id)
|
||||
};
|
||||
if let Some(responsehandler) = handler {
|
||||
responsehandler.invoke(resp)
|
||||
}
|
||||
}
|
||||
|
||||
fn respond(&self, id: u64, result: Result<Value, Value>) {
|
||||
let mut response = json!({ "id": id });
|
||||
match result {
|
||||
Ok(result) => response["result"] = result,
|
||||
Err(error) => response["error"] = json!(error),
|
||||
};
|
||||
|
||||
#[allow(deprecated)]
|
||||
let _ = self.sender.send(response);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::RpcMessage;
|
||||
|
||||
pub type LspRpcMessage = RpcMessage<LspRequest, LspNotification, LspResponse>;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
#[serde(tag = "method", content = "params")]
|
||||
pub enum LspRequest {}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
#[serde(tag = "method", content = "params")]
|
||||
pub enum LspNotification {}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
#[serde(tag = "method", content = "params")]
|
||||
pub enum LspResponse {}
|
|
@ -283,15 +283,9 @@ pub struct ReadDirResponse {
|
|||
pub items: HashMap<PathBuf, FileNodeItem>,
|
||||
}
|
||||
|
||||
pub trait ProxyCallback: Send {
|
||||
fn call(self: Box<Self>, result: Result<ProxyResponse, RpcError>);
|
||||
}
|
||||
pub trait ProxyCallback: Send + FnOnce(Result<ProxyResponse, RpcError>) {}
|
||||
|
||||
impl<F: Send + FnOnce(Result<ProxyResponse, RpcError>)> ProxyCallback for F {
|
||||
fn call(self: Box<F>, result: Result<ProxyResponse, RpcError>) {
|
||||
(*self)(result)
|
||||
}
|
||||
}
|
||||
impl<F: Send + FnOnce(Result<ProxyResponse, RpcError>)> ProxyCallback for F {}
|
||||
|
||||
enum ResponseHandler {
|
||||
Callback(Box<dyn ProxyCallback>),
|
||||
|
@ -301,7 +295,7 @@ enum ResponseHandler {
|
|||
impl ResponseHandler {
|
||||
fn invoke(self, result: Result<ProxyResponse, RpcError>) {
|
||||
match self {
|
||||
ResponseHandler::Callback(f) => f.call(result),
|
||||
ResponseHandler::Callback(f) => f(result),
|
||||
ResponseHandler::Chan(tx) => {
|
||||
let _ = tx.send(result);
|
||||
}
|
||||
|
@ -359,10 +353,9 @@ pub fn mainloop<H>(&self, handler: &mut H)
|
|||
|
||||
fn request_common(&self, request: ProxyRequest, rh: ResponseHandler) {
|
||||
let id = self.id.fetch_add(1, Ordering::Relaxed);
|
||||
{
|
||||
let mut pending = self.pending.lock();
|
||||
pending.insert(id, rh);
|
||||
}
|
||||
|
||||
self.pending.lock().insert(id, rh);
|
||||
|
||||
let _ = self.tx.send(ProxyRpc::Request(id, request));
|
||||
}
|
||||
|
||||
|
@ -390,8 +383,7 @@ pub fn handle_response(
|
|||
id: RequestId,
|
||||
result: Result<ProxyResponse, RpcError>,
|
||||
) {
|
||||
let handler = { self.pending.lock().remove(&id) };
|
||||
if let Some(handler) = handler {
|
||||
if let Some(handler) = self.pending.lock().remove(&id) {
|
||||
handler.invoke(result);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue