pub use editor_types::{
Action,
CommandAction,
CommandBarAction,
CursorAction,
EditAction,
EditorAction,
HistoryAction,
InsertTextAction,
MacroAction,
PromptAction,
SelectionAction,
TabAction,
WindowAction,
};
use crate::{
commands::{Command, CommandMachine},
editing::application::*,
editing::context::{EditContext, Resolve},
editing::store::RegisterStore,
errors::{EditError, EditResult, UIResult},
prelude::*,
};
pub trait EditorActions<C, S, I>
where
I: ApplicationInfo,
{
fn edit(
&mut self,
action: &EditAction,
target: &EditTarget,
ctx: &C,
store: &mut S,
) -> EditResult<EditInfo, I>;
fn mark(&mut self, name: Mark, ctx: &C, store: &mut S) -> EditResult<EditInfo, I>;
fn complete(
&mut self,
style: &CompletionStyle,
comptype: &CompletionType,
display: &CompletionDisplay,
ctx: &C,
store: &mut S,
) -> EditResult<EditInfo, I>;
fn insert_text(
&mut self,
act: &InsertTextAction,
ctx: &C,
store: &mut S,
) -> EditResult<EditInfo, I>;
fn selection_command(
&mut self,
act: &SelectionAction,
ctx: &C,
store: &mut S,
) -> EditResult<EditInfo, I>;
fn cursor_command(
&mut self,
act: &CursorAction,
ctx: &C,
store: &mut S,
) -> EditResult<EditInfo, I>;
fn history_command(
&mut self,
act: &HistoryAction,
ctx: &C,
store: &mut S,
) -> EditResult<EditInfo, I>;
}
pub trait Editable<C, S, I>
where
I: ApplicationInfo,
{
fn editor_command(
&mut self,
act: &EditorAction,
ctx: &C,
store: &mut S,
) -> EditResult<EditInfo, I>;
}
pub trait Commandable<C, I>
where
C: Command,
I: ApplicationInfo,
{
fn command(
&mut self,
action: &CommandAction,
ctx: &C::Context,
rstore: &mut RegisterStore,
) -> UIResult<Vec<(C::Action, C::Context)>, I>;
}
impl<C, I> Commandable<C, I> for CommandMachine<C>
where
C: Command<Action = Action<I>, Context = EditContext>,
I: ApplicationInfo,
{
fn command(
&mut self,
action: &CommandAction,
ctx: &C::Context,
rstore: &mut RegisterStore,
) -> UIResult<Vec<(Action<I>, C::Context)>, I> {
match action {
CommandAction::Execute(count) => {
let count = ctx.resolve(count);
let cmd = rstore.get_last_cmd().to_string();
let msg = format!(":{cmd}");
let msg = Action::ShowInfoMessage(msg.into());
let mut acts = vec![(msg, ctx.clone())];
for _ in 0..count {
let mut res = self.input_cmd(cmd.as_str(), ctx.clone())?;
acts.append(&mut res);
}
Ok(acts)
},
CommandAction::Run(cmd) => {
rstore.set_last_cmd(cmd.as_str());
let acts = self.input_cmd(cmd, ctx.clone())?;
Ok(acts)
},
a => {
let msg = format!("unknown command action: {a:?}");
let err = EditError::Unimplemented(msg);
Err(err.into())
},
}
}
}
pub trait Promptable<C, S, I>
where
I: ApplicationInfo,
{
fn prompt(
&mut self,
act: &PromptAction,
ctx: &C,
store: &mut S,
) -> EditResult<Vec<(Action<I>, C)>, I>;
}
pub trait TabCount {
fn tabs(&self) -> usize;
}
pub trait TabContainer<C, S, I>: TabCount
where
I: ApplicationInfo,
{
fn tab_command(&mut self, act: &TabAction<I>, ctx: &C, store: &mut S) -> UIResult<EditInfo, I>;
}
pub trait WindowCount {
fn windows(&self) -> usize;
}
pub trait WindowContainer<C, S, I>: WindowCount
where
I: ApplicationInfo,
{
fn window_command(
&mut self,
action: &WindowAction<I>,
ctx: &C,
store: &mut S,
) -> UIResult<EditInfo, I>;
}
pub trait Jumpable<C, I>
where
I: ApplicationInfo,
{
fn jump(
&mut self,
list: PositionList,
dir: MoveDir1D,
count: usize,
ctx: &C,
) -> UIResult<usize, I>;
}
pub trait Scrollable<C, S, I>
where
I: ApplicationInfo,
{
fn scroll(&mut self, style: &ScrollStyle, ctx: &C, store: &mut S) -> EditResult<EditInfo, I>;
}
pub trait Searchable<C, S, I>
where
I: ApplicationInfo,
{
fn search(
&mut self,
dir: MoveDirMod,
count: Count,
ctx: &C,
store: &mut S,
) -> UIResult<EditInfo, I>;
}