use crate::{col, row, ColIndex, ColWidth, EditorBuffer, RowHeight, RowIndex};
#[derive(Clone, Eq, PartialEq)]
pub enum CaretColLocationInLine {
AtStart,
AtEnd,
InMiddle,
}
#[derive(Clone, Eq, PartialEq)]
pub enum CaretRowLocationInBuffer {
AtTop,
AtBottom,
InMiddle,
}
pub fn locate_col(editor_buffer: &EditorBuffer) -> CaretColLocationInLine {
if col_is_at_start_of_line(editor_buffer) {
CaretColLocationInLine::AtStart
} else if col_is_at_end_of_line(editor_buffer) {
CaretColLocationInLine::AtEnd
} else {
CaretColLocationInLine::InMiddle
}
}
fn col_is_at_start_of_line(buffer: &EditorBuffer) -> bool {
if buffer.line_at_caret_scr_adj().is_some() {
buffer.get_caret_scr_adj().col_index == col(0)
} else {
false
}
}
fn col_is_at_end_of_line(buffer: &EditorBuffer) -> bool {
if buffer.line_at_caret_scr_adj().is_some() {
let line_display_width = buffer.get_line_display_width_at_caret_scr_adj();
buffer.get_caret_scr_adj().col_index
== caret_scroll_index::col_index_for_width(line_display_width)
} else {
false
}
}
pub fn locate_row(buffer: &EditorBuffer) -> CaretRowLocationInBuffer {
if row_is_at_top_of_buffer(buffer) {
CaretRowLocationInBuffer::AtTop
} else if row_is_at_bottom_of_buffer(buffer) {
CaretRowLocationInBuffer::AtBottom
} else {
CaretRowLocationInBuffer::InMiddle
}
}
fn row_is_at_top_of_buffer(buffer: &EditorBuffer) -> bool {
buffer.get_caret_scr_adj().row_index == row(0)
}
fn row_is_at_bottom_of_buffer(buffer: &EditorBuffer) -> bool {
if buffer.is_empty() || buffer.get_lines().len() == 1 {
false
} else {
let max_row_index = buffer.get_max_row_index();
buffer.get_caret_scr_adj().row_index == max_row_index
}
}
pub mod caret_scroll_index {
use super::*;
pub fn col_index_for_width(col_amt: ColWidth) -> ColIndex {
col_amt.convert_to_col_index() + col(1)
}
pub fn row_index_for_height(row_amt: RowHeight) -> RowIndex {
row_amt.convert_to_row_index() + row(1)
}
#[test]
fn test_scroll_col_index_for_width() {
use crate::width;
let width = width(5);
let scroll_col_index = col_index_for_width(width);
assert_eq!(*scroll_col_index, *width);
}
#[test]
fn test_scroll_row_index_for_height() {
use crate::height;
let height = height(5);
let scroll_row_index = row_index_for_height(height);
assert_eq!(*scroll_row_index, *height);
}
}