|
|
@ -13,7 +13,7 @@ use crate::view::View;
|
|
|
|
/// state (usually by creating and applying a transaction).
|
|
|
|
/// state (usually by creating and applying a transaction).
|
|
|
|
pub type Command = fn(view: &mut View, count: usize);
|
|
|
|
pub type Command = fn(view: &mut View, count: usize);
|
|
|
|
|
|
|
|
|
|
|
|
const PADDING: u16 = 5;
|
|
|
|
const PADDING: usize = 5;
|
|
|
|
|
|
|
|
|
|
|
|
pub fn move_char_left(view: &mut View, count: usize) {
|
|
|
|
pub fn move_char_left(view: &mut View, count: usize) {
|
|
|
|
// TODO: use a transaction
|
|
|
|
// TODO: use a transaction
|
|
|
@ -137,8 +137,8 @@ pub fn move_file_end(view: &mut View, _count: usize) {
|
|
|
|
|
|
|
|
|
|
|
|
pub fn check_cursor_in_view(view: &mut View) -> bool {
|
|
|
|
pub fn check_cursor_in_view(view: &mut View) -> bool {
|
|
|
|
let cursor = view.state.selection().cursor();
|
|
|
|
let cursor = view.state.selection().cursor();
|
|
|
|
let line = view.state.doc().char_to_line(cursor) as u16;
|
|
|
|
let line = view.state.doc().char_to_line(cursor);
|
|
|
|
let document_end = view.first_line + view.size.1.saturating_sub(1);
|
|
|
|
let document_end = view.first_line + view.size.1.saturating_sub(1) as usize;
|
|
|
|
|
|
|
|
|
|
|
|
if (line > document_end.saturating_sub(PADDING)) | (line < view.first_line + PADDING) {
|
|
|
|
if (line > document_end.saturating_sub(PADDING)) | (line < view.first_line + PADDING) {
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
@ -147,28 +147,28 @@ pub fn check_cursor_in_view(view: &mut View) -> bool {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn page_up(view: &mut View, _count: usize) {
|
|
|
|
pub fn page_up(view: &mut View, _count: usize) {
|
|
|
|
let text = &view.state.doc;
|
|
|
|
view.first_line = view.first_line.saturating_sub(view.size.1 as usize);
|
|
|
|
view.first_line = view.first_line.saturating_sub(view.size.1);
|
|
|
|
|
|
|
|
let pos = text.line_to_char(view.last_line() as usize);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !check_cursor_in_view(view) {
|
|
|
|
if !check_cursor_in_view(view) {
|
|
|
|
let text = &view.state.doc;
|
|
|
|
let text = view.state.doc();
|
|
|
|
let pos = text.line_to_char(view.last_line().saturating_sub(PADDING as usize));
|
|
|
|
let pos = text.line_to_char(view.last_line().saturating_sub(PADDING as usize));
|
|
|
|
view.state.selection = Selection::single(pos, pos);
|
|
|
|
view.state.selection = Selection::single(pos, pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn page_down(view: &mut View, _count: usize) {
|
|
|
|
pub fn page_down(view: &mut View, _count: usize) {
|
|
|
|
let text = &view.state.doc;
|
|
|
|
view.first_line += view.size.1 as usize + PADDING;
|
|
|
|
view.first_line += view.size.1 + PADDING;
|
|
|
|
|
|
|
|
if view.first_line < view.state.doc().len_lines() as u16 {
|
|
|
|
if view.first_line < view.state.doc().len_lines() {
|
|
|
|
|
|
|
|
let text = view.state.doc();
|
|
|
|
let pos = text.line_to_char(view.first_line as usize);
|
|
|
|
let pos = text.line_to_char(view.first_line as usize);
|
|
|
|
view.state.selection = Selection::single(pos, pos);
|
|
|
|
view.state.selection = Selection::single(pos, pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn half_page_up(view: &mut View, _count: usize) {
|
|
|
|
pub fn half_page_up(view: &mut View, _count: usize) {
|
|
|
|
view.first_line = view.first_line.saturating_sub(view.size.1 / 2);
|
|
|
|
view.first_line = view.first_line.saturating_sub(view.size.1 as usize / 2);
|
|
|
|
|
|
|
|
|
|
|
|
if !check_cursor_in_view(view) {
|
|
|
|
if !check_cursor_in_view(view) {
|
|
|
|
let text = &view.state.doc;
|
|
|
|
let text = &view.state.doc;
|
|
|
|
let pos = text.line_to_char(view.last_line() - PADDING as usize);
|
|
|
|
let pos = text.line_to_char(view.last_line() - PADDING as usize);
|
|
|
@ -178,11 +178,11 @@ pub fn half_page_up(view: &mut View, _count: usize) {
|
|
|
|
|
|
|
|
|
|
|
|
pub fn half_page_down(view: &mut View, _count: usize) {
|
|
|
|
pub fn half_page_down(view: &mut View, _count: usize) {
|
|
|
|
let lines = view.state.doc().len_lines();
|
|
|
|
let lines = view.state.doc().len_lines();
|
|
|
|
if view.first_line < lines.saturating_sub(view.size.1 as usize) as u16 {
|
|
|
|
if view.first_line < lines.saturating_sub(view.size.1 as usize) {
|
|
|
|
view.first_line += view.size.1 / 2;
|
|
|
|
view.first_line += view.size.1 as usize / 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !check_cursor_in_view(view) {
|
|
|
|
if !check_cursor_in_view(view) {
|
|
|
|
let text = &view.state.doc;
|
|
|
|
let text = view.state.doc();
|
|
|
|
let pos = text.line_to_char(view.first_line as usize);
|
|
|
|
let pos = text.line_to_char(view.first_line as usize);
|
|
|
|
view.state.selection = Selection::single(pos, pos);
|
|
|
|
view.state.selection = Selection::single(pos, pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|