From 5c2d2fda213164fe2cba005f28cee263486587ed Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= <blaz@mxxn.io>
Date: Mon, 29 Mar 2021 15:21:48 +0900
Subject: [PATCH] Wire up opening in splits via pickers.

---
 helix-term/src/commands.rs  | 10 +++++-----
 helix-term/src/ui/mod.rs    |  5 ++---
 helix-term/src/ui/picker.rs | 32 ++++++++++++++++++++++----------
 3 files changed, 29 insertions(+), 18 deletions(-)

diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index e1a03ee1..0745c624 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -864,7 +864,7 @@ pub fn buffer_picker(cx: &mut Context) {
                 None => "[NEW]".into(),
             }
         },
-        |editor: &mut Editor, (_, path): &(DocumentId, Option<PathBuf>)| match path {
+        |editor: &mut Editor, (_, path): &(DocumentId, Option<PathBuf>), _action| match path {
             Some(path) => {
                 use helix_view::editor::Action;
                 editor
@@ -1082,10 +1082,10 @@ fn _goto(cx: &mut Context, locations: Vec<lsp::Location>) {
                     let line = item.range.start.line;
                     format!("{}:{}", file, line).into()
                 },
-                move |editor: &mut Editor, item| {
-                    editor.open(PathBuf::from(item.uri.path()), Action::Replace);
-                    // TODO: issues with doc already being broo
-                    let id = editor.view().doc;
+                move |editor: &mut Editor, item, action| {
+                    let id = editor
+                        .open(PathBuf::from(item.uri.path()), action)
+                        .expect("editor.open failed");
                     let doc = &mut editor.documents[id];
                     let definition_pos = item.range.start;
                     let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text(), definition_pos);
diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs
index 2d282867..8cb82fcb 100644
--- a/helix-term/src/ui/mod.rs
+++ b/helix-term/src/ui/mod.rs
@@ -103,10 +103,9 @@ pub fn file_picker(root: &str) -> Picker<PathBuf> {
             // format_fn
             path.strip_prefix("./").unwrap().to_str().unwrap().into()
         },
-        move |editor: &mut Editor, path: &PathBuf| {
-            use helix_view::editor::Action;
+        move |editor: &mut Editor, path: &PathBuf, action| {
             let document_id = editor
-                .open(path.into(), Action::Replace)
+                .open(path.into(), action)
                 .expect("editor.open failed");
         },
     )
diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs
index 853164c5..6ac35fba 100644
--- a/helix-term/src/ui/picker.rs
+++ b/helix-term/src/ui/picker.rs
@@ -14,6 +14,7 @@ use std::borrow::Cow;
 
 use crate::ui::{Prompt, PromptEvent};
 use helix_core::Position;
+use helix_view::editor::Action;
 use helix_view::Editor;
 
 pub struct Picker<T> {
@@ -28,14 +29,14 @@ pub struct Picker<T> {
     prompt: Prompt,
 
     format_fn: Box<dyn Fn(&T) -> Cow<str>>,
-    callback_fn: Box<dyn Fn(&mut Editor, &T)>,
+    callback_fn: Box<dyn Fn(&mut Editor, &T, Action)>,
 }
 
 impl<T> Picker<T> {
     pub fn new(
         options: Vec<T>,
         format_fn: impl Fn(&T) -> Cow<str> + 'static,
-        callback_fn: impl Fn(&mut Editor, &T) + 'static,
+        callback_fn: impl Fn(&mut Editor, &T, Action) + 'static,
     ) -> Self {
         let prompt = Prompt::new(
             "".to_string(),
@@ -133,13 +134,6 @@ impl<T> Component for Picker<T> {
         )));
 
         match key_event {
-            // KeyEvent {
-            //     code: KeyCode::Char(c),
-            //     modifiers: KeyModifiers::NONE,
-            // } => {
-            //     self.insert_char(c);
-            //     (self.callback_fn)(cx.editor, &self.line, PromptEvent::Update);
-            // }
             KeyEvent {
                 code: KeyCode::Up, ..
             }
@@ -165,7 +159,25 @@ impl<T> Component for Picker<T> {
                 ..
             } => {
                 if let Some(option) = self.selection() {
-                    (self.callback_fn)(&mut cx.editor, option);
+                    (self.callback_fn)(&mut cx.editor, option, Action::Replace);
+                }
+                return close_fn;
+            }
+            KeyEvent {
+                code: KeyCode::Char('x'),
+                modifiers: KeyModifiers::CONTROL,
+            } => {
+                if let Some(option) = self.selection() {
+                    (self.callback_fn)(&mut cx.editor, option, Action::VerticalSplit);
+                }
+                return close_fn;
+            }
+            KeyEvent {
+                code: KeyCode::Char('v'),
+                modifiers: KeyModifiers::CONTROL,
+            } => {
+                if let Some(option) = self.selection() {
+                    (self.callback_fn)(&mut cx.editor, option, Action::HorizontalSplit);
                 }
                 return close_fn;
             }