@@ -1,32 +1,42 @@
1−use crate::graphemes;
2−use crate::selection::{Range, Selection};
3−use crate::state::{Direction, Granularity, Mode, State};
4−use crate::transaction::{ChangeSet, Transaction};
5−use crate::Tendril;
1+use helix_core::{
2+ graphemes,
3+ state::{Direction, Granularity, Mode, State},
4+ ChangeSet, Range, Selection, Tendril, Transaction,
5+};
6+
7+use crate::editor::View;
68
79/// A command is a function that takes the current state and a count, and does a side-effect on the
810/// state (usually by creating and applying a transaction).
9−pub type Command = fn(state: &mut State, count: usize);
11+pub type Command = fn(view: &mut View, count: usize);
1012
11−pub fn move_char_left(state: &mut State, count: usize) {
13+pub fn move_char_left(view: &mut View, count: usize) {
1214 // TODO: use a transaction
13− let selection = state.move_selection(Direction::Backward, Granularity::Character, count);
14− state.selection = selection;
15+ let selection = view
16+ .state
17+ .move_selection(Direction::Backward, Granularity::Character, count);
18+ view.state.selection = selection;
1519}
1620
17−pub fn move_char_right(state: &mut State, count: usize) {
21+pub fn move_char_right(view: &mut View, count: usize) {
1822 // TODO: use a transaction
19− state.selection = state.move_selection(Direction::Forward, Granularity::Character, count);
23+ view.state.selection =
24+ view.state
25+ .move_selection(Direction::Forward, Granularity::Character, count);
2026}
2127
22−pub fn move_line_up(state: &mut State, count: usize) {
28+pub fn move_line_up(view: &mut View, count: usize) {
2329 // TODO: use a transaction
24− state.selection = state.move_selection(Direction::Backward, Granularity::Line, count);
30+ view.state.selection = view
31+ .state
32+ .move_selection(Direction::Backward, Granularity::Line, count);
2533}
2634
27−pub fn move_line_down(state: &mut State, count: usize) {
35+pub fn move_line_down(view: &mut View, count: usize) {
2836 // TODO: use a transaction
29− state.selection = state.move_selection(Direction::Forward, Granularity::Line, count);
37+ view.state.selection = view
38+ .state
39+ .move_selection(Direction::Forward, Granularity::Line, count);
3040}
3141
3242// avoid select by default by having a visual mode switch that makes movements into selects
@@ -37,21 +47,22 @@ pub fn move_line_down(state: &mut State, count: usize) {
3747// lastly, if it was append mode we shift cursor by 1?
3848
3949// inserts at the start of each selection
40−pub fn insert_mode(state: &mut State, _count: usize) {
41− state.mode = Mode::Insert;
50+pub fn insert_mode(view: &mut View, _count: usize) {
51+ view.state.mode = Mode::Insert;
4252
43− state.selection = state
53+ view.state.selection = view
54+ .state
4455 .selection
4556 .transform(|range| Range::new(range.to(), range.from()))
4657}
4758
4859// inserts at the end of each selection
49−pub fn append_mode(state: &mut State, _count: usize) {
50− state.mode = Mode::Insert;
60+pub fn append_mode(view: &mut View, _count: usize) {
61+ view.state.mode = Mode::Insert;
5162
5263 // TODO: as transaction
53− let text = &state.doc.slice(..);
54− state.selection = state.selection.transform(|range| {
64+ let text = &view.state.doc.slice(..);
65+ view.state.selection = view.state.selection.transform(|range| {
5566 // TODO: to() + next char
5667 Range::new(
5768 range.from(),
@@ -78,63 +89,63 @@ fn selection_lines(state: &State) -> Vec<usize> {
7889}
7990
8091// I inserts at the start of each line with a selection
81−pub fn prepend_to_line(state: &mut State, _count: usize) {
82− state.mode = Mode::Insert;
92+pub fn prepend_to_line(view: &mut View, _count: usize) {
93+ view.state.mode = Mode::Insert;
8394
84− let lines = selection_lines(state);
95+ let lines = selection_lines(&view.state);
8596
8697 let positions = lines
8798 .into_iter()
8899 .map(|index| {
89100 // adjust all positions to the start of the line.
90− state.doc.line_to_char(index)
101+ view.state.doc.line_to_char(index)
91102 })
92103 .map(|pos| Range::new(pos, pos));
93104
94105 let selection = Selection::new(positions.collect(), 0);
95106
96− let transaction = Transaction::new(state).with_selection(selection);
107+ let transaction = Transaction::new(&mut view.state).with_selection(selection);
97108
98− transaction.apply(state);
109+ transaction.apply(&mut view.state);
99110 // TODO: need to store into history if successful
100111}
101112
102113// A inserts at the end of each line with a selection
103−pub fn append_to_line(state: &mut State, _count: usize) {
104− state.mode = Mode::Insert;
114+pub fn append_to_line(view: &mut View, _count: usize) {
115+ view.state.mode = Mode::Insert;
105116
106− let lines = selection_lines(state);
117+ let lines = selection_lines(&view.state);
107118
108119 let positions = lines
109120 .into_iter()
110121 .map(|index| {
111122 // adjust all positions to the end of the line.
112− let line = state.doc.line(index);
113− let line_start = state.doc.line_to_char(index);
123+ let line = view.state.doc.line(index);
124+ let line_start = view.state.doc.line_to_char(index);
114125 line_start + line.len_chars() - 1
115126 })
116127 .map(|pos| Range::new(pos, pos));
117128
118129 let selection = Selection::new(positions.collect(), 0);
119130
120− let transaction = Transaction::new(state).with_selection(selection);
131+ let transaction = Transaction::new(&mut view.state).with_selection(selection);
121132
122− transaction.apply(state);
133+ transaction.apply(&mut view.state);
123134 // TODO: need to store into history if successful
124135}
125136
126137// o inserts a new line after each line with a selection
127−pub fn open_below(state: &mut State, _count: usize) {
128− state.mode = Mode::Insert;
138+pub fn open_below(view: &mut View, _count: usize) {
139+ view.state.mode = Mode::Insert;
129140
130− let lines = selection_lines(state);
141+ let lines = selection_lines(&view.state);
131142
132143 let positions: Vec<_> = lines
133144 .into_iter()
134145 .map(|index| {
135146 // adjust all positions to the end of the line.
136− let line = state.doc.line(index);
137− let line_start = state.doc.line_to_char(index);
147+ let line = view.state.doc.line(index);
148+ let line_start = view.state.doc.line_to_char(index);
138149 line_start + line.len_chars()
139150 })
140151 .collect();
@@ -155,51 +166,51 @@ pub fn open_below(state: &mut State, _count: usize) {
155166 0,
156167 );
157168
158− let transaction = Transaction::change(state, changes).with_selection(selection);
169+ let transaction = Transaction::change(&view.state, changes).with_selection(selection);
159170
160− transaction.apply(state);
171+ transaction.apply(&mut view.state);
161172 // TODO: need to store into history if successful
162173}
163174
164175// O inserts a new line before each line with a selection
165176
166−pub fn normal_mode(state: &mut State, _count: usize) {
177+pub fn normal_mode(view: &mut View, _count: usize) {
167178 // TODO: if leaving append mode, move cursor back by 1
168− state.mode = Mode::Normal;
179+ view.state.mode = Mode::Normal;
169180}
170181
171182// TODO: insert means add text just before cursor, on exit we should be on the last letter.
172−pub fn insert_char(state: &mut State, c: char) {
183+pub fn insert_char(view: &mut View, c: char) {
173184 let c = Tendril::from_char(c);
174− let transaction = Transaction::insert(&state, c);
185+ let transaction = Transaction::insert(&view.state, c);
175186
176− transaction.apply(state);
187+ transaction.apply(&mut view.state);
177188 // TODO: need to store into history if successful
178189}
179190
180191// TODO: handle indent-aware delete
181−pub fn delete_char_backward(state: &mut State, count: usize) {
182− let text = &state.doc.slice(..);
183− let transaction = Transaction::change_by_selection(state, |range| {
192+pub fn delete_char_backward(view: &mut View, count: usize) {
193+ let text = &view.state.doc.slice(..);
194+ let transaction = Transaction::change_by_selection(&view.state, |range| {
184195 (
185196 graphemes::nth_prev_grapheme_boundary(text, range.head, count),
186197 range.head,
187198 None,
188199 )
189200 });
190− transaction.apply(state);
201+ transaction.apply(&mut view.state);
191202 // TODO: need to store into history if successful
192203}
193204
194−pub fn delete_char_forward(state: &mut State, count: usize) {
195− let text = &state.doc.slice(..);
196− let transaction = Transaction::change_by_selection(state, |range| {
205+pub fn delete_char_forward(view: &mut View, count: usize) {
206+ let text = &view.state.doc.slice(..);
207+ let transaction = Transaction::change_by_selection(&view.state, |range| {
197208 (
198209 graphemes::nth_next_grapheme_boundary(text, range.head, count),
199210 range.head,
200211 None,
201212 )
202213 });
203− transaction.apply(state);
214+ transaction.apply(&mut view.state);
204215 // TODO: need to store into history if successful
205216}