From b738ae1bc7f42ce6756ee5d79307e416f86ef491 Mon Sep 17 00:00:00 2001 From: Jan Hrastnik Date: Tue, 2 Mar 2021 23:57:18 +0100 Subject: more goto lsp functions --- helix-view/src/document.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'helix-view') diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 82258bde..9ec70023 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -19,7 +19,7 @@ pub enum Mode { pub struct Document { pub state: State, // rope + selection /// File path on disk. - path: Option, + pub path: Option, // pub for testing /// Current editing mode. pub mode: Mode, -- cgit v1.2.3-70-g09d2 From 3869d7713e2dfd2621f7e5a656ebac0b13d10542 Mon Sep 17 00:00:00 2001 From: Jan Hrastnik Date: Sun, 7 Mar 2021 19:41:49 +0100 Subject: added goto functions in helix-term --- Cargo.lock | 1 + helix-term/src/commands.rs | 97 +++++++++++++++++++++++++++++++++++----------- helix-view/src/document.rs | 2 +- 3 files changed, 76 insertions(+), 24 deletions(-) (limited to 'helix-view') diff --git a/Cargo.lock b/Cargo.lock index 5fac3391..ac1f172a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -550,6 +550,7 @@ dependencies = [ "helix-view", "ignore", "log", + "lsp-types", "num_cpus", "once_cell", "pulldown-cmark", diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 9b48e803..87d7ea79 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -13,6 +13,11 @@ use once_cell::sync::Lazy; use crate::compositor::Compositor; use crate::ui::{self, Popup, Prompt, PromptEvent}; +use lsp_types as lsp; +use std::path::PathBuf; + +use smol::Executor; + use helix_view::{ document::Mode, view::{View, PADDING}, @@ -846,34 +851,13 @@ pub fn exit_select_mode(cx: &mut Context) { cx.doc().mode = Mode::Normal; } -pub fn goto_definition(cx: &mut Context) { +pub fn goto_generic(cx: &mut Context, res: Vec) { let doc = cx.doc(); - let language_server = match doc.language_server.as_ref() { - Some(language_server) => language_server, - None => return, - }; - - // TODO: blocking here is not ideal - let pos = helix_lsp::util::pos_to_lsp_pos(doc.text().slice(..), doc.selection().cursor()); - - // TODO: handle fails - let res = - smol::block_on(language_server.goto_definition(doc.identifier(), pos)).unwrap_or_default(); - - /* - if res.len() == 1 { - let definition_pos = res.get(0).unwrap().range.start; - let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text().slice(..), definition_pos); - doc.set_selection(Selection::point(new_pos)); - } else { - // show menu picker i guess - }*/ - doc.mode = Mode::Normal; log::info!("{:?}", res); - let filepath = doc.path.clone().unwrap(); + let filepath = doc.path().unwrap(); log::info!("{:?}", filepath); match &res.as_slice() { @@ -884,6 +868,9 @@ pub fn goto_definition(cx: &mut Context) { doc.set_selection(Selection::point(new_pos)); } else { // open new file + cx.editor + .open(PathBuf::from(location.uri.path()), cx.executor); + // TODO: go to position } } [] => (), // maybe show user message that no definition was found? @@ -907,6 +894,70 @@ pub fn goto_definition(cx: &mut Context) { } } +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, + None => return, + }; + + // TODO: blocking here is not ideal + let pos = helix_lsp::util::pos_to_lsp_pos(doc.text().slice(..), doc.selection().cursor()); + + // TODO: handle fails + let res = + smol::block_on(language_server.goto_definition(doc.identifier(), pos)).unwrap_or_default(); + goto_generic(cx, res); +} + +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, + None => return, + }; + + // TODO: blocking here is not ideal + let pos = helix_lsp::util::pos_to_lsp_pos(doc.text().slice(..), doc.selection().cursor()); + + // TODO: handle fails + let res = smol::block_on(language_server.goto_type_definition(doc.identifier(), pos)) + .unwrap_or_default(); + goto_generic(cx, res); +} + +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, + None => return, + }; + + // TODO: blocking here is not ideal + let pos = helix_lsp::util::pos_to_lsp_pos(doc.text().slice(..), doc.selection().cursor()); + + // TODO: handle fails + let res = smol::block_on(language_server.goto_implementation(doc.identifier(), pos)) + .unwrap_or_default(); + goto_generic(cx, res); +} + +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, + None => return, + }; + + // TODO: blocking here is not ideal + let pos = helix_lsp::util::pos_to_lsp_pos(doc.text().slice(..), doc.selection().cursor()); + + // TODO: handle fails + let res = + smol::block_on(language_server.goto_reference(doc.identifier(), pos)).unwrap_or_default(); + goto_generic(cx, res); +} + // NOTE: Transactions in this module get appended to history when we switch back to normal mode. pub mod insert { use super::*; diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 9ec70023..82258bde 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -19,7 +19,7 @@ pub enum Mode { pub struct Document { pub state: State, // rope + selection /// File path on disk. - pub path: Option, // pub for testing + path: Option, /// Current editing mode. pub mode: Mode, -- cgit v1.2.3-70-g09d2 From 4e461bea2f4cc19d0d25a62a4f278420c074b6e9 Mon Sep 17 00:00:00 2001 From: Jan Hrastnik Date: Mon, 15 Mar 2021 21:12:41 +0100 Subject: 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(-) (limited to 'helix-view') diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 22a8737f..20f51ef4 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 b04a07dd..38182126 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(()) } -- cgit v1.2.3-70-g09d2 From eadad13efabb6a838a366b27002e09e1d2580750 Mon Sep 17 00:00:00 2001 From: Jan Hrastnik Date: Tue, 16 Mar 2021 13:55:12 +0100 Subject: preparing for gd merge --- helix-term/src/commands.rs | 4 +--- helix-view/src/editor.rs | 18 +++++++++--------- 2 files changed, 10 insertions(+), 12 deletions(-) (limited to 'helix-view') diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 20f51ef4..45e8cd01 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -857,8 +857,6 @@ fn goto(cx: &mut Context, locations: Vec) { doc.mode = Mode::Normal; - log::info!("{:?}", locations); - match locations.as_slice() { [location] => { cx.editor @@ -874,7 +872,7 @@ fn goto(cx: &mut Context, locations: Vec) { locations, |item| { let file = item.uri.as_str(); - let line = item.range.start.line.to_string(); + let line = item.range.start.line; format!("{}:{}", file, line).into() }, move |editor: &mut Editor, item| { diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 38182126..99c0398f 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -34,6 +34,14 @@ impl Editor { pub fn open(&mut self, path: PathBuf, executor: &smol::Executor) -> Result<(), Error> { // TODO: try to find an open view/buffer first + let existing_view_option = self + .tree + .views() + .find(|v| path.to_str().unwrap() == v.0.doc.path().unwrap().to_str().unwrap()); + if let Some(existing_view) = existing_view_option { + self.tree.focus = existing_view.0.id; + return Ok(()); + } let mut doc = Document::load(path, self.theme.scopes())?; @@ -62,15 +70,7 @@ impl Editor { } let view = View::new(doc)?; - 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); - } + self.tree.insert(view); Ok(()) } -- cgit v1.2.3-70-g09d2 From e3ec5e31ec005e33da4c848b4272e81a6d21a5f0 Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Tue, 16 Mar 2021 23:05:43 +0900 Subject: Fix goto code before merging. --- helix-term/src/commands.rs | 4 +--- helix-view/src/editor.rs | 10 +++++----- 2 files changed, 6 insertions(+), 8 deletions(-) (limited to 'helix-view') diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 45e8cd01..3e90fb63 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -15,8 +15,6 @@ use crate::ui::{self, Popup, Prompt, PromptEvent}; use std::path::PathBuf; -use smol::Executor; - use helix_view::{ document::Mode, view::{View, PADDING}, @@ -853,6 +851,7 @@ pub fn exit_select_mode(cx: &mut Context) { } fn goto(cx: &mut Context, locations: Vec) { + let executor = cx.executor; let doc = cx.doc(); doc.mode = Mode::Normal; @@ -876,7 +875,6 @@ fn goto(cx: &mut Context, locations: Vec) { format!("{}:{}", file, line).into() }, move |editor: &mut Editor, item| { - 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; diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 99c0398f..5c94df27 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -33,13 +33,13 @@ impl Editor { } pub fn open(&mut self, path: PathBuf, executor: &smol::Executor) -> Result<(), Error> { - // TODO: try to find an open view/buffer first - let existing_view_option = self + let existing_view = self .tree .views() - .find(|v| path.to_str().unwrap() == v.0.doc.path().unwrap().to_str().unwrap()); - if let Some(existing_view) = existing_view_option { - self.tree.focus = existing_view.0.id; + .find(|(view, _)| view.doc.path() == Some(&path)); + + if let Some((view, _)) = existing_view { + self.tree.focus = view.id; return Ok(()); } -- cgit v1.2.3-70-g09d2