Instead of a menagerie of macros for implementing `Action`, now there are just two: * `actions!(editor, [MoveLeft, MoveRight])` * `#[derive(..., Action)]` with `#[action(namespace = editor)]` In both contexts, `///` doc comments can be provided and will be used in `JsonSchema`. In both contexts, parameters can provided in `#[action(...)]`: - `namespace = some_namespace` sets the namespace. In Zed this is required. - `name = "ActionName"` overrides the action's name. This must not contain "::". - `no_json` causes the `build` method to always error and `action_json_schema` to return `None` and allows actions not implement `serde::Serialize` and `schemars::JsonSchema`. - `no_register` skips registering the action. This is useful for implementing the `Action` trait while not supporting invocation by name or JSON deserialization. - `deprecated_aliases = ["editor::SomeAction"]` specifies deprecated old names for the action. These action names should *not* correspond to any actions that are registered. These old names can then still be used to refer to invoke this action. In Zed, the keymap JSON schema will accept these old names and provide warnings. - `deprecated = "Message about why this action is deprecation"` specifies a deprecation message. In Zed, the keymap JSON schema will cause this to be displayed as a warning. This is a new feature. Also makes the following changes since this seems like a good time to make breaking changes: * In `zed.rs` tests adds a test with an explicit list of namespaces. The rationale for this is that there is otherwise no checking of `namespace = ...` attributes. * `Action::debug_name` renamed to `name_for_type`, since its only difference with `name` was that it * `Action::name` now returns `&'static str` instead of `&str` to match the return of `name_for_type`. This makes the action trait more limited, but the code was already assuming that `name_for_type` is the same as `name`, and it requires `&'static`. So really this just makes the trait harder to misuse. * Various action reflection methods now use `&'static str` instead of `SharedString`. Release Notes: - N/A
240 lines
7.1 KiB
Rust
240 lines
7.1 KiB
Rust
mod appearance_settings_controls;
|
|
|
|
use std::any::TypeId;
|
|
use std::sync::Arc;
|
|
|
|
use command_palette_hooks::CommandPaletteFilter;
|
|
use editor::EditorSettingsControls;
|
|
use feature_flags::{FeatureFlag, FeatureFlagViewExt};
|
|
use fs::Fs;
|
|
use gpui::{
|
|
Action, App, AsyncWindowContext, Entity, EventEmitter, FocusHandle, Focusable, Task, actions,
|
|
};
|
|
use schemars::JsonSchema;
|
|
use serde::Deserialize;
|
|
use settings::{SettingsStore, VsCodeSettingsSource};
|
|
use ui::prelude::*;
|
|
use util::truncate_and_remove_front;
|
|
use workspace::item::{Item, ItemEvent};
|
|
use workspace::{Workspace, with_active_or_new_workspace};
|
|
|
|
use crate::appearance_settings_controls::AppearanceSettingsControls;
|
|
|
|
pub struct SettingsUiFeatureFlag;
|
|
|
|
impl FeatureFlag for SettingsUiFeatureFlag {
|
|
const NAME: &'static str = "settings-ui";
|
|
}
|
|
|
|
#[derive(Copy, Clone, Debug, Default, PartialEq, Deserialize, JsonSchema, Action)]
|
|
#[action(namespace = zed)]
|
|
pub struct ImportVsCodeSettings {
|
|
#[serde(default)]
|
|
pub skip_prompt: bool,
|
|
}
|
|
|
|
#[derive(Copy, Clone, Debug, Default, PartialEq, Deserialize, JsonSchema, Action)]
|
|
#[action(namespace = zed)]
|
|
pub struct ImportCursorSettings {
|
|
#[serde(default)]
|
|
pub skip_prompt: bool,
|
|
}
|
|
actions!(zed, [OpenSettingsEditor]);
|
|
|
|
pub fn init(cx: &mut App) {
|
|
cx.on_action(|_: &OpenSettingsEditor, cx| {
|
|
with_active_or_new_workspace(cx, move |workspace, window, cx| {
|
|
let existing = workspace
|
|
.active_pane()
|
|
.read(cx)
|
|
.items()
|
|
.find_map(|item| item.downcast::<SettingsPage>());
|
|
|
|
if let Some(existing) = existing {
|
|
workspace.activate_item(&existing, true, true, window, cx);
|
|
} else {
|
|
let settings_page = SettingsPage::new(workspace, cx);
|
|
workspace.add_item_to_active_pane(Box::new(settings_page), None, true, window, cx)
|
|
}
|
|
});
|
|
});
|
|
|
|
cx.observe_new(|workspace: &mut Workspace, window, cx| {
|
|
let Some(window) = window else {
|
|
return;
|
|
};
|
|
|
|
workspace.register_action(|_workspace, action: &ImportVsCodeSettings, window, cx| {
|
|
let fs = <dyn Fs>::global(cx);
|
|
let action = *action;
|
|
|
|
window
|
|
.spawn(cx, async move |cx: &mut AsyncWindowContext| {
|
|
handle_import_vscode_settings(
|
|
VsCodeSettingsSource::VsCode,
|
|
action.skip_prompt,
|
|
fs,
|
|
cx,
|
|
)
|
|
.await
|
|
})
|
|
.detach();
|
|
});
|
|
|
|
workspace.register_action(|_workspace, action: &ImportCursorSettings, window, cx| {
|
|
let fs = <dyn Fs>::global(cx);
|
|
let action = *action;
|
|
|
|
window
|
|
.spawn(cx, async move |cx: &mut AsyncWindowContext| {
|
|
handle_import_vscode_settings(
|
|
VsCodeSettingsSource::Cursor,
|
|
action.skip_prompt,
|
|
fs,
|
|
cx,
|
|
)
|
|
.await
|
|
})
|
|
.detach();
|
|
});
|
|
|
|
let settings_ui_actions = [TypeId::of::<OpenSettingsEditor>()];
|
|
|
|
CommandPaletteFilter::update_global(cx, |filter, _cx| {
|
|
filter.hide_action_types(&settings_ui_actions);
|
|
});
|
|
|
|
cx.observe_flag::<SettingsUiFeatureFlag, _>(
|
|
window,
|
|
move |is_enabled, _workspace, _, cx| {
|
|
if is_enabled {
|
|
CommandPaletteFilter::update_global(cx, |filter, _cx| {
|
|
filter.show_action_types(settings_ui_actions.iter());
|
|
});
|
|
} else {
|
|
CommandPaletteFilter::update_global(cx, |filter, _cx| {
|
|
filter.hide_action_types(&settings_ui_actions);
|
|
});
|
|
}
|
|
},
|
|
)
|
|
.detach();
|
|
})
|
|
.detach();
|
|
}
|
|
|
|
async fn handle_import_vscode_settings(
|
|
source: VsCodeSettingsSource,
|
|
skip_prompt: bool,
|
|
fs: Arc<dyn Fs>,
|
|
cx: &mut AsyncWindowContext,
|
|
) {
|
|
let vscode_settings =
|
|
match settings::VsCodeSettings::load_user_settings(source, fs.clone()).await {
|
|
Ok(vscode_settings) => vscode_settings,
|
|
Err(err) => {
|
|
log::error!("{err}");
|
|
let _ = cx.prompt(
|
|
gpui::PromptLevel::Info,
|
|
&format!("Could not find or load a {source} settings file"),
|
|
None,
|
|
&["Ok"],
|
|
);
|
|
return;
|
|
}
|
|
};
|
|
|
|
let prompt = if skip_prompt {
|
|
Task::ready(Some(0))
|
|
} else {
|
|
let prompt = cx.prompt(
|
|
gpui::PromptLevel::Warning,
|
|
&format!(
|
|
"Importing {} settings may overwrite your existing settings. \
|
|
Will import settings from {}",
|
|
vscode_settings.source,
|
|
truncate_and_remove_front(&vscode_settings.path.to_string_lossy(), 128),
|
|
),
|
|
None,
|
|
&["Ok", "Cancel"],
|
|
);
|
|
cx.spawn(async move |_| prompt.await.ok())
|
|
};
|
|
if prompt.await != Some(0) {
|
|
return;
|
|
}
|
|
|
|
cx.update(|_, cx| {
|
|
let source = vscode_settings.source;
|
|
let path = vscode_settings.path.clone();
|
|
cx.global::<SettingsStore>()
|
|
.import_vscode_settings(fs, vscode_settings);
|
|
log::info!("Imported {source} settings from {}", path.display());
|
|
})
|
|
.ok();
|
|
}
|
|
|
|
pub struct SettingsPage {
|
|
focus_handle: FocusHandle,
|
|
}
|
|
|
|
impl SettingsPage {
|
|
pub fn new(_workspace: &Workspace, cx: &mut Context<Workspace>) -> Entity<Self> {
|
|
cx.new(|cx| Self {
|
|
focus_handle: cx.focus_handle(),
|
|
})
|
|
}
|
|
}
|
|
|
|
impl EventEmitter<ItemEvent> for SettingsPage {}
|
|
|
|
impl Focusable for SettingsPage {
|
|
fn focus_handle(&self, _cx: &App) -> FocusHandle {
|
|
self.focus_handle.clone()
|
|
}
|
|
}
|
|
|
|
impl Item for SettingsPage {
|
|
type Event = ItemEvent;
|
|
|
|
fn tab_icon(&self, _window: &Window, _cx: &App) -> Option<Icon> {
|
|
Some(Icon::new(IconName::Settings))
|
|
}
|
|
|
|
fn tab_content_text(&self, _detail: usize, _cx: &App) -> SharedString {
|
|
"Settings".into()
|
|
}
|
|
|
|
fn show_toolbar(&self) -> bool {
|
|
false
|
|
}
|
|
|
|
fn to_item_events(event: &Self::Event, mut f: impl FnMut(ItemEvent)) {
|
|
f(*event)
|
|
}
|
|
}
|
|
|
|
impl Render for SettingsPage {
|
|
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
|
v_flex()
|
|
.p_4()
|
|
.size_full()
|
|
.gap_4()
|
|
.child(Label::new("Settings").size(LabelSize::Large))
|
|
.child(
|
|
v_flex().gap_1().child(Label::new("Appearance")).child(
|
|
v_flex()
|
|
.elevation_2(cx)
|
|
.child(AppearanceSettingsControls::new()),
|
|
),
|
|
)
|
|
.child(
|
|
v_flex().gap_1().child(Label::new("Editor")).child(
|
|
v_flex()
|
|
.elevation_2(cx)
|
|
.child(EditorSettingsControls::new()),
|
|
),
|
|
)
|
|
}
|
|
}
|