@@ -3,12 +3,13 @@ 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},
@@ -19,36 +20,40 @@ pub type Command = fn(view: &mut View, count: usize);
20pub fn move_char_left(view: &mut View, count: usize) {
21pub fn move_char_left(view: &mut View, count: usize) {
21 // TODO: use a transaction
22 // TODO: use a transaction
22− let selection = view
23+ let selection =
24− .move_selection(Direction::Backward, Granularity::Character, count);
26+ .move_selection(Direction::Backward, Granularity::Character, count);
25− view.state.selection = selection;
27+ view.doc.state.selection = selection;
28pub fn move_char_right(view: &mut View, count: usize) {
30pub fn move_char_right(view: &mut View, count: usize) {
29 // TODO: use a transaction
31 // TODO: use a transaction
30− view.state.selection =
32+ view.doc.state.selection =
32 .move_selection(Direction::Forward, Granularity::Character, count);
35 .move_selection(Direction::Forward, Granularity::Character, count);
35pub fn move_line_up(view: &mut View, count: usize) {
38pub fn move_line_up(view: &mut View, count: usize) {
36 // TODO: use a transaction
39 // TODO: use a transaction
37− view.state.selection = view
40+ view.doc.state.selection =
39− .move_selection(Direction::Backward, Granularity::Line, count);
43+ .move_selection(Direction::Backward, Granularity::Line, count);
42pub fn move_line_down(view: &mut View, count: usize) {
46pub fn move_line_down(view: &mut View, count: usize) {
43 // TODO: use a transaction
47 // TODO: use a transaction
44− view.state.selection = view
48+ view.doc.state.selection =
46− .move_selection(Direction::Forward, Granularity::Line, count);
51+ .move_selection(Direction::Forward, Granularity::Line, count);
49pub fn move_line_end(view: &mut View, _count: usize) {
54pub fn move_line_end(view: &mut View, _count: usize) {
50 // TODO: use a transaction
55 // TODO: use a transaction
51− let lines = selection_lines(&view.state);
56+ let lines = selection_lines(&view.doc.state);
53 let positions = lines
58 let positions = lines
54 .into_iter()
59 .into_iter()
@@ -57,89 +62,89 @@ pub fn move_line_end(view: &mut View, _count: usize) {
58 // Line end is pos at the start of next line - 1
63 // Line end is pos at the start of next line - 1
59 // subtract another 1 because the line ends with \n
64 // subtract another 1 because the line ends with \n
60− view.state.doc.line_to_char(index + 1).saturating_sub(2)
65+ view.doc.text().line_to_char(index + 1).saturating_sub(2)
62 .map(|pos| Range::new(pos, pos));
67 .map(|pos| Range::new(pos, pos));
64 let selection = Selection::new(positions.collect(), 0);
69 let selection = Selection::new(positions.collect(), 0);
66− let transaction = Transaction::new(&mut view.state).with_selection(selection);
71+ let transaction = Transaction::new(&mut view.doc.state).with_selection(selection);
68− transaction.apply(&mut view.state);
73+ view.doc.apply(&transaction);
71pub fn move_line_start(view: &mut View, _count: usize) {
76pub fn move_line_start(view: &mut View, _count: usize) {
72− let lines = selection_lines(&view.state);
77+ let lines = selection_lines(&view.doc.state);
74 let positions = lines
79 let positions = lines
75 .into_iter()
80 .into_iter()
76 .map(|index| {
81 .map(|index| {
77 // adjust all positions to the start of the line.
82 // adjust all positions to the start of the line.
78− view.state.doc.line_to_char(index)
83+ view.doc.text().line_to_char(index)
80 .map(|pos| Range::new(pos, pos));
85 .map(|pos| Range::new(pos, pos));
82 let selection = Selection::new(positions.collect(), 0);
87 let selection = Selection::new(positions.collect(), 0);
84− let transaction = Transaction::new(&mut view.state).with_selection(selection);
89+ let transaction = Transaction::new(&mut view.doc.state).with_selection(selection);
86− transaction.apply(&mut view.state);
91+ view.doc.apply(&transaction);
89pub fn move_next_word_start(view: &mut View, count: usize) {
94pub fn move_next_word_start(view: &mut View, count: usize) {
90− let pos = view.state.move_pos(
95+ let pos = view.doc.state.move_pos(
91− view.state.selection.cursor(),
96+ view.doc.state.selection.cursor(),
92 Direction::Forward,
97 Direction::Forward,
93 Granularity::Word,
98 Granularity::Word,
97 // TODO: use a transaction
102 // TODO: use a transaction
98− view.state.selection = Selection::single(pos, pos);
103+ view.doc.state.selection = Selection::single(pos, pos);
101pub fn move_prev_word_start(view: &mut View, count: usize) {
106pub fn move_prev_word_start(view: &mut View, count: usize) {
102− let pos = view.state.move_pos(
107+ let pos = view.doc.state.move_pos(
103− view.state.selection.cursor(),
108+ view.doc.state.selection.cursor(),
104 Direction::Backward,
109 Direction::Backward,
105 Granularity::Word,
110 Granularity::Word,
109 // TODO: use a transaction
114 // TODO: use a transaction
110− view.state.selection = Selection::single(pos, pos);
115+ view.doc.state.selection = Selection::single(pos, pos);
113pub fn move_next_word_end(view: &mut View, count: usize) {
118pub fn move_next_word_end(view: &mut View, count: usize) {
114 let pos = State::move_next_word_end(
119 let pos = State::move_next_word_end(
115− &view.state.doc().slice(..),
120+ &view.doc.text().slice(..),
116− view.state.selection.cursor(),
121+ view.doc.state.selection.cursor(),
120 // TODO: use a transaction
125 // TODO: use a transaction
121− view.state.selection = Selection::single(pos, pos);
126+ view.doc.state.selection = Selection::single(pos, pos);
124pub fn move_file_start(view: &mut View, _count: usize) {
129pub fn move_file_start(view: &mut View, _count: usize) {
125 // TODO: use a transaction
130 // TODO: use a transaction
126− view.state.selection = Selection::single(0, 0);
131+ view.doc.state.selection = Selection::single(0, 0);
128− view.state.mode = Mode::Normal;
133+ view.doc.mode = Mode::Normal;
131pub fn move_file_end(view: &mut View, _count: usize) {
136pub fn move_file_end(view: &mut View, _count: usize) {
132 // TODO: use a transaction
137 // TODO: use a transaction
133− let text = &view.state.doc;
138+ let text = &view.doc.text();
134 let last_line = text.line_to_char(text.len_lines().saturating_sub(2));
139 let last_line = text.line_to_char(text.len_lines().saturating_sub(2));
135− view.state.selection = Selection::single(last_line, last_line);
140+ view.doc.state.selection = Selection::single(last_line, last_line);
137− view.state.mode = Mode::Normal;
142+ view.doc.mode = Mode::Normal;
140pub fn check_cursor_in_view(view: &mut View) -> bool {
145pub fn check_cursor_in_view(view: &mut View) -> bool {
141− let cursor = view.state.selection().cursor();
146+ let cursor = view.doc.state.selection().cursor();
142− let line = view.state.doc().char_to_line(cursor);
147+ let line = view.doc.text().char_to_line(cursor);
143 let document_end = view.first_line + view.size.1.saturating_sub(1) as usize;
148 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) {
150 if (line > document_end.saturating_sub(PADDING)) | (line < view.first_line + PADDING) {
@@ -156,19 +161,19 @@ pub fn page_up(view: &mut View, _count: usize) {
156 view.first_line = view.first_line.saturating_sub(view.size.1 as usize);
161 view.first_line = view.first_line.saturating_sub(view.size.1 as usize);
158 if !check_cursor_in_view(view) {
163 if !check_cursor_in_view(view) {
159− let text = view.state.doc();
164+ let text = view.doc.text();
160 let pos = text.line_to_char(view.last_line().saturating_sub(PADDING));
165 let pos = text.line_to_char(view.last_line().saturating_sub(PADDING));
161− view.state.selection = Selection::single(pos, pos);
166+ view.doc.state.selection = Selection::single(pos, pos);
165pub fn page_down(view: &mut View, _count: usize) {
170pub fn page_down(view: &mut View, _count: usize) {
166 view.first_line += view.size.1 as usize + PADDING;
171 view.first_line += view.size.1 as usize + PADDING;
168− if view.first_line < view.state.doc().len_lines() {
173+ if view.first_line < view.doc.text().len_lines() {
169− let text = view.state.doc();
174+ let text = view.doc.text();
170 let pos = text.line_to_char(view.first_line as usize);
175 let pos = text.line_to_char(view.first_line as usize);
171− view.state.selection = Selection::single(pos, pos);
176+ view.doc.state.selection = Selection::single(pos, pos);
@@ -180,79 +185,84 @@ pub fn half_page_up(view: &mut View, _count: usize) {
180 view.first_line = view.first_line.saturating_sub(view.size.1 as usize / 2);
185 view.first_line = view.first_line.saturating_sub(view.size.1 as usize / 2);
182 if !check_cursor_in_view(view) {
187 if !check_cursor_in_view(view) {
183− let text = &view.state.doc;
188+ let text = &view.doc.text();
184 let pos = text.line_to_char(view.last_line() - PADDING);
189 let pos = text.line_to_char(view.last_line() - PADDING);
185− view.state.selection = Selection::single(pos, pos);
190+ view.doc.state.selection = Selection::single(pos, pos);
189pub fn half_page_down(view: &mut View, _count: usize) {
194pub fn half_page_down(view: &mut View, _count: usize) {
190− let lines = view.state.doc().len_lines();
195+ let lines = view.doc.text().len_lines();
191 if view.first_line < lines.saturating_sub(view.size.1 as usize) {
196 if view.first_line < lines.saturating_sub(view.size.1 as usize) {
192 view.first_line += view.size.1 as usize / 2;
197 view.first_line += view.size.1 as usize / 2;
194 if !check_cursor_in_view(view) {
199 if !check_cursor_in_view(view) {
195− let text = view.state.doc();
200+ let text = view.doc.text();
196 let pos = text.line_to_char(view.first_line as usize);
201 let pos = text.line_to_char(view.first_line as usize);
197− view.state.selection = Selection::single(pos, pos);
202+ view.doc.state.selection = Selection::single(pos, pos);
200// avoid select by default by having a visual mode switch that makes movements into selects
205// avoid select by default by having a visual mode switch that makes movements into selects
202pub fn extend_char_left(view: &mut View, count: usize) {
207pub fn extend_char_left(view: &mut View, count: usize) {
203 // TODO: use a transaction
208 // TODO: use a transaction
204− let selection = view
209+ let selection =
206− .extend_selection(Direction::Backward, Granularity::Character, count);
212+ .extend_selection(Direction::Backward, Granularity::Character, count);
207− view.state.selection = selection;
213+ view.doc.state.selection = selection;
210pub fn extend_char_right(view: &mut View, count: usize) {
216pub fn extend_char_right(view: &mut View, count: usize) {
211 // TODO: use a transaction
217 // TODO: use a transaction
212− view.state.selection =
218+ view.doc.state.selection =
213− view.state
220+ .state
214 .extend_selection(Direction::Forward, Granularity::Character, count);
221 .extend_selection(Direction::Forward, Granularity::Character, count);
217pub fn extend_line_up(view: &mut View, count: usize) {
224pub fn extend_line_up(view: &mut View, count: usize) {
218 // TODO: use a transaction
225 // TODO: use a transaction
219− view.state.selection =
226+ view.doc.state.selection =
220− view.state
228+ .state
221 .extend_selection(Direction::Backward, Granularity::Line, count);
229 .extend_selection(Direction::Backward, Granularity::Line, count);
224pub fn extend_line_down(view: &mut View, count: usize) {
232pub fn extend_line_down(view: &mut View, count: usize) {
225 // TODO: use a transaction
233 // TODO: use a transaction
226− view.state.selection =
234+ view.doc.state.selection =
227− view.state
236+ .state
228 .extend_selection(Direction::Forward, Granularity::Line, count);
237 .extend_selection(Direction::Forward, Granularity::Line, count);
231pub fn split_selection_on_newline(view: &mut View, _count: usize) {
240pub fn split_selection_on_newline(view: &mut View, _count: usize) {
232− let text = &view.state.doc.slice(..);
241+ let text = &view.doc.text().slice(..);
233 // only compile the regex once
242 // only compile the regex once
234 #[allow(clippy::trivial_regex)]
243 #[allow(clippy::trivial_regex)]
235 static REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\n").unwrap());
244 static REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\n").unwrap());
236 // TODO: use a transaction
245 // TODO: use a transaction
237− view.state.selection = selection::split_on_matches(text, view.state.selection(), ®EX)
246+ view.doc.state.selection = selection::split_on_matches(text, view.doc.state.selection(), ®EX)
240pub fn select_line(view: &mut View, _count: usize) {
249pub fn select_line(view: &mut View, _count: usize) {
241 // TODO: count
250 // TODO: count
242− let pos = view.state.selection().primary();
251+ let pos = view.doc.state.selection().primary();
243− let text = view.state.doc();
252+ let text = view.doc.text();
244 let line = text.char_to_line(pos.head);
253 let line = text.char_to_line(pos.head);
245 let start = text.line_to_char(line);
254 let start = text.line_to_char(line);
246 let end = text.line_to_char(line + 1).saturating_sub(1);
255 let end = text.line_to_char(line + 1).saturating_sub(1);
248 // TODO: use a transaction
257 // TODO: use a transaction
249− view.state.selection = Selection::single(start, end);
258+ view.doc.state.selection = Selection::single(start, end);
252pub fn delete_selection(view: &mut View, _count: usize) {
261pub fn delete_selection(view: &mut View, _count: usize) {
254− Transaction::change_by_selection(&view.state, |range| (range.from(), range.to() + 1, None));
262+ let transaction = Transaction::change_by_selection(&view.doc.state, |range| {
255− transaction.apply(&mut view.state);
263+ (range.from(), range.to() + 1, None)
265+ view.doc.apply(&transaction);
257 append_changes_to_history(view);
267 append_changes_to_history(view);
@@ -263,21 +273,23 @@ pub fn change_selection(view: &mut View, count: usize) {
265pub fn collapse_selection(view: &mut View, _count: usize) {
275pub fn collapse_selection(view: &mut View, _count: usize) {
266− view.state.selection = view
276+ view.doc.state.selection = view
268 .selection
279 .selection
269 .transform(|range| Range::new(range.head, range.head))
280 .transform(|range| Range::new(range.head, range.head))
272pub fn flip_selections(view: &mut View, _count: usize) {
283pub fn flip_selections(view: &mut View, _count: usize) {
273− view.state.selection = view
284+ view.doc.state.selection = view
275 .selection
287 .selection
276 .transform(|range| Range::new(range.head, range.anchor))
288 .transform(|range| Range::new(range.head, range.anchor))
279fn enter_insert_mode(view: &mut View) {
291fn enter_insert_mode(view: &mut View) {
280− view.state.mode = Mode::Insert;
292+ view.doc.mode = Mode::Insert;
282 append_changes_to_history(view);
294 append_changes_to_history(view);
@@ -285,7 +297,8 @@ fn enter_insert_mode(view: &mut View) {
285pub fn insert_mode(view: &mut View, _count: usize) {
297pub fn insert_mode(view: &mut View, _count: usize) {
286 enter_insert_mode(view);
298 enter_insert_mode(view);
288− view.state.selection = view
300+ view.doc.state.selection = view
290 .selection
303 .selection
291 .transform(|range| Range::new(range.to(), range.from()))
304 .transform(|range| Range::new(range.to(), range.from()))
@@ -294,11 +307,11 @@ pub fn insert_mode(view: &mut View, _count: usize) {
294// inserts at the end of each selection
307// inserts at the end of each selection
295pub fn append_mode(view: &mut View, _count: usize) {
308pub fn append_mode(view: &mut View, _count: usize) {
296 enter_insert_mode(view);
309 enter_insert_mode(view);
297− view.state.restore_cursor = true;
310+ view.doc.restore_cursor = true;
299 // TODO: as transaction
312 // TODO: as transaction
300− let text = &view.state.doc.slice(..);
313+ let text = &view.doc.text().slice(..);
301− view.state.selection = view.state.selection.transform(|range| {
314+ view.doc.state.selection = view.doc.state.selection.transform(|range| {
302 // TODO: to() + next char
315 // TODO: to() + next char
303 Range::new(
316 Range::new(
304 range.from(),
317 range.from(),
@@ -346,13 +359,13 @@ pub fn append_to_line(view: &mut View, count: usize) {
346pub fn open_below(view: &mut View, _count: usize) {
359pub fn open_below(view: &mut View, _count: usize) {
347 enter_insert_mode(view);
360 enter_insert_mode(view);
349− let lines = selection_lines(&view.state);
362+ let lines = selection_lines(&view.doc.state);
351 let positions: Vec<_> = lines
364 let positions: Vec<_> = lines
352 .into_iter()
365 .into_iter()
353 .map(|index| {
366 .map(|index| {
354 // adjust all positions to the end of the line/start of the next one.
367 // adjust all positions to the end of the line/start of the next one.
355− view.state.doc.line_to_char(index + 1)
368+ view.doc.text().line_to_char(index + 1)
357 .collect();
370 .collect();
@@ -373,63 +386,63 @@ pub fn open_below(view: &mut View, _count: usize) {
376− let transaction = Transaction::change(&view.state, changes).with_selection(selection);
389+ let transaction = Transaction::change(&view.doc.state, changes).with_selection(selection);
378− transaction.apply(&mut view.state);
391+ view.doc.apply(&transaction);
381// O inserts a new line before each line with a selection
394// O inserts a new line before each line with a selection
383fn append_changes_to_history(view: &mut View) {
396fn append_changes_to_history(view: &mut View) {
384− if view.state.changes.is_empty() {
397+ if view.doc.changes.is_empty() {
388− let new_changeset = ChangeSet::new(view.state.doc());
401+ let new_changeset = ChangeSet::new(view.doc.text());
389− let changes = std::mem::replace(&mut view.state.changes, new_changeset);
402+ let changes = std::mem::replace(&mut view.doc.changes, new_changeset);
390 // Instead of doing this messy merge we could always commit, and based on transaction
403 // 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.
404 // annotations either add a new layer or compose into the previous one.
392− let transaction = Transaction::from(changes).with_selection(view.state.selection().clone());
405+ let transaction = Transaction::from(changes).with_selection(view.doc.state.selection().clone());
394 // increment document version
407 // increment document version
395 // TODO: needs to happen on undo/redo too
408 // TODO: needs to happen on undo/redo too
396− view.state.version += 1;
409+ view.doc.version += 1;
398 // TODO: trigger lsp/documentDidChange with changes
411 // TODO: trigger lsp/documentDidChange with changes
400 // HAXX: we need to reconstruct the state as it was before the changes..
413 // HAXX: we need to reconstruct the state as it was before the changes..
401− let (doc, selection) = view.state.old_state.take().unwrap();
414+ let (doc, selection) = view.doc.old_state.take().unwrap();
402 let mut old_state = State::new(doc);
415 let mut old_state = State::new(doc);
403 old_state.selection = selection;
416 old_state.selection = selection;
405 // TODO: take transaction by value?
418 // TODO: take transaction by value?
406− view.history.commit_revision(&transaction, &old_state);
419+ view.doc.history.commit_revision(&transaction, &old_state);
409− view.state.old_state = Some((view.state.doc().clone(), view.state.selection.clone()));
422+ view.doc.old_state = Some((view.doc.text().clone(), view.doc.state.selection.clone()));
412pub fn normal_mode(view: &mut View, _count: usize) {
425pub fn normal_mode(view: &mut View, _count: usize) {
413− view.state.mode = Mode::Normal;
426+ view.doc.mode = Mode::Normal;
415 append_changes_to_history(view);
428 append_changes_to_history(view);
417 // if leaving append mode, move cursor back by 1
430 // if leaving append mode, move cursor back by 1
418− if view.state.restore_cursor {
431+ if view.doc.restore_cursor {
419− let text = &view.state.doc.slice(..);
432+ let text = &view.doc.text().slice(..);
420− view.state.selection = view.state.selection.transform(|range| {
433+ view.doc.state.selection = view.doc.state.selection.transform(|range| {
421 Range::new(
434 Range::new(
422 range.from(),
435 range.from(),
423 graphemes::prev_grapheme_boundary(text, range.to()),
436 graphemes::prev_grapheme_boundary(text, range.to()),
427− view.state.restore_cursor = false;
440+ view.doc.restore_cursor = false;
431pub fn goto_mode(view: &mut View, _count: usize) {
444pub fn goto_mode(view: &mut View, _count: usize) {
432− view.state.mode = Mode::Goto;
445+ view.doc.mode = Mode::Goto;
435// NOTE: Transactions in this module get appended to history when we switch back to normal mode.
448// NOTE: Transactions in this module get appended to history when we switch back to normal mode.
@@ -438,9 +451,9 @@ pub mod insert {
438 // TODO: insert means add text just before cursor, on exit we should be on the last letter.
451 // TODO: insert means add text just before cursor, on exit we should be on the last letter.
439 pub fn insert_char(view: &mut View, c: char) {
452 pub fn insert_char(view: &mut View, c: char) {
440 let c = Tendril::from_char(c);
453 let c = Tendril::from_char(c);
441− let transaction = Transaction::insert(&view.state, c);
454+ let transaction = Transaction::insert(&view.doc.state, c);
443− transaction.apply(&mut view.state);
456+ view.doc.apply(&transaction);
446 pub fn insert_tab(view: &mut View, _count: usize) {
459 pub fn insert_tab(view: &mut View, _count: usize) {
@@ -448,41 +461,44 @@ pub mod insert {
450 pub fn insert_newline(view: &mut View, _count: usize) {
463 pub fn insert_newline(view: &mut View, _count: usize) {
451− let transaction = Transaction::change_by_selection(&view.state, |range| {
464+ let transaction = Transaction::change_by_selection(&view.doc.state, |range| {
453− helix_core::indent::suggested_indent_for_pos(&view.state, range.head);
465+ let indent_level = helix_core::indent::suggested_indent_for_pos(
466+ view.doc.syntax.as_ref(),
454 let indent = " ".repeat(TAB_WIDTH).repeat(indent_level);
470 let indent = " ".repeat(TAB_WIDTH).repeat(indent_level);
455 let mut text = String::with_capacity(1 + indent.len());
471 let mut text = String::with_capacity(1 + indent.len());
456 text.push('\n');
472 text.push('\n');
457 text.push_str(&indent);
473 text.push_str(&indent);
458 (range.head, range.head, Some(text.into()))
474 (range.head, range.head, Some(text.into()))
460− transaction.apply(&mut view.state);
476+ view.doc.apply(&transaction);
463 // TODO: handle indent-aware delete
479 // TODO: handle indent-aware delete
464 pub fn delete_char_backward(view: &mut View, count: usize) {
480 pub fn delete_char_backward(view: &mut View, count: usize) {
465− let text = &view.state.doc.slice(..);
481+ let text = &view.doc.text().slice(..);
466− let transaction = Transaction::change_by_selection(&view.state, |range| {
482+ let transaction = Transaction::change_by_selection(&view.doc.state, |range| {
468 graphemes::nth_prev_grapheme_boundary(text, range.head, count),
484 graphemes::nth_prev_grapheme_boundary(text, range.head, count),
469 range.head,
485 range.head,
473− transaction.apply(&mut view.state);
489+ view.doc.apply(&transaction);
476 pub fn delete_char_forward(view: &mut View, count: usize) {
492 pub fn delete_char_forward(view: &mut View, count: usize) {
477− let text = &view.state.doc.slice(..);
493+ let text = &view.doc.text().slice(..);
478− let transaction = Transaction::change_by_selection(&view.state, |range| {
494+ let transaction = Transaction::change_by_selection(&view.doc.state, |range| {
480 range.head,
496 range.head,
481 graphemes::nth_next_grapheme_boundary(text, range.head, count),
497 graphemes::nth_next_grapheme_boundary(text, range.head, count),
485− transaction.apply(&mut view.state);
501+ view.doc.apply(&transaction);
@@ -493,13 +509,13 @@ pub fn insert_char_prompt(prompt: &mut Prompt, c: char) {
493// Undo / Redo
509// Undo / Redo
495pub fn undo(view: &mut View, _count: usize) {
511pub fn undo(view: &mut View, _count: usize) {
496− view.history.undo(&mut view.state);
512+ view.doc.history.undo(&mut view.doc.state);
498 // TODO: each command could simply return a Option<transaction>, then the higher level handles storing it?
514 // TODO: each command could simply return a Option<transaction>, then the higher level handles storing it?
501pub fn redo(view: &mut View, _count: usize) {
517pub fn redo(view: &mut View, _count: usize) {
502− view.history.redo(&mut view.state);
518+ view.doc.history.redo(&mut view.doc.state);
505// Yank / Paste
521// Yank / Paste
@@ -507,9 +523,10 @@ pub fn redo(view: &mut View, _count: usize) {
507pub fn yank(view: &mut View, _count: usize) {
523pub fn yank(view: &mut View, _count: usize) {
508 // TODO: should selections be made end inclusive?
524 // TODO: should selections be made end inclusive?
509 let values = view
525 let values = view
511 .selection()
528 .selection()
512− .fragments(&view.state.doc().slice(..))
529+ .fragments(&view.doc.text().slice(..))
513 .map(|cow| cow.into_owned())
530 .map(|cow| cow.into_owned())
514 .collect();
531 .collect();
@@ -550,18 +567,18 @@ pub fn paste(view: &mut View, _count: usize) {
550 let transaction = if linewise {
567 let transaction = if linewise {
551 // paste on the next line
568 // paste on the next line
552 // TODO: can simply take a range + modifier and compute the right pos without ifs
569 // TODO: can simply take a range + modifier and compute the right pos without ifs
553− let text = view.state.doc();
570+ let text = view.doc.text();
554− Transaction::change_by_selection(&view.state, |range| {
571+ Transaction::change_by_selection(&view.doc.state, |range| {
555 let line_end = text.line_to_char(text.char_to_line(range.head) + 1);
572 let line_end = text.line_to_char(text.char_to_line(range.head) + 1);
556 (line_end, line_end, Some(values.next().unwrap()))
573 (line_end, line_end, Some(values.next().unwrap()))
559− Transaction::change_by_selection(&view.state, |range| {
576+ Transaction::change_by_selection(&view.doc.state, |range| {
560 (range.head + 1, range.head + 1, Some(values.next().unwrap()))
577 (range.head + 1, range.head + 1, Some(values.next().unwrap()))
564− transaction.apply(&mut view.state);
581+ view.doc.apply(&transaction);
565 append_changes_to_history(view);
582 append_changes_to_history(view);
@@ -570,9 +587,9 @@ fn get_lines(view: &View) -> Vec<usize> {
570 let mut lines = Vec::new();
587 let mut lines = Vec::new();
572 // Get all line numbers
589 // Get all line numbers
573− for range in view.state.selection.ranges() {
590+ for range in view.doc.state.selection.ranges() {
574− let start = view.state.doc.char_to_line(range.from());
591+ let start = view.doc.text().char_to_line(range.from());
575− let end = view.state.doc.char_to_line(range.to());
592+ let end = view.doc.text().char_to_line(range.to());
577 for line in start..=end {
594 for line in start..=end {
578 lines.push(line)
595 lines.push(line)
@@ -590,13 +607,13 @@ pub fn indent(view: &mut View, _count: usize) {
590 let indent = Tendril::from(" ".repeat(TAB_WIDTH));
607 let indent = Tendril::from(" ".repeat(TAB_WIDTH));
592 let transaction = Transaction::change(
609 let transaction = Transaction::change(
593− &view.state,
610+ &view.doc.state,
594 lines.into_iter().map(|line| {
611 lines.into_iter().map(|line| {
595− let pos = view.state.doc.line_to_char(line);
612+ let pos = view.doc.text().line_to_char(line);
596 (pos, pos, Some(indent.clone()))
613 (pos, pos, Some(indent.clone()))
599− transaction.apply(&mut view.state);
616+ view.doc.apply(&transaction);
600 append_changes_to_history(view);
617 append_changes_to_history(view);
@@ -605,7 +622,7 @@ pub fn unindent(view: &mut View, _count: usize) {
605 let mut changes = Vec::with_capacity(lines.len());
622 let mut changes = Vec::with_capacity(lines.len());
607 for line_idx in lines {
624 for line_idx in lines {
608− let line = view.state.doc.line(line_idx);
625+ let line = view.doc.text().line(line_idx);
609 let mut width = 0;
626 let mut width = 0;
611 for ch in line.chars() {
628 for ch in line.chars() {
@@ -621,14 +638,14 @@ pub fn unindent(view: &mut View, _count: usize) {
623 if width > 0 {
640 if width > 0 {
624− let start = view.state.doc.line_to_char(line_idx);
641+ let start = view.doc.text().line_to_char(line_idx);
625 changes.push((start, start + width, None))
642 changes.push((start, start + width, None))
629− let transaction = Transaction::change(&view.state, changes.into_iter());
646+ let transaction = Transaction::change(&view.doc.state, changes.into_iter());
631− transaction.apply(&mut view.state);
648+ view.doc.apply(&transaction);
632 append_changes_to_history(view);
649 append_changes_to_history(view);