on master help sign in
Add a command module.
commite98cdebf1eb6ea5233c0d4cb0ae84c251fdae987 authorBlaž Hrastnik <blaz@mxxn.io> dateMon 8 Jun 2020, 00:15 (6 years ago) parent195aad4
3 files changed+57−15
helix-core/src/commands.rs+45 helix-core/src/lib.rs+1 helix-core/src/state.rs+11−15
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 @@
11#![allow(unused)]
22mod buffer;
3+pub mod commands;
34mod graphemes;
45mod selection;
56mod state;
helix-core/src/state.rs+11−15
@@ -3,8 +3,8 @@ use crate::{Buffer, Rope, RopeSlice, Selection, SelectionRange};
33
44/// A state represents the current editor state of a single buffer.
55pub struct State {
6− doc: Buffer,
7− selection: Selection,
6+ pub(crate) doc: Buffer,
7+ pub(crate) selection: Selection,
88}
99
1010#[derive(Copy, Clone, PartialEq, Eq)]
@@ -59,17 +59,17 @@ impl State {
5959 pos: usize,
6060 dir: Direction,
6161 granularity: Granularity,
62− n: usize,
62+ count: usize,
6363 ) -> usize {
6464 let text = &self.doc.contents;
6565 match (dir, granularity) {
6666 (Direction::Backward, Granularity::Character) => {
67− nth_prev_grapheme_boundary(&text.slice(..), pos, n)
67+ nth_prev_grapheme_boundary(&text.slice(..), pos, count)
6868 }
6969 (Direction::Forward, Granularity::Character) => {
70− nth_next_grapheme_boundary(&text.slice(..), pos, n)
70+ nth_next_grapheme_boundary(&text.slice(..), pos, count)
7171 }
72− (_, Granularity::Line) => move_vertically(&text.slice(..), dir, pos, n),
72+ (_, Granularity::Line) => move_vertically(&text.slice(..), dir, pos, count),
7373 _ => pos,
7474 }
7575 }
@@ -79,7 +79,7 @@ impl State {
7979 sel: Selection,
8080 dir: Direction,
8181 granularity: Granularity,
82− // TODO: n
82+ count: usize,
8383 ) -> Selection {
8484 // TODO: move all selections according to normal cursor move semantics by collapsing it
8585 // into cursors and moving them vertically
@@ -93,7 +93,7 @@ impl State {
9393 // range.to()
9494 // }
9595 // } else {
96− let pos = self.move_pos(range.head, dir, granularity, 1);
96+ let pos = self.move_pos(range.head, dir, granularity, count);
9797 // };
9898 SelectionRange::new(pos, pos)
9999 });
@@ -107,10 +107,10 @@ impl State {
107107 sel: Selection,
108108 dir: Direction,
109109 granularity: Granularity,
110− n: usize,
110+ count: usize,
111111 ) -> Selection {
112112 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);
114114 SelectionRange::new(range.anchor, pos)
115115 });
116116
@@ -138,7 +138,7 @@ pub fn pos_at_coords(text: &RopeSlice, coords: Coords) -> usize {
138138 nth_next_grapheme_boundary(text, line_start, col)
139139}
140140
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 {
142142 let (line, col) = coords_at_pos(text, pos);
143143
144144 let new_line = match dir {
@@ -159,10 +159,6 @@ fn move_vertically(text: &RopeSlice, dir: Direction, pos: usize, n: usize) -> us
159159 pos_at_coords(text, (new_line, new_col))
160160}
161161
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−
166162#[cfg(test)]
167163mod test {
168164 use super::*;