summaryrefslogtreecommitdiff
path: root/helix-view/src/view.rs
blob: b406b7564977ad0481a8ef797754a69a4ce5b150 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use anyhow::Error;

use std::borrow::Cow;

use crate::Document;
use helix_core::{
    graphemes::{grapheme_width, RopeGraphemes},
    indent::TAB_WIDTH,
    Position, RopeSlice,
};
use slotmap::DefaultKey as Key;
use tui::layout::Rect;

pub const PADDING: usize = 5;

pub struct View {
    pub id: Key,
    pub doc: Document,
    pub first_line: usize,
    pub area: Rect,
}

impl View {
    pub fn new(doc: Document) -> Result<Self, Error> {
        let view = Self {
            id: Key::default(),
            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.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

        if line > document_end.saturating_sub(PADDING) {
            // scroll down
            self.first_line += line - (document_end.saturating_sub(PADDING));
        } else if line < self.first_line + PADDING {
            // scroll up
            self.first_line = line.saturating_sub(PADDING);
        }
    }

    /// Calculates the last visible line on screen
    #[inline]
    pub fn last_line(&self) -> usize {
        let height = self.area.height.saturating_sub(1); // - 1 for statusline
        std::cmp::min(
            self.first_line + height as usize,
            self.doc.text().len_lines() - 1,
        )
    }

    /// 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.
    pub fn screen_coords_at_pos(&self, text: RopeSlice, pos: usize) -> Option<Position> {
        let line = text.char_to_line(pos);

        if line < self.first_line || line > self.last_line() {
            // 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" {
                col += TAB_WIDTH;
            } else {
                let grapheme = Cow::from(grapheme);
                col += grapheme_width(&grapheme);
            }
        }

        let row = line - self.first_line as usize;

        Some(Position::new(row, col))
    }

    // 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,
    //     }
    // }
}