Unnamed repository; edit this file 'description' to name the repository.
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

Crate Description
helix-core Core editing primitives, functional.
helix-syntax Tree-sitter grammars
helix-lsp Language server client
helix-view UI abstractions for use in backends, imperative shell.
helix-term Terminal UI
helix-tui TUI primitives, forked from tui-rs, inspired by Cursive

This document contains a high-level overview of Helix internals.

NOTE: Use cargo doc --open for API documentation as well as dependency documentation.

Core

The core contains basic building blocks used to construct the editor. It is heavily based on CodeMirror 6. The primitives are functional: most operations won't modify data in place but instead return a new copy.

The main data structure used for representing buffers is a Rope. We re-export the excellent ropey library. Ropes are cheap to clone, and allow us to easily make snapshots of a text state.

Multiple selections are a core editing primitive. Document selections are represented by a Selection. Each Range in the selection consists of a moving head and an immovable anchor. A single cursor in the editor is simply a selection with a single range, with the head and the anchor in the same position.

Ropes are modified by constructing an OT-like Transaction. It's represents a single coherent change to the document and can be applied to the rope. A transaction can be inverted to produce an undo. Selections and marks can be mapped over a transaction to translate to a position in the new text state after applying the transaction.

NOTE: Transaction::change/Transaction::change_by_selection is the main interface used to generate text edits.

Syntax is the interface used to interact with tree-sitter ASTs for syntax highling and other features.

View

The view layer was supposed to be a frontend-agnostic imperative library that would build on top of core to provide the common editor logic. Currently it's tied to the terminal UI.

A Document ties together the Rope, Selection(s), Syntax, document History, language server (etc.) into a comprehensive representation of an open file.

A View represents an open split in the UI. It holds the currently open document ID and other related state.

NOTE: Multiple views are able to display the same document, so the document contains selections for each view. To retrieve, document.selection() takes a ViewId.

The Editor holds the global state: all the open documents, a tree representation of all the view splits, and a registry of language servers. To open or close files, interact with the editor.

LSP

A language server protocol client.

Term

The terminal frontend.

The main function sets up a new Application that runs the event loop.

commands.rs is probably the most interesting file. It contains all commands (actions tied to keybindings).

keymap.rs links commands to key combinations.

TUI / Term

TODO: document Component and rendering related stuff

