Derive a separate ViewId type.

imgbot
Blaž Hrastnik 4 years ago
parent 8328fe926d
commit b24cdd1295

@ -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 tui::layout::Rect;
use std::path::PathBuf; use std::path::PathBuf;
use slotmap::{DefaultKey as Key, SlotMap}; use slotmap::SlotMap;
use anyhow::Error; use anyhow::Error;
@ -90,7 +90,7 @@ impl Editor {
Ok(id) Ok(id)
} }
pub fn close(&mut self, id: Key) { pub fn close(&mut self, id: ViewId) {
let view = self.tree.get(self.tree.focus); let view = self.tree.get(self.tree.focus);
// get around borrowck issues // get around borrowck issues
let language_servers = &mut self.language_servers; let language_servers = &mut self.language_servers;
@ -133,7 +133,7 @@ impl Editor {
self.tree.get_mut(self.tree.focus) 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 view = self.tree.get_mut(id);
let doc = &self.documents[view.doc]; let doc = &self.documents[view.doc];
view.ensure_cursor_in_view(doc) view.ensure_cursor_in_view(doc)

@ -6,6 +6,7 @@ pub mod view;
use slotmap::new_key_type; use slotmap::new_key_type;
new_key_type! { pub struct DocumentId; } new_key_type! { pub struct DocumentId; }
new_key_type! { pub struct ViewId; }
pub use document::Document; pub use document::Document;
pub use editor::Editor; pub use editor::Editor;

@ -1,24 +1,24 @@
use crate::View; use crate::{View, ViewId};
use slotmap::{DefaultKey as Key, HopSlotMap}; use slotmap::HopSlotMap;
use tui::layout::Rect; use tui::layout::Rect;
// the dimensions are recomputed on windo resize/tree change. // the dimensions are recomputed on windo resize/tree change.
// //
pub struct Tree { pub struct Tree {
root: Key, root: ViewId,
// (container, index inside the container) // (container, index inside the container)
pub focus: Key, pub focus: ViewId,
// fullscreen: bool, // fullscreen: bool,
area: Rect, area: Rect,
nodes: HopSlotMap<Key, Node>, nodes: HopSlotMap<ViewId, Node>,
// used for traversals // used for traversals
stack: Vec<(Key, Rect)>, stack: Vec<(ViewId, Rect)>,
} }
pub struct Node { pub struct Node {
parent: Key, parent: ViewId,
content: Content, content: Content,
} }
@ -30,14 +30,14 @@ pub enum Content {
impl Node { impl Node {
pub fn container() -> Self { pub fn container() -> Self {
Node { Node {
parent: Key::default(), parent: ViewId::default(),
content: Content::Container(Box::new(Container::new())), content: Content::Container(Box::new(Container::new())),
} }
} }
pub fn view(view: View) -> Self { pub fn view(view: View) -> Self {
Node { Node {
parent: Key::default(), parent: ViewId::default(),
content: Content::View(Box::new(view)), content: Content::View(Box::new(view)),
} }
} }
@ -53,7 +53,7 @@ pub enum Layout {
pub struct Container { pub struct Container {
layout: Layout, layout: Layout,
children: Vec<Key>, children: Vec<ViewId>,
area: Rect, area: Rect,
} }
@ -77,7 +77,7 @@ impl Tree {
pub fn new(area: Rect) -> Self { pub fn new(area: Rect) -> Self {
let root = Node::container(); let root = Node::container();
let mut nodes = HopSlotMap::new(); let mut nodes = HopSlotMap::with_key();
let root = nodes.insert(root); let root = nodes.insert(root);
// root is it's own parent // 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 focus = self.focus;
let parent = self.nodes[focus].parent; let parent = self.nodes[focus].parent;
let mut node = Node::view(view); let mut node = Node::view(view);
@ -131,7 +131,7 @@ impl Tree {
node node
} }
pub fn remove(&mut self, index: Key) { pub fn remove(&mut self, index: ViewId) {
let mut stack = Vec::new(); let mut stack = Vec::new();
if self.focus == index { 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] { match &self.nodes[index] {
Node { Node {
content: Content::View(view), 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] { match &mut self.nodes[index] {
Node { Node {
content: Content::View(view), content: Content::View(view),
@ -355,7 +355,7 @@ impl Tree {
pub struct Traverse<'a> { pub struct Traverse<'a> {
tree: &'a Tree, tree: &'a Tree,
stack: Vec<Key>, // TODO: reuse the one we use on update stack: Vec<ViewId>, // TODO: reuse the one we use on update
} }
impl<'a> Traverse<'a> { impl<'a> Traverse<'a> {
@ -368,7 +368,7 @@ impl<'a> Traverse<'a> {
} }
impl<'a> Iterator for Traverse<'a> { impl<'a> Iterator for Traverse<'a> {
type Item = (Key, &'a View); type Item = (ViewId, &'a View);
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
loop { loop {

@ -2,18 +2,17 @@ use anyhow::Error;
use std::borrow::Cow; use std::borrow::Cow;
use crate::{Document, DocumentId}; use crate::{Document, DocumentId, ViewId};
use helix_core::{ use helix_core::{
graphemes::{grapheme_width, RopeGraphemes}, graphemes::{grapheme_width, RopeGraphemes},
Position, RopeSlice, Position, RopeSlice,
}; };
use slotmap::DefaultKey as Key;
use tui::layout::Rect; use tui::layout::Rect;
pub const PADDING: usize = 5; pub const PADDING: usize = 5;
pub struct View { pub struct View {
pub id: Key, pub id: ViewId,
pub doc: DocumentId, pub doc: DocumentId,
pub first_line: usize, pub first_line: usize,
pub area: Rect, pub area: Rect,
@ -22,7 +21,7 @@ pub struct View {
impl View { impl View {
pub fn new(doc: DocumentId) -> Result<Self, Error> { pub fn new(doc: DocumentId) -> Result<Self, Error> {
let view = Self { let view = Self {
id: Key::default(), id: ViewId::default(),
doc, doc,
first_line: 0, first_line: 0,
area: Rect::default(), // will get calculated upon inserting into tree area: Rect::default(), // will get calculated upon inserting into tree

Loading…
Cancel
Save