From 8a0ab447ecfa6c2c448603d469ba4fd06e95c754 Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Wed, 24 Mar 2021 14:28:26 +0900 Subject: editor.open can now either replace the current view or open in a split. --- helix-view/src/editor.rs | 103 ++++++++++++++++++++++++++++------------------- helix-view/src/tree.rs | 86 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 141 insertions(+), 48 deletions(-) (limited to 'helix-view/src') diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 0407f344..a2c3c101 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -16,6 +16,12 @@ pub struct Editor { pub executor: &'static smol::Executor<'static>, } +pub enum Action { + Replace, + HorizontalSplit, + VerticalSplit, +} + impl Editor { pub fn new(executor: &'static smol::Executor<'static>, mut area: tui::layout::Rect) -> Self { let theme = Theme::default(); @@ -41,50 +47,63 @@ impl Editor { } } - pub fn open(&mut self, path: PathBuf) -> Result { - let existing_view = self.documents().find(|doc| doc.path() == Some(&path)); - - // TODO: - // if view with doc, focus it - // else open new split - - // if let Some((view, _)) = existing_view { - // let id = view.doc.id; - // self.tree.focus = view.id; - // return Ok(id); - // } - - let mut doc = Document::load(path, self.theme.scopes())?; - - // try to find a language server based on the language name - let language_server = doc - .language - .as_ref() - .and_then(|language| self.language_servers.get(language, self.executor)); - - if let Some(language_server) = language_server { - doc.set_language_server(Some(language_server.clone())); - - let language_id = doc - .language() - .and_then(|s| s.split('.').last()) // source.rust - .map(ToOwned::to_owned) - .unwrap_or_default(); - - smol::block_on(language_server.text_document_did_open( - doc.url().unwrap(), - doc.version(), - doc.text(), - language_id, - )) - .unwrap(); + pub fn open(&mut self, path: PathBuf, action: Action) -> Result { + let id = self + .documents() + .find(|doc| doc.path() == Some(&path)) + .map(|doc| doc.id); + + let id = if let Some(id) = id { + id + } else { + let mut doc = Document::load(path, self.theme.scopes())?; + + // try to find a language server based on the language name + let language_server = doc + .language + .as_ref() + .and_then(|language| self.language_servers.get(language, self.executor)); + + if let Some(language_server) = language_server { + doc.set_language_server(Some(language_server.clone())); + + let language_id = doc + .language() + .and_then(|s| s.split('.').last()) // source.rust + .map(ToOwned::to_owned) + .unwrap_or_default(); + + smol::block_on(language_server.text_document_did_open( + doc.url().unwrap(), + doc.version(), + doc.text(), + language_id, + )) + .unwrap(); + } + + let id = self.documents.insert(doc); + self.documents[id].id = id; + id + }; + + use crate::tree::Layout; + match action { + Action::Replace => { + self.view_mut().doc = id; + // TODO: reset selection? + return Ok(id); + } + Action::HorizontalSplit => { + let view = View::new(id)?; + self.tree.split(view, Layout::Horizontal); + } + Action::VerticalSplit => { + let view = View::new(id)?; + self.tree.split(view, Layout::Vertical); + } } - let id = self.documents.insert(doc); - self.documents[id].id = id; - - let view = View::new(id)?; - self.tree.insert(view); self._refresh(); Ok(id) diff --git a/helix-view/src/tree.rs b/helix-view/src/tree.rs index 4751f840..f7ef7806 100644 --- a/helix-view/src/tree.rs +++ b/helix-view/src/tree.rs @@ -28,10 +28,10 @@ pub enum Content { } impl Node { - pub fn container() -> Self { + pub fn container(layout: Layout) -> Self { Node { parent: ViewId::default(), - content: Content::Container(Box::new(Container::new())), + content: Content::Container(Box::new(Container::new(layout))), } } @@ -45,6 +45,7 @@ impl Node { // TODO: screen coord to container + container coordinate helpers +#[derive(PartialEq, Eq)] pub enum Layout { Horizontal, Vertical, @@ -58,9 +59,9 @@ pub struct Container { } impl Container { - pub fn new() -> Self { + pub fn new(layout: Layout) -> Self { Self { - layout: Layout::Horizontal, + layout, children: Vec::new(), area: Rect::default(), } @@ -69,13 +70,13 @@ impl Container { impl Default for Container { fn default() -> Self { - Self::new() + Self::new(Layout::Horizontal) } } impl Tree { pub fn new(area: Rect) -> Self { - let root = Node::container(); + let root = Node::container(Layout::Horizontal); let mut nodes = HopSlotMap::with_key(); let root = nodes.insert(root); @@ -131,6 +132,79 @@ impl Tree { node } + pub fn split(&mut self, view: View, layout: Layout) -> ViewId { + let focus = self.focus; + let parent = self.nodes[focus].parent; + + let node = Node::view(view); + let node = self.nodes.insert(node); + self.get_mut(node).id = node; + + let container = match &mut self.nodes[parent] { + Node { + content: Content::Container(container), + .. + } => container, + _ => unreachable!(), + }; + + if container.layout == layout { + // insert node after the current item if there is children already + let pos = if container.children.is_empty() { + 0 + } else { + let pos = container + .children + .iter() + .position(|&child| child == focus) + .unwrap(); + pos + 1 + }; + container.children.insert(pos, node); + self.nodes[node].parent = parent; + } else { + let split = Node::container(layout); + let split = self.nodes.insert(split); + + let container = match &mut self.nodes[split] { + Node { + content: Content::Container(container), + .. + } => container, + _ => unreachable!(), + }; + container.children.push(focus); + container.children.push(node); + self.nodes[focus].parent = split; + self.nodes[node].parent = split; + + let container = match &mut self.nodes[parent] { + Node { + content: Content::Container(container), + .. + } => container, + _ => unreachable!(), + }; + + let pos = container + .children + .iter() + .position(|&child| child == focus) + .unwrap(); + + // replace focus on parent with split + container.children[pos] = split; + } + + // focus the new node + self.focus = node; + + // recalculate all the sizes + self.recalculate(); + + node + } + pub fn remove(&mut self, index: ViewId) { let mut stack = Vec::new(); -- cgit v1.2.3-70-g09d2