use escriba_search::Direction as SearchDirection;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::edit::Edit;
use crate::mode::Mode;
use crate::motion::TextObject;
use crate::motion::{Motion, Operator};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub enum Action {
Move(Motion),
Operator(Operator),
ApplyOperator {
op: Operator,
motion: Motion,
},
Edit(Edit),
ChangeMode(Mode),
Command {
name: String,
args: Vec<String>,
},
InsertChar(char),
SubmitCommand,
Undo,
Redo,
Save,
Quit,
SearchOpen(SearchDirection),
SearchRepeat {
reverse: bool,
},
SearchWord {
reverse: bool,
},
ClearSearchHighlight,
SearchSubmitOperated {
op: Operator,
},
TextObject(TextObject),
ApplyOperatorObject {
op: Operator,
object: TextObject,
},
RepeatLastChange,
JumpBack,
JumpForward,
PromptBackspace,
PromptCaret {
to: escriba_search::CaretMove,
},
SearchPreviewStep {
forward: bool,
},
PromptDelete,
PromptDeleteWord,
PromptClearToStart,
PromptHistory {
back: bool,
},
Pending,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct CountedAction {
pub count: u32,
pub action: Action,
}
impl CountedAction {
#[must_use]
pub fn once(action: Action) -> Self {
Self { count: 1, action }
}
#[must_use]
pub fn repeated(count: u32, action: Action) -> Self {
Self {
count: count.max(1),
action,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TextEffect {
Mutates,
Preserves,
}
impl Action {
#[must_use]
pub const fn text_effect(&self) -> TextEffect {
match self {
Self::Edit(_)
| Self::InsertChar(_)
| Self::ApplyOperator { .. }
| Self::ApplyOperatorObject { .. }
| Self::Undo
| Self::Redo
| Self::PromptBackspace
| Self::PromptDelete
| Self::PromptDeleteWord
| Self::PromptClearToStart
| Self::TextObject(_)
| Self::Command { .. }
| Self::SearchSubmitOperated { .. }
| Self::RepeatLastChange
| Self::ApplyOperatorObject { .. }
| Self::SubmitCommand => TextEffect::Mutates,
Self::Move(_)
| Self::Operator(_)
| Self::ChangeMode(_)
| Self::Save
| Self::Quit
| Self::SearchOpen(_)
| Self::SearchRepeat { .. }
| Self::SearchWord { .. }
| Self::ClearSearchHighlight
| Self::JumpBack
| Self::JumpForward
| Self::PromptCaret { .. }
| Self::PromptHistory { .. }
| Self::SearchPreviewStep { .. }
| Self::Pending => TextEffect::Preserves,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum HighlightEffect {
Keep,
Clear,
}
impl Action {
#[must_use]
pub const fn highlight_effect(&self) -> HighlightEffect {
match self {
Self::SearchOpen(_)
| Self::SearchRepeat { .. }
| Self::SearchWord { .. }
| Self::SearchSubmitOperated { .. }
| Self::ClearSearchHighlight
| Self::TextObject(_)
| Self::SearchPreviewStep { .. }
| Self::PromptHistory { .. }
| Self::PromptBackspace
| Self::PromptCaret { .. }
| Self::SearchPreviewStep { .. }
| Self::PromptDelete
| Self::PromptDeleteWord
| Self::PromptClearToStart
| Self::InsertChar(_)
| Self::SubmitCommand
| Self::Pending
| Self::JumpBack
| Self::JumpForward
| Self::Operator(_)
| Self::Save
| Self::Quit => HighlightEffect::Keep,
Self::Move(m) => match m {
Motion::SearchNext | Motion::SearchPrev => HighlightEffect::Keep,
_ => HighlightEffect::Clear,
},
Self::ApplyOperator { .. }
| Self::ApplyOperatorObject { .. }
| Self::RepeatLastChange
| Self::Edit(_)
| Self::ChangeMode(_)
| Self::Command { .. }
| Self::Undo
| Self::Redo => HighlightEffect::Clear,
}
}
}