Generate and use plug-vim config on nvim startup

This commit is contained in:
daa84 2017-10-26 17:56:53 +03:00
parent 2cefb06063
commit ba179f715d
11 changed files with 173 additions and 12 deletions

36
Cargo.lock generated
View File

@ -22,6 +22,7 @@ dependencies = [
"phf_codegen 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)",
"tempfile 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -277,6 +278,15 @@ name = "htmlescape"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "kernel32-sys"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lazy_static"
version = "0.2.8"
@ -437,6 +447,11 @@ dependencies = [
"magenta 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "redox_syscall"
version = "0.1.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "regex"
version = "0.2.2"
@ -519,6 +534,18 @@ dependencies = [
"unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "tempfile"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "thread_local"
version = "0.3.4"
@ -573,6 +600,11 @@ name = "winapi"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi-build"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "500909c4f87a9e52355b26626d890833e9e1d53ac566db76c36faa984b889699"
"checksum atk-sys 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c69658a4e18d5c9575f716e24559645d08a4044d6946c30c2e0025952c84d842"
@ -598,6 +630,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum gtk 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ce17f98e7dcdc9d06b3a5f7621d796a24937c04953481205b1be267c5a02697a"
"checksum gtk-sys 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "854b56ce6d6b05945f7735651482835c5ac1f8582142ce67306726259a3dafb0"
"checksum htmlescape 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e9025058dae765dee5070ec375f591e2ba14638c63feff74f13805a72e523163"
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
"checksum lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3b37545ab726dd833ec6420aaba8231c5b320814b9029ad585555d2a03e94fbf"
"checksum libc 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)" = "8a014d9226c2cc402676fbe9ea2e15dd5222cd1dd57f576b5b283178c944a264"
"checksum log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "880f77541efa6e5cc74e76910c9884d9859683118839d6a1dc3b11e63512565b"
@ -617,6 +650,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "3a8b4c6b8165cd1a1cd4b9b120978131389f64bdaf456435caa41e630edba903"
"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
"checksum rand 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)" = "eb250fd207a4729c976794d03db689c9be1d634ab5a1c9da9492a13d8fecbcdf"
"checksum redox_syscall 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)" = "8dde11f18c108289bef24469638a04dce49da56084f2d50618b226e47eb04509"
"checksum regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1731164734096285ec2a5ec7fea5248ae2f5485b3feeb0115af4fda2183b2d1b"
"checksum regex-syntax 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad890a5eef7953f55427c50575c680c42841653abd2b028b68cd223d157f62db"
"checksum rmp 0.8.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7ce560a5728f4eec697f07f8d7fa20608893d44b4f5b8f9f5f51a2987f3cffe2"
@ -627,6 +661,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum siphasher 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0df90a788073e8d0235a67e50441d47db7c8ad9debd91cbf43736a2a92d36537"
"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad"
"checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6"
"checksum tempfile 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "11ce2fe9db64b842314052e2421ac61a73ce41b898dc8e3750398b219c5fc1e0"
"checksum thread_local 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "1697c4b57aeeb7a536b647165a2825faddffb1d3bad386d507709bd51a90bb14"
"checksum toml 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "a7540f4ffc193e0d3c94121edb19b055670d369f77d5804db11ae053a45b6e7e"
"checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc"
@ -635,3 +670,4 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122"
"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"

View File

@ -35,6 +35,8 @@ serde = "1.0"
serde_derive = "1.0"
toml = "0.4"
tempfile = "2.2.0"
#[dependencies.neovim-lib]
#git = "https://github.com/daa84/neovim-lib"

View File

@ -30,3 +30,4 @@ fn get_xdg_config_dir() -> Result<PathBuf, String> {
home_dir.push(".config");
Ok(home_dir)
}

View File

@ -23,8 +23,11 @@ extern crate serde;
extern crate serde_derive;
extern crate toml;
extern crate tempfile;
mod sys;
mod nvim_config;
mod dirs;
mod color;
mod value;

View File

@ -13,6 +13,7 @@ use ui::UiMutex;
use ui_model::{ModelRect, ModelRectVec};
use shell;
use glib;
use nvim_config::NvimConfig;
use value::ValueMapExt;
@ -227,6 +228,7 @@ impl error::Error for NvimInitError {
pub fn start(
shell: Arc<UiMutex<shell::State>>,
nvim_bin_path: Option<&String>,
nvim_config: NvimConfig,
) -> result::Result<Neovim, NvimInitError> {
let mut cmd = if let Some(path) = nvim_bin_path {
Command::new(path)
@ -257,6 +259,12 @@ pub fn start(
cmd.arg("--cmd").arg("let &rtp.=',runtime'");
}
if let Some(nvim_config) = nvim_config.generate_config() {
if let Some(path) = nvim_config.path().to_str() {
cmd.arg("--cmd").arg(format!("source '{}'", path));
}
}
let session = Session::new_child_cmd(&mut cmd);
let session = match session {

45
src/nvim_config.rs Normal file
View File

@ -0,0 +1,45 @@
use std;
use std::fs::File;
use std::io::Write;
use tempfile;
use plug_manager;
#[derive(Clone)]
pub struct NvimConfig {
plug_config: Option<plug_manager::PlugManagerConfigSource>,
}
impl NvimConfig {
pub fn new(plug_config: Option<plug_manager::PlugManagerConfigSource>) -> Self {
NvimConfig { plug_config }
}
pub fn generate_config(&self) -> Option<tempfile::NamedTempFile> {
if self.plug_config.is_some() {
match self.write_file() {
Err(err) => {
error!("{}", err);
None
}
Ok(file) => Some(file),
}
} else {
None
}
}
fn write_file(&self) -> std::io::Result<tempfile::NamedTempFile> {
let temp_file = tempfile::NamedTempFile::new()?;
{
let mut file: &File = &temp_file;
let content = &self.plug_config.as_ref().unwrap().source;
if !content.is_empty() {
file.write_all(content.as_bytes())?;
}
file.sync_data()?;
}
Ok(temp_file)
}
}

View File

@ -3,6 +3,7 @@ use std::cell::RefCell;
use super::vim_plug;
use super::store::Store;
use nvim::NeovimClient;
pub struct Manager {
@ -18,10 +19,12 @@ impl Manager {
}
}
pub fn generate_plug_config(&mut self) -> Option<String> {
pub fn load_config(&mut self) -> Option<PlugManagerConfigSource> {
if Store::is_config_exists() {
self.plug_manage_state = PlugManageState::NvimGtk(Store::load());
Some("TODO".to_owned())
let store = Store::load();
let config = PlugManagerConfigSource::new(&store);
self.plug_manage_state = PlugManageState::NvimGtk(store);
Some(config)
} else {
None
}
@ -45,3 +48,23 @@ pub enum PlugManageState {
Configuration(Store),
Unknown,
}
#[derive(Clone)]
pub struct PlugManagerConfigSource {
pub source: String,
}
impl PlugManagerConfigSource {
pub fn new(store: &Store) -> Self {
let mut builder = "call plug#begin()".to_owned();
for plug in store.get_plugs() {
builder += &format!("Plug '{}'", plug.get_plug_path());
}
builder += "call plug#end()";
PlugManagerConfigSource { source: builder }
}
}

View File

@ -4,4 +4,4 @@ mod store;
mod manager;
pub use self::ui::Ui;
pub use self::manager::Manager;
pub use self::manager::{Manager, PlugManagerConfigSource};

View File

@ -66,10 +66,43 @@ impl SettingsLoader for Settings {
pub struct PlugInfo {
pub name: String,
pub url: String,
pub removed: bool,
}
impl PlugInfo {
pub fn new(name: String, url: String) -> Self {
PlugInfo { name, url }
PlugInfo {
name,
url,
removed: false,
}
}
pub fn get_plug_path(&self) -> String {
if self.url.contains("github.com") {
let mut path_comps: Vec<&str> = self.url
.trim_right_matches(".git")
.rsplit('/')
.take(2)
.collect();
path_comps.reverse();
path_comps.join("/")
} else {
self.url.clone()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_plug_path() {
let plug = PlugInfo::new(
"rust.vim".to_owned(),
"https://git::@github.com/rust-lang/rust.vim.git".to_owned(),
);
assert_eq!("rust-lang/rust.vim".to_owned(), plug.get_plug_path());
}
}

View File

@ -22,6 +22,7 @@ use ui_model::{UiModel, Attrs, ModelRect};
use color::{ColorModel, Color, COLOR_BLACK, COLOR_WHITE, COLOR_RED};
use nvim;
use nvim::{RedrawEvents, GuiApi, RepaintMode, ErrorReport, NeovimClient};
use nvim_config::NvimConfig;
use input;
use input::keyval_to_input_string;
use cursor::Cursor;
@ -77,13 +78,14 @@ pub struct State {
resize_state: Rc<Cell<ResizeState>>,
options: ShellOptions,
nvim_config: NvimConfig,
detach_cb: Option<Box<RefCell<FnMut() + Send + 'static>>>,
nvim_started_cb: Option<Box<RefCell<FnMut() + Send + 'static>>>,
}
impl State {
pub fn new(settings: Rc<RefCell<Settings>>, options: ShellOptions) -> State {
pub fn new(settings: Rc<RefCell<Settings>>, options: ShellOptions, nvim_config: NvimConfig) -> State {
let drawing_area = gtk::DrawingArea::new();
let popup_menu = RefCell::new(PopupMenu::new(&drawing_area));
let font_ctx = render::Context::new(FontDescription::from_string(DEFAULT_FONT_NAME));
@ -111,6 +113,7 @@ impl State {
resize_state: Rc::new(Cell::new(ResizeState::Wait)),
options,
nvim_config,
detach_cb: None,
nvim_started_cb: None,
@ -390,9 +393,9 @@ pub struct Shell {
}
impl Shell {
pub fn new(settings: Rc<RefCell<Settings>>, options: ShellOptions) -> Shell {
pub fn new(settings: Rc<RefCell<Settings>>, options: ShellOptions, nvim_config: NvimConfig) -> Shell {
let shell = Shell {
state: Arc::new(UiMutex::new(State::new(settings, options))),
state: Arc::new(UiMutex::new(State::new(settings, options, nvim_config))),
ui_state: Rc::new(RefCell::new(UiState::new())),
widget: gtk::Box::new(gtk::Orientation::Vertical, 0),
@ -765,11 +768,12 @@ fn show_nvim_init_error(err: &nvim::NvimInitError, state_arc: Arc<UiMutex<State>
fn init_nvim_async(
state_arc: Arc<UiMutex<State>>,
options: ShellOptions,
nvim_config: NvimConfig,
cols: usize,
rows: usize,
) {
// execute nvim
let mut nvim = match nvim::start(state_arc.clone(), options.nvim_bin_path.as_ref()) {
let mut nvim = match nvim::start(state_arc.clone(), options.nvim_bin_path.as_ref(), nvim_config) {
Ok(nvim) => nvim,
Err(err) => {
show_nvim_start_error(&err, state_arc);
@ -864,7 +868,8 @@ fn init_nvim(state_ref: &Arc<UiMutex<State>>) {
let state_arc = state_ref.clone();
let options = state.options.clone();
thread::spawn(move || init_nvim_async(state_arc, options, cols, rows));
let nvim_config = state.nvim_config.clone();
thread::spawn(move || init_nvim_async(state_arc, options, nvim_config, cols, rows));
}
}

View File

@ -10,6 +10,7 @@ use gtk::{ApplicationWindow, HeaderBar, ToolButton, Image, AboutDialog};
use gio::prelude::*;
use gio::{Menu, MenuExt, MenuItem, MenuItemExt, SimpleAction};
use nvim_config::NvimConfig;
use settings::Settings;
use shell::{Shell, ShellOptions};
use shell_dlg;
@ -52,13 +53,17 @@ impl Components {
impl Ui {
pub fn new(options: ShellOptions) -> Ui {
let mut plug_manager = plug_manager::Manager::new();
let plug_config = plug_manager.load_config();
let nvim_config = NvimConfig::new(plug_config);
let plug_manager = Arc::new(UiMutex::new(plug_manager));
let comps = Arc::new(UiMutex::new(Components::new()));
let settings = Rc::new(RefCell::new(Settings::new()));
let shell = Rc::new(RefCell::new(Shell::new(settings.clone(), options)));
let shell = Rc::new(RefCell::new(Shell::new(settings.clone(), options, nvim_config)));
settings.borrow_mut().set_shell(Rc::downgrade(&shell));
let projects = Projects::new(&comps.borrow().open_btn, shell.clone());
let plug_manager = Arc::new(UiMutex::new(plug_manager::Manager::new()));
Ui {
initialized: false,