canvist_core 0.2.0

Platform-agnostic editing engine for canvist: editor state, commands, selection motion, keymaps, input rules, and history
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
//! Input rules (rewrites triggered while typing) and paste rules
//! (rewrites applied to pasted text).
//!
//! Patterns use [`regex_lite`]: input and paste rules are authored by
//! extensions as ordinary regular expressions, and `regex-lite` keeps the
//! WebAssembly bundle small (it carries no Unicode tables) at the cost of
//! speed that rules do not need. Input rules only run when the typed text
//! ends in whitespace or punctuation, so ordinary typing never pays for
//! them.

use std::ops::Range;
use std::sync::Arc;

use canvist_model::Attrs;
use canvist_model::BlockId;
use canvist_model::BlockTypeId;
use canvist_model::InlineText;
use canvist_model::Mark;
use canvist_model::MarkTypeId;
use canvist_model::Point;
use canvist_model::Schema;
use canvist_model::Selection;
use canvist_model::StepError;
use regex_lite::Regex;

use crate::CommandResult;
use crate::EditKind;
use crate::EditorError;
use crate::EditorState;
use crate::commands;
use crate::position::parent_of;
use crate::structure;

/// The most text before the caret an input rule sees, as in TipTap.
pub const RULE_CONTEXT: u32 = 500;

/// Applies an input rule that matched.
pub type RuleHandler = Arc<dyn Fn(&mut EditorState, &RuleMatch) -> CommandResult + Send + Sync>;

/// Where an input rule matched.
///
/// Offsets are byte offsets in the block, counting the typed text as if it
/// had been inserted at the caret. It never is: when a rule applies, the
/// rule decides what the keystroke produces.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RuleMatch {
	/// The text block holding the caret.
	pub block: BlockId,
	/// Where the match starts.
	pub start: u32,
	/// The caret, where the typed text would go.
	pub caret: u32,
	/// The text being typed.
	pub typed: String,
	/// The matched text, including the typed text.
	pub text: String,
	/// The capture groups' ranges; group 0 is the whole match.
	pub groups: Vec<Option<Range<u32>>>,
}

impl RuleMatch {
	/// The text of capture group `index`.
	#[must_use]
	pub fn group(&self, index: usize) -> Option<&str> {
		let range = self.groups.get(index)?.as_ref()?;
		let start = (range.start - self.start) as usize;
		let end = (range.end - self.start) as usize;
		self.text.get(start..end)
	}

	/// Where the match starts once leading whitespace (matched by patterns
	/// like `(?:^|\s)`) is skipped.
	#[must_use]
	pub fn content_start(&self) -> u32 {
		let leading = self.text.len() - self.text.trim_start().len();
		self.start + leading as u32
	}
}

/// What input rules match against: up to [`RULE_CONTEXT`] bytes of the
/// caret's block before the caret, followed by the typed text.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RuleInput {
	/// The text block holding the caret.
	pub block: BlockId,
	/// The block offset where `haystack` starts.
	pub window: u32,
	/// The caret offset.
	pub caret: u32,
	/// The typed text.
	pub typed: String,
	/// The text before the caret followed by the typed text.
	pub haystack: String,
}

impl RuleInput {
	/// The rule input for typing `typed` at `caret`, or `None` when no rule
	/// should run: outside text blocks, and for typed text that ends in a
	/// letter or digit.
	#[must_use]
	pub fn new(state: &EditorState, caret: Point, typed: &str) -> Option<Self> {
		let triggers = typed
			.chars()
			.next_back()
			.is_some_and(|last| !last.is_alphanumeric());
		let text = state.doc().inline(caret.block)?.as_str();

		if !triggers {
			return None;
		}

		let mut window = caret.offset.saturating_sub(RULE_CONTEXT) as usize;

		while !text.is_char_boundary(window) {
			window += 1;
		}

		let before = text.get(window..caret.offset as usize)?;

		Some(Self {
			block: caret.block,
			window: window as u32,
			caret: caret.offset,
			typed: typed.to_owned(),
			haystack: format!("{before}{typed}"),
		})
	}
}

