use std::sync::Arc;
use std::sync::OnceLock;
use std::time::Duration;
use canvist_model::BlockTree;
use canvist_model::BlockTypeId;
use canvist_model::Commit;
use canvist_model::Document;
use canvist_model::Schema;
use canvist_model::Selection;
use canvist_model::Step;
use canvist_model::TreeContent;
use rustc_hash::FxHashMap;
use crate::Command;
use crate::CommandResult;
use crate::CommandSpec;
use crate::Context;
use crate::EditKind;
use crate::EditorError;
use crate::EditorState;
use crate::Extension;
use crate::ExtensionMeta;
use crate::History;
use crate::InputRule;
use crate::KeyPress;
use crate::Keymap;
use crate::LineNavigator;
use crate::NodeKind;
use crate::NodeMeta;
use crate::Origin;
use crate::ParagraphNavigator;
use crate::PasteRule;
use crate::Platform;
use crate::QuerySpec;
use crate::RuleInput;
use crate::Slice;
use crate::Transcript;
use crate::UndoHistory;
use crate::Update;
use crate::UpdateFilter;
use crate::commands;
use crate::context::AppliedRule;
use crate::dictation;
use crate::position;
pub struct Env<'a> {
pub now: Duration,
lines: Option<&'a mut dyn LineNavigator>,
fallback: ParagraphNavigator,
}
impl<'a> Env<'a> {
#[must_use]
pub fn new(now: Duration, lines: &'a mut dyn LineNavigator) -> Self {
Self {
now,
lines: Some(lines),
fallback: ParagraphNavigator,
}
}
pub fn lines(&mut self) -> &mut dyn LineNavigator {
match &mut self.lines {
Some(lines) => &mut **lines,
None => &mut self.fallback,
}
}
}
impl Env<'static> {
#[must_use]
pub const fn headless(now: Duration) -> Self {
Self {
now,
lines: None,
fallback: ParagraphNavigator,
}
}
}
impl std::fmt::Debug for Env<'_> {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("Env")
.field("now", &self.now)
.finish_non_exhaustive()
}
}
#[derive(Default)]
pub(crate) struct Registry {
extensions: Vec<Arc<dyn Extension>>,
keymap: Keymap,
input_rules: Vec<InputRule>,
paste_rules: Vec<PasteRule>,
update_filters: Vec<UpdateFilter>,
pub(crate) commands: FxHashMap<&'static str, CommandSpec>,
pub(crate) queries: FxHashMap<&'static str, QuerySpec>,
names: Vec<RegisteredNames>,
metadata: OnceLock<Vec<ExtensionMeta>>,
code_blocks: Vec<BlockTypeId>,
}
struct RegisteredNames {
commands: Vec<&'static str>,
queries: Vec<&'static str>,
}
impl Registry {
fn new(
extensions: Vec<Arc<dyn Extension>>,
schema: &Schema,
platform: Platform,
) -> Result<Self, EditorError> {
let mut registry = Self::default();
for extension in &extensions {
for (binding, command) in extension.keymap(schema, platform)? {
registry.keymap.bind(binding, platform, command)?;
}
registry.describe(extension.as_ref(), schema)?;
registry.input_rules.extend(extension.input_rules(schema)?);
registry.paste_rules.extend(extension.paste_rules(schema)?);
registry
.update_filters
.extend(extension.update_filters(schema)?);
registry.code_blocks.extend(extension.code_blocks(schema)?);
}
registry.extensions = extensions;
Ok(registry)
}
pub(crate) fn metadata(&self, schema: &Schema) -> &[ExtensionMeta] {
self.metadata.get_or_init(|| {
self.extensions
.iter()
.zip(&self.names)
.map(|(extension, names)| {
ExtensionMeta {
name: extension.name(),
nodes: extension
.nodes()
.into_iter()
.filter(|node| has_node(schema, node))
.collect(),
commands: names
.commands
.iter()
.filter_map(|name| self.commands.get(name))
.map(|spec| spec.meta().clone())
.collect(),
queries: names
.queries
.iter()
.filter_map(|name| self.queries.get(name))
.map(|spec| spec.meta().clone())
.collect(),
}
})
.collect()
})
}
pub(crate) fn allows(&self, state: &EditorState, update: &Update) -> bool {
self.update_filters
.iter()
.all(|allows| allows(state, update))
}
fn describe(&mut self, extension: &dyn Extension, schema: &Schema) -> Result<(), EditorError> {
let commands = extension.commands(schema)?;
let queries = extension.queries(schema)?;
let taken = |name: &str| EditorError::DuplicateName(name.to_owned());
self.names.push(RegisteredNames {
commands: commands.iter().map(|spec| spec.meta().name).collect(),
queries: queries.iter().map(|spec| spec.meta().name).collect(),
});
for spec in commands {
let name = spec.meta().name;
if self.commands.insert(name, spec).is_some() {
return Err(taken(name));
}
}
for spec in queries {
let name = spec.meta().name;
if self.queries.insert(name, spec).is_some() {
return Err(taken(name));
}
}
Ok(())
}
}
#[derive(Default)]
pub struct EditorBuilder {
pub(crate) extensions: Vec<Arc<dyn Extension>>,
platform: Platform,
client: u32,
history: Option<Box<dyn History>>,
}
impl EditorBuilder {
#[must_use]
pub fn extension(mut self, extension: impl Extension + 'static) -> Self {
self.extensions.push(Arc::new(extension));
self
}
#[must_use]
pub fn extensions(mut self, extensions: impl IntoIterator<Item = Arc<dyn Extension>>) -> Self {
self.extensions.extend(extensions);
self
}
#[must_use]
pub fn platform(mut self, platform: Platform) -> Self {
self.platform = platform;
self
}
#[must_use]
pub fn client(mut self, client: u32) -> Self {
self.client = client;
self
}
#[must_use]
pub fn history(mut self, history: impl History + 'static) -> Self {
self.history = Some(Box::new(history));
self
}
fn sorted(&self) -> Vec<Arc<dyn Extension>> {
let mut extensions = self.extensions.clone();
extensions.sort_by_key(|extension| std::cmp::Reverse(extension.priority()));
extensions
}
pub fn schema(&self) -> Result<Arc<Schema>, EditorError> {
let builder = self
.sorted()
.iter()
.fold(Schema::builder(), |builder, extension| {
extension.schema(builder)
});
Ok(Arc::new(builder.build()?))
}
pub fn build(self) -> Result<Editor, EditorError> {
self.build_with(|schema, client| Ok(Document::new(schema, client)))
}
pub fn build_with(
self,
content: impl FnOnce(Arc<Schema>, u32) -> Result<Document, EditorError>,
) -> Result<Editor, EditorError> {
let schema = self.schema()?;
let registry = Registry::new(self.sorted(), &schema, self.platform)?;
let state = EditorState::new(content(schema, self.client)?)?;
let history = self
.history
.unwrap_or_else(|| Box::new(UndoHistory::default()));
Ok(Editor {
state,
history,
registry,
platform: self.platform,
last_rule: None,
})
}
}
pub struct Editor {
pub(crate) state: EditorState,
pub(crate) history: Box<dyn History>,
pub(crate) registry: Registry,
platform: Platform,
pub(crate) last_rule: Option<AppliedRule>,
}
impl std::fmt::Debug for Editor {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("Editor")
.field("state", &self.state)
.field("platform", &self.platform)
.finish_non_exhaustive()
}
}
impl Editor {
#[must_use]
pub fn builder() -> EditorBuilder {
EditorBuilder::default()
}
#[must_use]
pub fn state(&self) -> &EditorState {
&self.state
}
#[must_use]
pub fn adornments(&self) -> Vec<crate::Adornment> {
let mut adornments: Vec<crate::Adornment> = self
.registry
.extensions
.iter()
.filter(|extension| extension.adorns())
.flat_map(|extension| extension.adornments(&self.state))
.collect();
adornments.sort_by_key(|adornment| (adornment.at.block, adornment.at.offset));
adornments
}
#[must_use]
pub fn into_state(self) -> EditorState {
self.state
}
#[must_use]
pub fn schema(&self) -> &Arc<Schema> {
self.state.schema()
}
#[must_use]
pub fn platform(&self) -> Platform {
self.platform
}
#[must_use]
pub fn can_undo(&self) -> bool {
self.history.can_undo()
}
#[must_use]
pub fn can_redo(&self) -> bool {
self.history.can_redo()
}
pub fn handle_key(&mut self, press: KeyPress<'_>, env: &mut Env<'_>) -> CommandResult {
let mut context = Context {
state: &mut self.state,
history: self.history.as_mut(),
lines: env.lines(),
last_rule: self.last_rule.as_ref(),
};
let update = first_applying(self.registry.keymap.lookup(press), &mut context)?;
self.finish(update, env.now)
}
pub fn run(&mut self, command: &Command, env: &mut Env<'_>) -> CommandResult {
let mut context = Context {
state: &mut self.state,
history: self.history.as_mut(),
lines: env.lines(),
last_rule: self.last_rule.as_ref(),
};
let update = command(&mut context)?;
self.finish(update, env.now)
}
pub fn insert_text(&mut self, text: &str, env: &mut Env<'_>) -> CommandResult {
if let Some(update) = self.apply_input_rules(text)? {
let finished = self.finish(Some(update), env.now)?;
self.last_rule = finished.as_ref().map(|update| {
AppliedRule {
version: self.state.doc().version(),
selection: self.state.selection(),
typed: text.to_owned(),
undo_steps: update.commit.undo_steps(),
selection_before: update.selection_before,
}
});
return Ok(finished);
}
let update = commands::insert_text(&mut self.state, text)?;
self.finish(update, env.now)
}
fn apply_input_rules(&mut self, text: &str) -> CommandResult {
let Some(caret) = self.state.selection().as_caret() else {
return Ok(None);
};
if self.state.composition().is_some() || self.in_code(caret.block) {
return Ok(None);
}
let Some(input) = RuleInput::new(&self.state, caret, text) else {
return Ok(None);
};
for rule in &self.registry.input_rules {
let Some(found) = rule.find(&input) else {
continue;
};
if let Some(mut update) = rule.apply(&mut self.state, &found)? {
update.kind = EditKind::InputRule;
return Ok(Some(update));
}
}
Ok(None)
}
fn in_code(&self, block: canvist_model::BlockId) -> bool {
let kind = self
.state
.doc()
.block(block)
.map(canvist_model::Block::kind);
kind.is_some_and(|kind| self.registry.code_blocks.contains(&kind))
}
pub fn undo(&mut self, env: &mut Env<'_>) -> CommandResult {
let update = self.history.undo(&mut self.state)?;
self.finish(update, env.now)
}
pub fn redo(&mut self, env: &mut Env<'_>) -> CommandResult {
let update = self.history.redo(&mut self.state)?;
self.finish(update, env.now)
}
pub fn select(&mut self, selection: Selection, env: &mut Env<'_>) -> CommandResult {
self.history.close_group();
let update = commands::set_selection(&mut self.state, selection)?;
self.finish(update, env.now)
}
pub fn select_with_affinity(
&mut self,
selection: Selection,
affinity: crate::Affinity,
env: &mut Env<'_>,
) -> CommandResult {
self.history.close_group();
let update = commands::set_selection_with_affinity(&mut self.state, selection, affinity)?;
self.finish(update, env.now)
}
pub fn copy(&self) -> Result<Slice, EditorError> {
commands::slice_selection(&self.state)
}
pub fn cut(&mut self, env: &mut Env<'_>) -> Result<(Slice, Option<Update>), EditorError> {
let slice = commands::slice_selection(&self.state)?;
let update = commands::delete_selection(&mut self.state)?;
Ok((slice, self.finish(update, env.now)?))
}
pub fn paste(&mut self, slice: Slice, env: &mut Env<'_>) -> CommandResult {
let mut slice = slice;
self.format_pasted(&mut slice)?;
let update = commands::replace_selection(&mut self.state, slice)?;
self.finish(update, env.now)
}
pub fn paste_text(&mut self, text: &str, env: &mut Env<'_>) -> CommandResult {
let in_code = position::text_range(self.state.doc(), self.state.selection())
.is_some_and(|range| self.in_code(range.from.block));
if !in_code {
let slice = Slice::from_plain_text(self.schema(), text);
return self.paste(slice, env);
}
let update = commands::insert_text(&mut self.state, text)?.map(|mut update| {
update.kind = EditKind::Paste;
update
});
self.finish(update, env.now)
}
fn format_pasted(&self, slice: &mut Slice) -> Result<(), EditorError> {
if self.registry.paste_rules.is_empty() {
return Ok(());
}
for tree in slice.blocks_mut() {
self.format_tree(tree)?;
}
Ok(())
}
fn format_tree(&self, tree: &mut BlockTree) -> Result<(), EditorError> {
let is_code = self.registry.code_blocks.contains(&tree.kind);
match &mut tree.content {
TreeContent::Inline(text) if !is_code => {
for rule in &self.registry.paste_rules {
rule.apply(text, self.schema())?;
}
}
TreeContent::Blocks(children) => {
for child in children {
self.format_tree(child)?;
}
}
TreeContent::Inline(_) | TreeContent::Leaf => {}
}
Ok(())
}
pub fn set_composition(&mut self, text: &str, env: &mut Env<'_>) -> CommandResult {
let update = commands::set_composition(&mut self.state, text)?;
self.finish(update, env.now)
}
pub fn commit_composition(&mut self, text: &str, env: &mut Env<'_>) -> CommandResult {
let update = commands::commit_composition(&mut self.state, text)?;
self.finish(update, env.now)
}
pub fn cancel_composition(&mut self, env: &mut Env<'_>) -> CommandResult {
let update = commands::cancel_composition(&mut self.state)?;
self.finish(update, env.now)
}
pub fn dictate(
&mut self,
segment: u32,
transcript: Transcript<'_>,
env: &mut Env<'_>,
) -> CommandResult {
self.state.dictation_mut().record(segment, transcript);
let update = dictation::insert_ready(&mut self.state, env.now)?;
self.finish(update, env.now)
}
pub fn stop_dictation(&mut self) -> bool {
self.state.dictation_mut().stop()
}
pub fn apply_remote(
&mut self,
steps: impl IntoIterator<Item = Step>,
env: &mut Env<'_>,
) -> Result<Update, EditorError> {
let update = self
.state
.apply_steps(steps, EditKind::Remote, Origin::Remote, None)?;
self.last_rule = None;
self.history.record(&update, env.now);
Ok(update)
}
pub fn apply_external<E: From<EditorError>>(
&mut self,
env: &mut Env<'_>,
change: impl FnOnce(&mut Document) -> Result<Commit, E>,
) -> Result<Update, E> {
let update = self
.state
.apply_external(EditKind::Remote, Origin::Remote, change)?;
if update.doc_changed() {
self.last_rule = None;
self.history.record(&update, env.now);
}
Ok(update)
}
pub(crate) fn finish(&mut self, update: Option<Update>, now: Duration) -> CommandResult {
self.last_rule = None;
let Some(update) = update else {
return Ok(None);
};
let update = if update.origin == Origin::Local {
self.fix_up(update)?
} else {
update
};
if update.origin == Origin::Local && !self.registry.allows(&self.state, &update) {
self.state.apply_steps(
update.commit.undo_steps(),
update.kind,
Origin::History,
Some(update.selection_before),
)?;
return Ok(None);
}
self.history.record(&update, now);
Ok(Some(update))
}
fn fix_up(&mut self, update: Update) -> Result<Update, EditorError> {
let mut update = update;
for extension in &self.registry.extensions {
if let Some(extra) = extension.after_update(&mut self.state, &update)? {
update = update.then(extra);
}
}
Ok(update)
}
}
fn has_node(schema: &Schema, node: &NodeMeta) -> bool {
match node.kind {
NodeKind::Block => schema.block_type(node.name).is_some(),
NodeKind::Mark => schema.mark_type(node.name).is_some(),
NodeKind::Embed => schema.embed_type(node.name).is_some(),
}
}
fn first_applying(commands: &[Command], context: &mut Context<'_>) -> CommandResult {
for command in commands {
if let Some(update) = command(context)? {
return Ok(Some(update));
}
}
Ok(None)
}