on master help sign in
Add a command module.
e98cdebBlaž Hrastnik
helix-core/src/commands.rsnew file+45
@@ -0,0 +1,45 @@
1+use crate::state::{Direction, Granularity, State};
2+
3+/// A command is a function that takes the current state and a count, and does a side-effect on the
4+/// state (usually by creating and applying a transaction).
5+type Command = fn(state: &mut State, count: usize);
6+
7+fn move_char_left(state: &mut State, count: usize) {
8+ // TODO: use a transaction
9+ state.selection = state.move_selection(
10+ state.selection,
11+ Direction::Backward,
12+ Granularity::Character,
13+ count,
14+ );
15+}
16+
17+fn move_char_right(state: &mut State, count: usize) {
18+ // TODO: use a transaction
19+ state.selection = state.move_selection(
20+ state.selection,
21+ Direction::Forward,
22+ Granularity::Character,
23+ count,
24+ );
25+}
26+
27+fn move_line_up(state: &mut State, count: usize) {
28+ // TODO: use a transaction
29+ state.selection = state.move_selection(
30+ state.selection,
31+ Direction::Backward,
32+ Granularity::Line,
33+ count,
34+ );
35+}
36+
37+fn move_line_down(state: &mut State, count: usize) {
38+ // TODO: use a transaction
39+ state.selection = state.move_selection(
40+ state.selection,
41+ Direction::Forward,
42+ Granularity::Line,
43+ count,
44+ );
45+}
helix-core/src/lib.rs+1
@@ -1,5 +1,6 @@
1#![allow(unused)]
1#![allow(unused)]
2mod buffer;
2mod buffer;
3+pub mod commands;
3mod graphemes;
4mod graphemes;
4mod selection;
5mod selection;
5mod state;
6mod state;
helix-core/src/state.rs+11−15
@@ -3,8 +3,8 @@ use crate::{Buffer, Rope, RopeSlice, Selection, SelectionRange};
3
3
4/// A state represents the current editor state of a single buffer.
4/// A state represents the current editor state of a single buffer.
5pub struct State {
5pub struct State {
6− doc: Buffer,
6+ pub(crate) doc: Buffer,
7− selection: Selection,
7+ pub(crate) selection: Selection,
8}
8}
9
9
10#[derive(Copy, Clone, PartialEq, Eq)]
10#[derive(Copy, Clone, PartialEq, Eq)]
@@ -59,17 +59,17 @@ impl State {
59 pos: usize,
59 pos: usize,
60 dir: Direction,
60 dir: Direction,
61 granularity: Granularity,
61 granularity: Granularity,
62− n: usize,
62+ count: usize,
63 ) -> usize {
63 ) -> usize {
64 let text = &self.doc.contents;
64 let text = &self.doc.contents;
65 match (dir, granularity) {
65 match (dir, granularity) {
66 (Direction::Backward, Granularity::Character) => {
66 (Direction::Backward, Granularity::Character) => {
67− nth_prev_grapheme_boundary(&text.slice(..), pos, n)
67+ nth_prev_grapheme_boundary(&text.slice(..), pos, count)
68 }
68 }
69 (Direction::Forward, Granularity::Character) => {
69 (Direction::Forward, Granularity::Character) => {
70− nth_next_grapheme_boundary(&text.slice(..), pos, n)
70+ nth_next_grapheme_boundary(&text.slice(..), pos, count)
71 }
71 }
72− (_, Granularity::Line) => move_vertically(&text.slice(..), dir, pos, n),
72+ (_, Granularity::Line) => move_vertically(&text.slice(..), dir, pos, count),
73 _ => pos,
73 _ => pos,
74 }
74 }
75 }
75 }
@@ -79,7 +79,7 @@ impl State {
79 sel: Selection,
79 sel: Selection,
80 dir: Direction,
80 dir: Direction,
81 granularity: Granularity,
81 granularity: Granularity,
82− // TODO: n
82+ count: usize,
83 ) -> Selection {
83 ) -> Selection {
84 // TODO: move all selections according to normal cursor move semantics by collapsing it
84 // TODO: move all selections according to normal cursor move semantics by collapsing it
85 // into cursors and moving them vertically
85 // into cursors and moving them vertically
@@ -93,7 +93,7 @@ impl State {
93 // range.to()
93 // range.to()
94 // }
94 // }
95 // } else {
95 // } else {
96− let pos = self.move_pos(range.head, dir, granularity, 1);
96+ let pos = self.move_pos(range.head, dir, granularity, count);
97 // };
97 // };
98 SelectionRange::new(pos, pos)
98 SelectionRange::new(pos, pos)
99 });
99 });
@@ -107,10 +107,10 @@ impl State {
107 sel: Selection,
107 sel: Selection,
108 dir: Direction,
108 dir: Direction,
109 granularity: Granularity,
109 granularity: Granularity,
110− n: usize,
110+ count: usize,
111 ) -> Selection {
111 ) -> Selection {
112 let ranges = sel.ranges.into_iter().map(|range| {
112 let ranges = sel.ranges.into_iter().map(|range| {
113− let pos = self.move_pos(range.head, dir, granularity, n);
113+ let pos = self.move_pos(range.head, dir, granularity, count);
114 SelectionRange::new(range.anchor, pos)
114 SelectionRange::new(range.anchor, pos)
115 });
115 });
116
116
@@ -138,7 +138,7 @@ pub fn pos_at_coords(text: &RopeSlice, coords: Coords) -> usize {
138 nth_next_grapheme_boundary(text, line_start, col)
138 nth_next_grapheme_boundary(text, line_start, col)
139}
139}
140
140
141−fn move_vertically(text: &RopeSlice, dir: Direction, pos: usize, n: usize) -> usize {
141+fn move_vertically(text: &RopeSlice, dir: Direction, pos: usize, count: usize) -> usize {
142 let (line, col) = coords_at_pos(text, pos);
142 let (line, col) = coords_at_pos(text, pos);
143
143
144 let new_line = match dir {
144 let new_line = match dir {
@@ -159,10 +159,6 @@ fn move_vertically(text: &RopeSlice, dir: Direction, pos: usize, n: usize) -> us
159 pos_at_coords(text, (new_line, new_col))
159 pos_at_coords(text, (new_line, new_col))
160}
160}
161
161
162−/// A command is a function that takes the current state and a count, and does a side-effect on the
163−/// state (usually by creating and applying a transaction).
164−type Command = fn(state: &mut State, count: usize) -> bool;
165−
166#[cfg(test)]
162#[cfg(test)]
167mod test {
163mod test {
168 use super::*;
164 use super::*;