on master help sign in
Derive a separate ViewId type.
b24cdd1Blaž Hrastnik
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};
2use tui::layout::Rect;
2use tui::layout::Rect;
3
3
4use std::path::PathBuf;
4use std::path::PathBuf;
5
5
6−use slotmap::{DefaultKey as Key, SlotMap};
6+use slotmap::SlotMap;
7
7
8use anyhow::Error;
8use anyhow::Error;
9
9
@@ -90,7 +90,7 @@ impl Editor {
90 Ok(id)
90 Ok(id)
91 }
91 }
92
92
93− pub fn close(&mut self, id: Key) {
93+ pub fn close(&mut self, id: ViewId) {
94 let view = self.tree.get(self.tree.focus);
94 let view = self.tree.get(self.tree.focus);
95 // get around borrowck issues
95 // get around borrowck issues
96 let language_servers = &mut self.language_servers;
96 let language_servers = &mut self.language_servers;
@@ -133,7 +133,7 @@ impl Editor {
133 self.tree.get_mut(self.tree.focus)
133 self.tree.get_mut(self.tree.focus)
134 }
134 }
135
135
136− pub fn ensure_cursor_in_view(&mut self, id: Key) {
136+ pub fn ensure_cursor_in_view(&mut self, id: ViewId) {
137 let view = self.tree.get_mut(id);
137 let view = self.tree.get_mut(id);
138 let doc = &self.documents[view.doc];
138 let doc = &self.documents[view.doc];
139 view.ensure_cursor_in_view(doc)
139 view.ensure_cursor_in_view(doc)
helix-view/src/lib.rs+1
@@ -6,6 +6,7 @@ pub mod view;
6
6
7use slotmap::new_key_type;
7use slotmap::new_key_type;
8new_key_type! { pub struct DocumentId; }
8new_key_type! { pub struct DocumentId; }
9+new_key_type! { pub struct ViewId; }
9
10
10pub use document::Document;
11pub use document::Document;
11pub use editor::Editor;
12pub use editor::Editor;
helix-view/src/tree.rs+17−17
@@ -1,24 +1,24 @@
1−use crate::View;
1+use crate::{View, ViewId};
2−use slotmap::{DefaultKey as Key, HopSlotMap};
2+use slotmap::HopSlotMap;
3use tui::layout::Rect;
3use tui::layout::Rect;
4
4
5// the dimensions are recomputed on windo resize/tree change.
5// the dimensions are recomputed on windo resize/tree change.
6//
6//
7pub struct Tree {
7pub struct Tree {
8− root: Key,
8+ root: ViewId,
9 // (container, index inside the container)
9 // (container, index inside the container)
10− pub focus: Key,
10+ pub focus: ViewId,
11 // fullscreen: bool,
11 // fullscreen: bool,
12 area: Rect,
12 area: Rect,
13
13
14− nodes: HopSlotMap<Key, Node>,
14+ nodes: HopSlotMap<ViewId, Node>,
15
15
16 // used for traversals
16 // used for traversals
17− stack: Vec<(Key, Rect)>,
17+ stack: Vec<(ViewId, Rect)>,
18}
18}
19
19
20pub struct Node {
20pub struct Node {
21− parent: Key,
21+ parent: ViewId,
22 content: Content,
22 content: Content,
23}
23}
24
24
@@ -30,14 +30,14 @@ pub enum Content {
30impl Node {
30impl Node {
31 pub fn container() -> Self {
31 pub fn container() -> Self {
32 Node {
32 Node {
33− parent: Key::default(),
33+ parent: ViewId::default(),
34 content: Content::Container(Box::new(Container::new())),
34 content: Content::Container(Box::new(Container::new())),
35 }
35 }
36 }
36 }
37
37
38 pub fn view(view: View) -> Self {
38 pub fn view(view: View) -> Self {
39 Node {
39 Node {
40− parent: Key::default(),
40+ parent: ViewId::default(),
41 content: Content::View(Box::new(view)),
41 content: Content::View(Box::new(view)),
42 }
42 }
43 }
43 }
@@ -53,7 +53,7 @@ pub enum Layout {
53
53
54pub struct Container {
54pub struct Container {
55 layout: Layout,
55 layout: Layout,
56− children: Vec<Key>,
56+ children: Vec<ViewId>,
57 area: Rect,
57 area: Rect,
58}
58}
59
59
@@ -77,7 +77,7 @@ impl Tree {
77 pub fn new(area: Rect) -> Self {
77 pub fn new(area: Rect) -> Self {
78 let root = Node::container();
78 let root = Node::container();
79
79
80− let mut nodes = HopSlotMap::new();
80+ let mut nodes = HopSlotMap::with_key();
81 let root = nodes.insert(root);
81 let root = nodes.insert(root);
82
82
83 // root is it's own parent
83 // root is it's own parent
@@ -93,7 +93,7 @@ impl Tree {
93 }
93 }
94 }
94 }
95
95
96− pub fn insert(&mut self, view: View) -> Key {
96+ pub fn insert(&mut self, view: View) -> ViewId {
97 let focus = self.focus;
97 let focus = self.focus;
98 let parent = self.nodes[focus].parent;
98 let parent = self.nodes[focus].parent;
99 let mut node = Node::view(view);
99 let mut node = Node::view(view);
@@ -131,7 +131,7 @@ impl Tree {
131 node
131 node
132 }
132 }
133
133
134− pub fn remove(&mut self, index: Key) {
134+ pub fn remove(&mut self, index: ViewId) {
135 let mut stack = Vec::new();
135 let mut stack = Vec::new();
136
136
137 if self.focus == index {
137 if self.focus == index {
@@ -188,7 +188,7 @@ impl Tree {
188 })
188 })
189 }
189 }
190
190
191− pub fn get(&self, index: Key) -> &View {
191+ pub fn get(&self, index: ViewId) -> &View {
192 match &self.nodes[index] {
192 match &self.nodes[index] {
193 Node {
193 Node {
194 content: Content::View(view),
194 content: Content::View(view),
@@ -198,7 +198,7 @@ impl Tree {
198 }
198 }
199 }
199 }
200
200
201− pub fn get_mut(&mut self, index: Key) -> &mut View {
201+ pub fn get_mut(&mut self, index: ViewId) -> &mut View {
202 match &mut self.nodes[index] {
202 match &mut self.nodes[index] {
203 Node {
203 Node {
204 content: Content::View(view),
204 content: Content::View(view),
@@ -355,7 +355,7 @@ impl Tree {
355
355
356pub struct Traverse<'a> {
356pub struct Traverse<'a> {
357 tree: &'a Tree,
357 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
359}
359}
360
360
361impl<'a> Traverse<'a> {
361impl<'a> Traverse<'a> {
@@ -368,7 +368,7 @@ impl<'a> Traverse<'a> {
368}
368}
369
369
370impl<'a> Iterator for Traverse<'a> {
370impl<'a> Iterator for Traverse<'a> {
371− type Item = (Key, &'a View);
371+ type Item = (ViewId, &'a View);
372
372
373 fn next(&mut self) -> Option<Self::Item> {
373 fn next(&mut self) -> Option<Self::Item> {
374 loop {
374 loop {
helix-view/src/view.rs+3−4
@@ -2,18 +2,17 @@ use anyhow::Error;
2
2
3use std::borrow::Cow;
3use std::borrow::Cow;
4
4
5−use crate::{Document, DocumentId};
5+use crate::{Document, DocumentId, ViewId};
6use helix_core::{
6use helix_core::{
7 graphemes::{grapheme_width, RopeGraphemes},
7 graphemes::{grapheme_width, RopeGraphemes},
8 Position, RopeSlice,
8 Position, RopeSlice,
9};
9};
10−use slotmap::DefaultKey as Key;
11use tui::layout::Rect;
10use tui::layout::Rect;
12
11
13pub const PADDING: usize = 5;
12pub const PADDING: usize = 5;
14
13
15pub struct View {
14pub struct View {
16− pub id: Key,
15+ pub id: ViewId,
17 pub doc: DocumentId,
16 pub doc: DocumentId,
18 pub first_line: usize,
17 pub first_line: usize,
19 pub area: Rect,
18 pub area: Rect,
@@ -22,7 +21,7 @@ pub struct View {
22impl View {
21impl View {
23 pub fn new(doc: DocumentId) -> Result<Self, Error> {
22 pub fn new(doc: DocumentId) -> Result<Self, Error> {
24 let view = Self {
23 let view = Self {
25− id: Key::default(),
24+ id: ViewId::default(),
26 doc,
25 doc,
27 first_line: 0,
26 first_line: 0,
28 area: Rect::default(), // will get calculated upon inserting into tree
27 area: Rect::default(), // will get calculated upon inserting into tree