Derive a separate ViewId type.

This commit is contained in:
Blaž Hrastnik 2021-03-24 14:03:20 +09:00
parent 8328fe926d
commit b24cdd1295
4 changed files with 25 additions and 25 deletions

View File

@ -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 @@ pub fn open(&mut self, path: PathBuf) -> Result<DocumentId, Error> {
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 @@ pub fn view_mut(&mut self) -> &mut View {
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)

View File

@ -6,6 +6,7 @@
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;

View File

@ -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<Key, Node>,
nodes: HopSlotMap<ViewId, Node>,
// 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<Key>,
children: Vec<ViewId>,
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 @@ pub fn new(area: Rect) -> Self {
}
}
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 @@ pub fn insert(&mut self, view: View) -> Key {
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 @@ pub fn views_mut(&mut self) -> impl Iterator<Item = (&mut View, bool)> {
})
}
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 @@ pub fn get(&self, index: Key) -> &View {
}
}
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 @@ pub fn focus_next(&mut self) {
pub struct Traverse<'a> {
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> {
@ -368,7 +368,7 @@ fn new(tree: &'a Tree) -> Self {
}
impl<'a> Iterator for Traverse<'a> {
type Item = (Key, &'a View);
type Item = (ViewId, &'a View);
fn next(&mut self) -> Option<Self::Item> {
loop {

View File

@ -2,18 +2,17 @@
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<Self, Error> {
let view = Self {
id: Key::default(),
id: ViewId::default(),
doc,
first_line: 0,
area: Rect::default(), // will get calculated upon inserting into tree