2020-10-19 10:18:03 +02:00
|
|
|
use std::borrow::Cow;
|
2020-09-21 11:24:16 +02:00
|
|
|
|
2021-03-24 06:03:20 +01:00
|
|
|
use crate::{Document, DocumentId, ViewId};
|
2020-10-01 21:16:24 +02:00
|
|
|
use helix_core::{
|
2021-04-09 11:48:56 +02:00
|
|
|
coords_at_pos,
|
2020-10-01 21:16:24 +02:00
|
|
|
graphemes::{grapheme_width, RopeGraphemes},
|
2021-03-24 10:01:26 +01:00
|
|
|
Position, RopeSlice, Selection,
|
2020-10-01 21:16:24 +02:00
|
|
|
};
|
|
|
|
use tui::layout::Rect;
|
2020-09-21 11:24:16 +02:00
|
|
|
|
2020-10-14 05:03:10 +02:00
|
|
|
pub const PADDING: usize = 5;
|
|
|
|
|
2021-03-24 10:01:26 +01:00
|
|
|
type Jump = (DocumentId, Selection);
|
|
|
|
|
|
|
|
pub struct JumpList {
|
|
|
|
jumps: Vec<Jump>,
|
|
|
|
current: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl JumpList {
|
|
|
|
pub fn new(initial: Jump) -> Self {
|
|
|
|
Self {
|
|
|
|
jumps: vec![initial],
|
|
|
|
current: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push(&mut self, jump: Jump) {
|
2021-03-29 09:32:42 +02:00
|
|
|
self.jumps.truncate(self.current);
|
|
|
|
// don't push duplicates
|
|
|
|
if self.jumps.last() != Some(&jump) {
|
|
|
|
self.jumps.push(jump);
|
|
|
|
self.current = self.jumps.len();
|
|
|
|
}
|
2021-03-24 10:01:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn forward(&mut self, count: usize) -> Option<&Jump> {
|
|
|
|
if self.current + count < self.jumps.len() {
|
|
|
|
self.current += count;
|
2021-06-08 18:40:38 +02:00
|
|
|
self.jumps.get(self.current)
|
|
|
|
} else {
|
|
|
|
None
|
2021-03-24 10:01:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 18:40:38 +02:00
|
|
|
// Taking view and doc to prevent unnecessary cloning when jump is not required.
|
|
|
|
pub fn backward(&mut self, view_id: ViewId, doc: &mut Document, count: usize) -> Option<&Jump> {
|
|
|
|
if let Some(current) = self.current.checked_sub(count) {
|
|
|
|
if self.current == self.jumps.len() {
|
|
|
|
let jump = (doc.id(), doc.selection(view_id).clone());
|
|
|
|
self.push(jump);
|
|
|
|
}
|
|
|
|
self.current = current;
|
|
|
|
self.jumps.get(self.current)
|
|
|
|
} else {
|
|
|
|
None
|
2021-03-24 10:01:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 11:24:16 +02:00
|
|
|
pub struct View {
|
2021-03-24 06:03:20 +01:00
|
|
|
pub id: ViewId,
|
2021-03-23 09:47:40 +01:00
|
|
|
pub doc: DocumentId,
|
2020-10-06 10:32:30 +02:00
|
|
|
pub first_line: usize,
|
2021-04-09 11:48:56 +02:00
|
|
|
pub first_col: usize,
|
2021-02-03 11:36:54 +01:00
|
|
|
pub area: Rect,
|
2021-03-24 10:01:26 +01:00
|
|
|
pub jumps: JumpList,
|
2020-09-21 11:24:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl View {
|
2021-05-07 07:40:11 +02:00
|
|
|
pub fn new(doc: DocumentId) -> Self {
|
|
|
|
Self {
|
2021-03-24 06:03:20 +01:00
|
|
|
id: ViewId::default(),
|
2020-10-22 07:35:07 +02:00
|
|
|
doc,
|
2020-09-21 11:24:16 +02:00
|
|
|
first_line: 0,
|
2021-04-09 11:48:56 +02:00
|
|
|
first_col: 0,
|
2021-02-03 11:36:54 +01:00
|
|
|
area: Rect::default(), // will get calculated upon inserting into tree
|
2021-03-24 10:01:26 +01:00
|
|
|
jumps: JumpList::new((doc, Selection::point(0))), // TODO: use actual sel
|
2021-05-07 07:40:11 +02:00
|
|
|
}
|
2020-09-21 11:24:16 +02:00
|
|
|
}
|
|
|
|
|
2021-03-23 09:47:40 +01:00
|
|
|
pub fn ensure_cursor_in_view(&mut self, doc: &Document) {
|
2021-03-31 10:17:01 +02:00
|
|
|
let cursor = doc.selection(self.id).cursor();
|
2021-04-09 11:48:56 +02:00
|
|
|
let pos = coords_at_pos(doc.text().slice(..), cursor);
|
|
|
|
let line = pos.row;
|
|
|
|
let col = pos.col;
|
2021-04-14 10:15:32 +02:00
|
|
|
let height = self.area.height.saturating_sub(1); // - 1 for statusline
|
|
|
|
let last_line = self.first_line + height as usize;
|
2021-04-14 07:27:47 +02:00
|
|
|
|
|
|
|
let scrolloff = PADDING.min(self.area.height as usize / 2); // TODO: user pref
|
2020-09-21 11:24:16 +02:00
|
|
|
|
2021-04-09 11:48:56 +02:00
|
|
|
// TODO: not ideal
|
|
|
|
const OFFSET: usize = 7; // 1 diagnostic + 5 linenr + 1 gutter
|
|
|
|
let last_col = self.first_col + (self.area.width as usize - OFFSET);
|
2020-09-21 11:24:16 +02:00
|
|
|
|
2021-04-14 07:27:47 +02:00
|
|
|
if line > last_line.saturating_sub(scrolloff) {
|
2020-09-21 11:24:16 +02:00
|
|
|
// scroll down
|
2021-04-14 07:27:47 +02:00
|
|
|
self.first_line += line - (last_line.saturating_sub(scrolloff));
|
|
|
|
} else if line < self.first_line + scrolloff {
|
2020-09-21 11:24:16 +02:00
|
|
|
// scroll up
|
2021-04-14 07:27:47 +02:00
|
|
|
self.first_line = line.saturating_sub(scrolloff);
|
2020-09-21 11:24:16 +02:00
|
|
|
}
|
2021-04-09 11:48:56 +02:00
|
|
|
|
2021-04-14 07:27:47 +02:00
|
|
|
if col > last_col.saturating_sub(scrolloff) {
|
2021-04-09 11:48:56 +02:00
|
|
|
// scroll right
|
2021-04-14 07:27:47 +02:00
|
|
|
self.first_col += col - (last_col.saturating_sub(scrolloff));
|
|
|
|
} else if col < self.first_col + scrolloff {
|
2021-04-09 11:48:56 +02:00
|
|
|
// scroll left
|
2021-04-14 07:27:47 +02:00
|
|
|
self.first_col = col.saturating_sub(scrolloff);
|
2021-04-09 11:48:56 +02:00
|
|
|
}
|
2020-09-21 11:24:16 +02:00
|
|
|
}
|
2020-10-01 21:16:24 +02:00
|
|
|
|
|
|
|
/// Calculates the last visible line on screen
|
|
|
|
#[inline]
|
2021-03-23 09:47:40 +01:00
|
|
|
pub fn last_line(&self, doc: &Document) -> usize {
|
2021-06-08 06:20:15 +02:00
|
|
|
let height = self.area.height.saturating_sub(1); // - 1 for statusline
|
2020-10-01 21:16:24 +02:00
|
|
|
std::cmp::min(
|
2021-03-16 07:30:29 +01:00
|
|
|
self.first_line + height as usize,
|
2021-03-16 10:27:57 +01:00
|
|
|
doc.text().len_lines() - 1,
|
2020-10-01 21:16:24 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Translates a document position to an absolute position in the terminal.
|
|
|
|
/// Returns a (line, col) position if the position is visible on screen.
|
|
|
|
// TODO: Could return width as well for the character width at cursor.
|
2021-03-23 09:47:40 +01:00
|
|
|
pub fn screen_coords_at_pos(
|
|
|
|
&self,
|
|
|
|
doc: &Document,
|
|
|
|
text: RopeSlice,
|
|
|
|
pos: usize,
|
|
|
|
) -> Option<Position> {
|
2020-10-01 21:16:24 +02:00
|
|
|
let line = text.char_to_line(pos);
|
|
|
|
|
2021-03-23 09:47:40 +01:00
|
|
|
if line < self.first_line || line > self.last_line(doc) {
|
2020-10-01 21:16:24 +02:00
|
|
|
// Line is not visible on screen
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let line_start = text.line_to_char(line);
|
|
|
|
let line_slice = text.slice(line_start..pos);
|
|
|
|
let mut col = 0;
|
2021-03-23 09:47:40 +01:00
|
|
|
let tab_width = doc.tab_width();
|
2020-10-01 21:16:24 +02:00
|
|
|
|
2021-02-18 10:34:22 +01:00
|
|
|
for grapheme in RopeGraphemes::new(line_slice) {
|
2020-10-01 21:16:24 +02:00
|
|
|
if grapheme == "\t" {
|
2021-03-22 05:47:39 +01:00
|
|
|
col += tab_width;
|
2020-10-01 21:16:24 +02:00
|
|
|
} else {
|
|
|
|
let grapheme = Cow::from(grapheme);
|
|
|
|
col += grapheme_width(&grapheme);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 01:30:40 +02:00
|
|
|
// It is possible for underflow to occur if the buffer length is larger than the terminal width.
|
|
|
|
let row = line.saturating_sub(self.first_line);
|
|
|
|
let col = col.saturating_sub(self.first_col);
|
2020-10-01 21:16:24 +02:00
|
|
|
|
|
|
|
Some(Position::new(row, col))
|
|
|
|
}
|
2020-10-22 07:35:07 +02:00
|
|
|
|
2021-02-18 10:34:22 +01:00
|
|
|
// pub fn traverse<F>(&self, text: RopeSlice, start: usize, end: usize, fun: F)
|
2020-10-23 06:51:08 +02:00
|
|
|
// where
|
|
|
|
// F: Fn(usize, usize),
|
|
|
|
// {
|
|
|
|
// let start = self.screen_coords_at_pos(text, start);
|
|
|
|
// let end = self.screen_coords_at_pos(text, end);
|
|
|
|
|
|
|
|
// match (start, end) {
|
|
|
|
// // fully on screen
|
|
|
|
// (Some(start), Some(end)) => {
|
|
|
|
// // we want to calculate ends of lines for each char..
|
|
|
|
// }
|
|
|
|
// // from start to end of screen
|
|
|
|
// (Some(start), None) => {}
|
|
|
|
// // from start of screen to end
|
|
|
|
// (None, Some(end)) => {}
|
|
|
|
// // not on screen
|
|
|
|
// (None, None) => return,
|
|
|
|
// }
|
|
|
|
// }
|
2020-09-21 11:24:16 +02:00
|
|
|
}
|