aboutsummaryrefslogtreecommitdiff
path: root/helix-view/src/editor.rs
blob: 9fb2ae363ae5924452ab09269a2ac20e541586e4 (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
use crate::theme::Theme;
use crate::{Document, View};

use std::path::PathBuf;

use anyhow::Error;

pub struct Editor {
    pub views: Vec<View>,
    pub focus: usize,
    pub should_close: bool,
    pub theme: Theme, // TODO: share one instance
}

impl Default for Editor {
    fn default() -> Self {
        Self::new()
    }
}

impl Editor {
    pub fn new() -> Self {
        let theme = Theme::default();

        Self {
            views: Vec::new(),
            focus: 0,
            should_close: false,
            theme,
        }
    }

    pub fn open(&mut self, path: PathBuf, size: (u16, u16)) -> Result<(), Error> {
        let pos = self.views.len();
        let doc = Document::load(path, self.theme.scopes())?;
        self.views.push(View::new(doc, size)?);
        self.focus = pos;
        Ok(())
    }

    pub fn view(&self) -> Option<&View> {
        self.views.get(self.focus)
    }

    pub fn view_mut(&mut self) -> Option<&mut View> {
        self.views.get_mut(self.focus)
    }
}