From b24cdd1295d1129dc730e02da2c6723938220d7e Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Wed, 24 Mar 2021 14:03:20 +0900 Subject: Derive a separate ViewId type. --- helix-view/src/editor.rs | 8 ++++---- helix-view/src/lib.rs | 1 + helix-view/src/tree.rs | 34 +++++++++++++++++----------------- helix-view/src/view.rs | 7 +++---- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 802ca3e1..0407f344 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -1,9 +1,9 @@ -use crate::{theme::Theme, tree::Tree, Document, DocumentId, View}; +use crate::{theme::Theme, tree::Tree, Document, DocumentId, View, ViewId}; use tui::layout::Rect; use std::path::PathBuf; -use slotmap::{DefaultKey as Key, SlotMap}; +use slotmap::SlotMap; use anyhow::Error; @@ -90,7 +90,7 @@ impl Editor { Ok(id) } - pub fn close(&mut self, id: Key) { + pub fn close(&mut self, id: ViewId) { let view = self.tree.get(self.tree.focus); // get around borrowck issues let language_servers = &mut self.language_servers; @@ -133,7 +133,7 @@ impl Editor { self.tree.get_mut(self.tree.focus) } - pub fn ensure_cursor_in_view(&mut self, id: Key) { + pub fn ensure_cursor_in_view(&mut self, id: ViewId) { let view = self.tree.get_mut(id); let doc = &self.documents[view.doc]; view.ensure_cursor_in_view(doc) diff --git a/helix-view/src/lib.rs b/helix-view/src/lib.rs index d98d2c5a..00bddad6 100644 --- a/helix-view/src/lib.rs +++ b/helix-view/src/lib.rs @@ -6,6 +6,7 @@ pub mod view; use slotmap::new_key_type; new_key_type! { pub struct DocumentId; } +new_key_type! { pub struct ViewId; } pub use document::Document; pub use editor::Editor; diff --git a/helix-view/src/tree.rs b/helix-view/src/tree.rs index e56566e3..4751f840 100644 --- a/helix-view/src/tree.rs +++ b/helix-view/src/tree.rs @@ -1,24 +1,24 @@ -use crate::View; -use slotmap::{DefaultKey as Key, HopSlotMap}; +use crate::{View, ViewId}; +use slotmap::HopSlotMap; use tui::layout::Rect; // the dimensions are recomputed on windo resize/tree change. // pub struct Tree { - root: Key, + root: ViewId, // (container, index inside the container) - pub focus: Key, + pub focus: ViewId, // fullscreen: bool, area: Rect, - nodes: HopSlotMap, + nodes: HopSlotMap, // used for traversals - stack: Vec<(Key, Rect)>, + stack: Vec<(ViewId, Rect)>, } pub struct Node { - parent: Key, + parent: ViewId, content: Content, } @@ -30,14 +30,14 @@ pub enum Content { impl Node { pub fn container() -> Self { Node { - parent: Key::default(), + parent: ViewId::default(), content: Content::Container(Box::new(Container::new())), } } pub fn view(view: View) -> Self { Node { - parent: Key::default(), + parent: ViewId::default(), content: Content::View(Box::new(view)), } } @@ -53,7 +53,7 @@ pub enum Layout { pub struct Container { layout: Layout, - children: Vec, + children: Vec, area: Rect, } @@ -77,7 +77,7 @@ impl Tree { pub fn new(area: Rect) -> Self { let root = Node::container(); - let mut nodes = HopSlotMap::new(); + let mut nodes = HopSlotMap::with_key(); let root = nodes.insert(root); // root is it's own parent @@ -93,7 +93,7 @@ impl Tree { } } - pub fn insert(&mut self, view: View) -> Key { + pub fn insert(&mut self, view: View) -> ViewId { let focus = self.focus; let parent = self.nodes[focus].parent; let mut node = Node::view(view); @@ -131,7 +131,7 @@ impl Tree { node } - pub fn remove(&mut self, index: Key) { + pub fn remove(&mut self, index: ViewId) { let mut stack = Vec::new(); if self.focus == index { @@ -188,7 +188,7 @@ impl Tree { }) } - pub fn get(&self, index: Key) -> &View { + pub fn get(&self, index: ViewId) -> &View { match &self.nodes[index] { Node { content: Content::View(view), @@ -198,7 +198,7 @@ impl Tree { } } - pub fn get_mut(&mut self, index: Key) -> &mut View { + pub fn get_mut(&mut self, index: ViewId) -> &mut View { match &mut self.nodes[index] { Node { content: Content::View(view), @@ -355,7 +355,7 @@ impl Tree { pub struct Traverse<'a> { tree: &'a Tree, - stack: Vec, // TODO: reuse the one we use on update + stack: Vec, // TODO: reuse the one we use on update } impl<'a> Traverse<'a> { @@ -368,7 +368,7 @@ impl<'a> Traverse<'a> { } impl<'a> Iterator for Traverse<'a> { - type Item = (Key, &'a View); + type Item = (ViewId, &'a View); fn next(&mut self) -> Option { loop { diff --git a/helix-view/src/view.rs b/helix-view/src/view.rs index 9d3f1412..5f6a0456 100644 --- a/helix-view/src/view.rs +++ b/helix-view/src/view.rs @@ -2,18 +2,17 @@ use anyhow::Error; use std::borrow::Cow; -use crate::{Document, DocumentId}; +use crate::{Document, DocumentId, ViewId}; use helix_core::{ graphemes::{grapheme_width, RopeGraphemes}, Position, RopeSlice, }; -use slotmap::DefaultKey as Key; use tui::layout::Rect; pub const PADDING: usize = 5; pub struct View { - pub id: Key, + pub id: ViewId, pub doc: DocumentId, pub first_line: usize, pub area: Rect, @@ -22,7 +21,7 @@ pub struct View { impl View { pub fn new(doc: DocumentId) -> Result { let view = Self { - id: Key::default(), + id: ViewId::default(), doc, first_line: 0, area: Rect::default(), // will get calculated upon inserting into tree -- cgit v1.2.3-70-g09d2