2020-06-05 09:02:10 +04:00
|
|
|
use crate::graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary, RopeGraphemes};
|
2020-09-07 06:28:52 +04:00
|
|
|
use crate::{Rope, RopeSlice, Selection, SelectionRange};
|
|
|
|
use anyhow::Error;
|
|
|
|
|
|
|
|
use std::path::PathBuf;
|
2020-05-25 08:02:21 +04:00
|
|
|
|
2020-09-14 14:50:46 +04:00
|
|
|
#[derive(Copy, Clone)]
|
2020-09-05 17:01:05 +04:00
|
|
|
pub enum Mode {
|
|
|
|
Normal,
|
|
|
|
Insert,
|
|
|
|
}
|
|
|
|
|
2020-05-25 08:02:21 +04:00
|
|
|
/// A state represents the current editor state of a single buffer.
|
|
|
|
pub struct State {
|
2020-09-07 06:28:52 +04:00
|
|
|
/// Path to file on disk.
|
2020-09-14 14:50:46 +04:00
|
|
|
pub(crate) path: Option<PathBuf>,
|
|
|
|
pub(crate) doc: Rope,
|
|
|
|
pub(crate) selection: Selection,
|
|
|
|
pub(crate) mode: Mode,
|
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,
|
|
|
|
Word,
|
|
|
|
Line,
|
|
|
|
// LineBoundary
|
|
|
|
}
|
|
|
|
|
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-09-07 06:28:52 +04:00
|
|
|
path: None,
|
2020-06-01 12:42:28 +04:00
|
|
|
doc,
|
2020-05-25 08:02:21 +04:00
|
|
|
selection: Selection::single(0, 0),
|
2020-09-05 17:01:05 +04:00
|
|
|
mode: Mode::Normal,
|
2020-05-25 08:02:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 06:28:52 +04:00
|
|
|
pub fn load(path: PathBuf) -> Result<Self, Error> {
|
|
|
|
use std::{env, fs::File, io::BufReader, path::PathBuf};
|
|
|
|
let _current_dir = env::current_dir()?;
|
|
|
|
|
|
|
|
let doc = Rope::from_reader(BufReader::new(File::open(path.clone())?))?;
|
|
|
|
|
|
|
|
// TODO: create if not found
|
|
|
|
|
|
|
|
let mut state = Self::new(doc);
|
|
|
|
state.path = Some(path);
|
|
|
|
|
|
|
|
Ok(state)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: doc/selection accessors
|
2020-05-25 08:02:21 +04:00
|
|
|
|
2020-09-14 14:50:46 +04:00
|
|
|
// TODO: be able to take either Rope or RopeSlice
|
|
|
|
#[inline]
|
|
|
|
pub fn doc(&self) -> &Rope {
|
|
|
|
&self.doc
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn selection(&self) -> &Selection {
|
|
|
|
&self.selection
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn mode(&self) -> Mode {
|
|
|
|
self.mode
|
|
|
|
}
|
|
|
|
|
|
|
|
// pub fn doc<R>(&self, range: R) -> RopeSlice
|
|
|
|
// where
|
|
|
|
// R: std::ops::RangeBounds<usize>,
|
|
|
|
// {
|
|
|
|
// self.doc.slice(range)
|
|
|
|
// }
|
|
|
|
|
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
|
|
|
|
2020-09-09 10:48:25 +04:00
|
|
|
// TODO: move that accepts a boundary matcher fn/list, we keep incrementing until we hit
|
|
|
|
// a boundary
|
|
|
|
|
|
|
|
// TODO: edits, does each keypress trigger a full command? I guess it's adding to the same
|
|
|
|
// transaction
|
|
|
|
// There should be three pieces of the state: current transaction, the original doc, "preview"
|
|
|
|
// of the new state.
|
|
|
|
// 1. apply the newly generated keypress as a transaction
|
|
|
|
// 2. compose onto a ongoing transaction
|
|
|
|
// 3. on insert mode leave, that transaction gets stored into undo history
|
|
|
|
|
2020-06-01 12:42:28 +04:00
|
|
|
pub fn move_pos(
|
|
|
|
&self,
|
|
|
|
pos: usize,
|
|
|
|
dir: Direction,
|
|
|
|
granularity: Granularity,
|
2020-06-07 19:15:39 +04:00
|
|
|
count: usize,
|
2020-06-01 12:42:28 +04:00
|
|
|
) -> usize {
|
2020-09-07 06:28:52 +04:00
|
|
|
let text = &self.doc;
|
2020-06-01 12:42:28 +04:00
|
|
|
match (dir, granularity) {
|
2020-09-04 13:18:59 +04:00
|
|
|
// TODO: clamp movement to line, prevent moving onto \n at the end
|
2020-06-01 12:42:28 +04:00
|
|
|
(Direction::Backward, Granularity::Character) => {
|
2020-06-07 19:15:39 +04:00
|
|
|
nth_prev_grapheme_boundary(&text.slice(..), pos, count)
|
2020-06-01 12:42:28 +04:00
|
|
|
}
|
|
|
|
(Direction::Forward, Granularity::Character) => {
|
2020-06-07 19:15:39 +04:00
|
|
|
nth_next_grapheme_boundary(&text.slice(..), pos, count)
|
2020-06-01 12:42:28 +04:00
|
|
|
}
|
2020-06-07 19:15:39 +04:00
|
|
|
(_, Granularity::Line) => move_vertically(&text.slice(..), dir, pos, count),
|
2020-06-01 12:42:28 +04:00
|
|
|
_ => pos,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
// TODO: move all selections according to normal cursor move semantics by collapsing it
|
|
|
|
// into cursors and moving them vertically
|
|
|
|
|
2020-09-13 18:38:54 +04:00
|
|
|
self.selection.transform(|range| {
|
2020-06-01 12:42:28 +04:00
|
|
|
// let pos = if !range.is_empty() {
|
|
|
|
// // if selection already exists, bump it to the start or end of current select first
|
|
|
|
// if dir == Direction::Backward {
|
|
|
|
// range.from()
|
|
|
|
// } else {
|
|
|
|
// range.to()
|
|
|
|
// }
|
|
|
|
// } else {
|
2020-06-07 19:15:39 +04:00
|
|
|
let pos = self.move_pos(range.head, dir, granularity, count);
|
2020-06-01 12:42:28 +04:00
|
|
|
// };
|
|
|
|
SelectionRange::new(pos, pos)
|
2020-09-13 18:38:54 +04:00
|
|
|
})
|
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 {
|
2020-09-13 18:38:54 +04:00
|
|
|
self.selection.transform(|range| {
|
2020-06-07 19:15:39 +04:00
|
|
|
let pos = self.move_pos(range.head, dir, granularity, count);
|
2020-06-01 12:42:28 +04:00
|
|
|
SelectionRange::new(range.anchor, pos)
|
2020-09-13 18:38:54 +04:00
|
|
|
})
|
2020-06-01 12:42:28 +04:00
|
|
|
}
|
2020-05-25 08:02:21 +04:00
|
|
|
}
|
2020-06-05 09:02:10 +04:00
|
|
|
|
|
|
|
/// Coordinates are a 0-indexed line and column pair.
|
|
|
|
type Coords = (usize, usize); // line, col
|
|
|
|
|
|
|
|
/// Convert a character index to (line, column) coordinates.
|
|
|
|
pub fn coords_at_pos(text: &RopeSlice, pos: usize) -> Coords {
|
|
|
|
let line = text.char_to_line(pos);
|
|
|
|
let line_start = text.line_to_char(line);
|
2020-09-04 13:18:59 +04:00
|
|
|
let col = text.slice(line_start..pos).len_chars();
|
2020-06-05 09:02:10 +04:00
|
|
|
(line, col)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert (line, column) coordinates to a character index.
|
|
|
|
pub fn pos_at_coords(text: &RopeSlice, coords: Coords) -> usize {
|
|
|
|
let (line, col) = coords;
|
|
|
|
let line_start = text.line_to_char(line);
|
|
|
|
nth_next_grapheme_boundary(text, line_start, col)
|
|
|
|
}
|
|
|
|
|
2020-06-07 19:15:39 +04:00
|
|
|
fn move_vertically(text: &RopeSlice, dir: Direction, pos: usize, count: usize) -> usize {
|
2020-06-07 19:08:21 +04:00
|
|
|
let (line, col) = coords_at_pos(text, pos);
|
|
|
|
|
|
|
|
let new_line = match dir {
|
2020-06-07 19:31:11 +04:00
|
|
|
Direction::Backward => line.saturating_sub(count),
|
|
|
|
Direction::Forward => std::cmp::min(line.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
|
|
|
|
|
|
|
let new_col = if new_line_len < col {
|
|
|
|
// TODO: preserve horiz here
|
|
|
|
new_line_len
|
|
|
|
} else {
|
|
|
|
col
|
|
|
|
};
|
|
|
|
|
|
|
|
pos_at_coords(text, (new_line, new_col))
|
|
|
|
}
|
|
|
|
|
2020-06-05 09:02:10 +04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_coords_at_pos() {
|
|
|
|
let text = Rope::from("ḧëḷḷö\nẅöṛḷḋ");
|
|
|
|
assert_eq!(coords_at_pos(&text.slice(..), 0), (0, 0));
|
2020-09-04 13:18:59 +04:00
|
|
|
assert_eq!(coords_at_pos(&text.slice(..), 5), (0, 5)); // position on \n
|
2020-06-05 09:02:10 +04:00
|
|
|
assert_eq!(coords_at_pos(&text.slice(..), 6), (1, 0)); // position on w
|
2020-09-04 13:18:59 +04:00
|
|
|
assert_eq!(coords_at_pos(&text.slice(..), 7), (1, 1)); // position on o
|
|
|
|
assert_eq!(coords_at_pos(&text.slice(..), 10), (1, 4)); // position on d
|
2020-06-05 09:02:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_pos_at_coords() {
|
|
|
|
let text = Rope::from("ḧëḷḷö\nẅöṛḷḋ");
|
|
|
|
assert_eq!(pos_at_coords(&text.slice(..), (0, 0)), 0);
|
|
|
|
assert_eq!(pos_at_coords(&text.slice(..), (0, 5)), 5); // position on \n
|
|
|
|
assert_eq!(pos_at_coords(&text.slice(..), (1, 0)), 6); // position on w
|
2020-09-04 13:18:59 +04:00
|
|
|
assert_eq!(pos_at_coords(&text.slice(..), (1, 1)), 7); // position on o
|
|
|
|
assert_eq!(pos_at_coords(&text.slice(..), (1, 4)), 10); // position on d
|
2020-06-05 09:02:10 +04:00
|
|
|
}
|
2020-06-07 19:08:21 +04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_vertical_move() {
|
|
|
|
let text = Rope::from("abcd\nefg\nwrs");
|
|
|
|
let pos = pos_at_coords(&text.slice(..), (0, 4));
|
|
|
|
let slice = text.slice(..);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
coords_at_pos(&slice, move_vertically(&slice, Direction::Forward, pos, 1)),
|
|
|
|
(1, 2)
|
|
|
|
);
|
|
|
|
}
|
2020-06-05 09:02:10 +04:00
|
|
|
}
|