keymap: Show error notification when keymap is invalid (#15977)
This adds an error notification that pops up when the user has an invalid keymap, similar to what we added for settings in #15905. Release Notes: - Added a popup that is displayed when the keymap is invalid
This commit is contained in:
parent
92496f33e7
commit
793cd88792
3 changed files with 43 additions and 14 deletions
|
@ -1,5 +1,4 @@
|
||||||
use crate::{settings_store::SettingsStore, Settings};
|
use crate::{settings_store::SettingsStore, Settings};
|
||||||
use anyhow::Result;
|
|
||||||
use fs::Fs;
|
use fs::Fs;
|
||||||
use futures::{channel::mpsc, StreamExt};
|
use futures::{channel::mpsc, StreamExt};
|
||||||
use gpui::{AppContext, BackgroundExecutor, ReadGlobal, UpdateGlobal};
|
use gpui::{AppContext, BackgroundExecutor, ReadGlobal, UpdateGlobal};
|
||||||
|
@ -67,7 +66,7 @@ pub fn watch_config_file(
|
||||||
pub fn handle_settings_file_changes(
|
pub fn handle_settings_file_changes(
|
||||||
mut user_settings_file_rx: mpsc::UnboundedReceiver<String>,
|
mut user_settings_file_rx: mpsc::UnboundedReceiver<String>,
|
||||||
cx: &mut AppContext,
|
cx: &mut AppContext,
|
||||||
settings_changed: impl Fn(Result<()>, &mut AppContext) + 'static,
|
settings_changed: impl Fn(Option<anyhow::Error>, &mut AppContext) + 'static,
|
||||||
) {
|
) {
|
||||||
let user_settings_content = cx
|
let user_settings_content = cx
|
||||||
.background_executor()
|
.background_executor()
|
||||||
|
@ -85,7 +84,7 @@ pub fn handle_settings_file_changes(
|
||||||
if let Err(err) = &result {
|
if let Err(err) = &result {
|
||||||
log::error!("Failed to load user settings: {err}");
|
log::error!("Failed to load user settings: {err}");
|
||||||
}
|
}
|
||||||
settings_changed(result, cx);
|
settings_changed(result.err(), cx);
|
||||||
cx.refresh();
|
cx.refresh();
|
||||||
});
|
});
|
||||||
if result.is_err() {
|
if result.is_err() {
|
||||||
|
|
|
@ -430,7 +430,7 @@ fn main() {
|
||||||
|
|
||||||
settings::init(cx);
|
settings::init(cx);
|
||||||
handle_settings_file_changes(user_settings_file_rx, cx, handle_settings_changed);
|
handle_settings_file_changes(user_settings_file_rx, cx, handle_settings_changed);
|
||||||
handle_keymap_file_changes(user_keymap_file_rx, cx);
|
handle_keymap_file_changes(user_keymap_file_rx, cx, handle_keymap_changed);
|
||||||
|
|
||||||
client::init_settings(cx);
|
client::init_settings(cx);
|
||||||
let client = Client::production(cx);
|
let client = Client::production(cx);
|
||||||
|
@ -543,15 +543,39 @@ fn main() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_settings_changed(result: Result<()>, cx: &mut AppContext) {
|
fn handle_keymap_changed(error: Option<anyhow::Error>, cx: &mut AppContext) {
|
||||||
|
struct KeymapParseErrorNotification;
|
||||||
|
let id = NotificationId::unique::<KeymapParseErrorNotification>();
|
||||||
|
|
||||||
|
for workspace in workspace::local_workspace_windows(cx) {
|
||||||
|
workspace
|
||||||
|
.update(cx, |workspace, cx| match &error {
|
||||||
|
Some(error) => {
|
||||||
|
workspace.show_notification(id.clone(), cx, |cx| {
|
||||||
|
cx.new_view(|_| {
|
||||||
|
MessageNotification::new(format!("Invalid keymap file\n{error}"))
|
||||||
|
.with_click_message("Open keymap file")
|
||||||
|
.on_click(|cx| {
|
||||||
|
cx.dispatch_action(zed_actions::OpenKeymap.boxed_clone());
|
||||||
|
cx.emit(DismissEvent);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
});
|
||||||
|
}
|
||||||
|
None => workspace.dismiss_notification(&id, cx),
|
||||||
|
})
|
||||||
|
.log_err();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_settings_changed(error: Option<anyhow::Error>, cx: &mut AppContext) {
|
||||||
struct SettingsParseErrorNotification;
|
struct SettingsParseErrorNotification;
|
||||||
let id = NotificationId::unique::<SettingsParseErrorNotification>();
|
let id = NotificationId::unique::<SettingsParseErrorNotification>();
|
||||||
|
|
||||||
for workspace in workspace::local_workspace_windows(cx) {
|
for workspace in workspace::local_workspace_windows(cx) {
|
||||||
workspace
|
workspace
|
||||||
.update(cx, |workspace, cx| match &result {
|
.update(cx, |workspace, cx| match &error {
|
||||||
Ok(()) => workspace.dismiss_notification(&id, cx),
|
Some(error) => {
|
||||||
Err(error) => {
|
|
||||||
workspace.show_notification(id.clone(), cx, |cx| {
|
workspace.show_notification(id.clone(), cx, |cx| {
|
||||||
cx.new_view(|_| {
|
cx.new_view(|_| {
|
||||||
MessageNotification::new(format!("Invalid settings file\n{error}"))
|
MessageNotification::new(format!("Invalid settings file\n{error}"))
|
||||||
|
@ -563,6 +587,7 @@ fn handle_settings_changed(result: Result<()>, cx: &mut AppContext) {
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
None => workspace.dismiss_notification(&id, cx),
|
||||||
})
|
})
|
||||||
.log_err();
|
.log_err();
|
||||||
}
|
}
|
||||||
|
|
|
@ -733,6 +733,7 @@ fn open_log_file(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) {
|
||||||
pub fn handle_keymap_file_changes(
|
pub fn handle_keymap_file_changes(
|
||||||
mut user_keymap_file_rx: mpsc::UnboundedReceiver<String>,
|
mut user_keymap_file_rx: mpsc::UnboundedReceiver<String>,
|
||||||
cx: &mut AppContext,
|
cx: &mut AppContext,
|
||||||
|
keymap_changed: impl Fn(Option<anyhow::Error>, &mut AppContext) + 'static,
|
||||||
) {
|
) {
|
||||||
BaseKeymap::register(cx);
|
BaseKeymap::register(cx);
|
||||||
VimModeSetting::register(cx);
|
VimModeSetting::register(cx);
|
||||||
|
@ -761,10 +762,14 @@ pub fn handle_keymap_file_changes(
|
||||||
_ = base_keymap_rx.next() => {}
|
_ = base_keymap_rx.next() => {}
|
||||||
user_keymap_content = user_keymap_file_rx.next() => {
|
user_keymap_content = user_keymap_file_rx.next() => {
|
||||||
if let Some(user_keymap_content) = user_keymap_content {
|
if let Some(user_keymap_content) = user_keymap_content {
|
||||||
if let Some(keymap_content) = KeymapFile::parse(&user_keymap_content).log_err() {
|
match KeymapFile::parse(&user_keymap_content) {
|
||||||
|
Ok(keymap_content) => {
|
||||||
|
cx.update(|cx| keymap_changed(None, cx)).log_err();
|
||||||
user_keymap = keymap_content;
|
user_keymap = keymap_content;
|
||||||
} else {
|
}
|
||||||
continue
|
Err(error) => {
|
||||||
|
cx.update(|cx| keymap_changed(Some(error), cx)).log_err();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3097,7 +3102,7 @@ mod tests {
|
||||||
PathBuf::from("/keymap.json"),
|
PathBuf::from("/keymap.json"),
|
||||||
);
|
);
|
||||||
handle_settings_file_changes(settings_rx, cx, |_, _| {});
|
handle_settings_file_changes(settings_rx, cx, |_, _| {});
|
||||||
handle_keymap_file_changes(keymap_rx, cx);
|
handle_keymap_file_changes(keymap_rx, cx, |_, _| {});
|
||||||
});
|
});
|
||||||
workspace
|
workspace
|
||||||
.update(cx, |workspace, cx| {
|
.update(cx, |workspace, cx| {
|
||||||
|
@ -3237,7 +3242,7 @@ mod tests {
|
||||||
);
|
);
|
||||||
|
|
||||||
handle_settings_file_changes(settings_rx, cx, |_, _| {});
|
handle_settings_file_changes(settings_rx, cx, |_, _| {});
|
||||||
handle_keymap_file_changes(keymap_rx, cx);
|
handle_keymap_file_changes(keymap_rx, cx, |_, _| {});
|
||||||
});
|
});
|
||||||
|
|
||||||
cx.background_executor.run_until_parked();
|
cx.background_executor.run_until_parked();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue