Moved to new version of neovim-lib, that fixes some issues
This commit is contained in:
53
src/nvim.rs
53
src/nvim.rs
@@ -1,4 +1,4 @@
|
||||
use neovim_lib::{Neovim, NeovimApi, Session, Value, Integer, UiAttachOptions, CallError};
|
||||
use neovim_lib::{Handler, Neovim, NeovimApi, Session, Value, UiAttachOptions, CallError};
|
||||
use std::io::{Result, Error, ErrorKind};
|
||||
use std::result;
|
||||
use ui_model::{UiModel, ModelRect};
|
||||
@@ -43,30 +43,27 @@ pub trait GuiApi {
|
||||
}
|
||||
|
||||
macro_rules! try_str {
|
||||
($exp:expr) => (match $exp {
|
||||
Value::String(ref val) => val,
|
||||
($exp:expr) => (match $exp.as_str() {
|
||||
Some(val) => val,
|
||||
_ => return Err("Can't convert argument to string".to_owned())
|
||||
})
|
||||
}
|
||||
|
||||
macro_rules! try_int {
|
||||
($expr:expr) => (match $expr {
|
||||
Value::Integer(Integer::U64(val)) => val as i64,
|
||||
Value::Integer(Integer::I64(val)) => val,
|
||||
($expr:expr) => (match $expr.as_i64() {
|
||||
Some(val) => val,
|
||||
_ => return Err("Can't convert argument to int".to_owned())
|
||||
})
|
||||
}
|
||||
|
||||
macro_rules! try_uint {
|
||||
($exp:expr) => (match $exp {
|
||||
Value::Integer(Integer::U64(val)) => val,
|
||||
($exp:expr) => (match $exp.as_u64() {
|
||||
Some(val) => val,
|
||||
_ => return Err("Can't convert argument to u64".to_owned())
|
||||
})
|
||||
}
|
||||
|
||||
pub fn initialize(ui: &mut Shell,
|
||||
nvim_bin_path: Option<&String>)
|
||||
-> Result<()> {
|
||||
pub fn initialize(ui: &mut Shell, nvim_bin_path: Option<&String>) -> Result<()> {
|
||||
let session = if let Some(path) = nvim_bin_path {
|
||||
Session::new_child_path(path)?
|
||||
} else {
|
||||
@@ -79,7 +76,7 @@ pub fn initialize(ui: &mut Shell,
|
||||
|
||||
let mut nvim = ui.nvim();
|
||||
|
||||
nvim.session.start_event_loop_cb(move |m, p| nvim_cb(m, p));
|
||||
nvim.session.start_event_loop_handler(NvimHandler::new());
|
||||
nvim.ui_attach(80, 24, UiAttachOptions::new()).map_err(|e| Error::new(ErrorKind::Other, e))?;
|
||||
nvim.command("runtime! ginit.vim").map_err(|e| Error::new(ErrorKind::Other, e))?;
|
||||
|
||||
@@ -92,6 +89,20 @@ pub fn open_file(nvim: &mut NeovimApi, file: Option<&String>) {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct NvimHandler {}
|
||||
|
||||
impl NvimHandler {
|
||||
pub fn new() -> NvimHandler {
|
||||
NvimHandler {}
|
||||
}
|
||||
}
|
||||
|
||||
impl Handler for NvimHandler {
|
||||
fn handle_notify(&mut self, name: &str, args: &Vec<Value>) {
|
||||
nvim_cb(name, args.clone());
|
||||
}
|
||||
}
|
||||
|
||||
fn nvim_cb(method: &str, params: Vec<Value>) {
|
||||
match method {
|
||||
"redraw" => {
|
||||
@@ -99,8 +110,8 @@ fn nvim_cb(method: &str, params: Vec<Value>) {
|
||||
let mut repaint_mode = RepaintMode::Nothing;
|
||||
|
||||
for ev in ¶ms {
|
||||
if let &Value::Array(ref ev_args) = ev {
|
||||
if let Value::String(ref ev_name) = ev_args[0] {
|
||||
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(),
|
||||
@@ -123,7 +134,7 @@ fn nvim_cb(method: &str, params: Vec<Value>) {
|
||||
}
|
||||
"Gui" => {
|
||||
if params.len() > 0 {
|
||||
if let Value::String(ev_name) = params[0].clone() {
|
||||
if let Some(ev_name) = params[0].as_str().map(String::from) {
|
||||
let args = params.iter().skip(1).cloned().collect();
|
||||
safe_call(move |ui| {
|
||||
call_gui_event(ui, &ev_name, &args)?;
|
||||
@@ -183,7 +194,7 @@ fn call(ui: &mut Shell, method: &str, args: &Vec<Value>) -> result::Result<Repai
|
||||
_ => {
|
||||
println!("Event {}({:?})", method, args);
|
||||
RepaintMode::Nothing
|
||||
},
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -191,9 +202,11 @@ fn safe_call<F>(cb: F)
|
||||
where F: Fn(&mut Shell) -> result::Result<(), String> + 'static + Send
|
||||
{
|
||||
glib::idle_add(move || {
|
||||
SHELL!(shell = { if let Err(msg) = cb(&mut shell) {
|
||||
println!("Error call function: {}", msg);
|
||||
}});
|
||||
SHELL!(shell = {
|
||||
if let Err(msg) = cb(&mut shell) {
|
||||
println!("Error call function: {}", msg);
|
||||
}
|
||||
});
|
||||
glib::Continue(false)
|
||||
});
|
||||
}
|
||||
@@ -250,7 +263,7 @@ mod tests {
|
||||
assert_eq!(1, rect.left);
|
||||
assert_eq!(1, rect.right);
|
||||
}
|
||||
_ => panic!("mode is worng")
|
||||
_ => panic!("mode is worng"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
27
src/shell.rs
27
src/shell.rs
@@ -11,7 +11,7 @@ use glib;
|
||||
use gtk::prelude::*;
|
||||
use gtk::DrawingArea;
|
||||
|
||||
use neovim_lib::{Neovim, NeovimApi, Value, Integer};
|
||||
use neovim_lib::{Neovim, NeovimApi, Value};
|
||||
|
||||
use settings;
|
||||
use ui_model::{UiModel, Cell, Attrs, Color, ModelRect, COLOR_BLACK, COLOR_WHITE, COLOR_RED};
|
||||
@@ -298,8 +298,8 @@ fn draw(shell: &Shell, ctx: &cairo::Context) {
|
||||
let line_height = shell.line_height.unwrap();
|
||||
let char_width = shell.char_width.unwrap();
|
||||
let clip = ctx.clip_extents();
|
||||
let mut model_clip = ModelRect::from_area(line_height, char_width,
|
||||
clip.0, clip.1, clip.2, clip.3);
|
||||
let mut model_clip =
|
||||
ModelRect::from_area(line_height, char_width, clip.0, clip.1, clip.2, clip.3);
|
||||
shell.model.limit_to_model(&mut model_clip);
|
||||
|
||||
let line_x = model_clip.left as f64 * char_width;
|
||||
@@ -356,7 +356,13 @@ fn draw(shell: &Shell, ctx: &cairo::Context) {
|
||||
let (bg, fg) = shell.colors(cell);
|
||||
|
||||
if row == line_idx && col == col_idx {
|
||||
shell.cursor.draw(ctx, shell, char_width, line_height, line_y, double_width, bg);
|
||||
shell.cursor.draw(ctx,
|
||||
shell,
|
||||
char_width,
|
||||
line_height,
|
||||
line_y,
|
||||
double_width,
|
||||
bg);
|
||||
|
||||
ctx.move_to(current_point.0, current_point.1);
|
||||
}
|
||||
@@ -536,8 +542,7 @@ impl RedrawEvents for Shell {
|
||||
&RepaintMode::Area(ref rect) => {
|
||||
match (&self.line_height, &self.char_width) {
|
||||
(&Some(line_height), &Some(char_width)) => {
|
||||
let (x, y, width, height) =
|
||||
rect.to_area(line_height, char_width);
|
||||
let (x, y, width, height) = rect.to_area(line_height, char_width);
|
||||
self.drawing_area.queue_draw_area(x, y, width, height);
|
||||
}
|
||||
_ => self.drawing_area.queue_draw(),
|
||||
@@ -560,20 +565,20 @@ impl RedrawEvents for Shell {
|
||||
let mut model_attrs = Attrs::new();
|
||||
|
||||
for &(ref key_val, ref val) in attrs {
|
||||
if let &Value::String(ref key) = key_val {
|
||||
match key.as_ref() {
|
||||
if let Some(key) = key_val.as_str() {
|
||||
match key {
|
||||
"foreground" => {
|
||||
if let &Value::Integer(Integer::U64(fg)) = val {
|
||||
if let Some(fg) = val.as_u64() {
|
||||
model_attrs.foreground = Some(split_color(fg));
|
||||
}
|
||||
}
|
||||
"background" => {
|
||||
if let &Value::Integer(Integer::U64(bg)) = val {
|
||||
if let Some(bg) = val.as_u64() {
|
||||
model_attrs.background = Some(split_color(bg));
|
||||
}
|
||||
}
|
||||
"special" => {
|
||||
if let &Value::Integer(Integer::U64(bg)) = val {
|
||||
if let Some(bg) = val.as_u64() {
|
||||
model_attrs.special = Some(split_color(bg));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ pub fn can_close_window(ui: &Ui) -> bool {
|
||||
} else {
|
||||
true
|
||||
}
|
||||
},
|
||||
}
|
||||
Err(ref err) => {
|
||||
println!("Error getting info from nvim: {}", err);
|
||||
true
|
||||
@@ -22,11 +22,11 @@ pub fn can_close_window(ui: &Ui) -> bool {
|
||||
|
||||
fn show_not_saved_dlg(ui: &Ui, changed_bufs: &Vec<String>) -> bool {
|
||||
let flags = gtk::DIALOG_MODAL | gtk::DIALOG_DESTROY_WITH_PARENT;
|
||||
let dlg = Dialog::new_with_buttons(Some("Question"), ui.window.as_ref(), flags, &[
|
||||
("_OK", gtk_sys::GTK_RESPONSE_ACCEPT as i32),
|
||||
("_Cancel",
|
||||
gtk_sys::GTK_RESPONSE_REJECT as i32)
|
||||
]);
|
||||
let dlg = Dialog::new_with_buttons(Some("Question"),
|
||||
ui.window.as_ref(),
|
||||
flags,
|
||||
&[("_OK", gtk_sys::GTK_RESPONSE_ACCEPT as i32),
|
||||
("_Cancel", gtk_sys::GTK_RESPONSE_REJECT as i32)]);
|
||||
|
||||
dlg.run();
|
||||
|
||||
@@ -36,23 +36,27 @@ fn show_not_saved_dlg(ui: &Ui, changed_bufs: &Vec<String>) -> bool {
|
||||
fn get_changed_buffers() -> Result<Vec<String>, CallError> {
|
||||
SHELL!(shell = {
|
||||
let mut nvim = shell.nvim();
|
||||
let buffers = nvim.get_buffers()?;
|
||||
let buffers = nvim.get_buffers().unwrap();
|
||||
|
||||
Ok(buffers.iter()
|
||||
.map(|buf| {
|
||||
(match buf.get_option(&mut nvim, "modified") {
|
||||
Ok(Value::Boolean(val)) => val,
|
||||
_ => {
|
||||
Ok(_) => {
|
||||
println!("Value must be boolean");
|
||||
false
|
||||
},
|
||||
}
|
||||
Err(ref err) => {
|
||||
println!("Something going wrong while getting buffer option: {}", err);
|
||||
false
|
||||
}
|
||||
},
|
||||
match buf.get_name(&mut nvim) {
|
||||
Ok(name) => name,
|
||||
Err(ref err) => {
|
||||
println!("Something going wrong while getting buffer name: {}", err);
|
||||
"<Error>".to_owned()
|
||||
},
|
||||
}
|
||||
})
|
||||
})
|
||||
.filter(|e| e.0)
|
||||
|
||||
Reference in New Issue
Block a user