2020-06-05 09:02:10 +04:00
|
|
|
use crate::graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary, RopeGraphemes};
|
2021-03-18 08:39:34 +04:00
|
|
|
use crate::{coords_at_pos, pos_at_coords, ChangeSet, Position, Range, Rope, RopeSlice, Selection};
|
2020-09-07 06:28:52 +04:00
|
|
|
|
2020-05-25 08:02:21 +04:00
|
|
|
/// A state represents the current editor state of a single buffer.
|
2020-10-23 06:36:46 +04:00
|
|
|
#[derive(Clone)]
|
2020-05-25 08:02:21 +04:00
|
|
|
pub struct State {
|
2020-09-19 18:16:00 +04:00
|
|
|
// TODO: fields should be private but we need to refactor commands.rs first
|
|
|
|
pub doc: Rope,
|
|
|
|
pub selection: Selection,
|
2020-05-25 08:02:21 +04:00
|
|
|
}
|
|
|
|
|
2020-06-01 12:42:28 +04:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
|
|
|
pub enum Direction {
|
|
|
|
Forward,
|
|
|
|
Backward,
|
|
|
|
}
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
|
|
|
pub enum Granularity {
|
|
|
|
Character,
|
|
|
|
Line,
|
|
|
|
}
|
|
|
|
|
2020-05-25 08:02:21 +04:00
|
|
|
impl State {
|
2020-05-28 09:45:44 +04:00
|
|
|
#[must_use]
|
2020-09-07 06:28:52 +04:00
|
|
|
pub fn new(doc: Rope) -> Self {
|
2020-05-25 08:02:21 +04:00
|
|
|
Self {
|
2020-06-01 12:42:28 +04:00
|
|
|
doc,
|
2021-03-14 05:04:03 +04:00
|
|
|
selection: Selection::point(0),
|
2020-05-25 08:02:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 12:47:35 +04:00
|
|
|
// update/transact:
|
|
|
|
// update(desc) => transaction ? transaction.doc() for applied doc
|
|
|
|
// transaction.apply(doc)
|
|
|
|
// doc.transact(fn -> ... end)
|
|
|
|
|
2020-05-25 08:02:21 +04:00
|
|
|
// replaceSelection (transaction that replaces selection)
|
|
|
|
// changeByRange
|
|
|
|
// changes
|
|
|
|
// slice
|
|
|
|
//
|
|
|
|
// getters:
|
|
|
|
// tabSize
|
|
|
|
// indentUnit
|
|
|
|
// languageDataAt()
|
|
|
|
//
|
|
|
|
// config:
|
|
|
|
// indentation
|
|
|
|
// tabSize
|
|
|
|
// lineUnit
|
|
|
|
// syntax
|
|
|
|
// foldable
|
|
|
|
// changeFilter/transactionFilter
|
2020-06-01 12:42:28 +04:00
|
|
|
|
2021-02-12 11:49:24 +04:00
|
|
|
pub fn move_range(
|
2020-06-01 12:42:28 +04:00
|
|
|
&self,
|
2021-02-12 11:49:24 +04:00
|
|
|
range: Range,
|
2020-06-01 12:42:28 +04:00
|
|
|
dir: Direction,
|
|
|
|
granularity: Granularity,
|
2020-06-07 19:15:39 +04:00
|
|
|
count: usize,
|
2021-02-12 11:49:24 +04:00
|
|
|
extend: bool,
|
|
|
|
) -> Range {
|
2021-03-15 09:40:15 +04:00
|
|
|
let text = self.doc.slice(..);
|
2021-02-12 11:49:24 +04:00
|
|
|
let pos = range.head;
|
2021-03-15 09:40:15 +04:00
|
|
|
let line = text.char_to_line(pos);
|
|
|
|
// TODO: we can optimize clamping by passing in RopeSlice limited to current line. that way
|
|
|
|
// we stop calculating past start/end of line.
|
|
|
|
let pos = match (dir, granularity) {
|
2020-06-01 12:42:28 +04:00
|
|
|
(Direction::Backward, Granularity::Character) => {
|
2020-09-28 20:11:17 +04:00
|
|
|
let start = text.line_to_char(line);
|
2021-03-15 09:40:15 +04:00
|
|
|
nth_prev_grapheme_boundary(text, pos, count).max(start)
|
2020-06-01 12:42:28 +04:00
|
|
|
}
|
|
|
|
(Direction::Forward, Granularity::Character) => {
|
2020-09-29 12:49:19 +04:00
|
|
|
// Line end is pos at the start of next line - 1
|
|
|
|
// subtract another 1 because the line ends with \n
|
|
|
|
let end = text.line_to_char(line + 1).saturating_sub(2);
|
2021-03-15 09:40:15 +04:00
|
|
|
nth_next_grapheme_boundary(text, pos, count).min(end)
|
2020-09-24 14:16:35 +04:00
|
|
|
}
|
2021-03-15 09:40:15 +04:00
|
|
|
(_, Granularity::Line) => return move_vertically(text, dir, range, count, extend),
|
|
|
|
};
|
|
|
|
Range::new(if extend { range.anchor } else { pos }, pos)
|
2020-06-01 12:42:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn move_selection(
|
|
|
|
&self,
|
|
|
|
dir: Direction,
|
|
|
|
granularity: Granularity,
|
2020-06-07 19:15:39 +04:00
|
|
|
count: usize,
|
2020-06-01 12:42:28 +04:00
|
|
|
) -> Selection {
|
2021-02-12 11:49:24 +04:00
|
|
|
self.selection
|
|
|
|
.transform(|range| self.move_range(range, dir, granularity, count, false))
|
2020-06-01 12:42:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn extend_selection(
|
|
|
|
&self,
|
|
|
|
dir: Direction,
|
|
|
|
granularity: Granularity,
|
2020-06-07 19:15:39 +04:00
|
|
|
count: usize,
|
2020-06-01 12:42:28 +04:00
|
|
|
) -> Selection {
|
2021-02-12 11:49:24 +04:00
|
|
|
self.selection
|
|
|
|
.transform(|range| self.move_range(range, dir, granularity, count, true))
|
2020-06-01 12:42:28 +04:00
|
|
|
}
|
2020-05-25 08:02:21 +04:00
|
|
|
}
|
2020-06-05 09:02:10 +04:00
|
|
|
|
2021-02-12 11:49:24 +04:00
|
|
|
fn move_vertically(
|
2021-02-18 13:34:22 +04:00
|
|
|
text: RopeSlice,
|
2021-02-12 11:49:24 +04:00
|
|
|
dir: Direction,
|
|
|
|
range: Range,
|
|
|
|
count: usize,
|
|
|
|
extend: bool,
|
|
|
|
) -> Range {
|
|
|
|
let Position { row, col } = coords_at_pos(text, range.head);
|
|
|
|
|
|
|
|
let horiz = range.horiz.unwrap_or(col as u32);
|
2020-06-07 19:08:21 +04:00
|
|
|
|
|
|
|
let new_line = match dir {
|
2020-09-17 09:57:49 +04:00
|
|
|
Direction::Backward => row.saturating_sub(count),
|
|
|
|
Direction::Forward => std::cmp::min(row.saturating_add(count), text.len_lines() - 1),
|
2020-06-07 19:08:21 +04:00
|
|
|
};
|
|
|
|
|
2020-09-04 13:18:59 +04:00
|
|
|
// convert to 0-indexed, subtract another 1 because len_chars() counts \n
|
|
|
|
let new_line_len = text.line(new_line).len_chars().saturating_sub(2);
|
2020-06-07 19:08:21 +04:00
|
|
|
|
2021-02-12 11:49:24 +04:00
|
|
|
let new_col = std::cmp::min(horiz as usize, new_line_len);
|
|
|
|
|
|
|
|
let pos = pos_at_coords(text, Position::new(new_line, new_col));
|
2020-06-07 19:08:21 +04:00
|
|
|
|
2021-02-12 11:49:24 +04:00
|
|
|
let mut range = Range::new(if extend { range.anchor } else { pos }, pos);
|
|
|
|
range.horiz = Some(horiz);
|
|
|
|
range
|
2020-06-07 19:08:21 +04:00
|
|
|
}
|
|
|
|
|
2020-06-05 09:02:10 +04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
2020-06-07 19:08:21 +04:00
|
|
|
#[test]
|
|
|
|
fn test_vertical_move() {
|
|
|
|
let text = Rope::from("abcd\nefg\nwrs");
|
|
|
|
let slice = text.slice(..);
|
2021-02-18 13:34:22 +04:00
|
|
|
let pos = pos_at_coords(slice, (0, 4).into());
|
2020-06-07 19:08:21 +04:00
|
|
|
|
2021-02-12 11:49:24 +04:00
|
|
|
let range = Range::new(pos, pos);
|
2020-06-07 19:08:21 +04:00
|
|
|
assert_eq!(
|
2021-02-12 11:49:24 +04:00
|
|
|
coords_at_pos(
|
2021-02-18 13:34:22 +04:00
|
|
|
slice,
|
|
|
|
move_vertically(slice, Direction::Forward, range, 1, false).head
|
2021-02-12 11:49:24 +04:00
|
|
|
),
|
2020-09-17 09:57:49 +04:00
|
|
|
(1, 2).into()
|
2020-06-07 19:08:21 +04:00
|
|
|
);
|
|
|
|
}
|
2020-06-05 09:02:10 +04:00
|
|
|
}
|