use canvist_model::Mark;
use canvist_model::Schema;
use canvist_model::Value;
use crate::Binding;
use crate::Command;
use crate::CommandSpec;
use crate::EditorError;
use crate::EditorState;
use crate::InputRule;
use crate::NodeKind;
use crate::NodeMeta;
use crate::PasteRule;
use crate::QuerySpec;
use crate::Registration;
use crate::ValueType;
use crate::command;
use crate::commands;
use crate::extension::mark_type;
use crate::query;
#[derive(Debug)]
pub struct SimpleMark {
pub name: &'static str,
pub toggle: &'static str,
pub set: &'static str,
pub unset: &'static str,
pub is_active: &'static str,
pub keys: &'static [&'static str],
pub input: &'static [&'static str],
pub paste: &'static [&'static str],
}
impl SimpleMark {
#[must_use]
pub fn toggle(&'static self) -> Command {
command(move |state| {
let kind = mark_type(state.schema(), self.name, self.name)?;
commands::toggle_mark(state, &Mark::new(kind))
})
}
#[must_use]
pub fn set(&'static self) -> Command {
command(move |state| {
let kind = mark_type(state.schema(), self.name, self.name)?;
commands::set_mark(state, &Mark::new(kind))
})
}
#[must_use]
pub fn unset(&'static self) -> Command {
command(move |state| {
let kind = mark_type(state.schema(), self.name, self.name)?;
commands::unset_mark(state, kind)
})
}
#[must_use]
pub fn is_active(&self, state: &EditorState) -> bool {
query::mark_active(state, self.name)
}
#[must_use]
pub fn nodes(&self) -> Vec<NodeMeta> {
vec![NodeMeta::new(NodeKind::Mark, self.name)]
}
#[must_use]
pub fn commands(&'static self) -> Vec<CommandSpec> {
vec![
CommandSpec::simple(
self.toggle,
"Add the mark to the selection, or remove it if every selected character has it.",
self.toggle(),
),
CommandSpec::simple(self.set, "Add the mark to the selection.", self.set()),
CommandSpec::simple(
self.unset,
"Remove the mark from the selection.",
self.unset(),
),
]
}
#[must_use]
pub fn queries(&'static self) -> Vec<QuerySpec> {
vec![QuerySpec::new(
self.is_active,
"Whether the mark is active at the selection.",
ValueType::Bool,
move |state, _| Ok(Value::Bool(self.is_active(state))),
)]
}
#[must_use]
pub fn keymap(&'static self) -> Vec<Binding> {
self.keys.iter().map(|&key| (key, self.toggle())).collect()
}
pub fn input_rules(&self, schema: &Schema) -> Result<Vec<InputRule>, EditorError> {
let kind = mark_type(schema, self.name, self.name)?;
self.input
.iter()
.map(|pattern| InputRule::mark(pattern, kind))
.collect()
}
pub fn paste_rules(&self, schema: &Schema) -> Result<Vec<PasteRule>, EditorError> {
let kind = mark_type(schema, self.name, self.name)?;
self.paste
.iter()
.map(|pattern| PasteRule::mark(pattern, kind))
.collect()
}
#[must_use]
pub fn handle(&'static self, registration: Registration) -> MarkHandle {
MarkHandle {
mark: self,
_registration: registration,
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct MarkHandle {
mark: &'static SimpleMark,
_registration: Registration,
}
impl MarkHandle {
#[must_use]
pub fn toggle(self) -> Command {
self.mark.toggle()
}
#[must_use]
pub fn set(self) -> Command {
self.mark.set()
}
#[must_use]
pub fn unset(self) -> Command {
self.mark.unset()
}
#[must_use]
pub fn is_active(self, state: &EditorState) -> bool {
self.mark.is_active(state)
}
}