Skip to main content

canvist_core/
base_keymap.rs

1//! The platform's standard text-editing keys.
2
3use std::sync::Arc;
4
5use canvist_model::Schema;
6
7use crate::Binding;
8use crate::Command;
9use crate::Context;
10use crate::Direction;
11use crate::EditorError;
12use crate::Extension;
13use crate::Platform;
14use crate::Unit;
15use crate::command;
16use crate::commands;
17use crate::motion;
18
19/// Arrow keys, Home and End, Backspace and Delete with their word and line
20/// variants, Enter, Select All, and Escape, bound the way each platform
21/// binds them. It has the lowest priority, so extensions override any key.
22#[derive(Clone, Copy, Debug, Default)]
23pub struct BaseKeymap;
24
25fn caret(direction: Direction, unit: Unit, extend: bool) -> Command {
26	Arc::new(move |cx: &mut Context<'_>| {
27		motion::move_caret(cx.state, direction, unit, extend, cx.lines)
28	})
29}
30
31fn vertical(direction: Direction, extend: bool) -> Command {
32	Arc::new(move |cx: &mut Context<'_>| {
33		motion::move_vertical(cx.state, direction, extend, cx.lines)
34	})
35}
36
37fn delete(direction: Direction, unit: Unit) -> Command {
38	Arc::new(move |cx: &mut Context<'_>| {
39		match direction {
40			Direction::Backward => commands::delete_backward(cx.state, unit, cx.lines),
41			Direction::Forward => commands::delete_forward(cx.state, unit, cx.lines),
42		}
43	})
44}
45
46/// Bindings for one motion `unit`: plain moves, Shift extends.
47fn motion_pair(bindings: &mut Vec<Binding>, keys: [&'static str; 4], unit: Unit) {
48	let [backward, forward, extend_backward, extend_forward] = keys;
49	bindings.push((backward, caret(Direction::Backward, unit, false)));
50	bindings.push((forward, caret(Direction::Forward, unit, false)));
51	bindings.push((extend_backward, caret(Direction::Backward, unit, true)));
52	bindings.push((extend_forward, caret(Direction::Forward, unit, true)));
53}
54
55fn common() -> Vec<Binding> {
56	let mut bindings = vec![
57		("Backspace", delete(Direction::Backward, Unit::Grapheme)),
58		(
59			"Shift-Backspace",
60			delete(Direction::Backward, Unit::Grapheme),
61		),
62		("Delete", delete(Direction::Forward, Unit::Grapheme)),
63		("Enter", command(commands::lift_empty_block)),
64		("Enter", command(commands::split_block)),
65		("Mod-a", command(commands::select_all)),
66		("Escape", command(commands::select_parent)),
67		("ArrowUp", vertical(Direction::Backward, false)),
68		("ArrowDown", vertical(Direction::Forward, false)),
69		("Shift-ArrowUp", vertical(Direction::Backward, true)),
70		("Shift-ArrowDown", vertical(Direction::Forward, true)),
71	];
72	let arrows = [
73		"ArrowLeft",
74		"ArrowRight",
75		"Shift-ArrowLeft",
76		"Shift-ArrowRight",
77	];
78	let home_end = ["Home", "End", "Shift-Home", "Shift-End"];
79	motion_pair(&mut bindings, arrows, Unit::Grapheme);
80	motion_pair(&mut bindings, home_end, Unit::Line);
81	bindings
82}
83
84fn apple() -> Vec<Binding> {
85	let mut bindings = vec![
86		("Alt-Backspace", delete(Direction::Backward, Unit::Word)),
87		("Alt-Delete", delete(Direction::Forward, Unit::Word)),
88		("Mod-Backspace", delete(Direction::Backward, Unit::Line)),
89		("Mod-Delete", delete(Direction::Forward, Unit::Line)),
90		("Ctrl-h", delete(Direction::Backward, Unit::Grapheme)),
91		("Ctrl-d", delete(Direction::Forward, Unit::Grapheme)),
92		("Ctrl-k", delete(Direction::Forward, Unit::Line)),
93		("Ctrl-a", caret(Direction::Backward, Unit::Line, false)),
94		("Ctrl-e", caret(Direction::Forward, Unit::Line, false)),
95	];
96	let words = [
97		"Alt-ArrowLeft",
98		"Alt-ArrowRight",
99		"Alt-Shift-ArrowLeft",
100		"Alt-Shift-ArrowRight",
101	];
102	let lines = [
103		"Mod-ArrowLeft",
104		"Mod-ArrowRight",
105		"Mod-Shift-ArrowLeft",
106		"Mod-Shift-ArrowRight",
107	];
108	let document = [
109		"Mod-ArrowUp",
110		"Mod-ArrowDown",
111		"Mod-Shift-ArrowUp",
112		"Mod-Shift-ArrowDown",
113	];
114	motion_pair(&mut bindings, words, Unit::Word);
115	motion_pair(&mut bindings, lines, Unit::Line);
116	motion_pair(&mut bindings, document, Unit::Document);
117	bindings
118}
119
120fn other() -> Vec<Binding> {
121	let mut bindings = vec![
122		("Ctrl-Backspace", delete(Direction::Backward, Unit::Word)),
123		("Ctrl-Delete", delete(Direction::Forward, Unit::Word)),
124	];
125	let words = [
126		"Ctrl-ArrowLeft",
127		"Ctrl-ArrowRight",
128		"Ctrl-Shift-ArrowLeft",
129		"Ctrl-Shift-ArrowRight",
130	];
131	let document = ["Ctrl-Home", "Ctrl-End", "Ctrl-Shift-Home", "Ctrl-Shift-End"];
132	motion_pair(&mut bindings, words, Unit::Word);
133	motion_pair(&mut bindings, document, Unit::Document);
134	bindings
135}
136
137impl Extension for BaseKeymap {
138	fn name(&self) -> &'static str {
139		"baseKeymap"
140	}
141
142	fn priority(&self) -> i32 {
143		0
144	}
145
146	fn keymap(&self, _schema: &Schema, platform: Platform) -> Result<Vec<Binding>, EditorError> {
147		let mut bindings = common();
148		bindings.extend(match platform {
149			Platform::Apple => apple(),
150			Platform::Other => other(),
151		});
152
153		Ok(bindings)
154	}
155}
156
157/// Backspace right after an input rule restores the text that triggered
158/// it, so `## ` then Backspace leaves a literal `## `. It runs before every
159/// other Backspace binding, such as joining list items, because the rule's
160/// effect is what the user is taking back.
161#[derive(Clone, Copy, Debug, Default)]
162pub struct UndoInputRule;
163
164impl Extension for UndoInputRule {
165	fn name(&self) -> &'static str {
166		"undoInputRule"
167	}
168
169	fn priority(&self) -> i32 {
170		i32::MAX
171	}
172
173	fn keymap(&self, _schema: &Schema, _platform: Platform) -> Result<Vec<Binding>, EditorError> {
174		Ok(vec![(
175			"Backspace",
176			Arc::new(crate::undo_input_rule) as Command,
177		)])
178	}
179}
180
181/// The standard keys add no commands of their own, so their handle is just
182/// the proof of registration.
183impl crate::TypedExtension for BaseKeymap {
184	type Handle = crate::Registration;
185
186	fn handle(&self, registration: crate::Registration) -> crate::Registration {
187		registration
188	}
189}
190
191impl crate::TypedExtension for UndoInputRule {
192	type Handle = crate::Registration;
193
194	fn handle(&self, registration: crate::Registration) -> crate::Registration {
195		registration
196	}
197}