/// Rewrites text as it is typed, e.g. `## ` into a heading.
#[derive(Clone)]
pub struct InputRule {
	pattern: Regex,
	trigger: Trigger,
	/// The typed characters the rule declared it runs on, which override
	/// the trigger read from its pattern.
	on: Option<&'static str>,
	/// Whether the pattern starts with `^`: it can only match from the
	/// start of the block.
	anchored: bool,
	/// Whether the pattern mentions `^` anywhere, as in `(?:^|\s)`: a
	/// shortened haystack must then start at a word boundary.
	uses_start: bool,
	lookback: Lookback,
	handler: RuleHandler,
}

/// How much text before the caret an unanchored rule searches. Regular
/// expressions cost time per byte, so a rule that runs on a keystroke must
/// look at a bounded amount of text whatever the paragraph's length.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Lookback {
	/// At most this many bytes, widened to the nearest word boundary.
	Bytes(u32),
	/// The word before the caret, for matches that never contain spaces
	/// (such as URLs), however long.
	Word,
}

/// The default [`Lookback`]: enough for Markdown emphasis around a phrase.
pub const DEFAULT_LOOKBACK: Lookback = Lookback::Bytes(64);

/// The typed characters that can complete a rule's match, read from the
/// end of its pattern. Skipping rules that cannot match keeps a keystroke
/// from scanning hundreds of bytes per rule.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Trigger {
	/// The pattern's end is too complex to read; always try it.
	Any,
	/// The pattern ends with `\s`.
	Whitespace,
	/// The pattern ends with this literal character.
	Char(char),
}

impl Trigger {
	/// Read the final atom of a `$`-anchored pattern.
	fn of(pattern: &str) -> Self {
		let body = pattern.strip_suffix('$').unwrap_or(pattern);

		let whitespace = body.ends_with(r"\s") && !body.ends_with(r"\\s");

		if whitespace || body.ends_with(r"[\s\n]") {
			return Self::Whitespace;
		}

		let mut tail = body.chars().rev();
		let (Some(last), before) = (tail.next(), tail.next()) else {
			return Self::Any;
		};
		let escaped = before == Some('\\');

		match (escaped, last) {
			(true, last) if last.is_ascii_punctuation() => Self::Char(last),

			(false, last) if !"\\.^$*+?()[]{}|".contains(last) => Self::Char(last),
			_ => Self::Any,
		}
	}

	fn accepts(self, typed: char) -> bool {
		match self {
			Self::Any => true,
			Self::Whitespace => typed.is_whitespace(),
			Self::Char(expected) => typed == expected,
		}
	}
}

impl std::fmt::Debug for InputRule {
	fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		formatter
			.debug_struct("InputRule")
			.field("pattern", &self.pattern.as_str())
			.finish_non_exhaustive()
	}
}

fn compile(pattern: &str) -> Result<Regex, EditorError> {
	Regex::new(pattern).map_err(|error| {
		EditorError::InvalidPattern {
			pattern: pattern.to_owned(),
			reason: error.to_string(),
		}
	})
}

impl InputRule {
	/// A rule that runs `handler` when `pattern` matches the text before the
	/// caret plus the typed text. The pattern must end with `$`.
	pub fn new(
		pattern: &str,
		handler: impl Fn(&mut EditorState, &RuleMatch) -> CommandResult + Send + Sync + 'static,
	) -> Result<Self, EditorError> {
		Self::with_handler(pattern, Arc::new(handler))
	}

