on master help sign in
Derive a separate ViewId type.
commitb24cdd1295d1129dc730e02da2c6723938220d7e authorBlaž Hrastnik <blaz@mxxn.io> dateWed 24 Mar 2021, 14:03 (6 years ago) parent8328fe9
4 files changed+25−25
helix-view/src/editor.rs+4−4 helix-view/src/lib.rs+1 helix-view/src/tree.rs+17−17 helix-view/src/view.rs+3−4
helix-view/src/editor.rs+4−4
@@ -1,9 +1,9 @@
1−use crate::{theme::Theme, tree::Tree, Document, DocumentId, View};
1+use crate::{theme::Theme, tree::Tree, Document, DocumentId, View, ViewId};
22use tui::layout::Rect;
33
44use std::path::PathBuf;
55
6−use slotmap::{DefaultKey as Key, SlotMap};
6+use slotmap::SlotMap;
77
88use anyhow::Error;
99
@@ -90,7 +90,7 @@ impl Editor {
9090 Ok(id)
9191 }
9292
93− pub fn close(&mut self, id: Key) {
93+ pub fn close(&mut self, id: ViewId) {
9494 let view = self.tree.get(self.tree.focus);
9595 // get around borrowck issues
9696 let language_servers = &mut self.language_servers;
@@ -133,7 +133,7 @@ impl Editor {
133133 self.tree.get_mut(self.tree.focus)
134134 }
135135
136− pub fn ensure_cursor_in_view(&mut self, id: Key) {
136+ pub fn ensure_cursor_in_view(&mut self, id: ViewId) {
137137 let view = self.tree.get_mut(id);
138138 let doc = &self.documents[view.doc];
139139 view.ensure_cursor_in_view(doc)
helix-view/src/lib.rs+1
@@ -6,6 +6,7 @@ pub mod view;
66
77use slotmap::new_key_type;
88new_key_type! { pub struct DocumentId; }
9+new_key_type! { pub struct ViewId; }
910
1011pub use document::Document;
1112pub use editor::Editor;
helix-view/src/tree.rs+17−17
@@ -1,24 +1,24 @@
1−use crate::View;
2−use slotmap::{DefaultKey as Key, HopSlotMap};
1+use crate::{View, ViewId};
2+use slotmap::HopSlotMap;
33use tui::layout::Rect;
44
55// the dimensions are recomputed on windo resize/tree change.
66//
77pub struct Tree {
8− root: Key,
8+ root: ViewId,
99 // (container, index inside the container)
10− pub focus: Key,
10+ pub focus: ViewId,
1111 // fullscreen: bool,
1212 area: Rect,
1313
14− nodes: HopSlotMap<Key, Node>,
14+ nodes: HopSlotMap<ViewId, Node>,
1515
1616 // used for traversals
17− stack: Vec<(Key, Rect)>,
17+ stack: Vec<(ViewId, Rect)>,
1818}
1919
2020pub struct Node {
21− parent: Key,
21+ parent: ViewId,
2222 content: Content,
2323}
2424
@@ -30,14 +30,14 @@ pub enum Content {
3030impl Node {
3131 pub fn container() -> Self {
3232 Node {
33− parent: Key::default(),
33+ parent: ViewId::default(),
3434 content: Content::Container(Box::new(Container::new())),
3535 }
3636 }
3737
3838 pub fn view(view: View) -> Self {
3939 Node {
40− parent: Key::default(),
40+ parent: ViewId::default(),
4141 content: Content::View(Box::new(view)),
4242 }
4343 }
@@ -53,7 +53,7 @@ pub enum Layout {
5353
5454pub struct Container {
5555 layout: Layout,
56− children: Vec<Key>,
56+ children: Vec<ViewId>,
5757 area: Rect,
5858}
5959
@@ -77,7 +77,7 @@ impl Tree {
7777 pub fn new(area: Rect) -> Self {
7878 let root = Node::container();
7979
80− let mut nodes = HopSlotMap::new();
80+ let mut nodes = HopSlotMap::with_key();
8181 let root = nodes.insert(root);
8282
8383 // root is it's own parent
@@ -93,7 +93,7 @@ impl Tree {
9393 }
9494 }
9595
96− pub fn insert(&mut self, view: View) -> Key {
96+ pub fn insert(&mut self, view: View) -> ViewId {
9797 let focus = self.focus;
9898 let parent = self.nodes[focus].parent;
9999 let mut node = Node::view(view);
@@ -131,7 +131,7 @@ impl Tree {
131131 node
132132 }
133133
134− pub fn remove(&mut self, index: Key) {
134+ pub fn remove(&mut self, index: ViewId) {
135135 let mut stack = Vec::new();
136136
137137 if self.focus == index {
@@ -188,7 +188,7 @@ impl Tree {
188188 })
189189 }
190190
191− pub fn get(&self, index: Key) -> &View {
191+ pub fn get(&self, index: ViewId) -> &View {
192192 match &self.nodes[index] {
193193 Node {
194194 content: Content::View(view),
@@ -198,7 +198,7 @@ impl Tree {
198198 }
199199 }
200200
201− pub fn get_mut(&mut self, index: Key) -> &mut View {
201+ pub fn get_mut(&mut self, index: ViewId) -> &mut View {
202202 match &mut self.nodes[index] {
203203 Node {
204204 content: Content::View(view),
@@ -355,7 +355,7 @@ impl Tree {
355355
356356pub struct Traverse<'a> {
357357 tree: &'a Tree,
358− stack: Vec<Key>, // TODO: reuse the one we use on update
358+ stack: Vec<ViewId>, // TODO: reuse the one we use on update
359359}
360360
361361impl<'a> Traverse<'a> {
@@ -368,7 +368,7 @@ impl<'a> Traverse<'a> {
368368}
369369
370370impl<'a> Iterator for Traverse<'a> {
371− type Item = (Key, &'a View);
371+ type Item = (ViewId, &'a View);
372372
373373 fn next(&mut self) -> Option<Self::Item> {
374374 loop {
helix-view/src/view.rs+3−4
@@ -2,18 +2,17 @@ use anyhow::Error;
22
33use std::borrow::Cow;
44
5−use crate::{Document, DocumentId};
5+use crate::{Document, DocumentId, ViewId};
66use helix_core::{
77 graphemes::{grapheme_width, RopeGraphemes},
88 Position, RopeSlice,
99};
10−use slotmap::DefaultKey as Key;
1110use tui::layout::Rect;
1211
1312pub const PADDING: usize = 5;
1413
1514pub struct View {
16− pub id: Key,
15+ pub id: ViewId,
1716 pub doc: DocumentId,
1817 pub first_line: usize,
1918 pub area: Rect,
@@ -22,7 +21,7 @@ pub struct View {
2221impl View {
2322 pub fn new(doc: DocumentId) -> Result<Self, Error> {
2423 let view = Self {
25− id: Key::default(),
24+ id: ViewId::default(),
2625 doc,
2726 first_line: 0,
2827 area: Rect::default(), // will get calculated upon inserting into tree