diff options
author | Blaž Hrastnik | 2020-09-21 09:24:16 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2020-09-21 09:24:16 +0000 |
commit | 935cfeae576f734e6cbd455bfa39df014700ae86 (patch) | |
tree | a4abe959b718f265ade4a66a844bfefadc546f90 /helix-view/src/view.rs | |
parent | 48330ddb5f36a1c5f44a636525089a019ce4439d (diff) |
Split parts of helix-term into helix-view.
It still largely depends on term for some types but I plan to change
that later.
Diffstat (limited to 'helix-view/src/view.rs')
-rw-r--r-- | helix-view/src/view.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/helix-view/src/view.rs b/helix-view/src/view.rs new file mode 100644 index 00000000..3f7a9974 --- /dev/null +++ b/helix-view/src/view.rs @@ -0,0 +1,48 @@ +use anyhow::Error; + +use std::path::PathBuf; + +use crate::theme::Theme; +use helix_core::State; + +pub struct View { + pub state: State, + pub first_line: u16, + pub size: (u16, u16), + pub theme: Theme, // TODO: share one instance +} + +impl View { + pub fn open(path: PathBuf, size: (u16, u16)) -> Result<View, Error> { + let mut state = State::load(path)?; + let theme = Theme::default(); + state.syntax.as_mut().unwrap().configure(theme.scopes()); + + let view = View { + state, + first_line: 0, + size, // TODO: pass in from term + theme, + }; + + Ok(view) + } + + pub fn ensure_cursor_in_view(&mut self) { + let cursor = self.state.selection().cursor(); + let line = self.state.doc().char_to_line(cursor) as u16; + let document_end = self.first_line + self.size.1.saturating_sub(1) - 1; + + let padding = 5u16; + + // 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); + } + } +} |