2020-06-05 09:02:10 +04:00
|
|
|
use crate::graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary, RopeGraphemes};
|
2020-09-22 13:23:48 +04:00
|
|
|
use crate::syntax::LOADER;
|
2020-10-20 08:58:34 +04:00
|
|
|
use crate::{ChangeSet, Diagnostic, Position, Range, Rope, RopeSlice, Selection, Syntax};
|
2020-09-07 06:28:52 +04:00
|
|
|
use anyhow::Error;
|
|
|
|
|
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,
|
2020-05-25 08:02:21 +04:00
|
|
|
selection: Selection::single(0, 0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 06:28:52 +04:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
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 {
|
2020-09-07 06:28:52 +04:00
|
|
|
let text = &self.doc;
|
2021-02-12 11:49:24 +04:00
|
|
|
let pos = range.head;
|
2020-06-01 12:42:28 +04:00
|
|
|
match (dir, granularity) {
|
|
|
|
(Direction::Backward, Granularity::Character) => {
|
2020-09-28 20:11:17 +04:00
|
|
|
// Clamp to line
|
|
|
|
let line = text.char_to_line(pos);
|
|
|
|
let start = text.line_to_char(line);
|
2021-02-12 11:49:24 +04:00
|
|
|
let pos = std::cmp::max(
|
2021-02-18 13:34:22 +04:00
|
|
|
nth_prev_grapheme_boundary(text.slice(..), pos, count),
|
2020-09-28 20:11:17 +04:00
|
|
|
start,
|
2021-02-12 11:49:24 +04:00
|
|
|
);
|
|
|
|
Range::new(if extend { range.anchor } else { pos }, pos)
|
2020-06-01 12:42:28 +04:00
|
|
|
}
|
|
|
|
(Direction::Forward, Granularity::Character) => {
|
2020-09-28 20:11:17 +04:00
|
|
|
// Clamp to line
|
|
|
|
let line = text.char_to_line(pos);
|
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-02-12 11:49:24 +04:00
|
|
|
let pos =
|
2021-02-18 13:34:22 +04:00
|
|
|
std::cmp::min(nth_next_grapheme_boundary(text.slice(..), pos, count), end);
|
2021-02-12 11:49:24 +04:00
|
|
|
Range::new(if extend { range.anchor } else { pos }, pos)
|
2020-09-24 14:16:35 +04:00
|
|
|
}
|
2021-02-18 13:34:22 +04:00
|
|
|
(_, Granularity::Line) => move_vertically(text.slice(..), dir, range, count, extend),
|
2020-06-01 12:42:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-24 12:29:28 +04:00
|
|
|
pub fn move_next_word_start(slice: RopeSlice, mut pos: usize, count: usize) -> usize {
|
2020-09-24 14:16:35 +04:00
|
|
|
// TODO: confirm it's fine without using graphemes, I think it should be
|
2021-02-24 12:29:28 +04:00
|
|
|
for _ in 0..count {
|
2021-03-11 05:44:38 +04:00
|
|
|
// TODO: if end return end
|
|
|
|
|
2021-02-24 12:29:28 +04:00
|
|
|
let ch = slice.char(pos);
|
|
|
|
let next = slice.char(pos.saturating_add(1));
|
|
|
|
if categorize(ch) != categorize(next) {
|
|
|
|
pos += 1;
|
|
|
|
}
|
2020-09-24 14:16:35 +04:00
|
|
|
|
2021-02-24 12:29:28 +04:00
|
|
|
// refetch
|
|
|
|
let ch = slice.char(pos);
|
2020-09-24 14:16:35 +04:00
|
|
|
|
2021-02-24 12:29:28 +04:00
|
|
|
if is_word(ch) {
|
|
|
|
skip_over_next(slice, &mut pos, is_word);
|
|
|
|
} else if ch.is_ascii_punctuation() {
|
|
|
|
skip_over_next(slice, &mut pos, |ch| ch.is_ascii_punctuation());
|
|
|
|
}
|
2020-09-24 14:16:35 +04:00
|
|
|
|
2021-02-24 12:29:28 +04:00
|
|
|
// TODO: don't include newline?
|
|
|
|
skip_over_next(slice, &mut pos, |ch| ch.is_ascii_whitespace());
|
|
|
|
}
|
2020-09-24 14:16:35 +04:00
|
|
|
|
|
|
|
pos
|
|
|
|
}
|
|
|
|
|
2021-02-24 12:29:28 +04:00
|
|
|
pub fn move_prev_word_start(slice: RopeSlice, mut pos: usize, count: usize) -> usize {
|
2020-09-24 14:16:35 +04:00
|
|
|
// TODO: confirm it's fine without using graphemes, I think it should be
|
2021-02-24 12:29:28 +04:00
|
|
|
for _ in 0..count {
|
2021-03-11 05:44:38 +04:00
|
|
|
if pos == 0 {
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2021-02-24 12:29:28 +04:00
|
|
|
let ch = slice.char(pos);
|
2021-03-11 05:44:38 +04:00
|
|
|
let prev = slice.char(pos - 1);
|
2020-09-24 14:16:35 +04:00
|
|
|
|
2021-02-24 12:29:28 +04:00
|
|
|
if categorize(ch) != categorize(prev) {
|
|
|
|
pos -= 1;
|
|
|
|
}
|
2020-09-24 14:16:35 +04:00
|
|
|
|
2021-02-24 12:29:28 +04:00
|
|
|
// TODO: skip while eol
|
2020-09-24 14:16:35 +04:00
|
|
|
|
2021-02-24 12:29:28 +04:00
|
|
|
// TODO: don't include newline?
|
|
|
|
skip_over_prev(slice, &mut pos, |ch| ch.is_ascii_whitespace());
|
2020-09-24 14:16:35 +04:00
|
|
|
|
2021-02-24 12:29:28 +04:00
|
|
|
// refetch
|
|
|
|
let ch = slice.char(pos);
|
2020-09-24 14:16:35 +04:00
|
|
|
|
2021-02-24 12:29:28 +04:00
|
|
|
if is_word(ch) {
|
|
|
|
skip_over_prev(slice, &mut pos, is_word);
|
|
|
|
} else if ch.is_ascii_punctuation() {
|
|
|
|
skip_over_prev(slice, &mut pos, |ch| ch.is_ascii_punctuation());
|
|
|
|
}
|
|
|
|
pos = pos.saturating_add(1)
|
2020-09-24 14:16:35 +04:00
|
|
|
}
|
|
|
|
|
2021-02-24 12:29:28 +04:00
|
|
|
pos
|
2020-09-24 14:16:35 +04:00
|
|
|
}
|
|
|
|
|
2021-02-24 12:29:28 +04:00
|
|
|
pub fn move_next_word_end(slice: RopeSlice, mut pos: usize, count: usize) -> usize {
|
|
|
|
for _ in 0..count {
|
2021-03-11 05:44:38 +04:00
|
|
|
// TODO: if end return end
|
|
|
|
|
2021-02-24 12:29:28 +04:00
|
|
|
// TODO: confirm it's fine without using graphemes, I think it should be
|
|
|
|
let ch = slice.char(pos);
|
|
|
|
let next = slice.char(pos.saturating_add(1));
|
|
|
|
if categorize(ch) != categorize(next) {
|
|
|
|
pos += 1;
|
|
|
|
}
|
2020-09-24 14:16:35 +04:00
|
|
|
|
2021-02-24 12:29:28 +04:00
|
|
|
// TODO: don't include newline?
|
|
|
|
skip_over_next(slice, &mut pos, |ch| ch.is_ascii_whitespace());
|
2020-09-24 14:16:35 +04:00
|
|
|
|
2021-02-24 12:29:28 +04:00
|
|
|
// refetch
|
|
|
|
let ch = slice.char(pos);
|
2020-09-24 14:16:35 +04:00
|
|
|
|
2021-02-24 12:29:28 +04:00
|
|
|
if is_word(ch) {
|
|
|
|
skip_over_next(slice, &mut pos, is_word);
|
|
|
|
} else if ch.is_ascii_punctuation() {
|
|
|
|
skip_over_next(slice, &mut pos, |ch| ch.is_ascii_punctuation());
|
|
|
|
}
|
|
|
|
pos = pos.saturating_sub(1)
|
2020-09-24 14:16:35 +04:00
|
|
|
}
|
|
|
|
|
2021-02-24 12:29:28 +04:00
|
|
|
pos
|
2020-09-24 14:16:35 +04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
/// Convert a character index to (line, column) coordinates.
|
2021-02-18 13:34:22 +04:00
|
|
|
pub fn coords_at_pos(text: RopeSlice, pos: usize) -> Position {
|
2020-06-05 09:02:10 +04:00
|
|
|
let line = text.char_to_line(pos);
|
|
|
|
let line_start = text.line_to_char(line);
|
2021-02-24 11:08:17 +04:00
|
|
|
let col = RopeGraphemes::new(text.slice(line_start..pos)).count();
|
2020-09-17 09:57:49 +04:00
|
|
|
Position::new(line, col)
|
2020-06-05 09:02:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert (line, column) coordinates to a character index.
|
2021-02-18 13:34:22 +04:00
|
|
|
pub fn pos_at_coords(text: RopeSlice, coords: Position) -> usize {
|
2020-09-17 09:57:49 +04:00
|
|
|
let Position { row, col } = coords;
|
|
|
|
let line_start = text.line_to_char(row);
|
2021-02-24 11:08:17 +04:00
|
|
|
// line_start + col
|
2020-06-05 09:02:10 +04:00
|
|
|
nth_next_grapheme_boundary(text, line_start, col)
|
|
|
|
}
|
|
|
|
|
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-09-24 14:16:35 +04:00
|
|
|
// used for by-word movement
|
|
|
|
|
|
|
|
fn is_word(ch: char) -> bool {
|
|
|
|
ch.is_alphanumeric() || ch == '_'
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
|
|
enum Category {
|
|
|
|
Whitespace,
|
2021-03-01 12:37:31 +04:00
|
|
|
Eol,
|
2020-09-24 14:16:35 +04:00
|
|
|
Word,
|
|
|
|
Punctuation,
|
|
|
|
}
|
|
|
|
fn categorize(ch: char) -> Category {
|
|
|
|
if ch == '\n' {
|
2021-03-01 12:37:31 +04:00
|
|
|
Category::Eol
|
2020-09-24 14:16:35 +04:00
|
|
|
} else if ch.is_ascii_whitespace() {
|
|
|
|
Category::Whitespace
|
|
|
|
} else if ch.is_ascii_punctuation() {
|
|
|
|
Category::Punctuation
|
|
|
|
} else if ch.is_ascii_alphanumeric() {
|
|
|
|
Category::Word
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 12:21:59 +04:00
|
|
|
#[inline]
|
|
|
|
pub fn skip_over_next<F>(slice: RopeSlice, pos: &mut usize, fun: F)
|
2020-09-24 14:16:35 +04:00
|
|
|
where
|
|
|
|
F: Fn(char) -> bool,
|
|
|
|
{
|
|
|
|
let mut chars = slice.chars_at(*pos);
|
|
|
|
|
2020-09-29 13:02:27 +04:00
|
|
|
for ch in chars {
|
2020-09-24 14:16:35 +04:00
|
|
|
if !fun(ch) {
|
|
|
|
break;
|
|
|
|
}
|
2021-03-11 05:44:38 +04:00
|
|
|
*pos += 1; // TODO: can go 1 over end of doc
|
2020-09-24 14:16:35 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 12:21:59 +04:00
|
|
|
#[inline]
|
|
|
|
pub fn skip_over_prev<F>(slice: RopeSlice, pos: &mut usize, fun: F)
|
2020-09-24 14:16:35 +04:00
|
|
|
where
|
|
|
|
F: Fn(char) -> bool,
|
|
|
|
{
|
|
|
|
// need to +1 so that prev() includes current char
|
|
|
|
let mut chars = slice.chars_at(*pos + 1);
|
|
|
|
|
|
|
|
while let Some(ch) = chars.prev() {
|
|
|
|
if !fun(ch) {
|
|
|
|
break;
|
|
|
|
}
|
2021-03-11 05:44:38 +04:00
|
|
|
*pos -= pos.saturating_sub(1);
|
2020-09-24 14:16:35 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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ẅöṛḷḋ");
|
2021-02-18 13:34:22 +04:00
|
|
|
let slice = text.slice(..);
|
2021-02-24 11:08:17 +04:00
|
|
|
// assert_eq!(coords_at_pos(slice, 0), (0, 0).into());
|
|
|
|
// assert_eq!(coords_at_pos(slice, 5), (0, 5).into()); // position on \n
|
|
|
|
// assert_eq!(coords_at_pos(slice, 6), (1, 0).into()); // position on w
|
|
|
|
// assert_eq!(coords_at_pos(slice, 7), (1, 1).into()); // position on o
|
|
|
|
// assert_eq!(coords_at_pos(slice, 10), (1, 4).into()); // position on d
|
|
|
|
|
|
|
|
// test with grapheme clusters
|
|
|
|
let text = Rope::from("a̐éö̲\r\n");
|
|
|
|
let slice = text.slice(..);
|
2021-02-18 13:34:22 +04:00
|
|
|
assert_eq!(coords_at_pos(slice, 0), (0, 0).into());
|
2021-02-24 11:08:17 +04:00
|
|
|
assert_eq!(coords_at_pos(slice, 2), (0, 1).into());
|
|
|
|
assert_eq!(coords_at_pos(slice, 4), (0, 2).into());
|
|
|
|
assert_eq!(coords_at_pos(slice, 7), (0, 3).into());
|
|
|
|
|
|
|
|
let text = Rope::from("किमपि");
|
|
|
|
let slice = text.slice(..);
|
|
|
|
assert_eq!(coords_at_pos(slice, 0), (0, 0).into());
|
|
|
|
assert_eq!(coords_at_pos(slice, 2), (0, 1).into());
|
|
|
|
assert_eq!(coords_at_pos(slice, 3), (0, 2).into());
|
|
|
|
assert_eq!(coords_at_pos(slice, 5), (0, 3).into());
|
2020-06-05 09:02:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_pos_at_coords() {
|
|
|
|
let text = Rope::from("ḧëḷḷö\nẅöṛḷḋ");
|
2021-02-18 13:34:22 +04:00
|
|
|
let slice = text.slice(..);
|
|
|
|
assert_eq!(pos_at_coords(slice, (0, 0).into()), 0);
|
|
|
|
assert_eq!(pos_at_coords(slice, (0, 5).into()), 5); // position on \n
|
|
|
|
assert_eq!(pos_at_coords(slice, (1, 0).into()), 6); // position on w
|
|
|
|
assert_eq!(pos_at_coords(slice, (1, 1).into()), 7); // position on o
|
|
|
|
assert_eq!(pos_at_coords(slice, (1, 4).into()), 10); // position on d
|
2021-02-24 11:08:17 +04:00
|
|
|
|
|
|
|
// test with grapheme clusters
|
|
|
|
let text = Rope::from("a̐éö̲\r\n");
|
|
|
|
let slice = text.slice(..);
|
|
|
|
assert_eq!(pos_at_coords(slice, (0, 0).into()), 0);
|
|
|
|
assert_eq!(pos_at_coords(slice, (0, 1).into()), 2);
|
|
|
|
assert_eq!(pos_at_coords(slice, (0, 2).into()), 4);
|
|
|
|
assert_eq!(pos_at_coords(slice, (0, 3).into()), 7); // \r\n is one char here
|
|
|
|
assert_eq!(pos_at_coords(slice, (0, 4).into()), 9);
|
|
|
|
let text = Rope::from("किमपि");
|
|
|
|
// 2 - 1 - 2 codepoints
|
|
|
|
// TODO: delete handling as per https://news.ycombinator.com/item?id=20058454
|
|
|
|
let slice = text.slice(..);
|
|
|
|
assert_eq!(pos_at_coords(slice, (0, 0).into()), 0);
|
|
|
|
assert_eq!(pos_at_coords(slice, (0, 1).into()), 2);
|
|
|
|
assert_eq!(pos_at_coords(slice, (0, 2).into()), 3);
|
|
|
|
assert_eq!(pos_at_coords(slice, (0, 3).into()), 5); // eol
|
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 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
|
|
|
}
|