	/// [`InputRule::new`] with the handler already boxed, so every rule
	/// shares one copy of the pattern checks in a WebAssembly build.
	fn with_handler(pattern: &str, handler: RuleHandler) -> Result<Self, EditorError> {
		if !pattern.ends_with('$') {
			return Err(EditorError::InvalidPattern {
				pattern: pattern.to_owned(),
				reason: "input rules must be anchored to the caret with `$`".to_owned(),
			});
		}

		Ok(Self {
			pattern: compile(pattern)?,
			trigger: Trigger::of(pattern),
			on: None,
			anchored: pattern.starts_with('^'),
			uses_start: pattern.contains('^'),
			lookback: DEFAULT_LOOKBACK,
			handler,
		})
	}

	/// Search a different amount of text before the caret. Rules anchored
	/// at the start of the block (`^...$`) ignore this.
	#[must_use]
	pub fn lookback(mut self, lookback: Lookback) -> Self {
		self.lookback = lookback;
		self
	}

	/// Only try the rule when the typed character is one of `chars`. A
	/// pattern ending in a group or class, such as `(")$`, cannot say this
	/// itself, so without it the rule runs on every punctuation keystroke.
	#[must_use]
	pub fn on(mut self, chars: &'static str) -> Self {
		self.on = Some(chars);
		self
	}

	/// Where in the haystack this rule's search starts, or `None` if the
	/// rule cannot match there at all.
	fn search_start(&self, input: &RuleInput) -> Option<usize> {
		if self.anchored {
			return (input.window == 0).then_some(0);
		}

		let before = &input.haystack[..input.haystack.len() - input.typed.len()];

		let mut cut = match self.lookback {
			Lookback::Word => before.rfind(char::is_whitespace).unwrap_or(0),
			Lookback::Bytes(bytes) => before.len().saturating_sub(bytes as usize),
		};

		// A byte count can land inside a multi-byte character.
		while !before.is_char_boundary(cut) {
			cut += 1;
		}

		let at_block_start = cut == 0 && input.window == 0;

		if at_block_start || !self.uses_start {
			return Some(cut);
		}

		// Start on a whitespace character so `^` cannot match mid-word.
		before[cut..]
			.find(char::is_whitespace)
			.map(|offset| cut + offset)
	}

	/// Replace the match (after any leading whitespace) with `replacement`,
	/// e.g. `->` into `→`.
	pub fn text(pattern: &str, replacement: &'static str) -> Result<Self, EditorError> {
		Self::new(pattern, move |state, found| {
			replace_text(state, found, replacement)
		})
	}

	/// Turn the text block into `kind`, removing the matched text, e.g.
	/// `^(#{1,6})\s$` into a heading whose level `attrs` reads from the match.
	pub fn block_type(
		pattern: &str,
		kind: BlockTypeId,
		attrs: impl Fn(&RuleMatch) -> Attrs + Send + Sync + 'static,
	) -> Result<Self, EditorError> {
		Self::new(pattern, move |state, found| {
			set_type(state, found, kind, attrs(found))
		})
	}

	/// Run `wrap` (for example wrapping the block in a list), then remove the
	/// matched text. If `wrap` does not apply, neither does the rule.
	pub fn wrap(
		pattern: &str,
		wrap: impl Fn(&mut EditorState, &RuleMatch) -> CommandResult + Send + Sync + 'static,
	) -> Result<Self, EditorError> {
		Self::new(pattern, move |state, found| {
			let Some(wrapped) = wrap(state, found)? else {
				return Ok(None);
			};
			let removed = remove_match(state, found)?;

			Ok(Some(match removed {
				Some(removed) => wrapped.then(removed),
				None => wrapped,
			}))
		})
	}

	/// Apply mark `kind` to capture group 1 and remove the delimiters around
	/// it, e.g. `**bold**`. Typing continues without the mark.
	pub fn mark(pattern: &str, kind: MarkTypeId) -> Result<Self, EditorError> {
		Self::new(pattern, move |state, found| apply_mark(state, found, kind))
	}

