vim: Add Multi Replace mode in Vim (#8469)

For #4440, I've only added support for normal, if it's visual mode,
would we like this to delete the current selection row and enter insert
mode?

---------

Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
This commit is contained in:
Hans 2024-03-15 10:31:53 +08:00 committed by GitHub
parent 72d36d0213
commit e836a979a2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 584 additions and 16 deletions

View file

@ -1,5 +1,6 @@
use std::{fmt::Display, ops::Range, sync::Arc};
use crate::motion::Motion;
use collections::HashMap;
use editor::Anchor;
use gpui::{Action, KeyContext};
@ -7,12 +8,11 @@ use language::{CursorShape, Selection, TransactionId};
use serde::{Deserialize, Serialize};
use workspace::searchable::Direction;
use crate::motion::Motion;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub enum Mode {
Normal,
Insert,
Replace,
Visual,
VisualLine,
VisualBlock,
@ -23,6 +23,7 @@ impl Display for Mode {
match self {
Mode::Normal => write!(f, "NORMAL"),
Mode::Insert => write!(f, "INSERT"),
Mode::Replace => write!(f, "REPLACE"),
Mode::Visual => write!(f, "VISUAL"),
Mode::VisualLine => write!(f, "VISUAL LINE"),
Mode::VisualBlock => write!(f, "VISUAL BLOCK"),
@ -33,7 +34,7 @@ impl Display for Mode {
impl Mode {
pub fn is_visual(&self) -> bool {
match self {
Mode::Normal | Mode::Insert => false,
Mode::Normal | Mode::Insert | Mode::Replace => false,
Mode::Visual | Mode::VisualLine | Mode::VisualBlock => true,
}
}
@ -67,6 +68,7 @@ pub struct EditorState {
pub post_count: Option<usize>,
pub operator_stack: Vec<Operator>,
pub replacements: Vec<(Range<editor::Anchor>, String)>,
pub current_tx: Option<TransactionId>,
pub current_anchor: Option<Selection<Anchor>>,
@ -159,17 +161,21 @@ impl EditorState {
CursorShape::Underscore
}
}
Mode::Replace => CursorShape::Underscore,
Mode::Visual | Mode::VisualLine | Mode::VisualBlock => CursorShape::Block,
Mode::Insert => CursorShape::Bar,
}
}
pub fn vim_controlled(&self) -> bool {
!matches!(self.mode, Mode::Insert)
|| matches!(
self.operator_stack.last(),
Some(Operator::FindForward { .. }) | Some(Operator::FindBackward { .. })
)
let is_insert_mode = matches!(self.mode, Mode::Insert);
if !is_insert_mode {
return true;
}
matches!(
self.operator_stack.last(),
Some(Operator::FindForward { .. }) | Some(Operator::FindBackward { .. })
)
}
pub fn should_autoindent(&self) -> bool {
@ -178,7 +184,9 @@ impl EditorState {
pub fn clip_at_line_ends(&self) -> bool {
match self.mode {
Mode::Insert | Mode::Visual | Mode::VisualLine | Mode::VisualBlock => false,
Mode::Insert | Mode::Visual | Mode::VisualLine | Mode::VisualBlock | Mode::Replace => {
false
}
Mode::Normal => true,
}
}
@ -195,6 +203,7 @@ impl EditorState {
Mode::Normal => "normal",
Mode::Visual | Mode::VisualLine | Mode::VisualBlock => "visual",
Mode::Insert => "insert",
Mode::Replace => "replace",
},
);
@ -221,6 +230,9 @@ impl EditorState {
active_operator.map(|op| op.id()).unwrap_or_else(|| "none"),
);
if self.mode == Mode::Replace {
context.add("VimWaiting");
}
context
}
}