From e719535d2a0980830795522c3bfee16623b98551 Mon Sep 17 00:00:00 2001 From: daa Date: Tue, 14 Mar 2017 23:12:31 +0300 Subject: [PATCH] Fix two time run --- src/main.rs | 15 +++++++++------ src/nvim.rs | 13 +++++++------ src/ui.rs | 7 +++++++ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 895518e..c39966c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,14 +40,17 @@ fn main() { fn activate(app: >k::Application) { ui::UI.with(|ui_cell| { let mut ui = ui_cell.borrow_mut(); - ui.init(app); + if !ui.initialized { + ui.init(app); - let path = nvim_bin_path(std::env::args()); - let open_arg = open_arg(); - nvim::initialize(&mut *ui, path.as_ref(), open_arg.as_ref()) - .expect("Can't start nvim instance"); + let path = nvim_bin_path(std::env::args()); + nvim::initialize(&mut *ui, path.as_ref()) + .expect("Can't start nvim instance"); - guard_dispatch_thread(&mut *ui); + guard_dispatch_thread(&mut *ui); + } + + nvim::open_file(ui.nvim(), open_arg().as_ref()); }); } diff --git a/src/nvim.rs b/src/nvim.rs index 041cd2b..3b6afaa 100644 --- a/src/nvim.rs +++ b/src/nvim.rs @@ -65,8 +65,7 @@ macro_rules! try_uint { } pub fn initialize(ui: &mut Ui, - nvim_bin_path: Option<&String>, - open_arg: Option<&String>) + nvim_bin_path: Option<&String>) -> Result<()> { let session = if let Some(path) = nvim_bin_path { Session::new_child_path(path)? @@ -84,13 +83,15 @@ pub fn initialize(ui: &mut Ui, 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))?; - if let Some(ref file) = open_arg { - nvim.command(&format!("e {}", file)).report_err(nvim); - } - Ok(()) } +pub fn open_file(nvim: &mut NeovimApi, file: Option<&String>) { + if let Some(file_name) = file { + nvim.command(&format!("e {}", file_name)).report_err(nvim); + } +} + fn nvim_cb(method: &str, params: Vec) { match method { "redraw" => { diff --git a/src/ui.rs b/src/ui.rs index fa60b1c..f4a9db7 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -46,6 +46,7 @@ enum NvimMode { pub struct Ui { pub model: UiModel, + pub initialized: bool, nvim: Option, drawing_area: DrawingArea, window: Option, @@ -82,6 +83,7 @@ impl Ui { mouse_enabled: false, mouse_pressed: false, font_desc: FontDescription::from_string(FONT_NAME), + initialized: false, } } @@ -98,6 +100,11 @@ impl Ui { } pub fn init(&mut self, app: >k::Application) { + if self.initialized { + return + } + self.initialized = true; + SET.with(|settings| { let mut settings = settings.borrow_mut(); settings.init(self);