@@ -3,12 +3,13 @@ use helix_core::{
33 indent::TAB_WIDTH,
44 regex::Regex,
55 register, selection,
6− state::{Direction, Granularity, Mode, State},
6+ state::{Direction, Granularity, State},
77 ChangeSet, Range, Selection, Tendril, Transaction,
88};
99use once_cell::sync::Lazy;
1010
1111use crate::{
12+ document::Mode,
1213 prompt::Prompt,
1314 view::{View, PADDING},
1415};
@@ -19,36 +20,40 @@ pub type Command = fn(view: &mut View, count: usize);
1920
2021pub fn move_char_left(view: &mut View, count: usize) {
2122 // TODO: use a transaction
22− let selection = view
23− .state
24− .move_selection(Direction::Backward, Granularity::Character, count);
25− view.state.selection = selection;
23+ let selection =
24+ view.doc
25+ .state
26+ .move_selection(Direction::Backward, Granularity::Character, count);
27+ view.doc.state.selection = selection;
2628}
2729
2830pub fn move_char_right(view: &mut View, count: usize) {
2931 // TODO: use a transaction
30− view.state.selection =
31− view.state
32+ view.doc.state.selection =
33+ view.doc
34+ .state
3235 .move_selection(Direction::Forward, Granularity::Character, count);
3336}
3437
3538pub fn move_line_up(view: &mut View, count: usize) {
3639 // TODO: use a transaction
37− view.state.selection = view
38− .state
39− .move_selection(Direction::Backward, Granularity::Line, count);
40+ view.doc.state.selection =
41+ view.doc
42+ .state
43+ .move_selection(Direction::Backward, Granularity::Line, count);
4044}
4145
4246pub fn move_line_down(view: &mut View, count: usize) {
4347 // TODO: use a transaction
44− view.state.selection = view
45− .state
46− .move_selection(Direction::Forward, Granularity::Line, count);
48+ view.doc.state.selection =
49+ view.doc
50+ .state
51+ .move_selection(Direction::Forward, Granularity::Line, count);
4752}
4853
4954pub fn move_line_end(view: &mut View, _count: usize) {
5055 // TODO: use a transaction
51− let lines = selection_lines(&view.state);
56+ let lines = selection_lines(&view.doc.state);
5257
5358 let positions = lines
5459 .into_iter()
@@ -57,89 +62,89 @@ pub fn move_line_end(view: &mut View, _count: usize) {
5762
5863 // Line end is pos at the start of next line - 1
5964 // 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)
6166 })
6267 .map(|pos| Range::new(pos, pos));
6368
6469 let selection = Selection::new(positions.collect(), 0);
6570
66− let transaction = Transaction::new(&mut view.state).with_selection(selection);
71+ let transaction = Transaction::new(&mut view.doc.state).with_selection(selection);
6772
68− transaction.apply(&mut view.state);
73+ view.doc.apply(&transaction);
6974}
7075
7176pub fn move_line_start(view: &mut View, _count: usize) {
72− let lines = selection_lines(&view.state);
77+ let lines = selection_lines(&view.doc.state);
7378
7479 let positions = lines
7580 .into_iter()
7681 .map(|index| {
7782 // 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)
7984 })
8085 .map(|pos| Range::new(pos, pos));
8186
8287 let selection = Selection::new(positions.collect(), 0);
8388
84− let transaction = Transaction::new(&mut view.state).with_selection(selection);
89+ let transaction = Transaction::new(&mut view.doc.state).with_selection(selection);
8590
86− transaction.apply(&mut view.state);
91+ view.doc.apply(&transaction);
8792}
8893
8994pub fn move_next_word_start(view: &mut View, count: usize) {
90− let pos = view.state.move_pos(
91− view.state.selection.cursor(),
95+ let pos = view.doc.state.move_pos(
96+ view.doc.state.selection.cursor(),
9297 Direction::Forward,
9398 Granularity::Word,
9499 count,
95100 );
96101
97102 // TODO: use a transaction
98− view.state.selection = Selection::single(pos, pos);
103+ view.doc.state.selection = Selection::single(pos, pos);
99104}
100105
101106pub fn move_prev_word_start(view: &mut View, count: usize) {
102− let pos = view.state.move_pos(
103− view.state.selection.cursor(),
107+ let pos = view.doc.state.move_pos(
108+ view.doc.state.selection.cursor(),
104109 Direction::Backward,
105110 Granularity::Word,
106111 count,
107112 );
108113
109114 // TODO: use a transaction
110− view.state.selection = Selection::single(pos, pos);
115+ view.doc.state.selection = Selection::single(pos, pos);
111116}
112117
113118pub fn move_next_word_end(view: &mut View, count: usize) {
114119 let pos = State::move_next_word_end(
115− &view.state.doc().slice(..),
116− view.state.selection.cursor(),
120+ &view.doc.text().slice(..),
121+ view.doc.state.selection.cursor(),
117122 count,
118123 );
119124
120125 // TODO: use a transaction
121− view.state.selection = Selection::single(pos, pos);
126+ view.doc.state.selection = Selection::single(pos, pos);
122127}
123128
124129pub fn move_file_start(view: &mut View, _count: usize) {
125130 // TODO: use a transaction
126− view.state.selection = Selection::single(0, 0);
131+ view.doc.state.selection = Selection::single(0, 0);
127132
128− view.state.mode = Mode::Normal;
133+ view.doc.mode = Mode::Normal;
129134}
130135
131136pub fn move_file_end(view: &mut View, _count: usize) {
132137 // TODO: use a transaction
133− let text = &view.state.doc;
138+ let text = &view.doc.text();
134139 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);
136141
137− view.state.mode = Mode::Normal;
142+ view.doc.mode = Mode::Normal;
138143}
139144
140145pub fn check_cursor_in_view(view: &mut View) -> bool {
141− let cursor = view.state.selection().cursor();
142− let line = view.state.doc().char_to_line(cursor);
146+ let cursor = view.doc.state.selection().cursor();
147+ let line = view.doc.text().char_to_line(cursor);
143148 let document_end = view.first_line + view.size.1.saturating_sub(1) as usize;
144149
145150 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) {
156161 view.first_line = view.first_line.saturating_sub(view.size.1 as usize);
157162
158163 if !check_cursor_in_view(view) {
159− let text = view.state.doc();
164+ let text = view.doc.text();
160165 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);
162167 }
163168}
164169
165170pub fn page_down(view: &mut View, _count: usize) {
166171 view.first_line += view.size.1 as usize + PADDING;
167172
168− if view.first_line < view.state.doc().len_lines() {
169− let text = view.state.doc();
173+ if view.first_line < view.doc.text().len_lines() {
174+ let text = view.doc.text();
170175 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);
172177 }
173178}
174179
@@ -180,79 +185,84 @@ pub fn half_page_up(view: &mut View, _count: usize) {
180185 view.first_line = view.first_line.saturating_sub(view.size.1 as usize / 2);
181186
182187 if !check_cursor_in_view(view) {
183− let text = &view.state.doc;
188+ let text = &view.doc.text();
184189 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);
186191 }
187192}
188193
189194pub 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();
191196 if view.first_line < lines.saturating_sub(view.size.1 as usize) {
192197 view.first_line += view.size.1 as usize / 2;
193198 }
194199 if !check_cursor_in_view(view) {
195− let text = view.state.doc();
200+ let text = view.doc.text();
196201 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);
198203 }
199204}
200205// avoid select by default by having a visual mode switch that makes movements into selects
201206
202207pub fn extend_char_left(view: &mut View, count: usize) {
203208 // TODO: use a transaction
204− let selection = view
205− .state
206− .extend_selection(Direction::Backward, Granularity::Character, count);
207− view.state.selection = selection;
209+ let selection =
210+ view.doc
211+ .state
212+ .extend_selection(Direction::Backward, Granularity::Character, count);
213+ view.doc.state.selection = selection;
208214}
209215
210216pub fn extend_char_right(view: &mut View, count: usize) {
211217 // TODO: use a transaction
212− view.state.selection =
213− view.state
218+ view.doc.state.selection =
219+ view.doc
220+ .state
214221 .extend_selection(Direction::Forward, Granularity::Character, count);
215222}
216223
217224pub fn extend_line_up(view: &mut View, count: usize) {
218225 // TODO: use a transaction
219− view.state.selection =
220− view.state
226+ view.doc.state.selection =
227+ view.doc
228+ .state
221229 .extend_selection(Direction::Backward, Granularity::Line, count);
222230}
223231
224232pub fn extend_line_down(view: &mut View, count: usize) {
225233 // TODO: use a transaction
226− view.state.selection =
227− view.state
234+ view.doc.state.selection =
235+ view.doc
236+ .state
228237 .extend_selection(Direction::Forward, Granularity::Line, count);
229238}
230239
231240pub fn split_selection_on_newline(view: &mut View, _count: usize) {
232− let text = &view.state.doc.slice(..);
241+ let text = &view.doc.text().slice(..);
233242 // only compile the regex once
234243 #[allow(clippy::trivial_regex)]
235244 static REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\n").unwrap());
236245 // 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)
238247}
239248
240249pub fn select_line(view: &mut View, _count: usize) {
241250 // TODO: count
242− let pos = view.state.selection().primary();
243− let text = view.state.doc();
251+ let pos = view.doc.state.selection().primary();
252+ let text = view.doc.text();
244253 let line = text.char_to_line(pos.head);
245254 let start = text.line_to_char(line);
246255 let end = text.line_to_char(line + 1).saturating_sub(1);
247256
248257 // TODO: use a transaction
249− view.state.selection = Selection::single(start, end);
258+ view.doc.state.selection = Selection::single(start, end);
250259}
251260
252261pub fn delete_selection(view: &mut View, _count: usize) {
253− let transaction =
254− Transaction::change_by_selection(&view.state, |range| (range.from(), range.to() + 1, None));
255− transaction.apply(&mut view.state);
262+ let transaction = Transaction::change_by_selection(&view.doc.state, |range| {
263+ (range.from(), range.to() + 1, None)
264+ });
265+ view.doc.apply(&transaction);
256266
257267 append_changes_to_history(view);
258268}
@@ -263,21 +273,23 @@ pub fn change_selection(view: &mut View, count: usize) {
263273}
264274
265275pub fn collapse_selection(view: &mut View, _count: usize) {
266− view.state.selection = view
276+ view.doc.state.selection = view
277+ .doc
267278 .state
268279 .selection
269280 .transform(|range| Range::new(range.head, range.head))
270281}
271282
272283pub fn flip_selections(view: &mut View, _count: usize) {
273− view.state.selection = view
284+ view.doc.state.selection = view
285+ .doc
274286 .state
275287 .selection
276288 .transform(|range| Range::new(range.head, range.anchor))
277289}
278290
279291fn enter_insert_mode(view: &mut View) {
280− view.state.mode = Mode::Insert;
292+ view.doc.mode = Mode::Insert;
281293
282294 append_changes_to_history(view);
283295}
@@ -285,7 +297,8 @@ fn enter_insert_mode(view: &mut View) {
285297pub fn insert_mode(view: &mut View, _count: usize) {
286298 enter_insert_mode(view);
287299
288− view.state.selection = view
300+ view.doc.state.selection = view
301+ .doc
289302 .state
290303 .selection
291304 .transform(|range| Range::new(range.to(), range.from()))
@@ -294,11 +307,11 @@ pub fn insert_mode(view: &mut View, _count: usize) {
294307// inserts at the end of each selection
295308pub fn append_mode(view: &mut View, _count: usize) {
296309 enter_insert_mode(view);
297− view.state.restore_cursor = true;
310+ view.doc.restore_cursor = true;
298311
299312 // TODO: as transaction
300− let text = &view.state.doc.slice(..);
301− view.state.selection = view.state.selection.transform(|range| {
313+ let text = &view.doc.text().slice(..);
314+ view.doc.state.selection = view.doc.state.selection.transform(|range| {
302315 // TODO: to() + next char
303316 Range::new(
304317 range.from(),
@@ -346,13 +359,13 @@ pub fn append_to_line(view: &mut View, count: usize) {
346359pub fn open_below(view: &mut View, _count: usize) {
347360 enter_insert_mode(view);
348361
349− let lines = selection_lines(&view.state);
362+ let lines = selection_lines(&view.doc.state);
350363
351364 let positions: Vec<_> = lines
352365 .into_iter()
353366 .map(|index| {
354367 // 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)
356369 })
357370 .collect();
358371
@@ -373,63 +386,63 @@ pub fn open_below(view: &mut View, _count: usize) {
373386 0,
374387 );
375388
376− let transaction = Transaction::change(&view.state, changes).with_selection(selection);
389+ let transaction = Transaction::change(&view.doc.state, changes).with_selection(selection);
377390
378− transaction.apply(&mut view.state);
391+ view.doc.apply(&transaction);
379392}
380393
381394// O inserts a new line before each line with a selection
382395
383396fn append_changes_to_history(view: &mut View) {
384− if view.state.changes.is_empty() {
397+ if view.doc.changes.is_empty() {
385398 return;
386399 }
387400
388− let new_changeset = ChangeSet::new(view.state.doc());
389− let changes = std::mem::replace(&mut view.state.changes, new_changeset);
401+ let new_changeset = ChangeSet::new(view.doc.text());
402+ let changes = std::mem::replace(&mut view.doc.changes, new_changeset);
390403 // Instead of doing this messy merge we could always commit, and based on transaction
391404 // 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());
393406
394407 // increment document version
395408 // TODO: needs to happen on undo/redo too
396− view.state.version += 1;
409+ view.doc.version += 1;
397410
398411 // TODO: trigger lsp/documentDidChange with changes
399412
400413 // 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();
402415 let mut old_state = State::new(doc);
403416 old_state.selection = selection;
404417
405418 // TODO: take transaction by value?
406− view.history.commit_revision(&transaction, &old_state);
419+ view.doc.history.commit_revision(&transaction, &old_state);
407420
408421 // HAXX
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()));
410423}
411424
412425pub fn normal_mode(view: &mut View, _count: usize) {
413− view.state.mode = Mode::Normal;
426+ view.doc.mode = Mode::Normal;
414427
415428 append_changes_to_history(view);
416429
417430 // if leaving append mode, move cursor back by 1
418− if view.state.restore_cursor {
419− let text = &view.state.doc.slice(..);
420− view.state.selection = view.state.selection.transform(|range| {
431+ if view.doc.restore_cursor {
432+ let text = &view.doc.text().slice(..);
433+ view.doc.state.selection = view.doc.state.selection.transform(|range| {
421434 Range::new(
422435 range.from(),
423436 graphemes::prev_grapheme_boundary(text, range.to()),
424437 )
425438 });
426439
427− view.state.restore_cursor = false;
440+ view.doc.restore_cursor = false;
428441 }
429442}
430443
431444pub fn goto_mode(view: &mut View, _count: usize) {
432− view.state.mode = Mode::Goto;
445+ view.doc.mode = Mode::Goto;
433446}
434447
435448// NOTE: Transactions in this module get appended to history when we switch back to normal mode.
@@ -438,9 +451,9 @@ pub mod insert {
438451 // TODO: insert means add text just before cursor, on exit we should be on the last letter.
439452 pub fn insert_char(view: &mut View, c: char) {
440453 let c = Tendril::from_char(c);
441− let transaction = Transaction::insert(&view.state, c);
454+ let transaction = Transaction::insert(&view.doc.state, c);
442455
443− transaction.apply(&mut view.state);
456+ view.doc.apply(&transaction);
444457 }
445458
446459 pub fn insert_tab(view: &mut View, _count: usize) {
@@ -448,41 +461,44 @@ pub mod insert {
448461 }
449462
450463 pub fn insert_newline(view: &mut View, _count: usize) {
451− let transaction = Transaction::change_by_selection(&view.state, |range| {
452− let indent_level =
453− helix_core::indent::suggested_indent_for_pos(&view.state, range.head);
464+ let transaction = Transaction::change_by_selection(&view.doc.state, |range| {
465+ let indent_level = helix_core::indent::suggested_indent_for_pos(
466+ view.doc.syntax.as_ref(),
467+ &view.doc.state,
468+ range.head,
469+ );
454470 let indent = " ".repeat(TAB_WIDTH).repeat(indent_level);
455471 let mut text = String::with_capacity(1 + indent.len());
456472 text.push('\n');
457473 text.push_str(&indent);
458474 (range.head, range.head, Some(text.into()))
459475 });
460− transaction.apply(&mut view.state);
476+ view.doc.apply(&transaction);
461477 }
462478
463479 // TODO: handle indent-aware delete
464480 pub fn delete_char_backward(view: &mut View, count: usize) {
465− let text = &view.state.doc.slice(..);
466− let transaction = Transaction::change_by_selection(&view.state, |range| {
481+ let text = &view.doc.text().slice(..);
482+ let transaction = Transaction::change_by_selection(&view.doc.state, |range| {
467483 (
468484 graphemes::nth_prev_grapheme_boundary(text, range.head, count),
469485 range.head,
470486 None,
471487 )
472488 });
473− transaction.apply(&mut view.state);
489+ view.doc.apply(&transaction);
474490 }
475491
476492 pub fn delete_char_forward(view: &mut View, count: usize) {
477− let text = &view.state.doc.slice(..);
478− let transaction = Transaction::change_by_selection(&view.state, |range| {
493+ let text = &view.doc.text().slice(..);
494+ let transaction = Transaction::change_by_selection(&view.doc.state, |range| {
479495 (
480496 range.head,
481497 graphemes::nth_next_grapheme_boundary(text, range.head, count),
482498 None,
483499 )
484500 });
485− transaction.apply(&mut view.state);
501+ view.doc.apply(&transaction);
486502 }
487503}
488504
@@ -493,13 +509,13 @@ pub fn insert_char_prompt(prompt: &mut Prompt, c: char) {
493509// Undo / Redo
494510
495511pub fn undo(view: &mut View, _count: usize) {
496− view.history.undo(&mut view.state);
512+ view.doc.history.undo(&mut view.doc.state);
497513
498514 // TODO: each command could simply return a Option<transaction>, then the higher level handles storing it?
499515}
500516
501517pub fn redo(view: &mut View, _count: usize) {
502− view.history.redo(&mut view.state);
518+ view.doc.history.redo(&mut view.doc.state);
503519}
504520
505521// Yank / Paste
@@ -507,9 +523,10 @@ pub fn redo(view: &mut View, _count: usize) {
507523pub fn yank(view: &mut View, _count: usize) {
508524 // TODO: should selections be made end inclusive?
509525 let values = view
526+ .doc
510527 .state
511528 .selection()
512− .fragments(&view.state.doc().slice(..))
529+ .fragments(&view.doc.text().slice(..))
513530 .map(|cow| cow.into_owned())
514531 .collect();
515532
@@ -550,18 +567,18 @@ pub fn paste(view: &mut View, _count: usize) {
550567 let transaction = if linewise {
551568 // paste on the next line
552569 // TODO: can simply take a range + modifier and compute the right pos without ifs
553− let text = view.state.doc();
554− Transaction::change_by_selection(&view.state, |range| {
570+ let text = view.doc.text();
571+ Transaction::change_by_selection(&view.doc.state, |range| {
555572 let line_end = text.line_to_char(text.char_to_line(range.head) + 1);
556573 (line_end, line_end, Some(values.next().unwrap()))
557574 })
558575 } else {
559− Transaction::change_by_selection(&view.state, |range| {
576+ Transaction::change_by_selection(&view.doc.state, |range| {
560577 (range.head + 1, range.head + 1, Some(values.next().unwrap()))
561578 })
562579 };
563580
564− transaction.apply(&mut view.state);
581+ view.doc.apply(&transaction);
565582 append_changes_to_history(view);
566583 }
567584}
@@ -570,9 +587,9 @@ fn get_lines(view: &View) -> Vec<usize> {
570587 let mut lines = Vec::new();
571588
572589 // Get all line numbers
573− for range in view.state.selection.ranges() {
574− let start = view.state.doc.char_to_line(range.from());
575− let end = view.state.doc.char_to_line(range.to());
590+ for range in view.doc.state.selection.ranges() {
591+ let start = view.doc.text().char_to_line(range.from());
592+ let end = view.doc.text().char_to_line(range.to());
576593
577594 for line in start..=end {
578595 lines.push(line)
@@ -590,13 +607,13 @@ pub fn indent(view: &mut View, _count: usize) {
590607 let indent = Tendril::from(" ".repeat(TAB_WIDTH));
591608
592609 let transaction = Transaction::change(
593− &view.state,
610+ &view.doc.state,
594611 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);
596613 (pos, pos, Some(indent.clone()))
597614 }),
598615 );
599− transaction.apply(&mut view.state);
616+ view.doc.apply(&transaction);
600617 append_changes_to_history(view);
601618}
602619
@@ -605,7 +622,7 @@ pub fn unindent(view: &mut View, _count: usize) {
605622 let mut changes = Vec::with_capacity(lines.len());
606623
607624 for line_idx in lines {
608− let line = view.state.doc.line(line_idx);
625+ let line = view.doc.text().line(line_idx);
609626 let mut width = 0;
610627
611628 for ch in line.chars() {
@@ -621,14 +638,14 @@ pub fn unindent(view: &mut View, _count: usize) {
621638 }
622639
623640 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);
625642 changes.push((start, start + width, None))
626643 }
627644 }
628645
629− let transaction = Transaction::change(&view.state, changes.into_iter());
646+ let transaction = Transaction::change(&view.doc.state, changes.into_iter());
630647
631− transaction.apply(&mut view.state);
648+ view.doc.apply(&transaction);
632649 append_changes_to_history(view);
633650}
634651