Wire up opening in splits via pickers.

This commit is contained in:
Blaž Hrastnik 2021-03-29 15:21:48 +09:00
parent aefafc25cd
commit 5c2d2fda21
3 changed files with 29 additions and 18 deletions

View File

@ -864,7 +864,7 @@ pub fn buffer_picker(cx: &mut Context) {
None => "[NEW]".into(), None => "[NEW]".into(),
} }
}, },
|editor: &mut Editor, (_, path): &(DocumentId, Option<PathBuf>)| match path { |editor: &mut Editor, (_, path): &(DocumentId, Option<PathBuf>), _action| match path {
Some(path) => { Some(path) => {
use helix_view::editor::Action; use helix_view::editor::Action;
editor editor
@ -1082,10 +1082,10 @@ fn _goto(cx: &mut Context, locations: Vec<lsp::Location>) {
let line = item.range.start.line; let line = item.range.start.line;
format!("{}:{}", file, line).into() format!("{}:{}", file, line).into()
}, },
move |editor: &mut Editor, item| { move |editor: &mut Editor, item, action| {
editor.open(PathBuf::from(item.uri.path()), Action::Replace); let id = editor
// TODO: issues with doc already being broo .open(PathBuf::from(item.uri.path()), action)
let id = editor.view().doc; .expect("editor.open failed");
let doc = &mut editor.documents[id]; let doc = &mut editor.documents[id];
let definition_pos = item.range.start; let definition_pos = item.range.start;
let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text(), definition_pos); let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text(), definition_pos);

View File

@ -103,10 +103,9 @@ pub fn file_picker(root: &str) -> Picker<PathBuf> {
// format_fn // format_fn
path.strip_prefix("./").unwrap().to_str().unwrap().into() path.strip_prefix("./").unwrap().to_str().unwrap().into()
}, },
move |editor: &mut Editor, path: &PathBuf| { move |editor: &mut Editor, path: &PathBuf, action| {
use helix_view::editor::Action;
let document_id = editor let document_id = editor
.open(path.into(), Action::Replace) .open(path.into(), action)
.expect("editor.open failed"); .expect("editor.open failed");
}, },
) )

View File

@ -14,6 +14,7 @@
use crate::ui::{Prompt, PromptEvent}; use crate::ui::{Prompt, PromptEvent};
use helix_core::Position; use helix_core::Position;
use helix_view::editor::Action;
use helix_view::Editor; use helix_view::Editor;
pub struct Picker<T> { pub struct Picker<T> {
@ -28,14 +29,14 @@ pub struct Picker<T> {
prompt: Prompt, prompt: Prompt,
format_fn: Box<dyn Fn(&T) -> Cow<str>>, 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> { impl<T> Picker<T> {
pub fn new( pub fn new(
options: Vec<T>, options: Vec<T>,
format_fn: impl Fn(&T) -> Cow<str> + 'static, 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 { ) -> Self {
let prompt = Prompt::new( let prompt = Prompt::new(
"".to_string(), "".to_string(),
@ -133,13 +134,6 @@ fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult {
))); )));
match key_event { 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 { KeyEvent {
code: KeyCode::Up, .. code: KeyCode::Up, ..
} }
@ -165,7 +159,25 @@ fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult {
.. ..
} => { } => {
if let Some(option) = self.selection() { 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; return close_fn;
} }