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};
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
20pub struct VimModeSetting(pub bool);
21
22impl Settings for VimModeSetting {
23    const KEY: Option<&'static str> = Some("vim_mode");
24
25    type FileContent = Option<bool>;
26
27    fn load(sources: SettingsSources<Self::FileContent>, _: &mut App) -> Result<Self> {
28        Ok(Self(
29            sources
30                .user
31                .or(sources.server)
32                .copied()
33                .flatten()
34                .unwrap_or(sources.default.ok_or_else(Self::missing_default)?),
35        ))
36    }
37
38    fn import_from_vscode(_vscode: &settings::VsCodeSettings, _current: &mut Self::FileContent) {
39        // TODO: could possibly check if any of the `vim.<foo>` keys are set?
40    }
41}
42
43/// Whether or not to enable Helix mode.
44///
45/// Default: false
46pub struct HelixModeSetting(pub bool);
47
48impl Settings for HelixModeSetting {
49    const KEY: Option<&'static str> = Some("helix_mode");
50
51    type FileContent = Option<bool>;
52
53    fn load(sources: SettingsSources<Self::FileContent>, _: &mut App) -> Result<Self> {
54        Ok(Self(
55            sources
56                .user
57                .or(sources.server)
58                .copied()
59                .flatten()
60                .unwrap_or(sources.default.ok_or_else(Self::missing_default)?),
61        ))
62    }
63
64    fn import_from_vscode(_vscode: &settings::VsCodeSettings, _current: &mut Self::FileContent) {
65        // TODO: could possibly check if any of the `helix.<foo>` keys are set?
66    }
67}