vim_mode_setting.rs

 1//! Contains the [`VimModeSetting`] and [`HelixModeSetting`] used to enable/disable Vim and Helix modes.
 2//!
 3//! This is in its own crate as we want other crates to be able to enable or
 4//! disable Vim/Helix modes without having to depend on the `vim` crate in its
 5//! entirety.
 6
 7use anyhow::Result;
 8use gpui::App;
 9use settings::{Settings, SettingsSources, SettingsUi};
10
11/// Initializes the `vim_mode_setting` crate.
12pub fn init(cx: &mut App) {
13    VimModeSetting::register(cx);
14    HelixModeSetting::register(cx);
15}
16
17/// Whether or not to enable Vim mode.
18///
19/// Default: false
20#[derive(SettingsUi)]
21pub struct VimModeSetting(pub bool);
22
23impl Settings for VimModeSetting {
24    const KEY: Option<&'static str> = Some("vim_mode");
25
26    type FileContent = Option<bool>;
27
28    fn load(sources: SettingsSources<Self::FileContent>, _: &mut App) -> Result<Self> {
29        Ok(Self(
30            sources
31                .user
32                .or(sources.server)
33                .copied()
34                .flatten()
35                .unwrap_or(sources.default.ok_or_else(Self::missing_default)?),
36        ))
37    }
38
39    fn import_from_vscode(_vscode: &settings::VsCodeSettings, _current: &mut Self::FileContent) {
40        // TODO: could possibly check if any of the `vim.<foo>` keys are set?
41    }
42}
43
44/// Whether or not to enable Helix mode.
45///
46/// Default: false
47#[derive(SettingsUi)]
48pub struct HelixModeSetting(pub bool);
49
50impl Settings for HelixModeSetting {
51    const KEY: Option<&'static str> = Some("helix_mode");
52
53    type FileContent = Option<bool>;
54
55    fn load(sources: SettingsSources<Self::FileContent>, _: &mut App) -> Result<Self> {
56        Ok(Self(
57            sources
58                .user
59                .or(sources.server)
60                .copied()
61                .flatten()
62                .unwrap_or(sources.default.ok_or_else(Self::missing_default)?),
63        ))
64    }
65
66    fn import_from_vscode(_vscode: &settings::VsCodeSettings, _current: &mut Self::FileContent) {
67        // TODO: could possibly check if any of the `helix.<foo>` keys are set?
68    }
69}