helix-mods/helix-view/src/view.rs

111 lines
3.4 KiB
Rust
Raw Normal View History

use anyhow::Error;
use std::borrow::Cow;
use crate::Document;
2020-10-01 21:16:24 +02:00
use helix_core::{
graphemes::{grapheme_width, RopeGraphemes},
2020-10-14 05:01:41 +02:00
indent::TAB_WIDTH,
Position, RopeSlice,
2020-10-01 21:16:24 +02:00
};
use tui::layout::Rect;
2020-10-14 05:03:10 +02:00
pub const PADDING: usize = 5;
// TODO: view should be View { doc: Document(state, history,..) }
// since we can have multiple views into the same file
pub struct View {
pub doc: Document,
2020-10-06 10:32:30 +02:00
pub first_line: usize,
pub area: Rect,
}
2021-02-09 07:40:30 +01:00
// TODO: popups should be a thing on the view with a rect + text
impl View {
pub fn new(doc: Document) -> Result<Self, Error> {
2020-09-29 11:02:27 +02:00
let view = Self {
doc,
first_line: 0,
area: Rect::default(), // will get calculated upon inserting into tree
};
Ok(view)
}
pub fn ensure_cursor_in_view(&mut self) {
let cursor = self.doc.state.selection().cursor();
let line = self.doc.text().char_to_line(cursor);
let document_end = self.first_line + (self.area.height as usize).saturating_sub(2);
// TODO: side scroll
2020-10-14 05:03:10 +02:00
if line > document_end.saturating_sub(PADDING) {
// scroll down
2020-10-14 05:03:10 +02:00
self.first_line += line - (document_end.saturating_sub(PADDING));
} else if line < self.first_line + PADDING {
// scroll up
2020-10-14 05:03:10 +02:00
self.first_line = line.saturating_sub(PADDING);
}
}
2020-10-01 21:16:24 +02:00
/// Calculates the last visible line on screen
#[inline]
2020-10-07 01:41:09 +02:00
pub fn last_line(&self) -> usize {
let viewport = Rect::new(6, 0, self.area.width, self.area.height - 2); // - 2 for statusline and prompt
2020-10-01 21:16:24 +02:00
std::cmp::min(
2020-10-06 10:32:30 +02:00
self.first_line + (viewport.height as usize),
self.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.
2020-10-07 01:41:09 +02:00
pub fn screen_coords_at_pos(&self, text: &RopeSlice, pos: usize) -> Option<Position> {
2020-10-01 21:16:24 +02:00
let line = text.char_to_line(pos);
2020-10-07 01:41:09 +02:00
if line < self.first_line as usize || line > self.last_line() {
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;
for grapheme in RopeGraphemes::new(&line_slice) {
if grapheme == "\t" {
2020-10-14 05:01:41 +02:00
col += TAB_WIDTH;
2020-10-01 21:16:24 +02:00
} else {
let grapheme = Cow::from(grapheme);
col += grapheme_width(&grapheme);
}
}
let row = line - self.first_line as usize;
Some(Position::new(row, col))
}
2020-10-23 06:51:08 +02:00
// pub fn traverse<F>(&self, text: &RopeSlice, start: usize, end: usize, fun: F)
// 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,
// }
// }
}