2023-04-19 04:18:22 +02:00
|
|
|
use std::sync::Arc;
|
2023-04-17 14:56:25 +02:00
|
|
|
use std::{net::TcpStream, sync::Mutex, collections::BTreeMap};
|
2023-04-11 17:12:22 +02:00
|
|
|
|
2023-04-19 19:18:48 +02:00
|
|
|
use codemp::operation::{OperationController, OperationFactory, OperationProcessor};
|
|
|
|
use codemp::client::CodempClient;
|
2023-04-11 06:20:40 +02:00
|
|
|
use codemp::proto::buffer_client::BufferClient;
|
2023-04-07 03:05:21 +02:00
|
|
|
use rmpv::Value;
|
|
|
|
|
2023-04-11 06:20:40 +02:00
|
|
|
|
2023-04-07 03:05:21 +02:00
|
|
|
use tokio::io::Stdout;
|
2023-04-11 06:20:40 +02:00
|
|
|
use clap::Parser;
|
2023-04-07 03:05:21 +02:00
|
|
|
|
2023-04-11 14:01:55 +02:00
|
|
|
use nvim_rs::{compat::tokio::Compat, create::tokio as create, Handler, Neovim};
|
2023-04-11 22:35:10 +02:00
|
|
|
use tracing::{error, warn, debug, info};
|
2023-04-07 03:05:21 +02:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct NeovimHandler {
|
2023-04-11 06:20:40 +02:00
|
|
|
client: CodempClient,
|
2023-04-19 04:18:22 +02:00
|
|
|
factories: Arc<Mutex<BTreeMap<String, Arc<OperationController>>>>,
|
2023-04-11 06:20:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn nullable_optional_str(args: &Vec<Value>, index: usize) -> Option<String> {
|
|
|
|
Some(args.get(index)?.as_str()?.to_string())
|
2023-04-07 03:05:21 +02:00
|
|
|
}
|
|
|
|
|
2023-04-11 17:12:22 +02:00
|
|
|
fn default_empty_str(args: &Vec<Value>, index: usize) -> String {
|
|
|
|
nullable_optional_str(args, index).unwrap_or("".into())
|
|
|
|
}
|
|
|
|
|
2023-04-12 03:29:42 +02:00
|
|
|
fn nullable_optional_number(args: &Vec<Value>, index: usize) -> Option<i64> {
|
|
|
|
Some(args.get(index)?.as_i64()?)
|
2023-04-11 17:12:22 +02:00
|
|
|
}
|
|
|
|
|
2023-04-12 03:29:42 +02:00
|
|
|
fn default_zero_number(args: &Vec<Value>, index: usize) -> i64 {
|
2023-04-11 17:12:22 +02:00
|
|
|
nullable_optional_number(args, index).unwrap_or(0)
|
|
|
|
}
|
|
|
|
|
2023-04-19 04:18:22 +02:00
|
|
|
impl NeovimHandler {
|
|
|
|
fn buffer_controller(&self, path: &String) -> Option<Arc<OperationController>> {
|
|
|
|
Some(self.factories.lock().unwrap().get(path)?.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-17 14:56:25 +02:00
|
|
|
#[tonic::async_trait]
|
2023-04-07 03:05:21 +02:00
|
|
|
impl Handler for NeovimHandler {
|
|
|
|
type Writer = Compat<Stdout>;
|
|
|
|
|
|
|
|
async fn handle_request(
|
|
|
|
&self,
|
|
|
|
name: String,
|
|
|
|
args: Vec<Value>,
|
|
|
|
nvim: Neovim<Compat<Stdout>>,
|
|
|
|
) -> Result<Value, Value> {
|
2023-04-11 17:12:22 +02:00
|
|
|
debug!("processing '{}' - {:?}", name, args);
|
2023-04-07 03:05:21 +02:00
|
|
|
match name.as_ref() {
|
|
|
|
"ping" => Ok(Value::from("pong")),
|
2023-04-11 06:20:40 +02:00
|
|
|
|
|
|
|
"create" => {
|
|
|
|
if args.len() < 1 {
|
|
|
|
return Err(Value::from("no path given"));
|
|
|
|
}
|
2023-04-11 17:12:22 +02:00
|
|
|
let path = default_empty_str(&args, 0);
|
2023-04-11 06:20:40 +02:00
|
|
|
let content = nullable_optional_str(&args, 1);
|
|
|
|
let mut c = self.client.clone();
|
|
|
|
match c.create(path, content).await {
|
|
|
|
Ok(r) => match r {
|
2023-04-12 01:38:47 +02:00
|
|
|
true => Ok(Value::Nil),
|
2023-04-11 06:20:40 +02:00
|
|
|
false => Err(Value::from("rejected")),
|
|
|
|
},
|
|
|
|
Err(e) => Err(Value::from(format!("could not create buffer: {}", e))),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
"insert" => {
|
|
|
|
if args.len() < 3 {
|
|
|
|
return Err(Value::from("not enough arguments"));
|
|
|
|
}
|
2023-04-11 17:12:22 +02:00
|
|
|
let path = default_empty_str(&args, 0);
|
|
|
|
let txt = default_empty_str(&args, 1);
|
2023-04-19 04:18:22 +02:00
|
|
|
let mut pos = default_zero_number(&args, 2) as i64;
|
|
|
|
|
|
|
|
if pos <= 0 { pos = 0 } // TODO wtf vim??
|
|
|
|
|
|
|
|
match self.buffer_controller(&path) {
|
|
|
|
None => Err(Value::from("no controller for given path")),
|
|
|
|
Some(controller) => {
|
|
|
|
match controller.apply(controller.insert(&txt, pos as u64)).await {
|
|
|
|
Err(e) => Err(Value::from(format!("could not send insert: {}", e))),
|
|
|
|
Ok(_res) => Ok(Value::Nil),
|
2023-04-11 22:35:10 +02:00
|
|
|
}
|
2023-04-19 04:18:22 +02:00
|
|
|
}
|
2023-04-11 14:24:40 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
"delete" => {
|
|
|
|
if args.len() < 3 {
|
|
|
|
return Err(Value::from("not enough arguments"));
|
|
|
|
}
|
2023-04-11 17:12:22 +02:00
|
|
|
let path = default_empty_str(&args, 0);
|
2023-04-12 03:29:42 +02:00
|
|
|
let pos = default_zero_number(&args, 1) as u64;
|
|
|
|
let count = default_zero_number(&args, 2) as u64;
|
2023-04-11 14:24:40 +02:00
|
|
|
|
2023-04-19 04:18:22 +02:00
|
|
|
match self.buffer_controller(&path) {
|
|
|
|
None => Err(Value::from("no controller for given path")),
|
|
|
|
Some(controller) => match controller.apply(controller.delete(pos, count)).await {
|
|
|
|
Err(e) => Err(Value::from(format!("could not send delete: {}", e))),
|
|
|
|
Ok(_res) => Ok(Value::Nil),
|
|
|
|
}
|
2023-04-11 06:20:40 +02:00
|
|
|
}
|
2023-04-12 16:58:28 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"replace" => {
|
|
|
|
if args.len() < 2 {
|
|
|
|
return Err(Value::from("not enough arguments"));
|
|
|
|
}
|
|
|
|
let path = default_empty_str(&args, 0);
|
|
|
|
let txt = default_empty_str(&args, 1);
|
|
|
|
|
2023-04-19 04:18:22 +02:00
|
|
|
match self.buffer_controller(&path) {
|
|
|
|
None => Err(Value::from("no controller for given path")),
|
|
|
|
Some(controller) => match controller.apply(controller.replace(&txt)).await {
|
|
|
|
Err(e) => Err(Value::from(format!("could not send replace: {}", e))),
|
|
|
|
Ok(_res) => Ok(Value::Nil),
|
|
|
|
}
|
2023-04-12 16:58:28 +02:00
|
|
|
}
|
2023-04-11 06:20:40 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"attach" => {
|
|
|
|
if args.len() < 1 {
|
|
|
|
return Err(Value::from("no path given"));
|
|
|
|
}
|
2023-04-11 17:12:22 +02:00
|
|
|
let path = default_empty_str(&args, 0);
|
2023-04-12 00:32:39 +02:00
|
|
|
let buffer = match nvim.get_current_buf().await {
|
2023-04-11 17:12:22 +02:00
|
|
|
Ok(b) => b,
|
|
|
|
Err(e) => return Err(Value::from(format!("could not get current buffer: {}", e))),
|
|
|
|
};
|
|
|
|
|
2023-04-11 06:20:40 +02:00
|
|
|
let mut c = self.client.clone();
|
|
|
|
|
2023-04-19 04:18:22 +02:00
|
|
|
match c.attach(path.clone()).await {
|
2023-04-11 06:20:40 +02:00
|
|
|
Err(e) => Err(Value::from(format!("could not attach to stream: {}", e))),
|
2023-04-19 04:18:22 +02:00
|
|
|
Ok(controller) => {
|
|
|
|
let _controller = controller.clone();
|
|
|
|
let lines : Vec<String> = _controller.content().split("\n").map(|x| x.to_string()).collect();
|
|
|
|
match buffer.set_lines(0, -1, false, lines).await {
|
|
|
|
Err(e) => Err(Value::from(format!("could not sync buffer: {}", e))),
|
|
|
|
Ok(()) => {
|
|
|
|
tokio::spawn(async move {
|
|
|
|
loop {
|
|
|
|
_controller.wait().await;
|
|
|
|
let lines : Vec<String> = _controller.content().split("\n").map(|x| x.to_string()).collect();
|
|
|
|
if let Err(e) = buffer.set_lines(0, -1, false, lines).await {
|
|
|
|
error!("could not update buffer: {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
self.factories.lock().unwrap().insert(path, controller);
|
|
|
|
Ok(Value::Nil)
|
|
|
|
}
|
2023-04-12 00:32:39 +02:00
|
|
|
}
|
|
|
|
},
|
2023-04-11 06:20:40 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-04-12 01:38:38 +02:00
|
|
|
"detach" => {
|
2023-04-19 04:18:22 +02:00
|
|
|
Err(Value::from("unimplemented! try with :q!"))
|
|
|
|
// if args.len() < 1 {
|
|
|
|
// return Err(Value::from("no path given"));
|
|
|
|
// }
|
|
|
|
// let path = default_empty_str(&args, 0);
|
|
|
|
// let mut c = self.client.clone();
|
|
|
|
// c.detach(path);
|
|
|
|
// Ok(Value::Nil)
|
2023-04-12 01:38:38 +02:00
|
|
|
},
|
|
|
|
|
2023-04-12 03:29:42 +02:00
|
|
|
"listen" => {
|
|
|
|
let ns = nvim.create_namespace("Cursor").await
|
|
|
|
.map_err(|e| Value::from(format!("could not create namespace: {}", e)))?;
|
|
|
|
|
|
|
|
let buf = nvim.get_current_buf().await
|
|
|
|
.map_err(|e| Value::from(format!("could not get current buf: {}", e)))?;
|
|
|
|
|
2023-04-19 04:18:22 +02:00
|
|
|
let mut c = self.client.clone();
|
|
|
|
match c.listen().await {
|
2023-04-12 03:29:42 +02:00
|
|
|
Err(e) => Err(Value::from(format!("could not listen cursors: {}", e))),
|
2023-04-19 04:18:22 +02:00
|
|
|
Ok(cursor) => {
|
|
|
|
let mut sub = cursor.sub();
|
|
|
|
debug!("spawning cursor processing worker");
|
|
|
|
tokio::spawn(async move {
|
|
|
|
loop {
|
|
|
|
match sub.recv().await {
|
|
|
|
Err(e) => return error!("error receiving cursor update from controller: {}", e),
|
|
|
|
Ok((_usr, cur)) => {
|
|
|
|
if let Err(e) = buf.clear_namespace(ns, 0, -1).await {
|
|
|
|
error!("could not clear previous cursor highlight: {}", e);
|
|
|
|
}
|
|
|
|
if let Err(e) = buf.add_highlight(ns, "ErrorMsg", cur.start.row-1, cur.start.col, cur.start.col+1).await {
|
|
|
|
error!("could not create highlight for cursor: {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Ok(Value::Nil)
|
|
|
|
},
|
2023-04-12 03:29:42 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
"cursor" => {
|
|
|
|
if args.len() < 3 {
|
|
|
|
return Err(Value::from("not enough args"));
|
|
|
|
}
|
|
|
|
let path = default_empty_str(&args, 0);
|
|
|
|
let row = default_zero_number(&args, 1);
|
|
|
|
let col = default_zero_number(&args, 2);
|
|
|
|
|
|
|
|
let mut c = self.client.clone();
|
|
|
|
match c.cursor(path, row, col).await {
|
2023-04-19 04:18:22 +02:00
|
|
|
Ok(_) => Ok(Value::Nil),
|
|
|
|
Err(e) => Err(Value:: from(format!("could not update cursor: {}", e))),
|
2023-04-12 03:29:42 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-04-11 06:20:40 +02:00
|
|
|
_ => Err(Value::from("unimplemented")),
|
2023-04-07 03:05:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn handle_notify(
|
|
|
|
&self,
|
2023-04-11 06:20:40 +02:00
|
|
|
_name: String,
|
|
|
|
_args: Vec<Value>,
|
|
|
|
_nvim: Neovim<Compat<Stdout>>,
|
2023-04-07 03:05:21 +02:00
|
|
|
) {
|
2023-04-11 06:20:40 +02:00
|
|
|
warn!("notify not handled");
|
2023-04-07 03:05:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-11 06:20:40 +02:00
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
struct CliArgs {
|
|
|
|
/// server host to connect to
|
|
|
|
#[arg(long, default_value = "http://[::1]:50051")]
|
|
|
|
host: String,
|
|
|
|
|
|
|
|
/// show debug level logs
|
|
|
|
#[arg(long, default_value_t = false)]
|
|
|
|
debug: bool,
|
2023-04-12 05:00:37 +02:00
|
|
|
|
|
|
|
/// dump raw tracing logs into this TCP host
|
|
|
|
#[arg(long)]
|
|
|
|
remote_debug: Option<String>,
|
2023-04-11 06:20:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-07 03:05:21 +02:00
|
|
|
#[tokio::main]
|
2023-04-11 14:01:55 +02:00
|
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
2023-04-11 06:20:40 +02:00
|
|
|
let args = CliArgs::parse();
|
|
|
|
|
2023-04-12 05:00:37 +02:00
|
|
|
match args.remote_debug {
|
|
|
|
Some(host) =>
|
|
|
|
tracing_subscriber::fmt()
|
|
|
|
.with_writer(Mutex::new(TcpStream::connect(host)?))
|
2023-04-11 17:12:22 +02:00
|
|
|
.with_max_level(if args.debug { tracing::Level::DEBUG } else { tracing::Level::INFO })
|
2023-04-12 05:00:37 +02:00
|
|
|
.init(),
|
|
|
|
|
|
|
|
None =>
|
|
|
|
tracing_subscriber::fmt()
|
2023-04-11 22:35:10 +02:00
|
|
|
.compact()
|
|
|
|
.without_time()
|
|
|
|
.with_ansi(false)
|
|
|
|
.with_writer(std::io::stderr)
|
2023-04-11 17:12:22 +02:00
|
|
|
.with_max_level(if args.debug { tracing::Level::DEBUG } else { tracing::Level::INFO })
|
2023-04-12 05:00:37 +02:00
|
|
|
.init(),
|
2023-04-11 17:12:22 +02:00
|
|
|
}
|
2023-04-07 03:05:21 +02:00
|
|
|
|
2023-04-13 00:54:01 +02:00
|
|
|
let client = BufferClient::connect(args.host.clone()).await?;
|
2023-04-07 03:05:21 +02:00
|
|
|
|
2023-04-11 06:20:40 +02:00
|
|
|
let handler: NeovimHandler = NeovimHandler {
|
|
|
|
client: client.into(),
|
2023-04-19 04:18:22 +02:00
|
|
|
factories: Arc::new(Mutex::new(BTreeMap::new())),
|
2023-04-11 06:20:40 +02:00
|
|
|
};
|
|
|
|
|
2023-04-12 05:00:18 +02:00
|
|
|
let (_nvim, io_handler) = create::new_parent(handler).await;
|
2023-04-07 03:05:21 +02:00
|
|
|
|
2023-04-13 00:54:01 +02:00
|
|
|
info!("++ codemp connected: {}", args.host);
|
2023-04-07 03:05:21 +02:00
|
|
|
|
2023-04-11 14:01:55 +02:00
|
|
|
if let Err(e) = io_handler.await? {
|
2023-04-12 05:00:18 +02:00
|
|
|
error!("worker stopped with error: {}", e);
|
2023-04-07 03:05:21 +02:00
|
|
|
}
|
2023-04-11 06:20:40 +02:00
|
|
|
|
|
|
|
Ok(())
|
2023-04-07 03:05:21 +02:00
|
|
|
}
|