mirror of
https://github.com/hexedtech/codemp.git
synced 2024-11-25 00:24:48 +01:00
fix: neovim rcp call and notify, added test buffer command
This commit is contained in:
parent
c213536c3b
commit
3425c57177
5 changed files with 71 additions and 92 deletions
|
@ -12,7 +12,7 @@ endif
|
||||||
|
|
||||||
let s:bin = "/home/alemi/projects/codemp/target/debug/codemp-client"
|
let s:bin = "/home/alemi/projects/codemp/target/debug/codemp-client"
|
||||||
|
|
||||||
function! codemp#init()
|
function codemp#init()
|
||||||
let result = s:StartJob()
|
let result = s:StartJob()
|
||||||
|
|
||||||
if 0 == result
|
if 0 == result
|
||||||
|
@ -21,11 +21,12 @@ function! codemp#init()
|
||||||
echoerr "codeMP: rpc process is not executable"
|
echoerr "codeMP: rpc process is not executable"
|
||||||
else
|
else
|
||||||
let s:jobid = result
|
let s:jobid = result
|
||||||
|
let g:codemp_jobid = result
|
||||||
call s:ConfigureJob(result)
|
call s:ConfigureJob(result)
|
||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:StartJob()
|
function s:StartJob()
|
||||||
if 0 == s:jobid
|
if 0 == s:jobid
|
||||||
let id = jobstart([s:bin], { 'rpc': v:true, 'on_stderr': function('s:OnStderr') })
|
let id = jobstart([s:bin], { 'rpc': v:true, 'on_stderr': function('s:OnStderr') })
|
||||||
return id
|
return id
|
||||||
|
@ -34,7 +35,7 @@ function! s:StartJob()
|
||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:StopJob()
|
function s:StopJob()
|
||||||
if 0 < s:jobid
|
if 0 < s:jobid
|
||||||
augroup codeMp
|
augroup codeMp
|
||||||
autocmd! " clear all previous autocommands
|
autocmd! " clear all previous autocommands
|
||||||
|
@ -53,7 +54,7 @@ function! s:StopJob()
|
||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:ConfigureJob(jobid)
|
function s:ConfigureJob(jobid)
|
||||||
augroup codeMp
|
augroup codeMp
|
||||||
" clear all previous autocommands
|
" clear all previous autocommands
|
||||||
autocmd!
|
autocmd!
|
||||||
|
@ -66,22 +67,28 @@ function! s:ConfigureJob(jobid)
|
||||||
augroup END
|
augroup END
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:NotifyInsertEnter()
|
function s:NotifyInsertEnter()
|
||||||
let [ bufnum, lnum, column, off ] = getpos('.')
|
" let [ bufnum, lnum, column, off ] = getpos('.')
|
||||||
call rpcnotify(s:jobid, 'insert-enter', v:insertmode, lnum, column)
|
call rpcnotify(s:jobid, 'insert', 1)
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:NotifyInsertLeave()
|
function s:NotifyInsertLeave()
|
||||||
|
call rpcnotify(s:jobid, 'insert', 0)
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! codemp#ping()
|
function codemp#buffer()
|
||||||
call rpcnotify(s:jobid, "ping")
|
call rpcrequest(s:jobid, "buffer")
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! codemp#test()
|
function codemp#ping()
|
||||||
call rpcnotify(s:jobid, "rpc")
|
call rpcrequest(s:jobid, "ping")
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:OnStderr(id, data, event) dict
|
function codemp#test()
|
||||||
echom 'codemp: stderr: ' . join(a:data, "\n")
|
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
|
endfunction
|
||||||
|
|
|
@ -2,30 +2,29 @@ pub mod manager;
|
||||||
mod nvim;
|
mod nvim;
|
||||||
|
|
||||||
use tokio::sync::mpsc;
|
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 manager::ConnectionManager;
|
||||||
use nvim::NeovimHandler;
|
use nvim::NeovimHandler;
|
||||||
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<(dyn std::error::Error + 'static)>> {
|
async fn main() -> Result<(), Box<(dyn std::error::Error + 'static)>> {
|
||||||
let (tx, rx) = mpsc::channel(32);
|
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 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.
|
// Any error should probably be logged, as stderr is not visible to users.
|
||||||
match io_handler.await {
|
match io_handler.await {
|
||||||
Err(err) => eprintln!("Error joining IO loop: {:?}", err),
|
Err(err) => eprintln!("Error joining IO loop: {:?}", err),
|
||||||
Ok(Err(err)) => eprintln!("Process ended with error: {:?}", err),
|
Ok(Err(err)) => eprintln!("Process ended with error: {:?}", err),
|
||||||
Ok(Ok(())) => println!("Finished"),
|
Ok(Ok(())) => eprintln!("Finished"),
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -25,11 +25,16 @@ impl ConnectionManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn process_packets(&mut self) {
|
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 {
|
loop {
|
||||||
if let Some(i) = self.rx.recv().await {
|
if let Some(i) = self.rx.recv().await {
|
||||||
let request = tonic::Request::new(SessionRequest {session_id: i});
|
let request = tonic::Request::new(SessionRequest {session_id: i});
|
||||||
let response = self.client.create(request).await.unwrap();
|
let response = self.client.create(request).await.unwrap();
|
||||||
println!("RESPONSE={:?}", response);
|
eprintln!("RESPONSE={:?}", response);
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,11 +3,7 @@ use rmpv::Value;
|
||||||
use tokio::io::Stdout;
|
use tokio::io::Stdout;
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
use nvim_rs::{compat::tokio::Compat, create::tokio::new_parent, rpc::IntoVal, Handler, Neovim};
|
use nvim_rs::{compat::tokio::Compat, Handler, Neovim};
|
||||||
use tonic::transport::Channel;
|
|
||||||
|
|
||||||
use crate::manager::proto_core::{session_client::SessionClient, SessionRequest};
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct NeovimHandler {
|
pub struct NeovimHandler {
|
||||||
|
@ -33,63 +29,35 @@ impl Handler for NeovimHandler {
|
||||||
match name.as_ref() {
|
match name.as_ref() {
|
||||||
"ping" => Ok(Value::from("pong")),
|
"ping" => Ok(Value::from("pong")),
|
||||||
"rpc" => {
|
"rpc" => {
|
||||||
|
eprintln!("Got 'rpc' from vim");
|
||||||
self.tx.send(0).await.unwrap();
|
self.tx.send(0).await.unwrap();
|
||||||
// let request = tonic::Request::new(SessionRequest {session_id: 0});
|
// let request = tonic::Request::new(SessionRequest {session_id: 0});
|
||||||
// let response = self.client.create(request).await.unwrap();
|
// let response = self.client.create(request).await.unwrap();
|
||||||
Ok(Value::from("sent"))
|
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(())
|
|
||||||
}
|
|
||||||
|
|
|
@ -44,17 +44,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let mut client = GreeterClient::connect("http://[::1]:50051").await?;
|
let mut client = GreeterClient::connect("http://[::1]:50051").await?;
|
||||||
|
|
||||||
let request = tonic::Request::new(HelloRequest {
|
let request = tonic::Request::new(HelloRequest {
|
||||||
name: "Tonic".into(),
|
name: "Tonic".into(),
|
||||||
});
|
});
|
||||||
|
|
||||||
let response = client.say_hello(request).await?;
|
let response = client.say_hello(request).await?;
|
||||||
|
|
||||||
println!("RESPONSE={:?}", response);
|
println!("RESPONSE={:?}", response);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in a new issue