fix: neovim rcp call and notify, added test buffer command

This commit is contained in:
əlemi 2022-07-11 02:08:37 +02:00
parent c213536c3b
commit 3425c57177
5 changed files with 71 additions and 92 deletions

View file

@ -12,7 +12,7 @@ endif
let s:bin = "/home/alemi/projects/codemp/target/debug/codemp-client"
function! codemp#init()
function codemp#init()
let result = s:StartJob()
if 0 == result
@ -21,11 +21,12 @@ function! codemp#init()
echoerr "codeMP: rpc process is not executable"
else
let s:jobid = result
let g:codemp_jobid = result
call s:ConfigureJob(result)
endif
endfunction
function! s:StartJob()
function s:StartJob()
if 0 == s:jobid
let id = jobstart([s:bin], { 'rpc': v:true, 'on_stderr': function('s:OnStderr') })
return id
@ -34,7 +35,7 @@ function! s:StartJob()
endif
endfunction
function! s:StopJob()
function s:StopJob()
if 0 < s:jobid
augroup codeMp
autocmd! " clear all previous autocommands
@ -53,7 +54,7 @@ function! s:StopJob()
endif
endfunction
function! s:ConfigureJob(jobid)
function s:ConfigureJob(jobid)
augroup codeMp
" clear all previous autocommands
autocmd!
@ -66,22 +67,28 @@ function! s:ConfigureJob(jobid)
augroup END
endfunction
function! s:NotifyInsertEnter()
let [ bufnum, lnum, column, off ] = getpos('.')
call rpcnotify(s:jobid, 'insert-enter', v:insertmode, lnum, column)
function s:NotifyInsertEnter()
" let [ bufnum, lnum, column, off ] = getpos('.')
call rpcnotify(s:jobid, 'insert', 1)
endfunction
function! s:NotifyInsertLeave()
function s:NotifyInsertLeave()
call rpcnotify(s:jobid, 'insert', 0)
endfunction
function! codemp#ping()
call rpcnotify(s:jobid, "ping")
function codemp#buffer()
call rpcrequest(s:jobid, "buffer")
endfunction
function! codemp#test()
call rpcnotify(s:jobid, "rpc")
function codemp#ping()
call rpcrequest(s:jobid, "ping")
endfunction
function! s:OnStderr(id, data, event) dict
echom 'codemp: stderr: ' . join(a:data, "\n")
function codemp#test()
call rpcrequest(s:jobid, "rpc")
endfunction
function s:OnStderr(id, data, event) dict
let g:msg = 'codemp: stderr: ' . join(a:data, "\n")
echo g:msg
endfunction

View file

@ -2,30 +2,29 @@ pub mod manager;
mod nvim;
use tokio::sync::mpsc;
use nvim_rs::{compat::tokio::Compat, create::tokio::new_parent, rpc::IntoVal, Handler, Neovim, Value};
use nvim_rs::{create::tokio::new_parent};
use manager::ConnectionManager;
use nvim::NeovimHandler;
#[tokio::main]
async fn main() -> Result<(), Box<(dyn std::error::Error + 'static)>> {
let (tx, rx) = mpsc::channel(32);
let mut mngr = ConnectionManager::new("http://[::1]:50051".to_string(), rx).await?;
tokio::spawn(async move {
mngr.process_packets().await
});
let handler: NeovimHandler = NeovimHandler::new(tx).await?;
let (nvim, io_handler) = new_parent(handler).await;
let (_nvim, io_handler) = new_parent(handler).await;
nvim.call(":echo", vec![Value::from("***REMOVED***")]).await.unwrap().unwrap();
// nvim.call(":echo", vec![Value::from("'***REMOVED***'")]).await.unwrap();
let mut mngr = ConnectionManager::new("http://[::1]:50051".to_string(), rx).await?;
let _task = tokio::spawn(async move {
mngr.process_packets().await
});
// Any error should probably be logged, as stderr is not visible to users.
match io_handler.await {
Err(err) => eprintln!("Error joining IO loop: {:?}", err),
Ok(Err(err)) => eprintln!("Process ended with error: {:?}", err),
Ok(Ok(())) => println!("Finished"),
Ok(Ok(())) => eprintln!("Finished"),
}
Ok(())

View file

@ -25,11 +25,16 @@ impl ConnectionManager {
}
pub async fn process_packets(&mut self) {
{
let request = tonic::Request::new(SessionRequest {session_id: -1});
let response = self.client.create(request).await.unwrap();
eprintln!("RESPONSE={:?}", response);
}
loop {
if let Some(i) = self.rx.recv().await {
let request = tonic::Request::new(SessionRequest {session_id: i});
let response = self.client.create(request).await.unwrap();
println!("RESPONSE={:?}", response);
eprintln!("RESPONSE={:?}", response);
} else {
break
}

View file

@ -3,11 +3,7 @@ use rmpv::Value;
use tokio::io::Stdout;
use tokio::sync::mpsc;
use nvim_rs::{compat::tokio::Compat, create::tokio::new_parent, rpc::IntoVal, Handler, Neovim};
use tonic::transport::Channel;
use crate::manager::proto_core::{session_client::SessionClient, SessionRequest};
use nvim_rs::{compat::tokio::Compat, Handler, Neovim};
#[derive(Clone)]
pub struct NeovimHandler {
@ -33,63 +29,35 @@ impl Handler for NeovimHandler {
match name.as_ref() {
"ping" => Ok(Value::from("pong")),
"rpc" => {
eprintln!("Got 'rpc' from vim");
self.tx.send(0).await.unwrap();
// let request = tonic::Request::new(SessionRequest {session_id: 0});
// let response = self.client.create(request).await.unwrap();
Ok(Value::from("sent"))
},
_ => unimplemented!(),
"buffer" => {
let buf = _neovim.create_buf(true, false).await.unwrap();
buf.set_lines(0, 1, false, vec!["codeMP".to_string()]).await.unwrap();
_neovim.set_current_buf(&buf).await.unwrap();
Ok(Value::from("ok"))
}
_ => {
eprintln!("[!] unexpected call");
Ok(Value::from(""))
},
}
}
async fn handle_notify(
&self,
name: String,
_args: Vec<Value>,
_neovim: Neovim<Compat<Stdout>>,
) {
match name.as_ref() {
"insert" => {},
"tick" => eprintln!("tock"),
_ => eprintln!("[!] unexpected notify",)
}
}
}
pub async fn run_nvim_plugin(tx: mpsc::Sender<i32>) -> Result<(), Box<(dyn std::error::Error + 'static)>> {
let handler: NeovimHandler = NeovimHandler::new(tx).await?;
let (nvim, io_handler) = new_parent(handler).await;
let curbuf = nvim.get_current_buf().await.unwrap();
let mut envargs = std::env::args();
let _ = envargs.next();
let testfile = envargs.next().unwrap();
std::fs::write(testfile, &format!("{:?}", curbuf.into_val())).unwrap();
// Any error should probably be logged, as stderr is not visible to users.
match io_handler.await {
Err( err) => eprintln!("Error joining IO loop: '{}'", joinerr),
Ok(Err(err)) => {
if !err.is_reader_error() {
// One last try, since there wasn't an error with writing to the
// stream
nvim
.err_writeln(&format!("Error: '{}'", err))
.await
.unwrap_or_else(|e| {
// We could inspect this error to see what was happening, and
// maybe retry, but at this point it's probably best
// to assume the worst and print a friendly and
// supportive message to our users
eprintln!("Well, dang... '{}'", e);
});
}
if !err.is_channel_closed() {
// Closed channel usually means neovim quit itself, or this plugin was
// told to quit by closing the channel, so it's not always an error
// condition.
eprintln!("Error: '{}'", err);
// let mut source = err.source();
// while let Some(e) = source {
// eprintln!("Caused by: '{}'", e);
// source = e.source();
// }
}
}
Ok(Ok(())) => {}
}
Ok(())
}

View file

@ -44,17 +44,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut client = GreeterClient::connect("http://[::1]:50051").await?;
let request = tonic::Request::new(HelloRequest {
name: "Tonic".into(),
});
let response = client.say_hello(request).await?;
println!("RESPONSE={:?}", response);
Ok(())
let mut client = GreeterClient::connect("http://[::1]:50051").await?;
let request = tonic::Request::new(HelloRequest {
name: "Tonic".into(),
});
let response = client.say_hello(request).await?;
println!("RESPONSE={:?}", response);
Ok(())
}
*/