nstraint::Ratio(2, 3)].as_ref()) /// .split(Rect { /// x: 0, /// y: 0, /// width: 9, /// height: 2, /// }); /// assert_eq!( /// chunks, /// vec![ /// Rect { /// x: 0, /// y: 0, /// width: 3, /// height: 2 /// }, /// Rect { /// x: 3, /// y: 0, /// width: 6, /// height: 2 /// } /// ] /// ); /// ``` pub fn split(&self, area: Rect) -> Vec<Rect> { // TODO: Maybe use a fixed size cache ? LAYOUT_CACHE.with(|c| { c.borrow_mut() .entry((area, self.clone())) .or_insert_with(|| split(area, self)) .clone() }) } } fn split(area: Rect, layout: &Layout) -> Vec<Rect> { let mut solver = Solver::new(); let mut vars: HashMap<Variable, (usize, usize)> = HashMap::new(); let elements = layout .constraints .iter() .map(|_| Element::new()) .collect::<Vec<Element>>(); let mut results = layout .constraints .iter() .map(|_| Rect::default()) .collect::<Vec<Rect>>(); let dest_area = area.inner(&layout.margin); for (i, e) in elements.iter().enumerate() { vars.insert(e.x, (i, 0)); vars.insert(e.y, (i, 1)); vars.insert(e.width, (i, 2)); vars.insert(e.height, (i, 3)); } let mut ccs: Vec<CassowaryConstraint> = Vec::with_capacity(elements.len() * 4 + layout.constraints.len() * 6); for elt in &elements { ccs.push(elt.width | GE(REQUIRED) | 0f64); ccs.push(elt.height | GE(REQUIRED) | 0f64); ccs.push(elt.left() | GE(REQUIRED) | f64::from(dest_area.left())); ccs.push(elt.top() | GE(REQUIRED) | f64::from(dest_area.top())); ccs.push(elt.right() | LE(REQUIRED) | f64::from(dest_area.right())); ccs.push(elt.bottom() | LE(REQUIRED) | f64::from(dest_area.bottom())); } if let Some(first) = elements.first() { ccs.push(match layout.direction { Direction::Horizontal => first.left() | EQ(REQUIRED) | f64::from(dest_area.left()), Direction::Vertical => first.top() | EQ(REQUIRED) | f64::from(dest_area.top()), }); } if let Some(last) = elements.last() { ccs.push(match layout.direction { Direction::Horizontal => last.right() | EQ(REQUIRED) | f64::from(dest_area.right()), Direction::Vertical => last.bottom() | EQ(REQUIRED) | f64::from(dest_area.bottom()), }); } match layout.direction { Direction::Horizontal => { for pair in elements.windows(2) { ccs.push((pair[0].x + pair[0].width) | EQ(REQUIRED) | pair[1].x); } for (i, size) in layout.constraints.iter().enumerate() { ccs.push(elements[i].y | EQ(REQUIRED) | f64::from(dest_area.y)); ccs.push(elements[i].height | EQ(REQUIRED) | f64::from(dest_area.height)); ccs.push(match *size { Constraint::Length(v) => elements[i].width | EQ(WEAK) | f64::from(v), Constraint::Percentage(v) => { elements[i].width | EQ(WEAK) | (f64::from(v * dest_area.width) / 100.0) } Constraint::Ratio(n, d) => { elements[i].width | EQ(WEAK) | (f64::from(dest_area.width) * f64::from(n) / f64::from(d)) } Constraint::Min(v) => elements[i].width | GE(WEAK) | f64::from(v), Constraint::Max(v) => elements[i].width | LE(WEAK) | f64::from(v), }); } } Direction::Vertical => { for pair in elements.windows(2) { ccs.push((pair[0].y + pair[0].height) | EQ(REQUIRED) | pair[1].y); } for (i, size) in layout.constraints.iter().enumerate() { ccs.push(elements[i].x | EQ(REQUIRED) | f64::from(dest_area.x)); ccs.push(elements[i].width | EQ(REQUIRED) | f64::from(dest_area.width)); ccs.push(match *size { Constraint::Length(v) => elements[i].height | EQ(WEAK) | f64::from(v), Constraint::Percentage(v) => { elements[i].height | EQ(WEAK) | (f64::from(v * dest_area.height) / 100.0) } Constraint::Ratio(n, d) => { elements[i].height | EQ(WEAK) | (f64::from(dest_area.height) * f64::from(n) / f64::from(d)) } Constraint::Min(v) => elements[i].height | GE(WEAK) | f64::from(v), Constraint::Max(v) => elements[i].height | LE(WEAK) | f64::from(v), }); } } } solver.add_constraints(&ccs).unwrap(); for &(var, value) in solver.fetch_changes() { let (index, attr) = vars[&var]; let value = if value.is_sign_negative() { 0 } else { value as u16 }; match attr { 0 => { results[index].x = value; } 1 => { results[index].y = value; } 2 => { results[index].width = value; } 3 => { results[index].height = value; } _ => {} } } // Fix imprecision by extending the last item a bit if necessary if let Some(last) = results.last_mut() { match layout.direction { Direction::Vertical => { last.height = dest_area.bottom() - last.y; } Direction::Horizontal => { last.width = dest_area.right() - last.x; } } } results } /// A container used by the solver inside split struct Element { x: Variable, y: Variable, width: Variable, height: Variable, } impl Element { fn new() -> Element { Element { x: Variable::new(), y: Variable::new(), width: Variable::new(), height: Variable::new(), } } fn left(&self) -> Variable { self.x } fn top(&self) -> Variable { self.y } fn right(&self) -> Expression { self.x + self.width } fn bottom(&self) -> Expression { self.y + self.height } } #[cfg(test)] mod tests { use super::*; #[test] fn test_vertical_split_by_height() { let target = Rect { x: 2, y: 2, width: 10, height: 10, }; let chunks = Layout::default() .direction(Direction::Vertical) .constraints( [ Constraint::Percentage(10), Constraint::Max(5), Constraint::Min(1), ] .as_ref(), ) .split(target); assert_eq!(target.height, chunks.iter().map(|r| r.height).sum::<u16>()); chunks.windows(2).for_each(|w| assert!(w[0].y <= w[1].y)); } }