migrator: In-memory migration and improved UX (#24621)

This PR adds:

- Support for deprecated keymap and settings (In-memory migration)
- Migration prompt only shown in `settings.json` / `keymap.json`.

Release Notes:

- The migration banner will only appear in `settings.json` and
`keymap.json` if you have deprecated settings or keybindings, allowing
you to migrate them to work with the new version on Zed.
This commit is contained in:
smit 2025-02-12 06:47:08 +05:30 committed by GitHub
parent 498bb518ff
commit 65934ae181
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 375 additions and 196 deletions

View file

@ -10,7 +10,7 @@ use schemars::{
schema::{ArrayValidation, InstanceType, Schema, SchemaObject, SubschemaValidation},
JsonSchema,
};
use serde::{Deserialize, Serialize};
use serde::Deserialize;
use serde_json::Value;
use std::{any::TypeId, fmt::Write, rc::Rc, sync::Arc, sync::LazyLock};
use util::{asset_str, markdown::MarkdownString};
@ -47,12 +47,12 @@ pub(crate) static KEY_BINDING_VALIDATORS: LazyLock<BTreeMap<TypeId, Box<dyn KeyB
/// Keymap configuration consisting of sections. Each section may have a context predicate which
/// determines whether its bindings are used.
#[derive(Debug, Deserialize, Default, Clone, JsonSchema, Serialize)]
#[derive(Debug, Deserialize, Default, Clone, JsonSchema)]
#[serde(transparent)]
pub struct KeymapFile(Vec<KeymapSection>);
/// Keymap section which binds keystrokes to actions.
#[derive(Debug, Deserialize, Default, Clone, JsonSchema, Serialize)]
#[derive(Debug, Deserialize, Default, Clone, JsonSchema)]
pub struct KeymapSection {
/// Determines when these bindings are active. When just a name is provided, like `Editor` or
/// `Workspace`, the bindings will be active in that context. Boolean expressions like `X && Y`,
@ -97,9 +97,9 @@ impl KeymapSection {
/// Unlike the other json types involved in keymaps (including actions), this doc-comment will not
/// be included in the generated JSON schema, as it manually defines its `JsonSchema` impl. The
/// actual schema used for it is automatically generated in `KeymapFile::generate_json_schema`.
#[derive(Debug, Deserialize, Default, Clone, Serialize)]
#[derive(Debug, Deserialize, Default, Clone)]
#[serde(transparent)]
pub struct KeymapAction(pub(crate) Value);
pub struct KeymapAction(Value);
impl std::fmt::Display for KeymapAction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@ -133,11 +133,9 @@ impl JsonSchema for KeymapAction {
pub enum KeymapFileLoadResult {
Success {
key_bindings: Vec<KeyBinding>,
keymap_file: KeymapFile,
},
SomeFailedToLoad {
key_bindings: Vec<KeyBinding>,
keymap_file: KeymapFile,
error_message: MarkdownString,
},
JsonParseFailure {
@ -152,7 +150,7 @@ impl KeymapFile {
pub fn load_asset(asset_path: &str, cx: &App) -> anyhow::Result<Vec<KeyBinding>> {
match Self::load(asset_str::<SettingsAssets>(asset_path).as_ref(), cx) {
KeymapFileLoadResult::Success { key_bindings, .. } => Ok(key_bindings),
KeymapFileLoadResult::Success { key_bindings } => Ok(key_bindings),
KeymapFileLoadResult::SomeFailedToLoad { error_message, .. } => Err(anyhow!(
"Error loading built-in keymap \"{asset_path}\": {error_message}"
)),
@ -202,7 +200,6 @@ impl KeymapFile {
if content.is_empty() {
return KeymapFileLoadResult::Success {
key_bindings: Vec::new(),
keymap_file: KeymapFile(Vec::new()),
};
}
let keymap_file = match parse_json_with_comments::<Self>(content) {
@ -296,10 +293,7 @@ impl KeymapFile {
}
if errors.is_empty() {
KeymapFileLoadResult::Success {
key_bindings,
keymap_file,
}
KeymapFileLoadResult::Success { key_bindings }
} else {
let mut error_message = "Errors in user keymap file.\n".to_owned();
for (context, section_errors) in errors {
@ -317,7 +311,6 @@ impl KeymapFile {
}
KeymapFileLoadResult::SomeFailedToLoad {
key_bindings,
keymap_file,
error_message: MarkdownString(error_message),
}
}
@ -619,7 +612,7 @@ fn inline_code_string(text: &str) -> MarkdownString {
#[cfg(test)]
mod tests {
use super::KeymapFile;
use crate::KeymapFile;
#[test]
fn can_deserialize_keymap_with_trailing_comma() {

View file

@ -1,7 +1,7 @@
use crate::{settings_store::SettingsStore, Settings};
use fs::Fs;
use futures::{channel::mpsc, StreamExt};
use gpui::{App, BackgroundExecutor, ReadGlobal, UpdateGlobal};
use gpui::{App, BackgroundExecutor, ReadGlobal};
use std::{path::PathBuf, sync::Arc, time::Duration};
pub const EMPTY_THEME_NAME: &str = "empty-theme";
@ -78,40 +78,6 @@ pub fn watch_config_file(
rx
}
pub fn handle_settings_file_changes(
mut user_settings_file_rx: mpsc::UnboundedReceiver<String>,
cx: &mut App,
settings_changed: impl Fn(Result<serde_json::Value, anyhow::Error>, &mut App) + 'static,
) {
let user_settings_content = cx
.background_executor()
.block(user_settings_file_rx.next())
.unwrap();
SettingsStore::update_global(cx, |store, cx| {
let result = store.set_user_settings(&user_settings_content, cx);
if let Err(err) = &result {
log::error!("Failed to load user settings: {err}");
}
settings_changed(result, cx);
});
cx.spawn(move |cx| async move {
while let Some(user_settings_content) = user_settings_file_rx.next().await {
let result = cx.update_global(|store: &mut SettingsStore, cx| {
let result = store.set_user_settings(&user_settings_content, cx);
if let Err(err) = &result {
log::error!("Failed to load user settings: {err}");
}
settings_changed(result, cx);
cx.refresh_windows();
});
if result.is_err() {
break; // App dropped
}
}
})
.detach();
}
pub fn update_settings_file<T: Settings>(
fs: Arc<dyn Fs>,
cx: &App,