Skip to content

Commit

Permalink
migrator: In-memory migration and improved UX (#24621)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
0xtimsb authored Feb 12, 2025
1 parent 498bb51 commit 65934ae
Show file tree
Hide file tree
Showing 6 changed files with 374 additions and 195 deletions.
14 changes: 10 additions & 4 deletions crates/editor/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12827,11 +12827,17 @@ impl Editor {
.and_then(|f| f.as_local())
}

fn target_file_abs_path(&self, cx: &mut Context<Self>) -> Option<PathBuf> {
pub fn target_file_abs_path(&self, cx: &mut Context<Self>) -> Option<PathBuf> {
self.active_excerpt(cx).and_then(|(_, buffer, _)| {
let project_path = buffer.read(cx).project_path(cx)?;
let project = self.project.as_ref()?.read(cx);
project.absolute_path(&project_path, cx)
let buffer = buffer.read(cx);
if let Some(project_path) = buffer.project_path(cx) {
let project = self.project.as_ref()?.read(cx);
project.absolute_path(&project_path, cx)
} else {
buffer
.file()
.and_then(|file| file.as_local().map(|file| file.abs_path(cx)))
}
})
}

Expand Down
23 changes: 8 additions & 15 deletions crates/settings/src/keymap_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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`,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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}"
)),
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -317,7 +311,6 @@ impl KeymapFile {
}
KeymapFileLoadResult::SomeFailedToLoad {
key_bindings,
keymap_file,
error_message: MarkdownString(error_message),
}
}
Expand Down Expand Up @@ -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() {
Expand Down
36 changes: 1 addition & 35 deletions crates/settings/src/settings_file.rs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions crates/zed/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use project::project_settings::ProjectSettings;
use recent_projects::{open_ssh_project, SshSettings};
use release_channel::{AppCommitSha, AppVersion, ReleaseChannel};
use session::{AppSession, Session};
use settings::{handle_settings_file_changes, watch_config_file, Settings, SettingsStore};
use settings::{watch_config_file, Settings, SettingsStore};
use simplelog::ConfigBuilder;
use std::{
env,
Expand All @@ -52,8 +52,9 @@ use welcome::{show_welcome_view, BaseKeymap, FIRST_OPEN};
use workspace::{AppState, SerializedWorkspaceLocation, WorkspaceSettings, WorkspaceStore};
use zed::{
app_menus, build_window_options, derive_paths_with_position, handle_cli_connection,
handle_keymap_file_changes, handle_settings_changed, initialize_workspace,
inline_completion_registry, open_paths_with_positions, OpenListener, OpenRequest,
handle_keymap_file_changes, handle_settings_changed, handle_settings_file_changes,
initialize_workspace, inline_completion_registry, open_paths_with_positions, OpenListener,
OpenRequest,
};

#[cfg(unix)]
Expand Down
Loading

0 comments on commit 65934ae

Please sign in to comment.