Refactor editor scrolling and implement scroll commands from vim mode
This commit is contained in:
parent
1920de81d9
commit
cffb064c16
29 changed files with 1244 additions and 683 deletions
|
@ -22,20 +22,9 @@ fn editor_focused(EditorFocused(editor): &EditorFocused, cx: &mut MutableAppCont
|
|||
vim.active_editor = Some(editor.downgrade());
|
||||
vim.selection_subscription = Some(cx.subscribe(editor, |editor, event, cx| {
|
||||
if editor.read(cx).leader_replica_id().is_none() {
|
||||
match event {
|
||||
editor::Event::SelectionsChanged { local: true } => {
|
||||
let newest_empty =
|
||||
editor.read(cx).selections.newest::<usize>(cx).is_empty();
|
||||
editor_local_selections_changed(newest_empty, cx);
|
||||
}
|
||||
editor::Event::IgnoredInput => {
|
||||
Vim::update(cx, |vim, cx| {
|
||||
if vim.active_operator().is_some() {
|
||||
vim.clear_operator(cx);
|
||||
}
|
||||
});
|
||||
}
|
||||
_ => (),
|
||||
if let editor::Event::SelectionsChanged { local: true } = event {
|
||||
let newest_empty = editor.read(cx).selections.newest::<usize>(cx).is_empty();
|
||||
editor_local_selections_changed(newest_empty, cx);
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use crate::{state::Mode, Vim};
|
||||
use editor::{Autoscroll, Bias};
|
||||
use editor::{scroll::autoscroll::Autoscroll, Bias};
|
||||
use gpui::{actions, MutableAppContext, ViewContext};
|
||||
use language::SelectionGoal;
|
||||
use workspace::Workspace;
|
||||
|
|
|
@ -2,7 +2,7 @@ mod change;
|
|||
mod delete;
|
||||
mod yank;
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::{borrow::Cow, cmp::Ordering};
|
||||
|
||||
use crate::{
|
||||
motion::Motion,
|
||||
|
@ -12,10 +12,13 @@ use crate::{
|
|||
};
|
||||
use collections::{HashMap, HashSet};
|
||||
use editor::{
|
||||
display_map::ToDisplayPoint, Anchor, Autoscroll, Bias, ClipboardSelection, DisplayPoint,
|
||||
display_map::ToDisplayPoint,
|
||||
scroll::{autoscroll::Autoscroll, scroll_amount::ScrollAmount},
|
||||
Anchor, Bias, ClipboardSelection, DisplayPoint, Editor,
|
||||
};
|
||||
use gpui::{actions, MutableAppContext, ViewContext};
|
||||
use gpui::{actions, impl_actions, MutableAppContext, ViewContext};
|
||||
use language::{AutoindentMode, Point, SelectionGoal};
|
||||
use serde::Deserialize;
|
||||
use workspace::Workspace;
|
||||
|
||||
use self::{
|
||||
|
@ -24,6 +27,9 @@ use self::{
|
|||
yank::{yank_motion, yank_object},
|
||||
};
|
||||
|
||||
#[derive(Clone, PartialEq, Deserialize)]
|
||||
struct Scroll(ScrollAmount);
|
||||
|
||||
actions!(
|
||||
vim,
|
||||
[
|
||||
|
@ -41,6 +47,8 @@ actions!(
|
|||
]
|
||||
);
|
||||
|
||||
impl_actions!(vim, [Scroll]);
|
||||
|
||||
pub fn init(cx: &mut MutableAppContext) {
|
||||
cx.add_action(insert_after);
|
||||
cx.add_action(insert_first_non_whitespace);
|
||||
|
@ -72,6 +80,13 @@ pub fn init(cx: &mut MutableAppContext) {
|
|||
})
|
||||
});
|
||||
cx.add_action(paste);
|
||||
cx.add_action(|_: &mut Workspace, Scroll(amount): &Scroll, cx| {
|
||||
Vim::update(cx, |vim, cx| {
|
||||
vim.update_active_editor(cx, |editor, cx| {
|
||||
scroll(editor, amount, cx);
|
||||
})
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
pub fn normal_motion(
|
||||
|
@ -367,6 +382,46 @@ fn paste(_: &mut Workspace, _: &Paste, cx: &mut ViewContext<Workspace>) {
|
|||
});
|
||||
}
|
||||
|
||||
fn scroll(editor: &mut Editor, amount: &ScrollAmount, cx: &mut ViewContext<Editor>) {
|
||||
let should_move_cursor = editor.newest_selection_on_screen(cx).is_eq();
|
||||
editor.scroll_screen(amount, cx);
|
||||
if should_move_cursor {
|
||||
let selection_ordering = editor.newest_selection_on_screen(cx);
|
||||
if selection_ordering.is_eq() {
|
||||
return;
|
||||
}
|
||||
|
||||
let visible_rows = if let Some(visible_rows) = editor.visible_line_count() {
|
||||
visible_rows as u32
|
||||
} else {
|
||||
return;
|
||||
};
|
||||
|
||||
let scroll_margin_rows = editor.vertical_scroll_margin() as u32;
|
||||
let top_anchor = editor.scroll_manager.anchor().top_anchor;
|
||||
|
||||
editor.change_selections(None, cx, |s| {
|
||||
s.replace_cursors_with(|snapshot| {
|
||||
let mut new_point = top_anchor.to_display_point(&snapshot);
|
||||
|
||||
match selection_ordering {
|
||||
Ordering::Less => {
|
||||
*new_point.row_mut() += scroll_margin_rows;
|
||||
new_point = snapshot.clip_point(new_point, Bias::Right);
|
||||
}
|
||||
Ordering::Greater => {
|
||||
*new_point.row_mut() += visible_rows - scroll_margin_rows as u32;
|
||||
new_point = snapshot.clip_point(new_point, Bias::Left);
|
||||
}
|
||||
Ordering::Equal => unreachable!(),
|
||||
}
|
||||
|
||||
vec![new_point]
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use indoc::indoc;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use crate::{motion::Motion, object::Object, state::Mode, utils::copy_selections_content, Vim};
|
||||
use editor::{
|
||||
char_kind, display_map::DisplaySnapshot, movement, Autoscroll, CharKind, DisplayPoint,
|
||||
char_kind, display_map::DisplaySnapshot, movement, scroll::autoscroll::Autoscroll, CharKind,
|
||||
DisplayPoint,
|
||||
};
|
||||
use gpui::MutableAppContext;
|
||||
use language::Selection;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::{motion::Motion, object::Object, utils::copy_selections_content, Vim};
|
||||
use collections::{HashMap, HashSet};
|
||||
use editor::{display_map::ToDisplayPoint, Autoscroll, Bias};
|
||||
use editor::{display_map::ToDisplayPoint, scroll::autoscroll::Autoscroll, Bias};
|
||||
use gpui::MutableAppContext;
|
||||
|
||||
pub fn delete_motion(vim: &mut Vim, motion: Motion, times: usize, cx: &mut MutableAppContext) {
|
||||
|
|
|
@ -18,6 +18,7 @@ impl Default for Mode {
|
|||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize)]
|
||||
pub enum Namespace {
|
||||
G,
|
||||
Z,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize)]
|
||||
|
@ -95,6 +96,7 @@ impl Operator {
|
|||
let operator_context = match operator {
|
||||
Some(Operator::Number(_)) => "n",
|
||||
Some(Operator::Namespace(Namespace::G)) => "g",
|
||||
Some(Operator::Namespace(Namespace::Z)) => "z",
|
||||
Some(Operator::Object { around: false }) => "i",
|
||||
Some(Operator::Object { around: true }) => "a",
|
||||
Some(Operator::Change) => "c",
|
||||
|
|
|
@ -81,6 +81,28 @@ pub fn init(cx: &mut MutableAppContext) {
|
|||
.detach();
|
||||
}
|
||||
|
||||
// Any keystrokes not mapped to vim should clar the active operator
|
||||
pub fn observe_keypresses(window_id: usize, cx: &mut MutableAppContext) {
|
||||
cx.observe_keystrokes(window_id, |_keystroke, _result, handled_by, cx| {
|
||||
dbg!(_keystroke);
|
||||
dbg!(_result);
|
||||
if let Some(handled_by) = handled_by {
|
||||
dbg!(handled_by.name());
|
||||
if handled_by.namespace() == "vim" {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Vim::update(cx, |vim, cx| {
|
||||
if vim.active_operator().is_some() {
|
||||
vim.clear_operator(cx);
|
||||
}
|
||||
});
|
||||
true
|
||||
})
|
||||
.detach()
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Vim {
|
||||
editors: HashMap<usize, WeakViewHandle<Editor>>,
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
use std::borrow::Cow;
|
||||
|
||||
use collections::HashMap;
|
||||
use editor::{display_map::ToDisplayPoint, Autoscroll, Bias, ClipboardSelection};
|
||||
use editor::{
|
||||
display_map::ToDisplayPoint, scroll::autoscroll::Autoscroll, Bias, ClipboardSelection,
|
||||
};
|
||||
use gpui::{actions, MutableAppContext, ViewContext};
|
||||
use language::{AutoindentMode, SelectionGoal};
|
||||
use workspace::Workspace;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue