Skip to content

Commit

Permalink
make config load return a Result so we can catch errors in main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
panekj committed May 25, 2024
1 parent e4bab0a commit 0581c4a
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 32 deletions.
4 changes: 2 additions & 2 deletions defaults/settings.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ bracket-pair-colorization = false
bracket-colorization-limit = 30000
files-exclude = "**/{.git,.svn,.hg,CVS,.DS_Store,Thumbs.db}" # Glob patterns

[remote]
[remote.entries]
# [remote]
# [remote.entries]

[terminal]
font-family = ""
Expand Down
14 changes: 10 additions & 4 deletions lapce-app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
sync::{atomic::AtomicU64, Arc},
};

use anyhow::{anyhow, Result};
use anyhow::{anyhow, Context, Result};
use clap::Parser;
use crossbeam_channel::Sender;
use floem::{
Expand Down Expand Up @@ -162,8 +162,12 @@ pub struct AppData {

impl AppData {
pub fn reload_config(&self) {
let config =
LapceConfig::load(&LapceWorkspace::default(), &[], &self.plugin_paths);
let Ok(config) =
LapceConfig::load(&LapceWorkspace::default(), &[], &self.plugin_paths)
else {
event!(Level::ERROR, "Failed to load config");
return;
};
let level_filter = config.core.log_level_filter.clone();
self.config.set(Arc::new(config));
if let Ok(new_targets) =
Expand Down Expand Up @@ -3670,7 +3674,9 @@ pub fn launch() {
);

let windows = scope.create_rw_signal(im::HashMap::new());
let config = LapceConfig::load(&LapceWorkspace::default(), &[], &plugin_paths);
let config = LapceConfig::load(&LapceWorkspace::default(), &[], &plugin_paths)
.context("Failed to load config")
.unwrap();
let config = scope.create_rw_signal(Arc::new(config));

let level_filter = config.get_untracked().core.log_level_filter.clone();
Expand Down
55 changes: 38 additions & 17 deletions lapce-app/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ const DEFAULT_UI_ICON_THEME: &str =
include_str!("../../defaults/icon-theme-ui.toml");

static DEFAULT_CONFIG: Lazy<config::Config> = Lazy::new(LapceConfig::default_config);
static DEFAULT_LAPCE_CONFIG: Lazy<LapceConfig> =
Lazy::new(LapceConfig::default_lapce_config);

static DEFAULT_DARK_THEME_CONFIG: Lazy<config::Config> = Lazy::new(|| {
config::Config::builder()
Expand Down Expand Up @@ -175,18 +173,32 @@ pub struct LapceConfig {
wrap_style_list: im::Vector<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum LapceConfigError {
#[error("Failed to deserialise config")]
DeserError(String),
#[error("Unknown error")]
Unknown,
}

#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum ConfigError {
#[error("Unknown error")]
Unknown,
}

impl LapceConfig {
pub fn load(
workspace: &LapceWorkspace,
disabled_volts: &[VoltID],
extra_plugin_paths: &[PathBuf],
) -> Self {
) -> Result<Self, LapceConfigError> {
let config = Self::merge_config(workspace, None, None, None);
let mut lapce_config: LapceConfig = match config.try_deserialize() {
Ok(config) => config,
Err(error) => {
error!("Failed to deserialize configuration file: {error}");
DEFAULT_LAPCE_CONFIG.clone()
LapceConfig::default_lapce_config()?.clone()
}
};

Expand All @@ -196,7 +208,7 @@ impl LapceConfig {
Self::load_icon_themes(disabled_volts, extra_plugin_paths);
lapce_config.available_ui_icon_themes =
Self::load_icon_themes(disabled_volts, extra_plugin_paths);
lapce_config.resolve_theme(workspace);
lapce_config.resolve_theme(workspace, Some(&lapce_config.clone()));

lapce_config.color_theme_list = lapce_config
.available_color_themes
Expand Down Expand Up @@ -231,7 +243,7 @@ impl LapceConfig {

lapce_config.terminal.get_indexed_colors();

lapce_config
Ok(lapce_config)
}

fn merge_config(
Expand Down Expand Up @@ -318,21 +330,30 @@ impl LapceConfig {
.unwrap()
}

fn default_lapce_config() -> LapceConfig {
let mut default_lapce_config: LapceConfig =
DEFAULT_CONFIG.clone().try_deserialize().expect("Failed to deserialize default config, this likely indicates a missing or misnamed field in settings.toml");
fn default_lapce_config() -> Result<LapceConfig, LapceConfigError> {
let mut default_lapce_config: LapceConfig = match DEFAULT_CONFIG
.clone()
.try_deserialize()
{
Ok(v) => v,
Err(e) => {
return Err(LapceConfigError::DeserError(format!("Failed to deserialize default config, this likely indicates a missing or misnamed field in settings.toml: {e}")));
}
};
default_lapce_config.color_theme = DEFAULT_DARK_THEME_COLOR_CONFIG.clone();
default_lapce_config.file_icon_theme =
DEFAULT_FILE_ICON_THEME_ICON_CONFIG.clone();
default_lapce_config.ui_icon_theme =
DEFAULT_UI_ICON_THEME_ICON_CONFIG.clone();
default_lapce_config.resolve_colors(None);
default_lapce_config
Ok(default_lapce_config)
}

fn resolve_theme(&mut self, workspace: &LapceWorkspace) {
let default_lapce_config = DEFAULT_LAPCE_CONFIG.clone();

fn resolve_theme(
&mut self,
workspace: &LapceWorkspace,
default_config: Option<&LapceConfig>,
) {
let color_theme_config = self
.available_color_themes
.get(&self.core.color_theme.to_lowercase())
Expand Down Expand Up @@ -388,7 +409,7 @@ impl LapceConfig {
}
self.plugins = new.plugins;
}
self.resolve_colors(Some(&default_lapce_config));
self.resolve_colors(default_config);
self.update_id();
}

Expand Down Expand Up @@ -422,21 +443,21 @@ impl LapceConfig {
/// Note that this does not save the config.
pub fn set_color_theme(&mut self, workspace: &LapceWorkspace, theme: &str) {
self.core.color_theme = theme.to_string();
self.resolve_theme(workspace);
self.resolve_theme(workspace, None);
}

/// Set the active file icon theme.
/// Note that this does not save the config.
pub fn set_file_icon_theme(&mut self, workspace: &LapceWorkspace, theme: &str) {
self.core.file_icon_theme = theme.to_string();
self.resolve_theme(workspace);
self.resolve_theme(workspace, None);
}

/// Set the active UI icon theme.
/// Note that this does not save the config.
pub fn set_ui_icon_theme(&mut self, workspace: &LapceWorkspace, theme: &str) {
self.core.ui_icon_theme = theme.to_string();
self.resolve_theme(workspace);
self.resolve_theme(workspace, None);
}

pub fn set_modal(&mut self, _workspace: &LapceWorkspace, modal: bool) {
Expand Down
2 changes: 1 addition & 1 deletion lapce-app/src/config/color_theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ color-preference = "dark"
// lapce_config.available_icon_themes = Some(vec![]);
lapce_config.core.color_theme = "test".to_string();

lapce_config.resolve_theme(&workspace);
lapce_config.resolve_theme(&workspace, None);

println!("Hot Pink: {:?}", Color::HOT_PINK);
// test basic override
Expand Down
16 changes: 12 additions & 4 deletions lapce-app/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use floem::{
ViewId,
};
use serde::{Deserialize, Serialize};
use tracing::{event, Level};

use crate::{
app::AppCommand,
Expand Down Expand Up @@ -91,8 +92,12 @@ impl WindowData {
app_command: Listener<AppCommand>,
) -> Self {
let cx = Scope::new();
let config =
LapceConfig::load(&LapceWorkspace::default(), &[], &extra_plugin_paths);
let Ok(config) =
LapceConfig::load(&LapceWorkspace::default(), &[], &extra_plugin_paths)
else {
event!(Level::ERROR, "Failed to load config");
panic!();
};
let config = cx.create_rw_signal(Arc::new(config));
let root_view_id = cx.create_rw_signal(ViewId::new());

Expand Down Expand Up @@ -184,11 +189,14 @@ impl WindowData {
}

pub fn reload_config(&self) {
let config = LapceConfig::load(
let Ok(config) = LapceConfig::load(
&LapceWorkspace::default(),
&[],
&self.common.extra_plugin_paths,
);
) else {
event!(Level::ERROR, "Failed to load config");
return;
};
self.config.set(Arc::new(config));
let window_tabs = self.window_tabs.get_untracked();
for (_, window_tab) in window_tabs {
Expand Down
14 changes: 10 additions & 4 deletions lapce-app/src/window_tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,14 @@ impl WindowTabData {
info
};

let config = LapceConfig::load(
let Ok(config) = LapceConfig::load(
&workspace,
&all_disabled_volts,
&window_common.extra_plugin_paths,
);
) else {
event!(Level::ERROR, "Failed to load config");
panic!("Failed to load config");
};
let lapce_command = Listener::new_empty(cx);
let workbench_command = Listener::new_empty(cx);
let internal_command = Listener::new_empty(cx);
Expand Down Expand Up @@ -595,11 +598,14 @@ impl WindowTabData {
let mut all_disabled_volts = disabled_volts;
all_disabled_volts.extend(workspace_disabled_volts);

let config = LapceConfig::load(
let Ok(config) = LapceConfig::load(
&self.workspace,
&all_disabled_volts,
&self.common.window_common.extra_plugin_paths,
);
) else {
event!(Level::ERROR, "Failed to load config");
return;
};
self.common.keypress.update(|keypress| {
keypress.update_keymaps(&config);
});
Expand Down

0 comments on commit 0581c4a

Please sign in to comment.