use std::time::Duration;
use canvist_model::Bias;
use canvist_model::BlockId;
use canvist_model::Commit;
use canvist_model::Document;
use canvist_model::InlineText;
use canvist_model::Point;
use canvist_model::Selection;
use crate::CommandResult;
use crate::EditKind;
use crate::EditorState;
use crate::EditorTransaction;
use crate::commands;
use crate::commands::insert_at;
use crate::position;
use crate::structure;
const SETTLING_MEMORY: Duration = Duration::from_secs(2);
const MAX_SETTLING: usize = 8;
const CLOSERS: &[char] = &[
'.', ',', ';', ':', '!', '?', ')', ']', '}', '…', '%', '”', '’', '»',
];
const OPENERS: &[char] = &['(', '[', '{', '“', '‘', '«', '"', '\'', '/', '-'];
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Transcript<'a> {
Partial(&'a str),
Final(&'a str),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Settling {
pub block: BlockId,
pub start: u32,
pub end: u32,
pub since: Duration,
}
impl Settling {
fn map(self, commit: &Commit) -> Option<Self> {
let start = commit.map(Point::new(self.block, self.start), Bias::After);
let end = commit.map(Point::new(self.block, self.end), Bias::Before);
let apart = start.point.block != end.point.block || end.point.offset <= start.point.offset;
if start.deleted || end.deleted || apart {
return None;
}
Some(Self {
block: start.point.block,
start: start.point.offset,
end: end.point.offset,
since: self.since,
})
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
struct Segment {
number: u32,
text: String,
is_final: bool,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Dictation {
pending: Vec<Segment>,
floor: u32,
settling: Vec<Settling>,
}
impl Dictation {
#[must_use]
pub fn is_pending(&self) -> bool {
!self.pending.is_empty()
}
#[must_use]
pub fn settling(&self) -> &[Settling] {
&self.settling
}
#[must_use]
pub fn provisional(&self, doc: &Document, caret: Point) -> Option<String> {
let (mut before, after) = neighbours(doc, caret);
let mut text = String::new();
for segment in &self.pending {
let Some(first) = segment.text.chars().next() else {
continue;
};
if space_between(before, first) {
text.push(' ');
}
text.push_str(&segment.text);
before = segment.text.chars().next_back();
}
if let (Some(last), Some(after)) = (before, after)
&& !text.is_empty()
&& space_between(Some(last), after)
{
text.push(' ');
}
(!text.is_empty()).then_some(text)
}
pub(crate) fn record(&mut self, number: u32, transcript: Transcript<'_>) -> bool {
if number < self.floor {
return false;
}
let (text, is_final) = match transcript {
Transcript::Partial(text) => (text, false),
Transcript::Final(text) => (text, true),
};
let segment = Segment {
number,
text: normalize(text),
is_final,
};
match self
.pending
.binary_search_by_key(&number, |segment| segment.number)
{
Ok(index) if self.pending[index].is_final => false,
Ok(index) => {
let changed = self.pending[index] != segment;
self.pending[index] = segment;
changed
}
Err(index) => {
self.pending.insert(index, segment);
true
}
}
}
fn ready(&self) -> impl Iterator<Item = &str> {
self.pending
.iter()
.take_while(|segment| segment.is_final)
.map(|segment| segment.text.as_str())
}
fn inserted(&mut self, count: usize) {
let Some(last) = count.checked_sub(1).and_then(|last| self.pending.get(last)) else {
return;
};
self.floor = last.number.saturating_add(1);
self.pending.drain(..count);
}
fn settle(&mut self, ranges: impl IntoIterator<Item = (Point, Point)>, now: Duration) {
self.settling
.retain(|range| now.saturating_sub(range.since) < SETTLING_MEMORY);
self.settling.extend(ranges.into_iter().map(|(start, end)| {
Settling {
block: start.block,
start: start.offset,
end: end.offset,
since: now,
}
}));
let excess = self.settling.len().saturating_sub(MAX_SETTLING);
self.settling.drain(..excess);
}
pub(crate) fn stop(&mut self) -> bool {
let showing = self.is_pending();
self.pending.clear();
self.floor = 0;
showing
}
pub(crate) fn map(&mut self, commit: &Commit) {
if commit.is_empty() || self.settling.is_empty() {
return;
}
self.settling = self
.settling
.iter()
.filter_map(|range| range.map(commit))
.collect();
}
}
pub(crate) fn insert_ready(state: &mut EditorState, now: Duration) -> CommandResult {
let ready: Vec<String> = state.dictation().ready().map(str::to_owned).collect();
let replaces = state.dictation().is_pending() && !is_caret(state.selection());
if ready.is_empty() && !replaces {
return Ok(None);
}
let marks = commands::active_marks(state);
let mut inserted = Vec::with_capacity(ready.len());
let update = state.change(EditKind::Dictation, |tr| {
let Some(mut caret) = replace_selection(tr)? else {
return Ok(false);
};
for text in ready.iter().filter(|text| !text.is_empty()) {
let (start, end) = insert_segment(tr, caret, text, &marks)?;
inserted.push((start, end));
caret = end;
}
pad_after(tr, caret, &marks)?;
tr.set_selection(Selection::caret(caret));
Ok(true)
})?;
if update.is_some() {
let count = ready.len();
let dictation = state.dictation_mut();
dictation.inserted(count);
dictation.settle(inserted, now);
}
Ok(update)
}
fn is_caret(selection: Selection) -> bool {
selection.as_caret().is_some()
}
fn replace_selection(tr: &mut EditorTransaction<'_>) -> Result<Option<Point>, crate::EditorError> {
let Some(range) = position::text_range(tr.doc(), tr.selection_before()) else {
return Ok(None);
};
if range.is_empty() {
return Ok(Some(range.from));
}
structure::delete_range(tr, range.from, range.to).map(Some)
}
fn insert_segment(
tr: &mut EditorTransaction<'_>,
caret: Point,
text: &str,
marks: &canvist_model::MarkSet,
) -> Result<(Point, Point), crate::EditorError> {
let (before, _) = neighbours(tr.doc(), caret);
let spaced = text
.chars()
.next()
.is_some_and(|first| space_between(before, first));
let content = if spaced {
format!(" {text}")
} else {
text.to_owned()
};
let end = insert_at(tr, caret, InlineText::plain(&content, marks.clone()))?;
let start = Point::new(end.block, end.offset - text.len() as u32);
Ok((start, end))
}
fn pad_after(
tr: &mut EditorTransaction<'_>,
caret: Point,
marks: &canvist_model::MarkSet,
) -> Result<(), crate::EditorError> {
let (before, after) = neighbours(tr.doc(), caret);
if let Some(after) = after
&& before.is_some()
&& space_between(before, after)
&& after.is_alphanumeric()
{
insert_at(tr, caret, InlineText::plain(" ", marks.clone()))?;
}
Ok(())
}
fn neighbours(doc: &Document, caret: Point) -> (Option<char>, Option<char>) {
let Some(text) = doc.inline(caret.block).map(InlineText::as_str) else {
return (None, None);
};
let offset = (caret.offset as usize).min(text.len());
let (before, after) = text.split_at_checked(offset).unwrap_or((text, ""));
(before.chars().next_back(), after.chars().next())
}
fn space_between(before: Option<char>, next: char) -> bool {
let Some(before) = before else {
return false;
};
!(before.is_whitespace()
|| next.is_whitespace()
|| OPENERS.contains(&before)
|| CLOSERS.contains(&next))
}
fn normalize(text: &str) -> String {
let mut normal = String::with_capacity(text.len());
for word in text.split_whitespace() {
if !normal.is_empty() {
normal.push(' ');
}
normal.push_str(word);
}
normal
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn spaces_follow_words_but_not_openers_or_line_starts() {
assert!(space_between(Some('d'), 'n'));
assert!(!space_between(None, 'n'));
assert!(!space_between(Some(' '), 'n'));
assert!(!space_between(Some('('), 'n'));
assert!(!space_between(Some('d'), '.'));
assert!(!space_between(Some('d'), ','));
}
#[test]
fn normalizing_collapses_white_space_and_line_breaks() {
assert_eq!(normalize(" the\n\nmeeting \t is "), "the meeting is");
assert_eq!(normalize(" \n "), "");
}
#[test]
fn a_final_segment_never_changes_again() {
let mut dictation = Dictation::default();
assert!(dictation.record(0, Transcript::Final("Done.")));
assert!(!dictation.record(0, Transcript::Partial("done and dusted")));
assert_eq!(dictation.ready().collect::<Vec<_>>(), ["Done."]);
}
#[test]
fn segments_wait_for_every_earlier_one() {
let mut dictation = Dictation::default();
dictation.record(1, Transcript::Final("Second."));
dictation.record(0, Transcript::Partial("first"));
assert_eq!(dictation.ready().count(), 0);
dictation.record(0, Transcript::Final("First."));
assert_eq!(dictation.ready().collect::<Vec<_>>(), ["First.", "Second."]);
}
#[test]
fn news_of_inserted_segments_is_ignored() {
let mut dictation = Dictation::default();
dictation.record(0, Transcript::Final("One."));
dictation.inserted(1);
assert!(!dictation.record(0, Transcript::Partial("one again")));
assert!(!dictation.is_pending());
}
#[test]
fn settling_ranges_are_capped_and_expire() {
let mut dictation = Dictation::default();
let block = BlockId::new(1, 1);
let range = |offset| (Point::new(block, offset), Point::new(block, offset + 1));
dictation.settle((0..12).map(range), Duration::ZERO);
assert_eq!(dictation.settling().len(), MAX_SETTLING);
assert_eq!(dictation.settling()[0].start, 4);
dictation.settle([range(20)], SETTLING_MEMORY);
assert_eq!(dictation.settling().len(), 1);
}
}