Introduce doc.selection()/set_selection()

pull/5/head
Blaž Hrastnik 4 years ago
parent e4070d4de0
commit 55fa86248c

@ -19,40 +19,38 @@ use crate::{
pub type Command = fn(view: &mut View, count: usize); pub type Command = fn(view: &mut View, count: usize);
pub fn move_char_left(view: &mut View, count: usize) { pub fn move_char_left(view: &mut View, count: usize) {
// TODO: use a transaction
let selection = let selection =
view.doc view.doc
.state .state
.move_selection(Direction::Backward, Granularity::Character, count); .move_selection(Direction::Backward, Granularity::Character, count);
view.doc.state.selection = selection; view.doc.set_selection(selection);
} }
pub fn move_char_right(view: &mut View, count: usize) { pub fn move_char_right(view: &mut View, count: usize) {
// TODO: use a transaction let selection =
view.doc.state.selection =
view.doc view.doc
.state .state
.move_selection(Direction::Forward, Granularity::Character, count); .move_selection(Direction::Forward, Granularity::Character, count);
view.doc.set_selection(selection);
} }
pub fn move_line_up(view: &mut View, count: usize) { pub fn move_line_up(view: &mut View, count: usize) {
// TODO: use a transaction let selection = view
view.doc.state.selection = .doc
view.doc .state
.state .move_selection(Direction::Backward, Granularity::Line, count);
.move_selection(Direction::Backward, Granularity::Line, count); view.doc.set_selection(selection);
} }
pub fn move_line_down(view: &mut View, count: usize) { pub fn move_line_down(view: &mut View, count: usize) {
// TODO: use a transaction let selection = view
view.doc.state.selection = .doc
view.doc .state
.state .move_selection(Direction::Forward, Granularity::Line, count);
.move_selection(Direction::Forward, Granularity::Line, count); view.doc.set_selection(selection);
} }
pub fn move_line_end(view: &mut View, _count: usize) { pub fn move_line_end(view: &mut View, _count: usize) {
// TODO: use a transaction
let lines = selection_lines(&view.doc.state); let lines = selection_lines(&view.doc.state);
let positions = lines let positions = lines
@ -68,9 +66,7 @@ pub fn move_line_end(view: &mut View, _count: usize) {
let selection = Selection::new(positions.collect(), 0); let selection = Selection::new(positions.collect(), 0);
let transaction = Transaction::new(&mut view.doc.state).with_selection(selection); view.doc.set_selection(selection);
view.doc.apply(&transaction);
} }
pub fn move_line_start(view: &mut View, _count: usize) { pub fn move_line_start(view: &mut View, _count: usize) {
@ -86,64 +82,58 @@ pub fn move_line_start(view: &mut View, _count: usize) {
let selection = Selection::new(positions.collect(), 0); let selection = Selection::new(positions.collect(), 0);
let transaction = Transaction::new(&mut view.doc.state).with_selection(selection); view.doc.set_selection(selection);
view.doc.apply(&transaction);
} }
pub fn move_next_word_start(view: &mut View, count: usize) { pub fn move_next_word_start(view: &mut View, count: usize) {
let pos = view.doc.state.move_pos( let pos = view.doc.state.move_pos(
view.doc.state.selection.cursor(), view.doc.selection().cursor(),
Direction::Forward, Direction::Forward,
Granularity::Word, Granularity::Word,
count, count,
); );
// TODO: use a transaction view.doc.set_selection(Selection::single(pos, pos));
view.doc.state.selection = Selection::single(pos, pos);
} }
pub fn move_prev_word_start(view: &mut View, count: usize) { pub fn move_prev_word_start(view: &mut View, count: usize) {
let pos = view.doc.state.move_pos( let pos = view.doc.state.move_pos(
view.doc.state.selection.cursor(), view.doc.selection().cursor(),
Direction::Backward, Direction::Backward,
Granularity::Word, Granularity::Word,
count, count,
); );
// TODO: use a transaction view.doc.set_selection(Selection::single(pos, pos));
view.doc.state.selection = Selection::single(pos, pos);
} }
pub fn move_next_word_end(view: &mut View, count: usize) { pub fn move_next_word_end(view: &mut View, count: usize) {
let pos = State::move_next_word_end( let pos = State::move_next_word_end(
&view.doc.text().slice(..), &view.doc.text().slice(..),
view.doc.state.selection.cursor(), view.doc.selection().cursor(),
count, count,
); );
// TODO: use a transaction view.doc.set_selection(Selection::single(pos, pos));
view.doc.state.selection = Selection::single(pos, pos);
} }
pub fn move_file_start(view: &mut View, _count: usize) { pub fn move_file_start(view: &mut View, _count: usize) {
// TODO: use a transaction view.doc.set_selection(Selection::single(0, 0));
view.doc.state.selection = Selection::single(0, 0);
view.doc.mode = Mode::Normal; view.doc.mode = Mode::Normal;
} }
pub fn move_file_end(view: &mut View, _count: usize) { pub fn move_file_end(view: &mut View, _count: usize) {
// TODO: use a transaction
let text = &view.doc.text(); let text = &view.doc.text();
let last_line = text.line_to_char(text.len_lines().saturating_sub(2)); let last_line = text.line_to_char(text.len_lines().saturating_sub(2));
view.doc.state.selection = Selection::single(last_line, last_line); view.doc
.set_selection(Selection::single(last_line, last_line));
view.doc.mode = Mode::Normal; view.doc.mode = Mode::Normal;
} }
pub fn check_cursor_in_view(view: &mut View) -> bool { pub fn check_cursor_in_view(view: &mut View) -> bool {
let cursor = view.doc.state.selection().cursor(); let cursor = view.doc.selection().cursor();
let line = view.doc.text().char_to_line(cursor); let line = view.doc.text().char_to_line(cursor);
let document_end = view.first_line + view.size.1.saturating_sub(1) as usize; let document_end = view.first_line + view.size.1.saturating_sub(1) as usize;
@ -163,7 +153,7 @@ pub fn page_up(view: &mut View, _count: usize) {
if !check_cursor_in_view(view) { if !check_cursor_in_view(view) {
let text = view.doc.text(); let text = view.doc.text();
let pos = text.line_to_char(view.last_line().saturating_sub(PADDING)); let pos = text.line_to_char(view.last_line().saturating_sub(PADDING));
view.doc.state.selection = Selection::single(pos, pos); view.doc.set_selection(Selection::single(pos, pos));
} }
} }
@ -173,7 +163,7 @@ pub fn page_down(view: &mut View, _count: usize) {
if view.first_line < view.doc.text().len_lines() { if view.first_line < view.doc.text().len_lines() {
let text = view.doc.text(); let text = view.doc.text();
let pos = text.line_to_char(view.first_line as usize); let pos = text.line_to_char(view.first_line as usize);
view.doc.state.selection = Selection::single(pos, pos); view.doc.set_selection(Selection::single(pos, pos));
} }
} }
@ -187,7 +177,7 @@ pub fn half_page_up(view: &mut View, _count: usize) {
if !check_cursor_in_view(view) { if !check_cursor_in_view(view) {
let text = &view.doc.text(); let text = &view.doc.text();
let pos = text.line_to_char(view.last_line() - PADDING); let pos = text.line_to_char(view.last_line() - PADDING);
view.doc.state.selection = Selection::single(pos, pos); view.doc.set_selection(Selection::single(pos, pos));
} }
} }
@ -199,42 +189,41 @@ pub fn half_page_down(view: &mut View, _count: usize) {
if !check_cursor_in_view(view) { if !check_cursor_in_view(view) {
let text = view.doc.text(); let text = view.doc.text();
let pos = text.line_to_char(view.first_line as usize); let pos = text.line_to_char(view.first_line as usize);
view.doc.state.selection = Selection::single(pos, pos); view.doc.set_selection(Selection::single(pos, pos));
} }
} }
// avoid select by default by having a visual mode switch that makes movements into selects // avoid select by default by having a visual mode switch that makes movements into selects
pub fn extend_char_left(view: &mut View, count: usize) { pub fn extend_char_left(view: &mut View, count: usize) {
// TODO: use a transaction
let selection = let selection =
view.doc view.doc
.state .state
.extend_selection(Direction::Backward, Granularity::Character, count); .extend_selection(Direction::Backward, Granularity::Character, count);
view.doc.state.selection = selection; view.doc.set_selection(selection);
} }
pub fn extend_char_right(view: &mut View, count: usize) { pub fn extend_char_right(view: &mut View, count: usize) {
// TODO: use a transaction let selection =
view.doc.state.selection =
view.doc view.doc
.state .state
.extend_selection(Direction::Forward, Granularity::Character, count); .extend_selection(Direction::Forward, Granularity::Character, count);
view.doc.set_selection(selection);
} }
pub fn extend_line_up(view: &mut View, count: usize) { pub fn extend_line_up(view: &mut View, count: usize) {
// TODO: use a transaction let selection = view
view.doc.state.selection = .doc
view.doc .state
.state .extend_selection(Direction::Backward, Granularity::Line, count);
.extend_selection(Direction::Backward, Granularity::Line, count); view.doc.set_selection(selection);
} }
pub fn extend_line_down(view: &mut View, count: usize) { pub fn extend_line_down(view: &mut View, count: usize) {
// TODO: use a transaction let selection = view
view.doc.state.selection = .doc
view.doc .state
.state .extend_selection(Direction::Forward, Granularity::Line, count);
.extend_selection(Direction::Forward, Granularity::Line, count); view.doc.set_selection(selection);
} }
pub fn split_selection_on_newline(view: &mut View, _count: usize) { pub fn split_selection_on_newline(view: &mut View, _count: usize) {
@ -242,20 +231,19 @@ pub fn split_selection_on_newline(view: &mut View, _count: usize) {
// only compile the regex once // only compile the regex once
#[allow(clippy::trivial_regex)] #[allow(clippy::trivial_regex)]
static REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\n").unwrap()); static REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\n").unwrap());
// TODO: use a transaction let selection = selection::split_on_matches(text, view.doc.selection(), &REGEX);
view.doc.state.selection = selection::split_on_matches(text, view.doc.state.selection(), &REGEX) view.doc.set_selection(selection);
} }
pub fn select_line(view: &mut View, _count: usize) { pub fn select_line(view: &mut View, _count: usize) {
// TODO: count // TODO: count
let pos = view.doc.state.selection().primary(); let pos = view.doc.selection().primary();
let text = view.doc.text(); let text = view.doc.text();
let line = text.char_to_line(pos.head); let line = text.char_to_line(pos.head);
let start = text.line_to_char(line); let start = text.line_to_char(line);
let end = text.line_to_char(line + 1).saturating_sub(1); let end = text.line_to_char(line + 1).saturating_sub(1);
// TODO: use a transaction view.doc.set_selection(Selection::single(start, end));
view.doc.state.selection = Selection::single(start, end);
} }
pub fn delete_selection(view: &mut View, _count: usize) { pub fn delete_selection(view: &mut View, _count: usize) {
@ -273,19 +261,21 @@ pub fn change_selection(view: &mut View, count: usize) {
} }
pub fn collapse_selection(view: &mut View, _count: usize) { pub fn collapse_selection(view: &mut View, _count: usize) {
view.doc.state.selection = view let selection = view
.doc .doc
.state .selection()
.selection .transform(|range| Range::new(range.head, range.head));
.transform(|range| Range::new(range.head, range.head))
view.doc.set_selection(selection);
} }
pub fn flip_selections(view: &mut View, _count: usize) { pub fn flip_selections(view: &mut View, _count: usize) {
view.doc.state.selection = view let selection = view
.doc .doc
.state .selection()
.selection .transform(|range| Range::new(range.head, range.anchor));
.transform(|range| Range::new(range.head, range.anchor))
view.doc.set_selection(selection);
} }
fn enter_insert_mode(view: &mut View) { fn enter_insert_mode(view: &mut View) {
@ -297,11 +287,11 @@ fn enter_insert_mode(view: &mut View) {
pub fn insert_mode(view: &mut View, _count: usize) { pub fn insert_mode(view: &mut View, _count: usize) {
enter_insert_mode(view); enter_insert_mode(view);
view.doc.state.selection = view let selection = view
.doc .doc
.state .selection()
.selection .transform(|range| Range::new(range.to(), range.from()));
.transform(|range| Range::new(range.to(), range.from())) view.doc.set_selection(selection);
} }
// inserts at the end of each selection // inserts at the end of each selection
@ -311,13 +301,14 @@ pub fn append_mode(view: &mut View, _count: usize) {
// TODO: as transaction // TODO: as transaction
let text = &view.doc.text().slice(..); let text = &view.doc.text().slice(..);
view.doc.state.selection = view.doc.state.selection.transform(|range| { let selection = view.doc.selection().transform(|range| {
// TODO: to() + next char // TODO: to() + next char
Range::new( Range::new(
range.from(), range.from(),
graphemes::next_grapheme_boundary(text, range.to()), graphemes::next_grapheme_boundary(text, range.to()),
) )
}) });
view.doc.set_selection(selection);
} }
// TODO: I, A, o and O can share a lot of the primitives. // TODO: I, A, o and O can share a lot of the primitives.
@ -402,7 +393,7 @@ fn append_changes_to_history(view: &mut View) {
let changes = std::mem::replace(&mut view.doc.changes, new_changeset); let changes = std::mem::replace(&mut view.doc.changes, new_changeset);
// Instead of doing this messy merge we could always commit, and based on transaction // Instead of doing this messy merge we could always commit, and based on transaction
// annotations either add a new layer or compose into the previous one. // annotations either add a new layer or compose into the previous one.
let transaction = Transaction::from(changes).with_selection(view.doc.state.selection().clone()); let transaction = Transaction::from(changes).with_selection(view.doc.selection().clone());
// increment document version // increment document version
// TODO: needs to happen on undo/redo too // TODO: needs to happen on undo/redo too
@ -424,12 +415,13 @@ pub fn normal_mode(view: &mut View, _count: usize) {
// if leaving append mode, move cursor back by 1 // if leaving append mode, move cursor back by 1
if view.doc.restore_cursor { if view.doc.restore_cursor {
let text = &view.doc.text().slice(..); let text = &view.doc.text().slice(..);
view.doc.state.selection = view.doc.state.selection.transform(|range| { let selection = view.doc.selection().transform(|range| {
Range::new( Range::new(
range.from(), range.from(),
graphemes::prev_grapheme_boundary(text, range.to()), graphemes::prev_grapheme_boundary(text, range.to()),
) )
}); });
view.doc.set_selection(selection);
view.doc.restore_cursor = false; view.doc.restore_cursor = false;
} }
@ -587,7 +579,7 @@ fn get_lines(view: &View) -> Vec<usize> {
let mut lines = Vec::new(); let mut lines = Vec::new();
// Get all line numbers // Get all line numbers
for range in view.doc.state.selection.ranges() { for range in view.doc.selection().ranges() {
let start = view.doc.text().char_to_line(range.from()); let start = view.doc.text().char_to_line(range.from());
let end = view.doc.text().char_to_line(range.to()); let end = view.doc.text().char_to_line(range.to());

@ -108,6 +108,11 @@ impl Document {
}; };
} }
pub fn set_selection(&mut self, selection: Selection) {
// TODO: use a transaction?
self.state.selection = selection;
}
pub fn apply(&mut self, transaction: &Transaction) -> bool { pub fn apply(&mut self, transaction: &Transaction) -> bool {
let old_doc = self.text().clone(); let old_doc = self.text().clone();
@ -119,6 +124,8 @@ impl Document {
changes.compose(transaction.changes().clone()).unwrap() changes.compose(transaction.changes().clone()).unwrap()
}); });
// TODO: when composing, replace transaction.selection too
// update tree-sitter syntax tree // update tree-sitter syntax tree
if let Some(syntax) = &mut self.syntax { if let Some(syntax) = &mut self.syntax {
// TODO: no unwrap // TODO: no unwrap
@ -150,6 +157,10 @@ impl Document {
&self.state.doc &self.state.doc
} }
pub fn selection(&self) -> &Selection {
&self.state.selection
}
// pub fn slice<R>(&self, range: R) -> RopeSlice where R: RangeBounds { // pub fn slice<R>(&self, range: R) -> RopeSlice where R: RangeBounds {
// self.state.doc.slice // self.state.doc.slice
// } // }

Loading…
Cancel
Save