|
|
@ -1,13 +1,14 @@
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::convert::TryInto;
|
|
|
|
use std::convert::TryInto;
|
|
|
|
|
|
|
|
use std::any::{Any, TypeId};
|
|
|
|
|
|
|
|
|
|
|
|
use rmpv::Value;
|
|
|
|
pub use rmpv::Value;
|
|
|
|
use nvim_rs::Neovim;
|
|
|
|
use nvim_rs::Neovim;
|
|
|
|
use nvim_rs::compat::tokio::Compat;
|
|
|
|
use nvim_rs::compat::tokio::Compat;
|
|
|
|
use flexi_logger::{Logger, Criterion, Naming, Cleanup};
|
|
|
|
use flexi_logger::{Logger, Criterion, Naming, Cleanup};
|
|
|
|
use tokio::process::ChildStdin;
|
|
|
|
use tokio::process::ChildStdin;
|
|
|
|
use parking_lot::Mutex;
|
|
|
|
use parking_lot::RwLock;
|
|
|
|
use log::warn;
|
|
|
|
use log::{error,warn};
|
|
|
|
|
|
|
|
|
|
|
|
use crate::error_handling::ResultPanicExplanation;
|
|
|
|
use crate::error_handling::ResultPanicExplanation;
|
|
|
|
|
|
|
|
|
|
|
@ -15,120 +16,179 @@ lazy_static! {
|
|
|
|
pub static ref SETTINGS: Settings = Settings::new();
|
|
|
|
pub static ref SETTINGS: Settings = Settings::new();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
// Trait to allow for conversion from rmpv::Value to any other data type.
|
|
|
|
pub enum Setting {
|
|
|
|
// Note: Feel free to implement this trait for custom types in each subsystem.
|
|
|
|
Bool(bool),
|
|
|
|
// The reverse conversion (MyType->Value) can be performed by implementing `From<MyType> for Value`
|
|
|
|
U16(u16),
|
|
|
|
pub trait FromValue {
|
|
|
|
String(String)
|
|
|
|
fn from_value(&mut self, value: Value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Setting {
|
|
|
|
// FromValue implementations for most typical types
|
|
|
|
fn new_bool(value: bool) -> Setting {
|
|
|
|
impl FromValue for f32 {
|
|
|
|
Setting::Bool(value)
|
|
|
|
fn from_value(&mut self, value: Value) {
|
|
|
|
|
|
|
|
if value.is_f32() {
|
|
|
|
|
|
|
|
*self = value.as_f64().unwrap() as f32;
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
error!("Setting expected an f32, but received {:?}", value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn read_bool(&self) -> bool {
|
|
|
|
|
|
|
|
if let Setting::Bool(value) = self {
|
|
|
|
|
|
|
|
*value
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
panic!("Could not read setting as bool");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn new_u16(value: u16) -> Setting {
|
|
|
|
impl FromValue for u64 {
|
|
|
|
Setting::U16(value)
|
|
|
|
fn from_value(&mut self, value: Value) {
|
|
|
|
|
|
|
|
if value.is_u64() {
|
|
|
|
|
|
|
|
*self = value.as_u64().unwrap();
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
error!("Setting expected a u64, but received {:?}", value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn read_u16(&self) -> u16 {
|
|
|
|
|
|
|
|
if let Setting::U16(value) = self {
|
|
|
|
|
|
|
|
*value
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
panic!("Could not read setting as u16");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn new_string(value: String) -> Setting {
|
|
|
|
|
|
|
|
Setting::String(value)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn read_string(&self) -> String {
|
|
|
|
impl FromValue for u32 {
|
|
|
|
if let Setting::String(value) = self {
|
|
|
|
fn from_value(&mut self, value: Value) {
|
|
|
|
value.clone()
|
|
|
|
if value.is_u64() {
|
|
|
|
} else {
|
|
|
|
*self = value.as_u64().unwrap() as u32;
|
|
|
|
panic!("Could not read setting as string");
|
|
|
|
}else{
|
|
|
|
|
|
|
|
error!("Setting expected a u32, but received {:?}", value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn parse(&mut self, value: Value) {
|
|
|
|
impl FromValue for i32 {
|
|
|
|
match self {
|
|
|
|
fn from_value(&mut self, value: Value) {
|
|
|
|
Setting::Bool(internal_bool) => {
|
|
|
|
if value.is_i64() {
|
|
|
|
if let Ok(value) = value.try_into() {
|
|
|
|
*self = value.as_i64().unwrap() as i32;
|
|
|
|
let intermediate: u64 = value;
|
|
|
|
}else{
|
|
|
|
*internal_bool = intermediate != 0;
|
|
|
|
error!("Setting expected an i32, but received {:?}", value);
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
Setting::U16(internal_u16) => {
|
|
|
|
|
|
|
|
if let Ok(value) = value.try_into() {
|
|
|
|
|
|
|
|
let intermediate: u64 = value;
|
|
|
|
|
|
|
|
*internal_u16 = intermediate as u16;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
Setting::String(internal_string) => {
|
|
|
|
|
|
|
|
if let Ok(value) = value.try_into() {
|
|
|
|
|
|
|
|
let intermediate: String = value;
|
|
|
|
|
|
|
|
*internal_string = intermediate;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl FromValue for String {
|
|
|
|
|
|
|
|
fn from_value(&mut self, value: Value) {
|
|
|
|
|
|
|
|
if value.is_str() {
|
|
|
|
|
|
|
|
*self = String::from(value.as_str().unwrap());
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
error!("Setting expected a string, but received {:?}", value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn unparse(&self) -> Value {
|
|
|
|
impl FromValue for bool {
|
|
|
|
match self {
|
|
|
|
fn from_value(&mut self, value: Value) {
|
|
|
|
Setting::Bool(internal_bool) => {
|
|
|
|
if value.is_bool() {
|
|
|
|
let value = if *internal_bool {
|
|
|
|
*self = value.as_bool().unwrap();
|
|
|
|
1
|
|
|
|
}else{
|
|
|
|
} else {
|
|
|
|
error!("Setting expected a string, but received {:?}", value);
|
|
|
|
0
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Value::from(value)
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
Setting::U16(internal_u16) => Value::from(*internal_u16),
|
|
|
|
|
|
|
|
Setting::String(internal_string) => Value::from(internal_string.as_str()),
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn clone(&self) -> Setting {
|
|
|
|
// Macro to register settings changed handlers.
|
|
|
|
match self {
|
|
|
|
// Note: Invocations to this macro must happen before the call to Settings::read_initial_values.
|
|
|
|
Setting::Bool(_) => Setting::new_bool(self.read_bool()),
|
|
|
|
#[macro_export]
|
|
|
|
Setting::U16(_) => Setting::new_u16(self.read_u16()),
|
|
|
|
macro_rules! register_nvim_setting {
|
|
|
|
Setting::String(_) => Setting::new_string(self.read_string()),
|
|
|
|
($vim_setting_name: expr, $type_name:ident :: $field_name: ident) => {{
|
|
|
|
|
|
|
|
// The update func sets a new value for a setting
|
|
|
|
|
|
|
|
fn update_func(value: Value) {
|
|
|
|
|
|
|
|
let mut s = SETTINGS.get::<$type_name>();
|
|
|
|
|
|
|
|
s.$field_name.from_value(value);
|
|
|
|
|
|
|
|
SETTINGS.set(&s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// The reader func retrieves the current value for a setting
|
|
|
|
|
|
|
|
fn reader_func() -> Value {
|
|
|
|
|
|
|
|
let s = SETTINGS.get::<$type_name>();
|
|
|
|
|
|
|
|
s.$field_name.into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SETTINGS.set_setting_handlers($vim_setting_name, update_func, reader_func);
|
|
|
|
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Function types to handle settings updates
|
|
|
|
|
|
|
|
type UpdateHandlerFunc = fn(Value);
|
|
|
|
|
|
|
|
type ReaderFunc = fn()->Value;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// The Settings struct acts as a global container where each of Neovide's subsystems can store
|
|
|
|
|
|
|
|
// their own settings. It will also coordinate updates between Neovide and nvim to make sure the
|
|
|
|
|
|
|
|
// settings remain consistent on both sides.
|
|
|
|
|
|
|
|
// Note: As right now we're only sending new setting values to Neovide during the
|
|
|
|
|
|
|
|
// read_initial_values call, after that point we should not modify the contents of the Settings
|
|
|
|
|
|
|
|
// struct except when prompted by an update event from nvim. Otherwise, the settings in Neovide and
|
|
|
|
|
|
|
|
// nvim will get out of sync.
|
|
|
|
pub struct Settings {
|
|
|
|
pub struct Settings {
|
|
|
|
pub neovim_arguments: Vec<String>,
|
|
|
|
pub neovim_arguments: Vec<String>,
|
|
|
|
pub settings: Mutex<HashMap<String, Setting>>
|
|
|
|
settings: RwLock<HashMap<TypeId, Box<dyn Any + Send + Sync>>>,
|
|
|
|
|
|
|
|
listeners: RwLock<HashMap<String, UpdateHandlerFunc>>,
|
|
|
|
|
|
|
|
readers: RwLock<HashMap<String, ReaderFunc>>,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Settings {
|
|
|
|
impl Settings {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn new() -> Settings {
|
|
|
|
|
|
|
|
let neovim_arguments = std::env::args().filter(|arg| {
|
|
|
|
|
|
|
|
if arg == "--log" {
|
|
|
|
|
|
|
|
Logger::with_str("neovide")
|
|
|
|
|
|
|
|
.log_to_file()
|
|
|
|
|
|
|
|
.rotate(Criterion::Size(10_000_000), Naming::Timestamps, Cleanup::KeepLogFiles(1))
|
|
|
|
|
|
|
|
.start()
|
|
|
|
|
|
|
|
.expect("Could not start logger");
|
|
|
|
|
|
|
|
false
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}).collect::<Vec<String>>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Settings{
|
|
|
|
|
|
|
|
neovim_arguments,
|
|
|
|
|
|
|
|
settings: RwLock::new(HashMap::new()),
|
|
|
|
|
|
|
|
listeners: RwLock::new(HashMap::new()),
|
|
|
|
|
|
|
|
readers: RwLock::new(HashMap::new()),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn set_setting_handlers(&self, property_name: &str, update_func: UpdateHandlerFunc, reader_func: ReaderFunc) {
|
|
|
|
|
|
|
|
self.listeners.write().insert(String::from(property_name), update_func);
|
|
|
|
|
|
|
|
self.readers.write().insert(String::from(property_name), reader_func);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn set<T: Clone + Send + Sync + 'static >(&self, t: &T) {
|
|
|
|
|
|
|
|
let type_id : TypeId = TypeId::of::<T>();
|
|
|
|
|
|
|
|
let t : T = (*t).clone();
|
|
|
|
|
|
|
|
self.settings.write().insert(type_id, Box::new(t));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn get<'a, T: Clone + Send + Sync + 'static>(&'a self) -> T {
|
|
|
|
|
|
|
|
let read_lock = self.settings.read();
|
|
|
|
|
|
|
|
let boxed = &read_lock.get(&TypeId::of::<T>()).expect("Trying to retrieve a settings object that doesn't exist");
|
|
|
|
|
|
|
|
let value: &T = boxed.downcast_ref::<T>().expect("Attempted to extract a settings object of the wrong type");
|
|
|
|
|
|
|
|
(*value).clone()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn read_initial_values(&self, nvim: &Neovim<Compat<ChildStdin>>) {
|
|
|
|
pub async fn read_initial_values(&self, nvim: &Neovim<Compat<ChildStdin>>) {
|
|
|
|
let keys : Vec<String> = self.settings.lock().keys().cloned().collect();
|
|
|
|
let keys : Vec<String> = self.listeners.read().keys().cloned().collect();
|
|
|
|
for name in keys {
|
|
|
|
for name in keys {
|
|
|
|
let variable_name = format!("neovide_{}", name.to_string());
|
|
|
|
let variable_name = format!("neovide_{}", name.to_string());
|
|
|
|
match nvim.get_var(&variable_name).await {
|
|
|
|
match nvim.get_var(&variable_name).await {
|
|
|
|
Ok(value) => self.settings.lock().get_mut(&name).unwrap().parse(value),
|
|
|
|
Ok(value) => {
|
|
|
|
|
|
|
|
self.listeners.read().get(&name).unwrap()(value);
|
|
|
|
|
|
|
|
},
|
|
|
|
Err(error) => {
|
|
|
|
Err(error) => {
|
|
|
|
warn!("Initial value load failed for {}: {}", name, error);
|
|
|
|
warn!("Initial value load failed for {}: {}", name, error);
|
|
|
|
let setting = self.get(&name);
|
|
|
|
let setting = self.readers.read().get(&name).unwrap()();
|
|
|
|
nvim.set_var(&variable_name, setting.unparse()).await.ok();
|
|
|
|
nvim.set_var(&variable_name, setting).await.ok();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn setup_changed_listeners(&self, nvim: &Neovim<Compat<ChildStdin>>) {
|
|
|
|
pub async fn setup_changed_listeners(&self, nvim: &Neovim<Compat<ChildStdin>>) {
|
|
|
|
let keys : Vec<String> = self.settings.lock().keys().cloned().collect();
|
|
|
|
let keys : Vec<String> = self.listeners.read().keys().cloned().collect();
|
|
|
|
for name in keys {
|
|
|
|
for name in keys {
|
|
|
|
let vimscript = format!(
|
|
|
|
let vimscript = format!(
|
|
|
|
concat!(
|
|
|
|
concat!(
|
|
|
@ -151,44 +211,6 @@ impl Settings {
|
|
|
|
let name: Result<String, _>= name.try_into();
|
|
|
|
let name: Result<String, _>= name.try_into();
|
|
|
|
let name = name.unwrap();
|
|
|
|
let name = name.unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
self.settings.lock().get_mut(&name).unwrap().parse(value);
|
|
|
|
self.listeners.read().get(&name).unwrap()(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn get(&self, name: &str) -> Setting {
|
|
|
|
|
|
|
|
let settings = self.settings.lock();
|
|
|
|
|
|
|
|
let setting = settings.get(name).expect(&format!("Could not find option {}", name));
|
|
|
|
|
|
|
|
setting.clone()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn new() -> Settings {
|
|
|
|
|
|
|
|
let mut no_idle = false;
|
|
|
|
|
|
|
|
let mut buffer_frames = 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let neovim_arguments = std::env::args().filter(|arg| {
|
|
|
|
|
|
|
|
if arg == "--log" {
|
|
|
|
|
|
|
|
Logger::with_str("neovide")
|
|
|
|
|
|
|
|
.log_to_file()
|
|
|
|
|
|
|
|
.rotate(Criterion::Size(10_000_000), Naming::Timestamps, Cleanup::KeepLogFiles(1))
|
|
|
|
|
|
|
|
.start()
|
|
|
|
|
|
|
|
.expect("Could not start logger");
|
|
|
|
|
|
|
|
false
|
|
|
|
|
|
|
|
} else if arg == "--noIdle" {
|
|
|
|
|
|
|
|
no_idle = true;
|
|
|
|
|
|
|
|
false
|
|
|
|
|
|
|
|
} else if arg == "--extraBufferFrames" {
|
|
|
|
|
|
|
|
buffer_frames = 60;
|
|
|
|
|
|
|
|
false
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}).collect::<Vec<String>>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut settings = HashMap::new();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
settings.insert("no_idle".to_string(), Setting::new_bool(no_idle));
|
|
|
|
|
|
|
|
settings.insert("extra_buffer_frames".to_string(), Setting::new_u16(buffer_frames));
|
|
|
|
|
|
|
|
settings.insert("refresh_rate".to_string(), Setting::new_u16(60));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Settings { neovim_arguments, settings: Mutex::new(settings) }
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|