Skip to content

Commit

Permalink
helix settings: initial
Browse files Browse the repository at this point in the history
  • Loading branch information
blufony committed May 20, 2024
1 parent 6dfca5a commit 5633e33
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 32 deletions.
20 changes: 19 additions & 1 deletion assets/keymaps/vim.json
Original file line number Diff line number Diff line change
Expand Up @@ -575,9 +575,27 @@
}
},
{
"context": "Editor && vim_mode == insert",
"context": "Editor && vim_mode == helixnormal",
"bindings": {
"escape": ["vim::SwitchMode", "HelixNormal"]

}
},
{
"context": "Editor && vim_mode == insert && HelixControl",
"bindings": {
"escape": ["vim::SwitchMode", "HelixNormal"]
}
},
{
"context": "Editor && vim_mode == insert && !HelixControl",
"bindings": {
"escape": "vim::NormalBefore",
}
},
{
"context": "Editor && vim_mode == insert",
"bindings": {
"ctrl-c": "vim::NormalBefore",
"ctrl-[": "vim::NormalBefore",
"ctrl-x ctrl-o": "editor::ShowCompletions",
Expand Down
1 change: 1 addition & 0 deletions assets/settings/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"multi_cursor_modifier": "alt",
// Whether to enable vim modes and key bindings.
"vim_mode": false,
"helix_mode": false,
// Whether to show the informational hover box when moving the mouse
// over symbols in the editor.
"hover_popover_enabled": true,
Expand Down
12 changes: 11 additions & 1 deletion crates/vim/src/helix.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
use editor::{movement, scroll::Autoscroll};
use gpui::actions;
use language::{char_kind, CharKind};
use ui::WindowContext;
use ui::{ViewContext, WindowContext};
use workspace::Workspace;

use crate::{motion::Motion, normal::normal_motion, visual::visual_motion, Vim};

actions!(helix, [SelectNextLine,]);

pub fn register(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) {
workspace.register_action(|_: &mut Workspace, _: &SelectNextLine, cx: _| select_next_line(cx));
}

fn select_next_line(cx: &mut WindowContext) {}

pub fn helix_normal_motion(motion: Motion, times: Option<usize>, cx: &mut WindowContext) {
// Helix only selects the last of the motions
if let Some(times) = times {
Expand Down
8 changes: 7 additions & 1 deletion crates/vim/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use std::{fmt::Display, ops::Range, sync::Arc};

use crate::surrounds::SurroundsType;
use crate::HelixModeSetting;
use crate::{motion::Motion, object::Object};
use collections::HashMap;
use editor::Anchor;
use gpui::{Action, KeyContext};
use language::{CursorShape, Selection, TransactionId};
use serde::{Deserialize, Serialize};
use settings::Settings;
use ui::WindowContext;
use workspace::searchable::Direction;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)]
Expand Down Expand Up @@ -208,7 +211,7 @@ impl EditorState {
self.operator_stack.last().cloned()
}

pub fn keymap_context_layer(&self) -> KeyContext {
pub fn keymap_context_layer(&self, cx: &mut WindowContext) -> KeyContext {
let mut context = KeyContext::new_with_defaults();
context.set(
"vim_mode",
Expand All @@ -220,6 +223,9 @@ impl EditorState {
Mode::Replace => "replace",
},
);
if HelixModeSetting::get_global(cx).0 {
context.add("HelixControl");
}

if self.vim_controlled() {
context.add("VimControl");
Expand Down
52 changes: 23 additions & 29 deletions crates/vim/src/vim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,8 @@ impl_actions!(vim, [SwitchMode, PushOperator, Number]);
pub fn init(cx: &mut AppContext) {
cx.set_global(Vim::default());
VimModeSetting::register(cx);
HelixModeSetting::register(cx);
VimSettings::register(cx);
ModalEditorTypeSetting::register(cx);
let test = ModalEditorTypeSetting::get_global(cx).0;
println!("{:?}", test);

cx.observe_keystrokes(observe_keystrokes).detach();
editor_events::init(cx);
Expand Down Expand Up @@ -166,6 +164,7 @@ fn register(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) {
object::register(workspace, cx);
visual::register(workspace, cx);
change_list::register(workspace, cx);
helix::register(workspace, cx);
}

/// Called whenever an keystroke is typed so vim can observe all actions
Expand Down Expand Up @@ -778,7 +777,11 @@ impl Vim {
let active_editor = workspace.active_item_as::<Editor>(cx);
if let Some(active_editor) = active_editor {
self.activate_editor(active_editor, cx);
self.switch_mode(Mode::Normal, false, cx);
if HelixModeSetting::get_global(cx).0 {
self.switch_mode(Mode::HelixNormal, false, cx);
} else {
self.switch_mode(Mode::Normal, false, cx);
}
}
})
.ok();
Expand Down Expand Up @@ -818,10 +821,10 @@ impl Vim {
editor.set_autoindent(state.should_autoindent());
editor.selections.line_mode = matches!(state.mode, Mode::VisualLine);
if editor.is_focused(cx) || editor.mouse_menu_is_focused(cx) {
editor.set_keymap_context_layer::<Self>(state.keymap_context_layer(), cx);
editor.set_keymap_context_layer::<Self>(state.keymap_context_layer(cx), cx);
// disable vim mode if a sub-editor (inline assist, rename, etc.) is focused
} else if editor.focus_handle(cx).contains_focused(cx) {
editor.remove_keymap_context_layer::<Self>(cx);
editor.remove_keymap_context_layer::<Self>(cx)
}
});
}
Expand Down Expand Up @@ -851,6 +854,20 @@ impl Settings for VimModeSetting {
}
}

pub struct HelixModeSetting(pub bool);

impl Settings for HelixModeSetting {
const KEY: Option<&'static str> = Some("helix_mode");

type FileContent = Option<bool>;

fn load(sources: SettingsSources<Self::FileContent>, _: &mut AppContext) -> Result<Self> {
Ok(Self(sources.user.copied().flatten().unwrap_or(
sources.default.ok_or_else(Self::missing_default)?,
)))
}
}

/// Controls when to use system clipboard.
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
Expand Down Expand Up @@ -889,26 +906,3 @@ impl Settings for VimSettings {
sources.json_merge()
}
}

#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
enum VimModes {
Off,
Vim,
Helix,
}

#[derive(Deserialize)]
struct ModalEditorTypeSetting(pub VimModes);

impl Settings for ModalEditorTypeSetting {
const KEY: Option<&'static str> = Some("modal_editor_type");

type FileContent = Option<VimModes>;

fn load(sources: SettingsSources<Self::FileContent>, _: &mut AppContext) -> Result<Self> {
Ok(Self(sources.user.copied().flatten().unwrap_or(
sources.default.ok_or_else(Self::missing_default)?,
)))
}
}

0 comments on commit 5633e33

Please sign in to comment.