diff options
author | Blaž Hrastnik | 2021-02-03 10:36:54 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2021-02-03 10:36:54 +0000 |
commit | 448c1abba04e11f77e53629dc06fe47619a741d4 (patch) | |
tree | 47a5ae9d08bf49796348d981120105cc054e736b /helix-view | |
parent | 2bea5db7bdb3ad3fa029df830d824cd5c26a153f (diff) |
View tree implementation: render multiple split views.
Cursors are still a bit buggy and we should render in focus statusbar
differently than in the other pane.
Diffstat (limited to 'helix-view')
-rw-r--r-- | helix-view/Cargo.toml | 2 | ||||
-rw-r--r-- | helix-view/src/editor.rs | 34 | ||||
-rw-r--r-- | helix-view/src/lib.rs | 1 | ||||
-rw-r--r-- | helix-view/src/view.rs | 10 |
4 files changed, 21 insertions, 26 deletions
diff --git a/helix-view/Cargo.toml b/helix-view/Cargo.toml index 0a435811..938f35e5 100644 --- a/helix-view/Cargo.toml +++ b/helix-view/Cargo.toml @@ -24,3 +24,5 @@ url = "2" smol = "1" futures-util = "0.3" + +slotmap = "1" diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 54a098f9..eb745066 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -1,45 +1,36 @@ use crate::theme::Theme; +use crate::tree::Tree; use crate::{Document, View}; +use slotmap::DefaultKey as Key; use std::path::PathBuf; use anyhow::Error; pub struct Editor { - pub views: Vec<View>, - pub focus: usize, + pub tree: Tree, + // pub documents: Vec<Document>, + pub focus: Key, pub should_close: bool, pub theme: Theme, // TODO: share one instance pub language_servers: helix_lsp::Registry, } -impl Default for Editor { - fn default() -> Self { - Self::new() - } -} - impl Editor { - pub fn new() -> Self { + pub fn new(area: tui::layout::Rect) -> Self { let theme = Theme::default(); let language_servers = helix_lsp::Registry::new(); Self { - views: Vec::new(), - focus: 0, + tree: Tree::new(area), + focus: Key::default(), should_close: false, theme, language_servers, } } - pub fn open( - &mut self, - path: PathBuf, - size: (u16, u16), - executor: &smol::Executor, - ) -> Result<(), Error> { - let pos = self.views.len(); + pub fn open(&mut self, path: PathBuf, executor: &smol::Executor) -> Result<(), Error> { let mut doc = Document::load(path, self.theme.scopes())?; // try to find a language server based on the language name @@ -60,16 +51,17 @@ impl Editor { .unwrap(); } - self.views.push(View::new(doc, size)?); + let view = View::new(doc)?; + let pos = self.tree.insert(view); self.focus = pos; Ok(()) } pub fn view(&self) -> &View { - self.views.get(self.focus).unwrap() + self.tree.get(self.focus) } pub fn view_mut(&mut self) -> &mut View { - self.views.get_mut(self.focus).unwrap() + self.tree.get_mut(self.focus) } } diff --git a/helix-view/src/lib.rs b/helix-view/src/lib.rs index f28c8116..05de7e9f 100644 --- a/helix-view/src/lib.rs +++ b/helix-view/src/lib.rs @@ -1,6 +1,7 @@ pub mod document; pub mod editor; pub mod theme; +pub mod tree; pub mod view; pub use document::Document; diff --git a/helix-view/src/view.rs b/helix-view/src/view.rs index df41e3ae..24b50d81 100644 --- a/helix-view/src/view.rs +++ b/helix-view/src/view.rs @@ -17,15 +17,15 @@ pub const PADDING: usize = 5; pub struct View { pub doc: Document, pub first_line: usize, - pub size: (u16, u16), + pub area: Rect, } impl View { - pub fn new(doc: Document, size: (u16, u16)) -> Result<Self, Error> { + pub fn new(doc: Document) -> Result<Self, Error> { let view = Self { doc, first_line: 0, - size, + area: Rect::default(), // will get calculated upon inserting into tree }; Ok(view) @@ -34,7 +34,7 @@ impl 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.size.1 as usize).saturating_sub(2); + let document_end = self.first_line + (self.area.height as usize).saturating_sub(2); // TODO: side scroll @@ -50,7 +50,7 @@ impl View { /// Calculates the last visible line on screen #[inline] pub fn last_line(&self) -> usize { - let viewport = Rect::new(6, 0, self.size.0, self.size.1 - 2); // - 2 for statusline and prompt + let viewport = Rect::new(6, 0, self.area.width, self.area.height - 2); // - 2 for statusline and prompt std::cmp::min( self.first_line + (viewport.height as usize), self.doc.text().len_lines() - 1, |