use warp_core::safe_info;use warpui_core::keymap::Keystroke;use warpui_core::{Entity, ModelContext, ModelHandle, ViewContext}; use crate::register::{BLACK_HOLE_REGISTER, valid_register_name}; /// ASCII code for backspace./// In Normal and Visual modes, Vim treats backspace as a leftward character motion.const BACKSPACE_CHAR: char = '\u{8}'; /// Represents the Vim modes we currently support. Insert mode is the default instead of normal/// mode to match the behavior of Bash and Zsh's vi mode.#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]pub enum VimMode { Normal, #[default] Insert, Visual(MotionType), Replace,} impl VimMode { /// Convenience method for constructing a [`ModeTransition`] at a specific insert position. /// Note that a non-default [`InsertPosition`] is only applicable for [`VimMode::Insert`]. fn transition_with_motion(&self, position: InsertPosition) -> ModeTransition { ModeTransition { mode: *self, position, } }} /// A finite-state automaton for interpreting the meaning of Vim key sequences. It takes/// individual keystrokes, performs necessary state transitions for each one, and emits a VimEvent/// every time the corresponding editor View needs to change in turn.#[derive(Default, Debug)]pub struct VimFSA { pub mode: VimMode, /// The characters entered which have yet to form a completed command. /// See ":help showcmd" in Vim. showcmd: String, pending_action: Option<PendingAction>, pending_action_count: Option<String>, pending_operand_count: Option<String>, /// When you do something like "vaw", set this field after "va" was typed. pending_visual_object: Option<TextObjectInclusion>, /// This remembers the last `f`, `F`, `t`, `T` command so that it can be repeated with `;` or /// `,`. last_find_motion: Option<FindCharMotion>, /// Vim has char-named registers which act as separate clipboards. These can be specified on a /// per-command basis using the `"` command, see ':help "' in Vim. register: char, /// Holds the last [`VimEvent`] where [`VimEventType::for_dot_repeat`] returns `Some`. dot_repeat_event: Option<VimEvent>, continuous_replace: bool,} #[derive(Clone, Copy, Debug, Eq, PartialEq)]pub enum Direction { Backward, Forward,} impl Direction { pub fn opposite(&self) -> Self { match self { Self::Backward => Self::Forward, Self::Forward => Self::Backward, } }} /// For WordMotions, indicates whether we're moving between the word beginnings or endings.#[derive(Copy, Clone, Debug, PartialEq)]pub enum WordBound { Start, End,} /// This enum distinguishes between `w`/`W`, `b`/`B`, etc./// See https://vimdoc.sourceforge.net/htmldoc/motion.html#word/// or enter ":help word" in Vim.#[derive(Clone, Copy, Debug, Eq, PartialEq)]pub enum WordType { /// More traditional definition of words, as used by `w`, `b`, etc. which break on punctuation. Default, /// Words which include any punctuation, as used by `W`, `B`, etc. The Vim docs refer to these /// as "WORD" in all caps. Enter ":help WORD" in Vim. /// /// Their source code uses the term "bigword" instead. See here: /// https://github.com/neovim/neovim/blob/0fd8eb8aa/src/nvim/textobject.c#L486 BigWord,} /// In general, word motions have upper/lowercase pairs, e.g. `w` and `W` which perform analogous/// motions on different definitions of "word". The lowercase versions match what most other/// programs consider a "word", and so this is named "Default". The uppercase ones redefine/// word-breaking symbols as being word-characters, and is referred to in the Vim docs as a "WORD",/// but the Vim source code calls them "bigwords".impl From<char> for WordType { fn from(c: char) -> Self { if c.is_ascii_uppercase() { Self::BigWord } else { Self::Default } }} /// Word-based motions of the cursor, e.g. for the "w" and "b" commands.#[derive(Clone, Debug)]pub struct WordMotion { pub direction: Direction, pub bound: WordBound, pub word_type: WordType,} impl WordMotion { pub fn new(direction: Direction, bound: WordBound, word_type: WordType) -> Self { Self { direction, bound, word_type, } }} /// Line-based motions of the cursor, e.g. for the "$" and "0" commands./// Note that not all line-based operations have a direction.#[derive(Clone, Copy, Debug, Eq, PartialEq)]pub enum LineMotion { Start, FirstNonWhitespace, End,} /// Character-based motions of the cursor/// such as "h", "j", "k", "l" and arrow keys.#[derive(Clone, Copy, Debug, Eq, PartialEq)]pub enum CharacterMotion { Up, Down, Left, Right, /// WrappingLeft wraps around line boundaries, /// whereas Left stops at line boundaries. WrappingLeft, /// WrappingRight wraps around line boundaries, /// whereas Right stops at line boundaries. WrappingRight,} /// Motions "+", "-", "_"#[derive(Clone, Copy, Debug, Eq, PartialEq)]pub enum FirstNonWhitespaceMotion { Up, Down, DownMinusOne,} /// Motions for "f", "F", "t", and "T".#[derive(Clone, Debug)]pub struct FindCharMotion { pub direction: Direction, pub destination: FindCharDestination, pub is_repetition: bool, pub c: char,} impl FindCharMotion { fn with_opposite_direction(mut self) -> Self { self.direction = self.direction.opposite(); self } /// Sets `initial_search` to false. fn repetition(mut self) -> Self { self.is_repetition = true; self }} /// A pending operand is an as-yet incomplete operand, which is whatever comes after an operator./// An example would be when `di` is entered and we're waiting for the last character, e.g. `diw`./// This is similar to the [`PendingAction`] enum, but these must be different enums because the/// possible characters for pending operands is a subset of the pending actions.#[derive(Clone, Debug)]enum PendingOperand { /// Can only be ge, gg in this context G, TextObject(TextObjectInclusion), FindChar { direction: Direction, destination: FindCharDestination, }, SquareBracket(Direction),} impl From<char> for PendingOperand { fn from(c: char) -> Self { match c { 'g' => Self::G, 'i' => Self::TextObject(TextObjectInclusion::Inner), 'a' => Self::TextObject(TextObjectInclusion::Around), 'f' => Self::FindChar { direction: Direction::Forward, destination: FindCharDestination::AtChar, }, 't' => Self::FindChar { direction: Direction::Forward, destination: FindCharDestination::BeforeChar, }, 'F' => Self::FindChar { direction: Direction::Backward, destination: FindCharDestination::AtChar, }, 'T' => Self::FindChar { direction: Direction::Backward, destination: FindCharDestination::BeforeChar, }, '[' => Self::SquareBracket(Direction::Backward), ']' => Self::SquareBracket(Direction::Forward), _ => panic!("invalid char for PendingOperand: {c}"), } }} /// This distinguishes between `f` vs `t`, as well as `F` vs `T`.#[derive(Clone, Copy, Debug)]pub enum FindCharDestination { /// When the match is found, move the cursor _on_ that matched char. AtChar, /// When the match is found, move the cursor _before_ that matched char. Note that "before" /// when moving left means "one char to the _right_ of the match". BeforeChar,} /// An enum for vim actions./// For example: delete, change#[derive(Clone, Debug)]enum PendingAction { Operation { operator: VimOperator, pending_operand: Option<PendingOperand>, }, /// the full "g" command G, /// the full "z" command (e.g. zz) Z, FindChar { direction: Direction, destination: FindCharDestination, }, SquareBracket(Direction), SetRegister,} impl From<char> for PendingAction { fn from(c: char) -> Self { match c { 'd' => Self::Operation { operator: VimOperator::Delete, pending_operand: None, }, 'c' => Self::Operation { operator: VimOperator::Change, pending_operand: None, }, 'y' => Self::Operation { operator: VimOperator::Yank, pending_operand: None, }, '>' => Self::Operation { operator: VimOperator::Indent, pending_operand: None, }, '<' => Self::Operation { operator: VimOperator::Dedent, pending_operand: None, }, 'g' => Self::G, 'z' => Self::Z, 'f' => Self::FindChar { direction: Direction::Forward, destination: FindCharDestination::AtChar, }, 't' => Self::FindChar { direction: Direction::Forward, destination: FindCharDestination::BeforeChar, }, 'F' => Self::FindChar { direction: Direction::Backward, destination: FindCharDestination::AtChar, }, 'T' => Self::FindChar { direction: Direction::Backward, destination: FindCharDestination::BeforeChar, }, '[' => Self::SquareBracket(Direction::Backward), ']' => Self::SquareBracket(Direction::Forward), '"' => Self::SetRegister, _ => panic!("Invalid char for PendingAction: {c}"), } }} /// An enum for all vim motions.#[derive(Clone, Debug)]pub enum VimMotion { Character(CharacterMotion), Word(WordMotion), Line(LineMotion), FirstNonWhitespace(FirstNonWhitespaceMotion), /// See ":help f" in Vim. FindChar(FindCharMotion), JumpToFirstLine, JumpToLastLine, /// Jump to a specific line number. See ":help gg" and ":help G" in Vim. JumpToLine(u32), /// See ":help %" in Vim. JumpToMatchingBracket, /// See ":help [" in Vim. JumpToUnmatchedBracket(BracketChar), /// See ":help {" in Vim. Paragraph(Direction),} /// This enum mirrors Vim's "MotionType", see:/// https://github.com/neovim/neovim/blob/0fd8eb8/src/nvim/normal.h#L19-L24/// See ":help linewise" in Vim.#[derive(Copy, Clone, Debug, Eq, PartialEq)]pub enum MotionType { Charwise, Linewise,} /// From the Vim docs on text objects:/// > The commands that start with "a" select "a"n object including white space, the commands/// > starting with "i" select an "inner" object without white space, or just the white space. Thus/// > the "inner" commands always select less text than the "a" commands."/// > See https://vimdoc.sourceforge.net/htmldoc/motion.html#text-objects/// > or enter ":help text-objects" in Vim.#[derive(Clone, Copy, Debug)]pub enum TextObjectInclusion { /// For text objects like "a word" or "a block", etc. Around, /// For text objects like "inner word" or "inner block", etc. Inner,} /// Each text object is a "product" of an object type, e.g. word, sentence, block, and two states/// which the Vim docs refer to as "a" or "inner". So, there's "a word" and "inner word".#[derive(Clone, Debug)]pub struct VimTextObject { pub inclusion: TextObjectInclusion, pub object_type: TextObjectType,} impl From<char> for TextObjectType { fn from(c: char) -> Self { match c { 'w' | 'W' => TextObjectType::Word(WordType::from(c)), 'p' => TextObjectType::Paragraph, '\'' | '"' | '`' => TextObjectType::Quote(QuoteType::from(c)), 'b' | 'B' | '(' | ')' | '[' | ']' | '{' | '}' => { TextObjectType::Block(BracketType::from(c)) } _ => panic!("Invalid char for TextObjectType: {c}"), } }} #[derive(Clone, Copy, Debug, Eq, PartialEq)]pub enum QuoteType { Single, Double, Backtick,} impl QuoteType { pub fn is_char(&self, c: char) -> bool { match self { Self::Single => c == '\'', Self::Double => c == '"', Self::Backtick => c == '`', } }} impl From<char> for QuoteType { fn from(c: char) -> Self { match c { '\'' => Self::Single, '"' => Self::Double, '`' => Self::Backtick, _ => panic!("invalid char for QuoteType: {c}"), } }} /// Whether a bracket is opening "{" or closing "}".#[derive(Clone, Copy, Debug, Eq, PartialEq)]pub enum BracketEnd { Opening, Closing,} /// What kind of bracket, either "(", "[", or "{".#[derive(Clone, Copy, Debug, Eq, PartialEq)]pub enum BracketType { Parenthesis, CurlyBrace, SquareBracket,} impl From<char> for BracketType { fn from(c: char) -> Self { match c { '(' | ')' | 'b' => Self::Parenthesis, '[' | ']' => Self::SquareBracket, '{' | '}' | 'B' => Self::CurlyBrace, _ => panic!("Invalid char for BracketType: {c}"), } }} /// Struct to fully represent a bracket character.#[derive(Clone, Debug, Eq, PartialEq)]pub struct BracketChar { pub(crate) end: BracketEnd, pub(crate) kind: BracketType,} impl BracketChar { /// Check if the raw character passed in is the same as self. pub fn is_char(&self, c: char) -> bool { match Self::try_from(c) { Ok(other) => *self == other, Err(_) => false, } } /// Check if the raw character passed in complements self to form a pair, e.g. "(" and ")". pub fn complements(&self, other: char) -> bool { match Self::try_from(other) { Ok(other) => self.kind == other.kind && self.end != other.end, Err(_) => false, } }} impl TryFrom<char> for BracketChar { type Error = (); fn try_from(c: char) -> Result<Self, Self::Error> { Ok(match c { '(' => Self { end: BracketEnd::Opening, kind: BracketType::Parenthesis, }, ')' => Self { end: BracketEnd::Closing, kind: BracketType::Parenthesis, }, '[' => Self { end: BracketEnd::Opening, kind: BracketType::SquareBracket, }, ']' => Self { end: BracketEnd::Closing, kind: BracketType::SquareBracket, }, '{' => Self { end: BracketEnd::Opening, kind: BracketType::CurlyBrace, }, '}' => Self { end: BracketEnd::Closing, kind: BracketType::CurlyBrace, }, _ => return Err(()), }) }} #[derive(Clone, Debug)]pub enum TextObjectType { /// Enter ":help aw" in Vim. Word(WordType), /// Enter ":help ap" in Vim. Paragraph, /// Enter ":help aquote" in Vim. Quote(QuoteType), /// Enter ":help a{" in Vim. Block(BracketType),} /// This enumerates all the positions relative to the cursor where insert mode can begin.Lines 1–500 of 2239. Load more
use warp_core::safe_info; use warpui_core::keymap::Keystroke; use warpui_core::{Entity, ModelContext, ModelHandle, ViewContext}; use crate::register::{BLACK_HOLE_REGISTER, valid_register_name}; /// ASCII code for backspace. /// In Normal and Visual modes, Vim treats backspace as a leftward character motion. const BACKSPACE_CHAR: char = '\u{8}'; /// Represents the Vim modes we currently support. Insert mode is the default instead of normal /// mode to match the behavior of Bash and Zsh's vi mode. #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] pub enum VimMode { Normal, #[default] Insert, Visual(MotionType), Replace, } impl VimMode { /// Convenience method for constructing a [`ModeTransition`] at a specific insert position. /// Note that a non-default [`InsertPosition`] is only applicable for [`VimMode::Insert`]. fn transition_with_motion(&self, position: InsertPosition) -> ModeTransition { ModeTransition { mode: *self, position, } } } /// A finite-state automaton for interpreting the meaning of Vim key sequences. It takes /// individual keystrokes, performs necessary state transitions for each one, and emits a VimEvent /// every time the corresponding editor View needs to change in turn. #[derive(Default, Debug)] pub struct VimFSA { pub mode: VimMode, /// The characters entered which have yet to form a completed command. /// See ":help showcmd" in Vim. showcmd: String, pending_action: Option<PendingAction>, pending_action_count: Option<String>, pending_operand_count: Option<String>, /// When you do something like "vaw", set this field after "va" was typed. pending_visual_object: Option<TextObjectInclusion>, /// This remembers the last `f`, `F`, `t`, `T` command so that it can be repeated with `;` or /// `,`. last_find_motion: Option<FindCharMotion>, /// Vim has char-named registers which act as separate clipboards. These can be specified on a /// per-command basis using the `"` command, see ':help "' in Vim. register: char, /// Holds the last [`VimEvent`] where [`VimEventType::for_dot_repeat`] returns `Some`. dot_repeat_event: Option<VimEvent>, continuous_replace: bool, } #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum Direction { Backward, Forward, } impl Direction { pub fn opposite(&self) -> Self { match self { Self::Backward => Self::Forward, Self::Forward => Self::Backward, } } } /// For WordMotions, indicates whether we're moving between the word beginnings or endings. #[derive(Copy, Clone, Debug, PartialEq)] pub enum WordBound { Start, End, } /// This enum distinguishes between `w`/`W`, `b`/`B`, etc. /// See https://vimdoc.sourceforge.net/htmldoc/motion.html#word /// or enter ":help word" in Vim. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum WordType { /// More traditional definition of words, as used by `w`, `b`, etc. which break on punctuation. Default, /// Words which include any punctuation, as used by `W`, `B`, etc. The Vim docs refer to these /// as "WORD" in all caps. Enter ":help WORD" in Vim. /// /// Their source code uses the term "bigword" instead. See here: /// https://github.com/neovim/neovim/blob/0fd8eb8aa/src/nvim/textobject.c#L486 BigWord, } /// In general, word motions have upper/lowercase pairs, e.g. `w` and `W` which perform analogous /// motions on different definitions of "word". The lowercase versions match what most other /// programs consider a "word", and so this is named "Default". The uppercase ones redefine /// word-breaking symbols as being word-characters, and is referred to in the Vim docs as a "WORD", /// but the Vim source code calls them "bigwords". impl From<char> for WordType { fn from(c: char) -> Self { if c.is_ascii_uppercase() { Self::BigWord } else { Self::Default } } } /// Word-based motions of the cursor, e.g. for the "w" and "b" commands. #[derive(Clone, Debug)] pub struct WordMotion { pub direction: Direction, pub bound: WordBound, pub word_type: WordType, } impl WordMotion { pub fn new(direction: Direction, bound: WordBound, word_type: WordType) -> Self { Self { direction, bound, word_type, } } } /// Line-based motions of the cursor, e.g. for the "$" and "0" commands. /// Note that not all line-based operations have a direction. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum LineMotion { Start, FirstNonWhitespace, End, } /// Character-based motions of the cursor /// such as "h", "j", "k", "l" and arrow keys. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum CharacterMotion { Up, Down, Left, Right, /// WrappingLeft wraps around line boundaries, /// whereas Left stops at line boundaries. WrappingLeft, /// WrappingRight wraps around line boundaries, /// whereas Right stops at line boundaries. WrappingRight, } /// Motions "+", "-", "_" #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum FirstNonWhitespaceMotion { Up, Down, DownMinusOne, } /// Motions for "f", "F", "t", and "T". #[derive(Clone, Debug)] pub struct FindCharMotion { pub direction: Direction, pub destination: FindCharDestination, pub is_repetition: bool, pub c: char, } impl FindCharMotion { fn with_opposite_direction(mut self) -> Self { self.direction = self.direction.opposite(); self } /// Sets `initial_search` to false. fn repetition(mut self) -> Self { self.is_repetition = true; self } } /// A pending operand is an as-yet incomplete operand, which is whatever comes after an operator. /// An example would be when `di` is entered and we're waiting for the last character, e.g. `diw`. /// This is similar to the [`PendingAction`] enum, but these must be different enums because the /// possible characters for pending operands is a subset of the pending actions. #[derive(Clone, Debug)] enum PendingOperand { /// Can only be ge, gg in this context G, TextObject(TextObjectInclusion), FindChar { direction: Direction, destination: FindCharDestination, }, SquareBracket(Direction), } impl From<char> for PendingOperand { fn from(c: char) -> Self { match c { 'g' => Self::G, 'i' => Self::TextObject(TextObjectInclusion::Inner), 'a' => Self::TextObject(TextObjectInclusion::Around), 'f' => Self::FindChar { direction: Direction::Forward, destination: FindCharDestination::AtChar, }, 't' => Self::FindChar { direction: Direction::Forward, destination: FindCharDestination::BeforeChar, }, 'F' => Self::FindChar { direction: Direction::Backward, destination: FindCharDestination::AtChar, }, 'T' => Self::FindChar { direction: Direction::Backward, destination: FindCharDestination::BeforeChar, }, '[' => Self::SquareBracket(Direction::Backward), ']' => Self::SquareBracket(Direction::Forward), _ => panic!("invalid char for PendingOperand: {c}"), } } } /// This distinguishes between `f` vs `t`, as well as `F` vs `T`. #[derive(Clone, Copy, Debug)] pub enum FindCharDestination { /// When the match is found, move the cursor _on_ that matched char. AtChar, /// When the match is found, move the cursor _before_ that matched char. Note that "before" /// when moving left means "one char to the _right_ of the match". BeforeChar, } /// An enum for vim actions. /// For example: delete, change #[derive(Clone, Debug)] enum PendingAction { Operation { operator: VimOperator, pending_operand: Option<PendingOperand>, }, /// the full "g" command G, /// the full "z" command (e.g. zz) Z, FindChar { direction: Direction, destination: FindCharDestination, }, SquareBracket(Direction), SetRegister, } impl From<char> for PendingAction { fn from(c: char) -> Self { match c { 'd' => Self::Operation { operator: VimOperator::Delete, pending_operand: None, }, 'c' => Self::Operation { operator: VimOperator::Change, pending_operand: None, }, 'y' => Self::Operation { operator: VimOperator::Yank, pending_operand: None, }, '>' => Self::Operation { operator: VimOperator::Indent, pending_operand: None, }, '<' => Self::Operation { operator: VimOperator::Dedent, pending_operand: None, }, 'g' => Self::G, 'z' => Self::Z, 'f' => Self::FindChar { direction: Direction::Forward, destination: FindCharDestination::AtChar, }, 't' => Self::FindChar { direction: Direction::Forward, destination: FindCharDestination::BeforeChar, }, 'F' => Self::FindChar { direction: Direction::Backward, destination: FindCharDestination::AtChar, }, 'T' => Self::FindChar { direction: Direction::Backward, destination: FindCharDestination::BeforeChar, }, '[' => Self::SquareBracket(Direction::Backward), ']' => Self::SquareBracket(Direction::Forward), '"' => Self::SetRegister, _ => panic!("Invalid char for PendingAction: {c}"), } } } /// An enum for all vim motions. #[derive(Clone, Debug)] pub enum VimMotion { Character(CharacterMotion), Word(WordMotion), Line(LineMotion), FirstNonWhitespace(FirstNonWhitespaceMotion), /// See ":help f" in Vim. FindChar(FindCharMotion), JumpToFirstLine, JumpToLastLine, /// Jump to a specific line number. See ":help gg" and ":help G" in Vim. JumpToLine(u32), /// See ":help %" in Vim. JumpToMatchingBracket, /// See ":help [" in Vim. JumpToUnmatchedBracket(BracketChar), /// See ":help {" in Vim. Paragraph(Direction), } /// This enum mirrors Vim's "MotionType", see: /// https://github.com/neovim/neovim/blob/0fd8eb8/src/nvim/normal.h#L19-L24 /// See ":help linewise" in Vim. #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub enum MotionType { Charwise, Linewise, } /// From the Vim docs on text objects: /// > The commands that start with "a" select "a"n object including white space, the commands /// > starting with "i" select an "inner" object without white space, or just the white space. Thus /// > the "inner" commands always select less text than the "a" commands." /// > See https://vimdoc.sourceforge.net/htmldoc/motion.html#text-objects /// > or enter ":help text-objects" in Vim. #[derive(Clone, Copy, Debug)] pub enum TextObjectInclusion { /// For text objects like "a word" or "a block", etc. Around, /// For text objects like "inner word" or "inner block", etc. Inner, } /// Each text object is a "product" of an object type, e.g. word, sentence, block, and two states /// which the Vim docs refer to as "a" or "inner". So, there's "a word" and "inner word". #[derive(Clone, Debug)] pub struct VimTextObject { pub inclusion: TextObjectInclusion, pub object_type: TextObjectType, } impl From<char> for TextObjectType { fn from(c: char) -> Self { match c { 'w' | 'W' => TextObjectType::Word(WordType::from(c)), 'p' => TextObjectType::Paragraph, '\'' | '"' | '`' => TextObjectType::Quote(QuoteType::from(c)), 'b' | 'B' | '(' | ')' | '[' | ']' | '{' | '}' => { TextObjectType::Block(BracketType::from(c)) } _ => panic!("Invalid char for TextObjectType: {c}"), } } } #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum QuoteType { Single, Double, Backtick, } impl QuoteType { pub fn is_char(&self, c: char) -> bool { match self { Self::Single => c == '\'', Self::Double => c == '"', Self::Backtick => c == '`', } } } impl From<char> for QuoteType { fn from(c: char) -> Self { match c { '\'' => Self::Single, '"' => Self::Double, '`' => Self::Backtick, _ => panic!("invalid char for QuoteType: {c}"), } } } /// Whether a bracket is opening "{" or closing "}". #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum BracketEnd { Opening, Closing, } /// What kind of bracket, either "(", "[", or "{". #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum BracketType { Parenthesis, CurlyBrace, SquareBracket, } impl From<char> for BracketType { fn from(c: char) -> Self { match c { '(' | ')' | 'b' => Self::Parenthesis, '[' | ']' => Self::SquareBracket, '{' | '}' | 'B' => Self::CurlyBrace, _ => panic!("Invalid char for BracketType: {c}"), } } } /// Struct to fully represent a bracket character. #[derive(Clone, Debug, Eq, PartialEq)] pub struct BracketChar { pub(crate) end: BracketEnd, pub(crate) kind: BracketType, } impl BracketChar { /// Check if the raw character passed in is the same as self. pub fn is_char(&self, c: char) -> bool { match Self::try_from(c) { Ok(other) => *self == other, Err(_) => false, } } /// Check if the raw character passed in complements self to form a pair, e.g. "(" and ")". pub fn complements(&self, other: char) -> bool { match Self::try_from(other) { Ok(other) => self.kind == other.kind && self.end != other.end, Err(_) => false, } } } impl TryFrom<char> for BracketChar { type Error = (); fn try_from(c: char) -> Result<Self, Self::Error> { Ok(match c { '(' => Self { end: BracketEnd::Opening, kind: BracketType::Parenthesis, }, ')' => Self { end: BracketEnd::Closing, kind: BracketType::Parenthesis, }, '[' => Self { end: BracketEnd::Opening, kind: BracketType::SquareBracket, }, ']' => Self { end: BracketEnd::Closing, kind: BracketType::SquareBracket, }, '{' => Self { end: BracketEnd::Opening, kind: BracketType::CurlyBrace, }, '}' => Self { end: BracketEnd::Closing, kind: BracketType::CurlyBrace, }, _ => return Err(()), }) } } #[derive(Clone, Debug)] pub enum TextObjectType { /// Enter ":help aw" in Vim. Word(WordType), /// Enter ":help ap" in Vim. Paragraph, /// Enter ":help aquote" in Vim. Quote(QuoteType), /// Enter ":help a{" in Vim. Block(BracketType), } /// This enumerates all the positions relative to the cursor where insert mode can begin.