From bb9adcc1d59f46315c00b9de8a8e642a97b83af3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christopher=20L=C3=BCbbemeier?= Date: Tue, 20 Mar 2018 18:43:31 +0100 Subject: [PATCH] Open multiple files in one instance --- src/main.rs | 23 +++++++++++------------ src/nvim/mod.rs | 18 +++++++++++++----- src/shell.rs | 19 +++++++++---------- 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/src/main.rs b/src/main.rs index 95a7794..c269fbc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,7 +63,6 @@ use gio::prelude::*; use ui::Ui; -use misc::escape_filename; use shell::ShellOptions; const BIN_PATH_ARG: &str = "--nvim-bin-path"; @@ -104,23 +103,23 @@ fn main() { } fn open(app: >k::Application, files: &[gio::File], _: &str) { - for f in files { - let mut ui = Ui::new(ShellOptions::new( - nvim_bin_path(std::env::args()), - f.get_path().and_then(|p| { - p.to_str().map(|path| escape_filename(path).to_string()) - }), - nvim_timeout(std::env::args()), - )); + let files_list: Vec = files + .into_iter() + .filter_map(|f| f.get_path()?.to_str().map(str::to_owned)) + .collect(); + let mut ui = Ui::new(ShellOptions::new( + nvim_bin_path(std::env::args()), + files_list, + nvim_timeout(std::env::args()), + )); - ui.init(app, !nvim_disable_win_state(std::env::args())); - } + ui.init(app, !nvim_disable_win_state(std::env::args())); } fn activate(app: >k::Application) { let mut ui = Ui::new(ShellOptions::new( nvim_bin_path(std::env::args()), - None, + Vec::new(), nvim_timeout(std::env::args()), )); diff --git a/src/nvim/mod.rs b/src/nvim/mod.rs index 925db89..01100a8 100644 --- a/src/nvim/mod.rs +++ b/src/nvim/mod.rs @@ -20,8 +20,9 @@ use std::result; use std::sync::Arc; use std::time::Duration; -use neovim_lib::{Neovim, NeovimApi, Session, UiAttachOptions}; +use neovim_lib::{Neovim, NeovimApi, NeovimApiAsync, Session, UiAttachOptions}; +use misc::escape_filename; use ui::UiMutex; use shell; use nvim_config::NvimConfig; @@ -138,7 +139,7 @@ pub fn start( pub fn post_start_init( nvim: NeovimClientAsync, - open_path: Option<&String>, + open_paths: Vec, cols: u64, rows: u64, ) -> result::Result<(), NvimInitError> { @@ -154,11 +155,18 @@ pub fn post_start_init( .command("runtime! ginit.vim") .map_err(NvimInitError::new_post_init)?; - if let Some(path) = open_path { + if !open_paths.is_empty() { + let command = open_paths + .iter() + .fold(":ar".to_owned(), |command, filename| { + let filename = escape_filename(filename); + command + " " + &filename + }); nvim.borrow() .unwrap() - .command(&format!("e {}", path)) - .map_err(NvimInitError::new_post_init)?; + .command_async(&command) + .cb(|r| r.report_err()) + .call(); } Ok(()) diff --git a/src/shell.rs b/src/shell.rs index 5fc1688..df91d60 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -382,19 +382,19 @@ impl UiState { #[derive(Clone)] pub struct ShellOptions { nvim_bin_path: Option, - open_path: Option, + open_paths: Vec, timeout: Option, } impl ShellOptions { pub fn new( nvim_bin_path: Option, - open_path: Option, + open_paths: Vec, timeout: Option, ) -> Self { ShellOptions { nvim_bin_path, - open_path, + open_paths, timeout, } } @@ -580,12 +580,13 @@ impl Shell { .drawing_area .connect_drag_data_received(move |_, _, _, _, s, _, _| { let uris = s.get_uris(); - let command = uris.iter() - .filter_map(|uri| decode_uri(uri)) - .fold(":ar".to_owned(), |command, filename| { + let command = uris.iter().filter_map(|uri| decode_uri(uri)).fold( + ":ar".to_owned(), + |command, filename| { let filename = escape_filename(&filename); command + " " + &filename - }); + }, + ); let state = ref_state.borrow_mut(); let mut nvim = state.nvim().unwrap(); nvim.command_async(&command).cb(|r| r.report_err()).call() @@ -884,9 +885,7 @@ fn init_nvim_async( }); // attach ui - if let Err(err) = - nvim::post_start_init(nvim, options.open_path.as_ref(), cols as u64, rows as u64) - { + if let Err(err) = nvim::post_start_init(nvim, options.open_paths, cols as u64, rows as u64) { show_nvim_init_error(&err, state_arc.clone()); } else { set_nvim_initialized(state_arc);