From 4e461bea2f4cc19d0d25a62a4f278420c074b6e9 Mon Sep 17 00:00:00 2001 From: Jan Hrastnik Date: Mon, 15 Mar 2021 21:12:41 +0100 Subject: [PATCH] editor.open now checks if view already exists --- helix-term/src/commands.rs | 15 ++++++++------- helix-view/src/editor.rs | 10 +++++++++- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 22a8737f2..20f51ef4b 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -852,7 +852,7 @@ pub fn exit_select_mode(cx: &mut Context) { cx.doc().mode = Mode::Normal; } -fn goto(cx: &'static mut Context<'static>, locations: Vec) { +fn goto(cx: &mut Context, locations: Vec) { let doc = cx.doc(); doc.mode = Mode::Normal; @@ -878,8 +878,9 @@ fn goto(cx: &'static mut Context<'static>, locations: Vec) { format!("{}:{}", file, line).into() }, move |editor: &mut Editor, item| { - cx.editor.open(PathBuf::from(item.uri.path()), cx.executor); - let doc = cx.doc(); + let executor = smol::Executor::new(); + editor.open(PathBuf::from(item.uri.path()), &executor); + let mut doc = &mut editor.view_mut().doc; let definition_pos = item.range.start; let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text().slice(..), definition_pos); @@ -891,7 +892,7 @@ fn goto(cx: &'static mut Context<'static>, locations: Vec) { } } -pub fn goto_definition(cx: &'static mut Context<'static>) { +pub fn goto_definition(cx: &mut Context) { let doc = cx.doc(); let language_server = match doc.language_server.as_ref() { Some(language_server) => language_server, @@ -907,7 +908,7 @@ pub fn goto_definition(cx: &'static mut Context<'static>) { goto(cx, res); } -pub fn goto_type_definition(cx: &'static mut Context<'static>) { +pub fn goto_type_definition(cx: &mut Context) { let doc = cx.doc(); let language_server = match doc.language_server.as_ref() { Some(language_server) => language_server, @@ -923,7 +924,7 @@ pub fn goto_type_definition(cx: &'static mut Context<'static>) { goto(cx, res); } -pub fn goto_implementation(cx: &'static mut Context<'static>) { +pub fn goto_implementation(cx: &mut Context) { let doc = cx.doc(); let language_server = match doc.language_server.as_ref() { Some(language_server) => language_server, @@ -939,7 +940,7 @@ pub fn goto_implementation(cx: &'static mut Context<'static>) { goto(cx, res); } -pub fn goto_reference(cx: &'static mut Context<'static>) { +pub fn goto_reference(cx: &mut Context) { let doc = cx.doc(); let language_server = match doc.language_server.as_ref() { Some(language_server) => language_server, diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index b04a07ddf..38182126b 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -62,7 +62,15 @@ impl Editor { } let view = View::new(doc)?; - self.tree.insert(view); + let existing_view_option = self + .tree + .views() + .find(|v| view.doc.path().unwrap().to_str() == v.0.doc.path().unwrap().to_str()); + if let Some(existing_view) = existing_view_option { + self.tree.focus = existing_view.0.id; + } else { + self.tree.insert(view); + } Ok(()) }