Add initial vim mode mode switching

Co-authored-by: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Keith Simmons 2022-03-24 19:24:36 -07:00
parent ccc276da7a
commit bb9b36dccd
16 changed files with 683 additions and 49 deletions

View file

@ -456,6 +456,8 @@ pub struct Editor {
pending_rename: Option<RenameState>,
searchable: bool,
cursor_shape: CursorShape,
keymap_context_layers: BTreeMap<TypeId, gpui::keymap::Context>,
input_enabled: bool,
leader_replica_id: Option<u16>,
}
@ -932,6 +934,8 @@ impl Editor {
searchable: true,
override_text_style: None,
cursor_shape: Default::default(),
keymap_context_layers: Default::default(),
input_enabled: true,
leader_replica_id: None,
};
this.end_selection(cx);
@ -1000,6 +1004,10 @@ impl Editor {
)
}
pub fn mode(&self) -> EditorMode {
self.mode
}
pub fn set_placeholder_text(
&mut self,
placeholder_text: impl Into<Arc<str>>,
@ -1063,6 +1071,19 @@ impl Editor {
cx.notify();
}
pub fn set_keymap_context_layer<Tag: 'static>(&mut self, context: gpui::keymap::Context) {
self.keymap_context_layers
.insert(TypeId::of::<Tag>(), context);
}
pub fn remove_keymap_context_layer<Tag: 'static>(&mut self) {
self.keymap_context_layers.remove(&TypeId::of::<Tag>());
}
pub fn set_input_enabled(&mut self, input_enabled: bool) {
self.input_enabled = input_enabled;
}
pub fn scroll_position(&self, cx: &mut ViewContext<Self>) -> Vector2F {
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
compute_scroll_position(&display_map, self.scroll_position, &self.scroll_top_anchor)
@ -1742,6 +1763,11 @@ impl Editor {
}
pub fn handle_input(&mut self, action: &Input, cx: &mut ViewContext<Self>) {
if !self.input_enabled {
cx.propagate_action();
return;
}
let text = action.0.as_ref();
if !self.skip_autoclose_end(text, cx) {
self.transact(cx, |this, cx| {
@ -5733,26 +5759,31 @@ impl View for Editor {
}
fn keymap_context(&self, _: &AppContext) -> gpui::keymap::Context {
let mut cx = Self::default_keymap_context();
let mut context = Self::default_keymap_context();
let mode = match self.mode {
EditorMode::SingleLine => "single_line",
EditorMode::AutoHeight { .. } => "auto_height",
EditorMode::Full => "full",
};
cx.map.insert("mode".into(), mode.into());
context.map.insert("mode".into(), mode.into());
if self.pending_rename.is_some() {
cx.set.insert("renaming".into());
context.set.insert("renaming".into());
}
match self.context_menu.as_ref() {
Some(ContextMenu::Completions(_)) => {
cx.set.insert("showing_completions".into());
context.set.insert("showing_completions".into());
}
Some(ContextMenu::CodeActions(_)) => {
cx.set.insert("showing_code_actions".into());
context.set.insert("showing_code_actions".into());
}
None => {}
}
cx
for layer in self.keymap_context_layers.values() {
context.extend(layer);
}
context
}
}