neovim-gtk/src/nvim.rs

325 lines
10 KiB
Rust
Raw Normal View History

2016-03-19 10:27:39 +00:00
use std::io::{Result, Error, ErrorKind};
2016-03-28 14:03:21 +00:00
use std::result;
use std::sync::Arc;
use ui::UiMutex;
use neovim_lib::{Handler, Neovim, NeovimApi, Session, Value, UiAttachOptions, CallError};
2017-04-22 11:31:06 +00:00
use ui_model::{ModelRect, ModelRectVec};
use shell;
2016-03-28 14:14:10 +00:00
use glib;
2016-03-19 10:27:39 +00:00
2016-03-23 11:59:18 +00:00
pub trait RedrawEvents {
2017-03-18 09:45:05 +00:00
fn on_cursor_goto(&mut self, row: u64, col: u64) -> RepaintMode;
2016-03-28 15:05:10 +00:00
2017-03-18 09:45:05 +00:00
fn on_put(&mut self, text: &str) -> RepaintMode;
2016-03-29 09:22:16 +00:00
2017-03-18 09:45:05 +00:00
fn on_clear(&mut self) -> RepaintMode;
2016-03-29 09:43:01 +00:00
2017-03-18 09:45:05 +00:00
fn on_resize(&mut self, columns: u64, rows: u64) -> RepaintMode;
2016-03-31 13:52:22 +00:00
2017-03-17 22:18:41 +00:00
fn on_redraw(&self, mode: &RepaintMode);
2016-03-31 16:19:08 +00:00
2017-03-18 09:45:05 +00:00
fn on_highlight_set(&mut self, attrs: &Vec<(Value, Value)>) -> RepaintMode;
2017-03-18 09:45:05 +00:00
fn on_eol_clear(&mut self) -> RepaintMode;
2016-04-03 15:13:18 +00:00
2017-03-18 09:45:05 +00:00
fn on_set_scroll_region(&mut self, top: u64, bot: u64, left: u64, right: u64) -> RepaintMode;
2016-04-03 15:13:18 +00:00
2017-03-18 09:45:05 +00:00
fn on_scroll(&mut self, count: i64) -> RepaintMode;
2017-03-18 09:45:05 +00:00
fn on_update_bg(&mut self, bg: i64) -> RepaintMode;
2017-03-18 09:45:05 +00:00
fn on_update_fg(&mut self, fg: i64) -> RepaintMode;
2016-05-04 06:23:39 +00:00
2017-03-18 09:45:05 +00:00
fn on_update_sp(&mut self, sp: i64) -> RepaintMode;
2017-03-18 09:45:05 +00:00
fn on_mode_change(&mut self, mode: &str) -> RepaintMode;
fn on_mouse(&mut self, on: bool) -> RepaintMode;
fn on_busy(&mut self, busy: bool) -> RepaintMode;
fn popupmenu_show(&mut self,
menu: &Vec<Vec<&str>>,
selected: i64,
row: u64,
col: u64)
-> RepaintMode;
fn popupmenu_hide(&mut self) -> RepaintMode;
fn popupmenu_select(&mut self, selected: i64) -> RepaintMode;
2016-03-28 14:03:21 +00:00
}
2017-03-06 21:05:48 +00:00
pub trait GuiApi {
fn set_font(&mut self, font_desc: &str);
}
2016-03-28 14:03:21 +00:00
macro_rules! try_str {
($exp:expr) => ($exp.as_str().ok_or("Can't convert argument to string".to_owned())?)
2016-03-28 14:03:21 +00:00
}
macro_rules! try_int {
($expr:expr) => ($expr.as_i64().ok_or("Can't convert argument to int".to_owned())?)
2016-04-03 15:13:18 +00:00
}
macro_rules! try_uint {
($exp:expr) => ($exp.as_u64().ok_or("Can't convert argument to u64".to_owned())?)
2016-03-19 10:27:39 +00:00
}
pub fn initialize(shell: Arc<UiMutex<shell::State>>,
nvim_bin_path: Option<&String>,
external_popup: bool)
-> Result<Neovim> {
let session = if let Some(path) = nvim_bin_path {
2017-04-27 15:49:09 +00:00
match Session::new_child_path(path) {
Err(e) => {
println!("Error execute {}", path);
return Err(From::from(e));
}
Ok(s) => s,
}
} else {
Session::new_child()?
};
let mut nvim = Neovim::new(session);
nvim.session
.start_event_loop_handler(NvimHandler::new(shell));
let mut opts = UiAttachOptions::new();
opts.set_popupmenu_external(external_popup);
nvim.ui_attach(80, 24, opts)
.map_err(|e| Error::new(ErrorKind::Other, e))?;
nvim.command("runtime! ginit.vim")
.map_err(|e| Error::new(ErrorKind::Other, e))?;
Ok(nvim)
}
pub struct NvimHandler {
shell: Arc<UiMutex<shell::State>>,
2017-03-14 20:12:31 +00:00
}
impl NvimHandler {
pub fn new(shell: Arc<UiMutex<shell::State>>) -> NvimHandler {
NvimHandler { shell: shell }
}
fn nvim_cb(&self, method: &str, params: Vec<Value>) {
match method {
"redraw" => {
self.safe_call(move |ui| {
let mut repaint_mode = RepaintMode::Nothing;
for ev in &params {
if let Some(ev_args) = ev.as_array() {
if let Some(ev_name) = ev_args[0].as_str() {
for ref local_args in ev_args.iter().skip(1) {
let args = match *local_args {
&Value::Array(ref ar) => ar.clone(),
_ => vec![],
};
let call_reapint_mode = call(ui, ev_name, &args)?;
2017-04-22 19:25:05 +00:00
repaint_mode = repaint_mode.join(call_reapint_mode);
}
} else {
println!("Unsupported event {:?}", ev_args);
2017-03-06 21:05:48 +00:00
}
} else {
println!("Unsupported event type {:?}", ev);
2017-03-06 21:05:48 +00:00
}
2016-03-31 12:08:32 +00:00
}
2017-03-06 21:05:48 +00:00
ui.on_redraw(&repaint_mode);
Ok(())
});
}
"Gui" => {
if params.len() > 0 {
if let Some(ev_name) = params[0].as_str().map(String::from) {
let args = params.iter().skip(1).cloned().collect();
self.safe_call(move |ui| {
call_gui_event(ui, &ev_name, &args)?;
ui.on_redraw(&RepaintMode::All);
Ok(())
});
} else {
println!("Unsupported event {:?}", params);
}
2016-03-23 11:59:18 +00:00
} else {
2017-03-06 21:05:48 +00:00
println!("Unsupported event {:?}", params);
2016-03-19 10:27:39 +00:00
}
}
_ => {
println!("Notification {}({:?})", method, params);
2016-03-19 10:27:39 +00:00
}
}
}
fn safe_call<F>(&self, cb: F)
where F: Fn(&mut shell::State) -> result::Result<(), String> + 'static + Send
{
let shell = self.shell.clone();
glib::idle_add(move || {
if let Err(msg) = cb(&mut shell.borrow_mut()) {
println!("Error call function: {}", msg);
}
glib::Continue(false)
});
}
}
impl Handler for NvimHandler {
fn handle_notify(&mut self, name: &str, args: &Vec<Value>) {
self.nvim_cb(name, args.clone());
2017-03-06 21:05:48 +00:00
}
}
2016-03-31 13:52:22 +00:00
fn call_gui_event(ui: &mut shell::State,
method: &str,
args: &Vec<Value>)
-> result::Result<(), String> {
2017-03-06 21:05:48 +00:00
match method {
"Font" => ui.set_font(try_str!(args[0])),
_ => return Err(format!("Unsupported event {}({:?})", method, args)),
2016-03-19 10:27:39 +00:00
}
2017-03-06 21:05:48 +00:00
Ok(())
2016-03-19 10:27:39 +00:00
}
2016-03-28 14:03:21 +00:00
fn call(ui: &mut shell::State,
method: &str,
args: &Vec<Value>)
-> result::Result<RepaintMode, String> {
let repaint_mode = match method {
"cursor_goto" => ui.on_cursor_goto(try_uint!(args[0]), try_uint!(args[1])),
"put" => ui.on_put(try_str!(args[0])),
"clear" => ui.on_clear(),
"resize" => ui.on_resize(try_uint!(args[0]), try_uint!(args[1])),
"highlight_set" => {
if let Value::Map(ref attrs) = args[0] {
ui.on_highlight_set(attrs);
} else {
panic!("Supports only map value as argument");
}
RepaintMode::Nothing
}
"eol_clear" => ui.on_eol_clear(),
"set_scroll_region" => {
ui.on_set_scroll_region(try_uint!(args[0]),
try_uint!(args[1]),
try_uint!(args[2]),
try_uint!(args[3]));
RepaintMode::Nothing
}
"scroll" => ui.on_scroll(try_int!(args[0])),
"update_bg" => ui.on_update_bg(try_int!(args[0])),
"update_fg" => ui.on_update_fg(try_int!(args[0])),
"update_sp" => ui.on_update_sp(try_int!(args[0])),
"mode_change" => ui.on_mode_change(try_str!(args[0])),
"mouse_on" => ui.on_mouse(true),
"mouse_off" => ui.on_mouse(false),
"busy_start" => ui.on_busy(true),
"busy_stop" => ui.on_busy(false),
"popupmenu_show" => {
let mut menu_items = Vec::new();
let items = args[0].as_array().ok_or("Error get menu list array")?;
for item in items {
let item_line: result::Result<Vec<_>, &str> = item.as_array()
.ok_or("Error get menu item array")?
.iter()
.map(|col| col.as_str().ok_or("Error get menu column"))
.collect();
menu_items.push(item_line?);
}
ui.popupmenu_show(&menu_items,
try_int!(args[1]),
try_uint!(args[2]),
try_uint!(args[3]))
}
"popupmenu_hide" => ui.popupmenu_hide(),
"popupmenu_select" => ui.popupmenu_select(try_int!(args[0])),
_ => {
println!("Event {}({:?})", method, args);
RepaintMode::Nothing
}
};
Ok(repaint_mode)
2016-03-28 14:03:21 +00:00
}
2017-03-08 19:22:58 +00:00
pub trait ErrorReport {
2017-03-09 13:18:13 +00:00
fn report_err(&self, nvim: &mut NeovimApi);
2017-03-08 19:22:58 +00:00
}
impl<T> ErrorReport for result::Result<T, CallError> {
2017-03-09 13:18:13 +00:00
fn report_err(&self, _: &mut NeovimApi) {
if let &Err(ref err) = self {
println!("{}", err);
2017-03-08 19:22:58 +00:00
//nvim.report_error(&err_msg).expect("Error report error :)");
}
}
}
2017-03-17 22:18:41 +00:00
2017-04-22 19:18:59 +00:00
#[derive(Clone, Debug)]
2017-03-17 22:18:41 +00:00
pub enum RepaintMode {
2017-03-18 09:45:05 +00:00
Nothing,
2017-03-17 22:18:41 +00:00
All,
2017-04-22 11:31:06 +00:00
AreaList(ModelRectVec),
2017-03-17 22:18:41 +00:00
Area(ModelRect),
}
2017-03-18 09:45:05 +00:00
impl RepaintMode {
2017-04-22 19:25:05 +00:00
pub fn join(self, mode: RepaintMode) -> RepaintMode {
2017-03-18 09:45:05 +00:00
match (self, mode) {
2017-04-22 19:25:05 +00:00
(RepaintMode::Nothing, m) => m,
(m, RepaintMode::Nothing) => m,
(RepaintMode::All, _) => RepaintMode::All,
(_, RepaintMode::All) => RepaintMode::All,
(RepaintMode::Area(mr1), RepaintMode::Area(mr2)) => {
let mut vec = ModelRectVec::new(mr1);
vec.join(&mr2);
2017-04-22 11:31:06 +00:00
RepaintMode::AreaList(vec)
}
2017-04-22 19:25:05 +00:00
(RepaintMode::AreaList(mut target), RepaintMode::AreaList(source)) => {
2017-04-22 19:18:59 +00:00
for s in &source.list {
2017-04-22 19:25:05 +00:00
target.join(&s);
2017-04-22 19:18:59 +00:00
}
2017-04-22 19:25:05 +00:00
RepaintMode::AreaList(target)
2017-04-22 11:31:06 +00:00
}
2017-04-22 19:25:05 +00:00
(RepaintMode::AreaList(mut list), RepaintMode::Area(l2)) => {
list.join(&l2);
2017-04-22 11:31:06 +00:00
RepaintMode::AreaList(list)
}
2017-04-22 19:25:05 +00:00
(RepaintMode::Area(l1), RepaintMode::AreaList(mut list)) => {
list.join(&l1);
2017-04-22 11:31:06 +00:00
RepaintMode::AreaList(list)
2017-03-18 09:45:05 +00:00
}
}
}
}
2017-03-20 15:18:08 +00:00
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_mode() {
let mode = RepaintMode::Area(ModelRect::point(1, 1));
2017-04-23 08:48:00 +00:00
let mode = mode.join(RepaintMode::Nothing);
2017-03-20 15:18:08 +00:00
match mode {
RepaintMode::Area(ref rect) => {
assert_eq!(1, rect.top);
assert_eq!(1, rect.bot);
assert_eq!(1, rect.left);
assert_eq!(1, rect.right);
}
_ => panic!("mode is worng"),
2017-03-20 15:18:08 +00:00
}
}
}