@@ -3,52 +3,65 @@ use helix_core::{
3 indent::TAB_WIDTH,
3 indent::TAB_WIDTH,
4 regex::Regex,
4 regex::Regex,
5 register, selection,
5 register, selection,
6− state::{Direction, Granularity, Mode, State},
6+ state::{Direction, Granularity, State},
7 ChangeSet, Range, Selection, Tendril, Transaction,
7 ChangeSet, Range, Selection, Tendril, Transaction,
9use once_cell::sync::Lazy;
9use once_cell::sync::Lazy;
11use crate::{
11use crate::{
12 prompt::Prompt,
13 prompt::Prompt,
13 view::{View, PADDING},
14 view::{View, PADDING},
17+pub struct Context<'a, 'b> {
19+ pub view: &'a mut View,
20+ pub executor: &'a smol::Executor<'b>,
16/// A command is a function that takes the current state and a count, and does a side-effect on the
23/// A command is a function that takes the current state and a count, and does a side-effect on the
17/// state (usually by creating and applying a transaction).
24/// state (usually by creating and applying a transaction).
18−pub type Command = fn(view: &mut View, count: usize);
25+pub type Command = fn(cx: &mut Context);
20−pub fn move_char_left(view: &mut View, count: usize) {
27+pub fn move_char_left(cx: &mut Context) {
21− // TODO: use a transaction
22− let selection = view
28+ let selection =
24− .move_selection(Direction::Backward, Granularity::Character, count);
32+ .move_selection(Direction::Backward, Granularity::Character, cx.count);
25− view.state.selection = selection;
33+ cx.view.doc.set_selection(selection);
28−pub fn move_char_right(view: &mut View, count: usize) {
36+pub fn move_char_right(cx: &mut Context) {
29− // TODO: use a transaction
30− view.state.selection =
37+ let selection =
32− .move_selection(Direction::Forward, Granularity::Character, count);
41+ .move_selection(Direction::Forward, Granularity::Character, cx.count);
42+ cx.view.doc.set_selection(selection);
35−pub fn move_line_up(view: &mut View, count: usize) {
45+pub fn move_line_up(cx: &mut Context) {
36− // TODO: use a transaction
37− view.state.selection = view
46+ let selection =
39− .move_selection(Direction::Backward, Granularity::Line, count);
50+ .move_selection(Direction::Backward, Granularity::Line, cx.count);
51+ cx.view.doc.set_selection(selection);
42−pub fn move_line_down(view: &mut View, count: usize) {
54+pub fn move_line_down(cx: &mut Context) {
43− // TODO: use a transaction
44− view.state.selection = view
55+ let selection =
46− .move_selection(Direction::Forward, Granularity::Line, count);
59+ .move_selection(Direction::Forward, Granularity::Line, cx.count);
60+ cx.view.doc.set_selection(selection);
49−pub fn move_line_end(view: &mut View, _count: usize) {
63+pub fn move_line_end(cx: &mut Context) {
50− // TODO: use a transaction
51− let lines = selection_lines(&view.state);
64+ let lines = selection_lines(&cx.view.doc.state);
53 let positions = lines
66 let positions = lines
54 .into_iter()
67 .into_iter()
@@ -57,89 +70,80 @@ pub fn move_line_end(view: &mut View, _count: usize) {
58 // Line end is pos at the start of next line - 1
71 // Line end is pos at the start of next line - 1
59 // subtract another 1 because the line ends with \n
72 // subtract another 1 because the line ends with \n
60− view.state.doc.line_to_char(index + 1).saturating_sub(2)
73+ cx.view.doc.text().line_to_char(index + 1).saturating_sub(2)
62 .map(|pos| Range::new(pos, pos));
75 .map(|pos| Range::new(pos, pos));
64 let selection = Selection::new(positions.collect(), 0);
77 let selection = Selection::new(positions.collect(), 0);
66− let transaction = Transaction::new(&mut view.state).with_selection(selection);
79+ cx.view.doc.set_selection(selection);
68− transaction.apply(&mut view.state);
71−pub fn move_line_start(view: &mut View, _count: usize) {
82+pub fn move_line_start(cx: &mut Context) {
72− let lines = selection_lines(&view.state);
83+ let lines = selection_lines(&cx.view.doc.state);
74 let positions = lines
85 let positions = lines
75 .into_iter()
86 .into_iter()
76 .map(|index| {
87 .map(|index| {
77 // adjust all positions to the start of the line.
88 // adjust all positions to the start of the line.
78− view.state.doc.line_to_char(index)
89+ cx.view.doc.text().line_to_char(index)
80 .map(|pos| Range::new(pos, pos));
91 .map(|pos| Range::new(pos, pos));
82 let selection = Selection::new(positions.collect(), 0);
93 let selection = Selection::new(positions.collect(), 0);
84− let transaction = Transaction::new(&mut view.state).with_selection(selection);
95+ cx.view.doc.set_selection(selection);
86− transaction.apply(&mut view.state);
89−pub fn move_next_word_start(view: &mut View, count: usize) {
98+pub fn move_next_word_start(cx: &mut Context) {
90− let pos = view.state.move_pos(
99+ let pos = cx.view.doc.state.move_pos(
91− view.state.selection.cursor(),
100+ cx.view.doc.selection().cursor(),
92 Direction::Forward,
101 Direction::Forward,
93 Granularity::Word,
102 Granularity::Word,
97− // TODO: use a transaction
98− view.state.selection = Selection::single(pos, pos);
106+ cx.view.doc.set_selection(Selection::point(pos));
101−pub fn move_prev_word_start(view: &mut View, count: usize) {
109+pub fn move_prev_word_start(cx: &mut Context) {
102− let pos = view.state.move_pos(
110+ let pos = cx.view.doc.state.move_pos(
103− view.state.selection.cursor(),
111+ cx.view.doc.selection().cursor(),
104 Direction::Backward,
112 Direction::Backward,
105 Granularity::Word,
113 Granularity::Word,
106− count,
114+ cx.count,
109− // TODO: use a transaction
110− view.state.selection = Selection::single(pos, pos);
117+ cx.view.doc.set_selection(Selection::point(pos));
113−pub fn move_next_word_end(view: &mut View, count: usize) {
120+pub fn move_next_word_end(cx: &mut Context) {
114 let pos = State::move_next_word_end(
121 let pos = State::move_next_word_end(
115− &view.state.doc().slice(..),
122+ &cx.view.doc.text().slice(..),
116− view.state.selection.cursor(),
123+ cx.view.doc.selection().cursor(),
117− count,
124+ cx.count,
120− // TODO: use a transaction
121− view.state.selection = Selection::single(pos, pos);
127+ cx.view.doc.set_selection(Selection::point(pos));
124−pub fn move_file_start(view: &mut View, _count: usize) {
130+pub fn move_file_start(cx: &mut Context) {
125− // TODO: use a transaction
131+ cx.view.doc.set_selection(Selection::point(0));
126− view.state.selection = Selection::single(0, 0);
128− view.state.mode = Mode::Normal;
133+ cx.view.doc.mode = Mode::Normal;
131−pub fn move_file_end(view: &mut View, _count: usize) {
136+pub fn move_file_end(cx: &mut Context) {
132− // TODO: use a transaction
133− let text = &view.state.doc;
137+ let text = &cx.view.doc.text();
134 let last_line = text.line_to_char(text.len_lines().saturating_sub(2));
138 let last_line = text.line_to_char(text.len_lines().saturating_sub(2));
135− view.state.selection = Selection::single(last_line, last_line);
139+ cx.view.doc.set_selection(Selection::point(last_line));
137− view.state.mode = Mode::Normal;
141+ cx.view.doc.mode = Mode::Normal;
140−pub fn check_cursor_in_view(view: &mut View) -> bool {
144+pub fn check_cursor_in_view(view: &View) -> bool {
141− let cursor = view.state.selection().cursor();
145+ let cursor = view.doc.selection().cursor();
142− let line = view.state.doc().char_to_line(cursor);
146+ let line = view.doc.text().char_to_line(cursor);
143 let document_end = view.first_line + view.size.1.saturating_sub(1) as usize;
147 let document_end = view.first_line + view.size.1.saturating_sub(1) as usize;
145 if (line > document_end.saturating_sub(PADDING)) | (line < view.first_line + PADDING) {
149 if (line > document_end.saturating_sub(PADDING)) | (line < view.first_line + PADDING) {
@@ -148,168 +152,186 @@ pub fn check_cursor_in_view(view: &mut View) -> bool {
151−pub fn page_up(view: &mut View, _count: usize) {
155+pub fn page_up(cx: &mut Context) {
152− if view.first_line < PADDING {
156+ if cx.view.first_line < PADDING {
156− view.first_line = view.first_line.saturating_sub(view.size.1 as usize);
160+ cx.view.first_line = cx.view.first_line.saturating_sub(cx.view.size.1 as usize);
158− if !check_cursor_in_view(view) {
162+ if !check_cursor_in_view(cx.view) {
159− let text = view.state.doc();
163+ let text = cx.view.doc.text();
160− let pos = text.line_to_char(view.last_line().saturating_sub(PADDING));
164+ let pos = text.line_to_char(cx.view.last_line().saturating_sub(PADDING));
161− view.state.selection = Selection::single(pos, pos);
165+ cx.view.doc.set_selection(Selection::point(pos));
165−pub fn page_down(view: &mut View, _count: usize) {
169+pub fn page_down(cx: &mut Context) {
166− view.first_line += view.size.1 as usize + PADDING;
170+ cx.view.first_line += cx.view.size.1 as usize + PADDING;
168− if view.first_line < view.state.doc().len_lines() {
172+ if cx.view.first_line < cx.view.doc.text().len_lines() {
169− let text = view.state.doc();
173+ let text = cx.view.doc.text();
170− let pos = text.line_to_char(view.first_line as usize);
174+ let pos = text.line_to_char(cx.view.first_line as usize);
171− view.state.selection = Selection::single(pos, pos);
175+ cx.view.doc.set_selection(Selection::point(pos));
175−pub fn half_page_up(view: &mut View, _count: usize) {
179+pub fn half_page_up(cx: &mut Context) {
176− if view.first_line < PADDING {
180+ if cx.view.first_line < PADDING {
184+ cx.view.first_line = cx
180− view.first_line = view.first_line.saturating_sub(view.size.1 as usize / 2);
187+ .saturating_sub(cx.view.size.1 as usize / 2);
182− if !check_cursor_in_view(view) {
189+ if !check_cursor_in_view(cx.view) {
183− let text = &view.state.doc;
190+ let text = &cx.view.doc.text();
184− let pos = text.line_to_char(view.last_line() - PADDING);
191+ let pos = text.line_to_char(cx.view.last_line() - PADDING);
185− view.state.selection = Selection::single(pos, pos);
192+ cx.view.doc.set_selection(Selection::point(pos));
189−pub fn half_page_down(view: &mut View, _count: usize) {
196+pub fn half_page_down(cx: &mut Context) {
190− let lines = view.state.doc().len_lines();
197+ let lines = cx.view.doc.text().len_lines();
191− if view.first_line < lines.saturating_sub(view.size.1 as usize) {
198+ if cx.view.first_line < lines.saturating_sub(cx.view.size.1 as usize) {
192− view.first_line += view.size.1 as usize / 2;
199+ cx.view.first_line += cx.view.size.1 as usize / 2;
194− if !check_cursor_in_view(view) {
201+ if !check_cursor_in_view(cx.view) {
195− let text = view.state.doc();
202+ let text = cx.view.doc.text();
196− let pos = text.line_to_char(view.first_line as usize);
203+ let pos = text.line_to_char(cx.view.first_line as usize);
197− view.state.selection = Selection::single(pos, pos);
204+ cx.view.doc.set_selection(Selection::point(pos));
200// avoid select by default by having a visual mode switch that makes movements into selects
207// avoid select by default by having a visual mode switch that makes movements into selects
202−pub fn extend_char_left(view: &mut View, count: usize) {
209+pub fn extend_char_left(cx: &mut Context) {
203− // TODO: use a transaction
204− let selection = view
210+ let selection =
206− .extend_selection(Direction::Backward, Granularity::Character, count);
214+ .extend_selection(Direction::Backward, Granularity::Character, cx.count);
207− view.state.selection = selection;
215+ cx.view.doc.set_selection(selection);
210−pub fn extend_char_right(view: &mut View, count: usize) {
218+pub fn extend_char_right(cx: &mut Context) {
211− // TODO: use a transaction
212− view.state.selection =
219+ let selection =
213− view.state
222+ .state
214− .extend_selection(Direction::Forward, Granularity::Character, count);
223+ .extend_selection(Direction::Forward, Granularity::Character, cx.count);
224+ cx.view.doc.set_selection(selection);
217−pub fn extend_line_up(view: &mut View, count: usize) {
227+pub fn extend_line_up(cx: &mut Context) {
218− // TODO: use a transaction
219− view.state.selection =
228+ let selection =
220− view.state
231+ .state
221− .extend_selection(Direction::Backward, Granularity::Line, count);
232+ .extend_selection(Direction::Backward, Granularity::Line, cx.count);
233+ cx.view.doc.set_selection(selection);
224−pub fn extend_line_down(view: &mut View, count: usize) {
236+pub fn extend_line_down(cx: &mut Context) {
225− // TODO: use a transaction
226− view.state.selection =
237+ let selection =
227− view.state
240+ .state
228− .extend_selection(Direction::Forward, Granularity::Line, count);
241+ .extend_selection(Direction::Forward, Granularity::Line, cx.count);
242+ cx.view.doc.set_selection(selection);
231−pub fn split_selection_on_newline(view: &mut View, _count: usize) {
245+pub fn split_selection_on_newline(cx: &mut Context) {
232− let text = &view.state.doc.slice(..);
246+ let text = &cx.view.doc.text().slice(..);
233 // only compile the regex once
247 // only compile the regex once
234 #[allow(clippy::trivial_regex)]
248 #[allow(clippy::trivial_regex)]
235 static REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\n").unwrap());
249 static REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\n").unwrap());
236− // TODO: use a transaction
237− view.state.selection = selection::split_on_matches(text, view.state.selection(), ®EX)
250+ let selection = selection::split_on_matches(text, cx.view.doc.selection(), ®EX);
251+ cx.view.doc.set_selection(selection);
240−pub fn select_line(view: &mut View, _count: usize) {
254+pub fn select_line(cx: &mut Context) {
241 // TODO: count
255 // TODO: count
242− let pos = view.state.selection().primary();
256+ let pos = cx.view.doc.selection().primary();
243− let text = view.state.doc();
257+ let text = cx.view.doc.text();
244 let line = text.char_to_line(pos.head);
258 let line = text.char_to_line(pos.head);
245 let start = text.line_to_char(line);
259 let start = text.line_to_char(line);
246 let end = text.line_to_char(line + 1).saturating_sub(1);
260 let end = text.line_to_char(line + 1).saturating_sub(1);
248− // TODO: use a transaction
249− view.state.selection = Selection::single(start, end);
262+ cx.view.doc.set_selection(Selection::single(start, end));
252−pub fn delete_selection(view: &mut View, _count: usize) {
265+pub fn delete_selection(cx: &mut Context) {
254− Transaction::change_by_selection(&view.state, |range| (range.from(), range.to() + 1, None));
266+ let transaction = Transaction::change_by_selection(&cx.view.doc.state, |range| {
255− transaction.apply(&mut view.state);
267+ (range.from(), range.to() + 1, None)
269+ cx.view.doc.apply(&transaction);
257− append_changes_to_history(view);
271+ append_changes_to_history(cx);
260−pub fn change_selection(view: &mut View, count: usize) {
274+pub fn change_selection(cx: &mut Context) {
261− delete_selection(view, count);
275+ delete_selection(cx);
262− insert_mode(view, count);
276+ insert_mode(cx);
265−pub fn collapse_selection(view: &mut View, _count: usize) {
279+pub fn collapse_selection(cx: &mut Context) {
266− view.state.selection = view
280+ let selection = cx
268− .selection
283+ .selection()
269− .transform(|range| Range::new(range.head, range.head))
284+ .transform(|range| Range::new(range.head, range.head));
286+ cx.view.doc.set_selection(selection);
272−pub fn flip_selections(view: &mut View, _count: usize) {
289+pub fn flip_selections(cx: &mut Context) {
273− view.state.selection = view
290+ let selection = cx
275− .selection
293+ .selection()
276− .transform(|range| Range::new(range.head, range.anchor))
294+ .transform(|range| Range::new(range.head, range.anchor));
296+ cx.view.doc.set_selection(selection);
279−fn enter_insert_mode(view: &mut View) {
299+fn enter_insert_mode(cx: &mut Context) {
280− view.state.mode = Mode::Insert;
300+ cx.view.doc.mode = Mode::Insert;
282− append_changes_to_history(view);
302+ append_changes_to_history(cx);
284// inserts at the start of each selection
304// inserts at the start of each selection
285−pub fn insert_mode(view: &mut View, _count: usize) {
305+pub fn insert_mode(cx: &mut Context) {
286− enter_insert_mode(view);
306+ enter_insert_mode(cx);
288− view.state.selection = view
308+ let selection = cx
290− .selection
311+ .selection()
291− .transform(|range| Range::new(range.to(), range.from()))
312+ .transform(|range| Range::new(range.to(), range.from()));
313+ cx.view.doc.set_selection(selection);
294// inserts at the end of each selection
316// inserts at the end of each selection
295−pub fn append_mode(view: &mut View, _count: usize) {
317+pub fn append_mode(cx: &mut Context) {
296− enter_insert_mode(view);
318+ enter_insert_mode(cx);
297− view.state.restore_cursor = true;
319+ cx.view.doc.restore_cursor = true;
299 // TODO: as transaction
321 // TODO: as transaction
300− let text = &view.state.doc.slice(..);
322+ let text = &cx.view.doc.text().slice(..);
301− view.state.selection = view.state.selection.transform(|range| {
323+ let selection = cx.view.doc.selection().transform(|range| {
302 // TODO: to() + next char
324 // TODO: to() + next char
303 Range::new(
325 Range::new(
304 range.from(),
326 range.from(),
305 graphemes::next_grapheme_boundary(text, range.to()),
327 graphemes::next_grapheme_boundary(text, range.to()),
330+ cx.view.doc.set_selection(selection);
310// TODO: I, A, o and O can share a lot of the primitives.
333// TODO: I, A, o and O can share a lot of the primitives.
312−pub fn command_mode(_view: &mut View, _count: usize) {
334+pub fn command_mode(_cx: &mut Context) {
313 unimplemented!()
335 unimplemented!()
@@ -329,30 +351,30 @@ fn selection_lines(state: &State) -> Vec<usize> {
331// I inserts at the start of each line with a selection
353// I inserts at the start of each line with a selection
332−pub fn prepend_to_line(view: &mut View, count: usize) {
354+pub fn prepend_to_line(cx: &mut Context) {
333− enter_insert_mode(view);
355+ enter_insert_mode(cx);
335− move_line_start(view, count);
357+ move_line_start(cx);
338// A inserts at the end of each line with a selection
360// A inserts at the end of each line with a selection
339−pub fn append_to_line(view: &mut View, count: usize) {
361+pub fn append_to_line(cx: &mut Context) {
340− enter_insert_mode(view);
362+ enter_insert_mode(cx);
342− move_line_end(view, count);
364+ move_line_end(cx);
345// o inserts a new line after each line with a selection
367// o inserts a new line after each line with a selection
346−pub fn open_below(view: &mut View, _count: usize) {
368+pub fn open_below(cx: &mut Context) {
347− enter_insert_mode(view);
369+ enter_insert_mode(cx);
349− let lines = selection_lines(&view.state);
371+ let lines = selection_lines(&cx.view.doc.state);
351 let positions: Vec<_> = lines
373 let positions: Vec<_> = lines
352 .into_iter()
374 .into_iter()
353 .map(|index| {
375 .map(|index| {
354 // adjust all positions to the end of the line/start of the next one.
376 // adjust all positions to the end of the line/start of the next one.
355− view.state.doc.line_to_char(index + 1)
377+ cx.view.doc.text().line_to_char(index + 1)
357 .collect();
379 .collect();
@@ -373,111 +395,119 @@ pub fn open_below(view: &mut View, _count: usize) {
376− let transaction = Transaction::change(&view.state, changes).with_selection(selection);
398+ let transaction = Transaction::change(&cx.view.doc.state, changes).with_selection(selection);
378− transaction.apply(&mut view.state);
400+ cx.view.doc.apply(&transaction);
381// O inserts a new line before each line with a selection
403// O inserts a new line before each line with a selection
383−fn append_changes_to_history(view: &mut View) {
405+fn append_changes_to_history(cx: &mut Context) {
384− if view.state.changes.is_empty() {
406+ if cx.view.doc.changes.is_empty() {
388− let new_changeset = ChangeSet::new(view.state.doc());
410+ let new_changeset = ChangeSet::new(cx.view.doc.text());
389− let changes = std::mem::replace(&mut view.state.changes, new_changeset);
411+ let changes = std::mem::replace(&mut cx.view.doc.changes, new_changeset);
390 // Instead of doing this messy merge we could always commit, and based on transaction
412 // Instead of doing this messy merge we could always commit, and based on transaction
391 // annotations either add a new layer or compose into the previous one.
413 // annotations either add a new layer or compose into the previous one.
392− let transaction = Transaction::from(changes).with_selection(view.state.selection().clone());
414+ let transaction = Transaction::from(changes).with_selection(cx.view.doc.selection().clone());
394− // HAXX: we need to reconstruct the state as it was before the changes..
416+ // increment document version
395− let (doc, selection) = view.state.old_state.take().unwrap();
417+ // TODO: needs to happen on undo/redo too
396− let mut old_state = State::new(doc);
418+ cx.view.doc.version += 1;
397− old_state.selection = selection;
420+ // TODO: trigger lsp/documentDidChange with changes
422+ // HAXX: we need to reconstruct the state as it was before the changes..
423+ let old_state = std::mem::replace(&mut cx.view.doc.old_state, cx.view.doc.state.clone());
399 // TODO: take transaction by value?
424 // TODO: take transaction by value?
400− view.history.commit_revision(&transaction, &old_state);
428+ .commit_revision(&transaction, &old_state);
402− // TODO: need to start the state with these vals
430+ // TODO: notify LSP of changes
404− view.state.old_state = Some((view.state.doc().clone(), view.state.selection.clone()));
407−pub fn normal_mode(view: &mut View, _count: usize) {
433+pub fn normal_mode(cx: &mut Context) {
408− view.state.mode = Mode::Normal;
434+ cx.view.doc.mode = Mode::Normal;
410− append_changes_to_history(view);
436+ append_changes_to_history(cx);
412 // if leaving append mode, move cursor back by 1
438 // if leaving append mode, move cursor back by 1
413− if view.state.restore_cursor {
439+ if cx.view.doc.restore_cursor {
414− let text = &view.state.doc.slice(..);
440+ let text = &cx.view.doc.text().slice(..);
415− view.state.selection = view.state.selection.transform(|range| {
441+ let selection = cx.view.doc.selection().transform(|range| {
416 Range::new(
442 Range::new(
417 range.from(),
443 range.from(),
418 graphemes::prev_grapheme_boundary(text, range.to()),
444 graphemes::prev_grapheme_boundary(text, range.to()),
447+ cx.view.doc.set_selection(selection);
422− view.state.restore_cursor = false;
449+ cx.view.doc.restore_cursor = false;
426−pub fn goto_mode(view: &mut View, _count: usize) {
453+pub fn goto_mode(cx: &mut Context) {
427− view.state.mode = Mode::Goto;
454+ cx.view.doc.mode = Mode::Goto;
430// NOTE: Transactions in this module get appended to history when we switch back to normal mode.
457// NOTE: Transactions in this module get appended to history when we switch back to normal mode.
431pub mod insert {
458pub mod insert {
432 use super::*;
459 use super::*;
433 // TODO: insert means add text just before cursor, on exit we should be on the last letter.
460 // TODO: insert means add text just before cursor, on exit we should be on the last letter.
434− pub fn insert_char(view: &mut View, c: char) {
461+ pub fn insert_char(cx: &mut Context, c: char) {
435 let c = Tendril::from_char(c);
462 let c = Tendril::from_char(c);
436− let transaction = Transaction::insert(&view.state, c);
463+ let transaction = Transaction::insert(&cx.view.doc.state, c);
438− transaction.apply(&mut view.state);
465+ cx.view.doc.apply(&transaction);
441− pub fn insert_tab(view: &mut View, _count: usize) {
468+ pub fn insert_tab(cx: &mut Context) {
442− insert_char(view, '\t');
469+ insert_char(cx, '\t');
445− pub fn insert_newline(view: &mut View, _count: usize) {
472+ pub fn insert_newline(cx: &mut Context) {
446− let transaction = Transaction::change_by_selection(&view.state, |range| {
473+ let transaction = Transaction::change_by_selection(&cx.view.doc.state, |range| {
448− helix_core::indent::suggested_indent_for_pos(&view.state, range.head);
474+ let indent_level = helix_core::indent::suggested_indent_for_pos(
475+ cx.view.doc.syntax.as_ref(),
449 let indent = " ".repeat(TAB_WIDTH).repeat(indent_level);
479 let indent = " ".repeat(TAB_WIDTH).repeat(indent_level);
450 let mut text = String::with_capacity(1 + indent.len());
480 let mut text = String::with_capacity(1 + indent.len());
451 text.push('\n');
481 text.push('\n');
452 text.push_str(&indent);
482 text.push_str(&indent);
453 (range.head, range.head, Some(text.into()))
483 (range.head, range.head, Some(text.into()))
455− transaction.apply(&mut view.state);
485+ cx.view.doc.apply(&transaction);
458 // TODO: handle indent-aware delete
488 // TODO: handle indent-aware delete
459− pub fn delete_char_backward(view: &mut View, count: usize) {
489+ pub fn delete_char_backward(cx: &mut Context) {
460− let text = &view.state.doc.slice(..);
490+ let text = &cx.view.doc.text().slice(..);
461− let transaction = Transaction::change_by_selection(&view.state, |range| {
491+ let transaction = Transaction::change_by_selection(&cx.view.doc.state, |range| {
463− graphemes::nth_prev_grapheme_boundary(text, range.head, count),
493+ graphemes::nth_prev_grapheme_boundary(text, range.head, cx.count),
464 range.head,
494 range.head,
468− transaction.apply(&mut view.state);
498+ cx.view.doc.apply(&transaction);
471− pub fn delete_char_forward(view: &mut View, count: usize) {
501+ pub fn delete_char_forward(cx: &mut Context) {
472− let text = &view.state.doc.slice(..);
502+ let text = &cx.view.doc.text().slice(..);
473− let transaction = Transaction::change_by_selection(&view.state, |range| {
503+ let transaction = Transaction::change_by_selection(&cx.view.doc.state, |range| {
475 range.head,
505 range.head,
476− graphemes::nth_next_grapheme_boundary(text, range.head, count),
506+ graphemes::nth_next_grapheme_boundary(text, range.head, cx.count),
480− transaction.apply(&mut view.state);
510+ cx.view.doc.apply(&transaction);
@@ -487,24 +517,32 @@ pub fn insert_char_prompt(prompt: &mut Prompt, c: char) {
488// Undo / Redo
518// Undo / Redo
490−pub fn undo(view: &mut View, _count: usize) {
520+pub fn undo(cx: &mut Context) {
491− view.history.undo(&mut view.state);
521+ if let Some(revert) = cx.view.doc.history.undo() {
522+ cx.view.doc.version += 1;
523+ cx.view.doc.apply(&revert);
493 // TODO: each command could simply return a Option<transaction>, then the higher level handles storing it?
526 // TODO: each command could simply return a Option<transaction>, then the higher level handles storing it?
496−pub fn redo(view: &mut View, _count: usize) {
529+pub fn redo(cx: &mut Context) {
497− view.history.redo(&mut view.state);
530+ if let Some(transaction) = cx.view.doc.history.redo() {
531+ cx.view.doc.version += 1;
532+ cx.view.doc.apply(&transaction);
500// Yank / Paste
536// Yank / Paste
502−pub fn yank(view: &mut View, _count: usize) {
538+pub fn yank(cx: &mut Context) {
503 // TODO: should selections be made end inclusive?
539 // TODO: should selections be made end inclusive?
504− let values = view
540+ let values = cx
506 .selection()
544 .selection()
507− .fragments(&view.state.doc().slice(..))
545+ .fragments(&cx.view.doc.text().slice(..))
508 .map(|cow| cow.into_owned())
546 .map(|cow| cow.into_owned())
509 .collect();
547 .collect();
@@ -513,7 +551,7 @@ pub fn yank(view: &mut View, _count: usize) {
513 register::set(reg, values);
551 register::set(reg, values);
516−pub fn paste(view: &mut View, _count: usize) {
554+pub fn paste(cx: &mut Context) {
517 // TODO: allow specifying reg
555 // TODO: allow specifying reg
518 let reg = '"';
556 let reg = '"';
519 if let Some(values) = register::get(reg) {
557 if let Some(values) = register::get(reg) {
@@ -545,19 +583,19 @@ pub fn paste(view: &mut View, _count: usize) {
545 let transaction = if linewise {
583 let transaction = if linewise {
546 // paste on the next line
584 // paste on the next line
547 // TODO: can simply take a range + modifier and compute the right pos without ifs
585 // TODO: can simply take a range + modifier and compute the right pos without ifs
548− let text = view.state.doc();
586+ let text = cx.view.doc.text();
549− Transaction::change_by_selection(&view.state, |range| {
587+ Transaction::change_by_selection(&cx.view.doc.state, |range| {
550 let line_end = text.line_to_char(text.char_to_line(range.head) + 1);
588 let line_end = text.line_to_char(text.char_to_line(range.head) + 1);
551 (line_end, line_end, Some(values.next().unwrap()))
589 (line_end, line_end, Some(values.next().unwrap()))
554− Transaction::change_by_selection(&view.state, |range| {
592+ Transaction::change_by_selection(&cx.view.doc.state, |range| {
555 (range.head + 1, range.head + 1, Some(values.next().unwrap()))
593 (range.head + 1, range.head + 1, Some(values.next().unwrap()))
559− transaction.apply(&mut view.state);
597+ cx.view.doc.apply(&transaction);
560− append_changes_to_history(view);
598+ append_changes_to_history(cx);
@@ -565,9 +603,9 @@ fn get_lines(view: &View) -> Vec<usize> {
565 let mut lines = Vec::new();
603 let mut lines = Vec::new();
567 // Get all line numbers
605 // Get all line numbers
568− for range in view.state.selection.ranges() {
606+ for range in view.doc.selection().ranges() {
569− let start = view.state.doc.char_to_line(range.from());
607+ let start = view.doc.text().char_to_line(range.from());
570− let end = view.state.doc.char_to_line(range.to());
608+ let end = view.doc.text().char_to_line(range.to());
572 for line in start..=end {
610 for line in start..=end {
573 lines.push(line)
611 lines.push(line)
@@ -578,29 +616,29 @@ fn get_lines(view: &View) -> Vec<usize> {
581−pub fn indent(view: &mut View, _count: usize) {
619+pub fn indent(cx: &mut Context) {
582− let lines = get_lines(view);
620+ let lines = get_lines(cx.view);
584 // Indent by one level
622 // Indent by one level
585 let indent = Tendril::from(" ".repeat(TAB_WIDTH));
623 let indent = Tendril::from(" ".repeat(TAB_WIDTH));
587 let transaction = Transaction::change(
625 let transaction = Transaction::change(
588− &view.state,
626+ &cx.view.doc.state,
589 lines.into_iter().map(|line| {
627 lines.into_iter().map(|line| {
590− let pos = view.state.doc.line_to_char(line);
628+ let pos = cx.view.doc.text().line_to_char(line);
591 (pos, pos, Some(indent.clone()))
629 (pos, pos, Some(indent.clone()))
594− transaction.apply(&mut view.state);
632+ cx.view.doc.apply(&transaction);
595− append_changes_to_history(view);
633+ append_changes_to_history(cx);
598−pub fn unindent(view: &mut View, _count: usize) {
636+pub fn unindent(cx: &mut Context) {
599− let lines = get_lines(view);
637+ let lines = get_lines(cx.view);
600 let mut changes = Vec::with_capacity(lines.len());
638 let mut changes = Vec::with_capacity(lines.len());
602 for line_idx in lines {
640 for line_idx in lines {
603− let line = view.state.doc.line(line_idx);
641+ let line = cx.view.doc.text().line(line_idx);
604 let mut width = 0;
642 let mut width = 0;
606 for ch in line.chars() {
644 for ch in line.chars() {
@@ -616,18 +654,27 @@ pub fn unindent(view: &mut View, _count: usize) {
618 if width > 0 {
656 if width > 0 {
619− let start = view.state.doc.line_to_char(line_idx);
657+ let start = cx.view.doc.text().line_to_char(line_idx);
620 changes.push((start, start + width, None))
658 changes.push((start, start + width, None))
624− let transaction = Transaction::change(&view.state, changes.into_iter());
662+ let transaction = Transaction::change(&cx.view.doc.state, changes.into_iter());
626− transaction.apply(&mut view.state);
664+ cx.view.doc.apply(&transaction);
627− append_changes_to_history(view);
665+ append_changes_to_history(cx);
630−pub fn indent_selection(_view: &mut View, _count: usize) {
668+pub fn indent_selection(_cx: &mut Context) {
631 // loop over each line and recompute proper indentation
669 // loop over each line and recompute proper indentation
632 unimplemented!()
670 unimplemented!()
675+pub fn save(cx: &mut Context) {
676+ // Spawns an async task to actually do the saving. This way we prevent blocking.
678+ // TODO: handle save errors somehow?
679+ cx.executor.spawn(cx.view.doc.save()).detach();