	/// Replace the matched text with an atomic block of `kind`, e.g. `---`
	/// into a horizontal rule.
	pub fn leaf(pattern: &str, kind: BlockTypeId) -> Result<Self, EditorError> {
		Self::new(pattern, move |state, found| insert_leaf(state, found, kind))
	}

	/// Match against the text before the caret plus the typed text.
	#[must_use]
	pub fn find(&self, input: &RuleInput) -> Option<RuleMatch> {
		let last = input.typed.chars().next_back()?;

		let triggered = match self.on {
			Some(chars) => chars.contains(last),
			None => self.trigger.accepts(last),
		};

		if !triggered {
			return None;
		}

		let start = self.search_start(input)?;
		let haystack = &input.haystack[start..];

		// Most rules do not match; answering that is cheaper than capturing.
		if !self.pattern.is_match(haystack) {
			return None;
		}

		let captures = self.pattern.captures(haystack)?;
		let whole = captures.get(0)?;
		let offset = |at: usize| input.window + (start + at) as u32;
		let groups = (0..captures.len())
			.map(|index| {
				captures
					.get(index)
					.map(|group| offset(group.start())..offset(group.end()))
			})
			.collect();

		Some(RuleMatch {
			block: input.block,
			start: offset(whole.start()),
			caret: input.caret,
			typed: input.typed.clone(),
			text: whole.as_str().to_owned(),
			groups,
		})
	}

	/// Run the rule's handler for a match.
	pub fn apply(&self, state: &mut EditorState, found: &RuleMatch) -> CommandResult {
		(self.handler)(state, found)
	}
}

fn caret_at(block: BlockId, offset: u32) -> Selection {
	Selection::caret(Point::new(block, offset))
}

fn replace_text(state: &mut EditorState, found: &RuleMatch, replacement: &str) -> CommandResult {
	let from = found.content_start().min(found.caret);
	let marks = commands::marks_at(state.doc(), Point::new(found.block, from));

	state.change(EditKind::InputRule, |tr| {
		tr.delete_text(found.block, from..found.caret)?;
		let end = tr.insert_text(Point::new(found.block, from), replacement, marks)?;

		tr.set_selection(Selection::caret(end));
		Ok(true)
	})
}

fn set_type(
	state: &mut EditorState,
	found: &RuleMatch,
	kind: BlockTypeId,
	attrs: Attrs,
) -> CommandResult {
	let doc = state.doc();

	if !structure::allows(doc, parent_of(doc, found.block), kind) {
		return Ok(None);
	}

	state.change(EditKind::InputRule, |tr| {
		tr.delete_text(found.block, found.start..found.caret)?;
		tr.set_block(found.block, kind, attrs)?;
		tr.set_selection(caret_at(found.block, found.start));
		Ok(true)
	})
}

fn remove_match(state: &mut EditorState, found: &RuleMatch) -> CommandResult {
	if found.start == found.caret {
		return Ok(None);
	}

	state.change(EditKind::InputRule, |tr| {
		tr.delete_text(found.block, found.start..found.caret)?;
		tr.set_selection(caret_at(found.block, found.start));
		Ok(true)
	})
}

fn apply_mark(state: &mut EditorState, found: &RuleMatch, kind: MarkTypeId) -> CommandResult {
	let Some(content) = found.groups.get(1).cloned().flatten() else {
		return Ok(None);
	};
	let open = found.content_start();

	if content.end > found.caret || content.is_empty() {
		return Ok(None);
	}

	state.change(EditKind::InputRule, |tr| {
		tr.delete_text(found.block, content.end..found.caret)?;
		tr.add_mark(found.block, content.clone(), Mark::new(kind))?;
		tr.delete_text(found.block, open..content.start)?;
		let end = Point::new(found.block, content.end - (content.start - open));
		let marks = commands::marks_at(tr.doc(), end).without(kind);

		tr.set_selection(Selection::caret(end));
		tr.set_stored_marks(Some(marks));
		Ok(true)
	})
}

fn insert_leaf(state: &mut EditorState, found: &RuleMatch, kind: BlockTypeId) -> CommandResult {
	let doc = state.doc();

	if !structure::allows(doc, parent_of(doc, found.block), kind) {
		return Ok(None);
	}

	let removed = remove_match(state, found)?;
	let inserted = commands::insert_leaf(state, kind, Attrs::new())?;

	Ok(match (removed, inserted) {
		(Some(removed), Some(inserted)) => Some(removed.then(inserted)),
		(removed, inserted) => removed.or(inserted),
	})
}

/// Computes a mark's attributes from the matched text, e.g. a link's href.
pub type MarkAttrs = Arc<dyn Fn(&str) -> Attrs + Send + Sync>;

/// Formats matching text in pasted content, e.g. URLs into links or
/// `**bold**` into bold text.
#[derive(Clone)]
pub struct PasteRule {
	pattern: Regex,
	kind: MarkTypeId,
	attrs: MarkAttrs,
}

impl std::fmt::Debug for PasteRule {
	fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		formatter
			.debug_struct("PasteRule")
			.field("pattern", &self.pattern.as_str())
			.field("kind", &self.kind)
			.finish_non_exhaustive()
	}
}

impl PasteRule {
	/// Apply mark `kind` to every match. With a capture group, only group 1
	/// is marked and the rest of the match (its delimiters) is removed.
	pub fn mark(pattern: &str, kind: MarkTypeId) -> Result<Self, EditorError> {
		Self::mark_with(pattern, kind, |_| Attrs::new())
	}

	/// Like [`PasteRule::mark`], with attributes computed from the marked
	/// text.
	pub fn mark_with(
		pattern: &str,
		kind: MarkTypeId,
		attrs: impl Fn(&str) -> Attrs + Send + Sync + 'static,
	) -> Result<Self, EditorError> {
		Ok(Self {
			pattern: compile(pattern)?,
			kind,
			attrs: Arc::new(attrs),
		})
	}

	/// Format every match in `text`.
	pub fn apply(&self, text: &mut InlineText, schema: &Schema) -> Result<(), EditorError> {
		let source = text.as_str().to_owned();
		let matches: Vec<(Range<u32>, Range<u32>)> = self
			.pattern
			.captures_iter(&source)
			.filter_map(|captures| {
				let whole = captures.get(0)?;
				let content = captures.get(1).unwrap_or(whole);
				let leading = whole.as_str().len() - whole.as_str().trim_start().len();
				let open = (whole.start() + leading) as u32;
				Some((
					open..whole.end() as u32,
					content.start() as u32..content.end() as u32,
				))
			})
			.collect();

		for (whole, content) in matches.into_iter().rev() {
			let marked = &source[content.start as usize..content.end as usize];
			let mark = Mark::with_attrs(self.kind, (self.attrs)(marked));
			text.add_mark(content.clone(), &mark, schema)
				.map_err(StepError::from)?;
			text.remove(content.end..whole.end.max(content.end))
				.map_err(StepError::from)?;
			text.remove(whole.start.min(content.start)..content.start)
				.map_err(StepError::from)?;
		}

		Ok(())
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn reads_triggers_from_the_end_of_patterns() {
		assert_eq!(Trigger::of(r"^(#{1,6})\s$"), Trigger::Whitespace);
		assert_eq!(Trigger::of(r"^```([a-z]+)?[\s\n]$"), Trigger::Whitespace);
		assert_eq!(Trigger::of(r"(?:^|\s)\*\*([^*]+)\*\*$"), Trigger::Char('*'));
		assert_eq!(Trigger::of(r"(?:^|\s)_([^_]+)_$"), Trigger::Char('_'));
		assert_eq!(Trigger::of(r"->$"), Trigger::Char('>'));
		assert_eq!(Trigger::of(r"^(?:---|___\s)$"), Trigger::Any);
		assert_eq!(Trigger::of(r"a.$"), Trigger::Any);
	}
}