From d3ddc8dea6844f836199196c44b08e63fdf837d0 Mon Sep 17 00:00:00 2001 From: Jan Hrastnik Date: Sun, 21 Feb 2021 23:22:38 +0100 Subject: wip --- helix-lsp/src/client.rs | 39 +++++++++++++++++++++++++++++++++++++++ helix-term/helix.log | 0 helix-term/src/commands.rs | 2 ++ helix-term/src/keymap.rs | 8 ++++++-- helix-term/test2.txt | 1 + 5 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 helix-term/helix.log create mode 100644 helix-term/test2.txt diff --git a/helix-lsp/src/client.rs b/helix-lsp/src/client.rs index a9b7fe20..a747dc55 100644 --- a/helix-lsp/src/client.rs +++ b/helix-lsp/src/client.rs @@ -582,4 +582,43 @@ impl Client { Ok(response.unwrap_or_default()) } + + pub async fn goto_definition( + &self, + text_document: lsp::TextDocumentIdentifier, + position: lsp::Position, + ) -> anyhow::Result> { + let params = lsp::GotoDefinitionParams { + text_document_position_params: lsp::TextDocumentPositionParams { + text_document, + position, + }, + work_done_progress_params: lsp::WorkDoneProgressParams { + work_done_token: None, + }, + partial_result_params: lsp::PartialResultParams { + partial_result_token: None, + }, + }; + + let response = self.request::(params).await?; + + let items = match response { + Some(lsp::GotoDefinitionResponse::Scalar(location)) => vec![location], + Some(lsp::GotoDefinitionResponse::Array(location_vec)) => location_vec, + Some(lsp::GotoDefinitionResponse::Link(location_link_vec)) => { + let mut location_vec: Vec = Vec::new(); + location_link_vec.into_iter().for_each(|location_link| { + let link = lsp::Location { + uri: location_link.target_uri, + range: location_link.target_range, + }; + location_vec.append(&mut link); + }); + location_vec + } + }; + + Ok(items) + } } diff --git a/helix-term/helix.log b/helix-term/helix.log new file mode 100644 index 00000000..e69de29b diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 353d79cc..98e79b44 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -846,6 +846,8 @@ pub fn exit_select_mode(cx: &mut Context) { cx.doc().mode = Mode::Normal; } +pub fn goto_definition(cx: &mut Context) {} + // 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-term/src/keymap.rs b/helix-term/src/keymap.rs index 67490003..d9fe348f 100644 --- a/helix-term/src/keymap.rs +++ b/helix-term/src/keymap.rs @@ -311,8 +311,12 @@ pub fn default() -> Keymaps { code: KeyCode::Esc, modifiers: Modifiers::NONE } => commands::normal_mode as Command, - key!('g') => commands::move_file_start as Command, - key!('e') => commands::move_file_end as Command, + key!('g') => commands::move_file_start, + key!('e') => commands::move_file_end, + key!('d') => commands::goto_definition, + key!('t') => commands::goto_type_definition, + key!('r') => commands::goto_reference, + key!('i') => commands::goto_implementation, ), ) } diff --git a/helix-term/test2.txt b/helix-term/test2.txt new file mode 100644 index 00000000..aa674488 --- /dev/null +++ b/helix-term/test2.txt @@ -0,0 +1 @@ +awdwadwadwa njvkrnvjre eahdwau kjvreng -- cgit v1.2.3-70-g09d2 From 8a68a043400342a02496c4ff622dd86e6f40a89c Mon Sep 17 00:00:00 2001 From: Jan Hrastnik Date: Sun, 21 Feb 2021 23:43:28 +0100 Subject: gotodefiniton now runs but doesnt return anything --- helix-lsp/src/client.rs | 5 ++++- helix-term/src/commands.rs | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/helix-lsp/src/client.rs b/helix-lsp/src/client.rs index a747dc55..f3d3b7e1 100644 --- a/helix-lsp/src/client.rs +++ b/helix-lsp/src/client.rs @@ -603,6 +603,8 @@ impl Client { let response = self.request::(params).await?; + println!("{:?}", response); + let items = match response { Some(lsp::GotoDefinitionResponse::Scalar(location)) => vec![location], Some(lsp::GotoDefinitionResponse::Array(location_vec)) => location_vec, @@ -613,10 +615,11 @@ impl Client { uri: location_link.target_uri, range: location_link.target_range, }; - location_vec.append(&mut link); + location_vec.push(link) }); location_vec } + None => Vec::new(), }; Ok(items) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 98e79b44..8b7e3600 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -846,7 +846,27 @@ pub fn exit_select_mode(cx: &mut Context) { cx.doc().mode = Mode::Normal; } -pub fn goto_definition(cx: &mut Context) {} +pub fn goto_definition(cx: &mut Context) { + let language_server = cx + .editor + .language_servers + .get("source.rust", &cx.executor) + .unwrap(); + use log::info; + + let doc = cx.doc(); + + // 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(cx.doc().identifier(), pos)) + .unwrap_or_default(); + + println!("{:?}", res); + + cx.doc().mode = Mode::Normal; +} // NOTE: Transactions in this module get appended to history when we switch back to normal mode. pub mod insert { -- cgit v1.2.3-70-g09d2 From 18ec8adc7f24d0cde3d185202f8656b5cf7fefb0 Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Mon, 22 Feb 2021 12:12:56 +0900 Subject: Simplify code a bit. --- helix-term/src/commands.rs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 8b7e3600..fee4f362 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -847,25 +847,21 @@ pub fn exit_select_mode(cx: &mut Context) { } pub fn goto_definition(cx: &mut Context) { - let language_server = cx - .editor - .language_servers - .get("source.rust", &cx.executor) - .unwrap(); - use log::info; - 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(cx.doc().identifier(), pos)) - .unwrap_or_default(); - - println!("{:?}", res); + let res = + smol::block_on(language_server.goto_definition(doc.identifier(), pos)).unwrap_or_default(); - cx.doc().mode = Mode::Normal; + doc.mode = Mode::Normal; } // NOTE: Transactions in this module get appended to history when we switch back to normal mode. -- cgit v1.2.3-70-g09d2 From 0322c28e6bc1f3bf13842b7db47aafbe5752d45c Mon Sep 17 00:00:00 2001 From: Jan Hrastnik Date: Tue, 23 Feb 2021 23:56:06 +0100 Subject: gd now works for singular definition --- helix-lsp/src/client.rs | 2 -- helix-term/src/commands.rs | 8 ++++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/helix-lsp/src/client.rs b/helix-lsp/src/client.rs index f3d3b7e1..e0005c09 100644 --- a/helix-lsp/src/client.rs +++ b/helix-lsp/src/client.rs @@ -603,8 +603,6 @@ impl Client { let response = self.request::(params).await?; - println!("{:?}", response); - let items = match response { Some(lsp::GotoDefinitionResponse::Scalar(location)) => vec![location], Some(lsp::GotoDefinitionResponse::Array(location_vec)) => location_vec, diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index fee4f362..b2809f96 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -861,6 +861,14 @@ pub fn goto_definition(cx: &mut Context) { 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; } -- cgit v1.2.3-70-g09d2 From 294791dffd707d0725db5eb35b5165fd1bb2c24a Mon Sep 17 00:00:00 2001 From: Jan Hrastnik Date: Sun, 28 Feb 2021 00:39:13 +0100 Subject: added picker for gd, but yet to test it. also need to load appropriate file when definition isnt in same file --- helix-term/src/commands.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index b2809f96..f07a3933 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -861,15 +861,42 @@ pub fn goto_definition(cx: &mut Context) { 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; + + match &res.as_slice() { + [location] => { + let definition_pos = location.range.start; + let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text().slice(..), definition_pos); + doc.set_selection(Selection::point(new_pos)); + } + [] => (), // maybe show user message that no definition was found? + _ => { + let snapshot = doc.state.clone(); + let mut picker = ui::Picker::new( + res, + |item| { + let file = item.uri.as_str(); + let line = item.range.start.line.to_string(); + format!("{}:{}", file, line).into() + }, + move |editor: &mut Editor, item| { + let doc = &mut editor.view_mut().doc; + + // revert state to what it was before the last update + doc.state = snapshot.clone(); + }, + ); + } + } } // NOTE: Transactions in this module get appended to history when we switch back to normal mode. -- cgit v1.2.3-70-g09d2 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-lsp/src/client.rs | 116 ++++++++++++++++++++++++++++++++++++++------- helix-term/src/commands.rs | 14 ++++-- helix-view/src/document.rs | 2 +- 3 files changed, 112 insertions(+), 20 deletions(-) diff --git a/helix-lsp/src/client.rs b/helix-lsp/src/client.rs index e0005c09..cd07699d 100644 --- a/helix-lsp/src/client.rs +++ b/helix-lsp/src/client.rs @@ -583,6 +583,30 @@ impl Client { Ok(response.unwrap_or_default()) } + pub async fn goto_generic( + &self, + response: Option, + ) -> anyhow::Result> { + let items = match response { + Some(lsp::GotoDefinitionResponse::Scalar(location)) => vec![location], + Some(lsp::GotoDefinitionResponse::Array(location_vec)) => location_vec, + Some(lsp::GotoDefinitionResponse::Link(location_link_vec)) => { + let mut location_vec: Vec = Vec::new(); + location_link_vec.into_iter().for_each(|location_link| { + let link = lsp::Location { + uri: location_link.target_uri, + range: location_link.target_range, + }; + location_vec.push(link) + }); + location_vec + } + None => Vec::new(), + }; + + Ok(items) + } + pub async fn goto_definition( &self, text_document: lsp::TextDocumentIdentifier, @@ -603,23 +627,83 @@ impl Client { let response = self.request::(params).await?; - let items = match response { - Some(lsp::GotoDefinitionResponse::Scalar(location)) => vec![location], - Some(lsp::GotoDefinitionResponse::Array(location_vec)) => location_vec, - Some(lsp::GotoDefinitionResponse::Link(location_link_vec)) => { - let mut location_vec: Vec = Vec::new(); - location_link_vec.into_iter().for_each(|location_link| { - let link = lsp::Location { - uri: location_link.target_uri, - range: location_link.target_range, - }; - location_vec.push(link) - }); - location_vec - } - None => Vec::new(), + self.goto_generic(response).await + } + + pub async fn goto_type_definition( + &self, + text_document: lsp::TextDocumentIdentifier, + position: lsp::Position, + ) -> anyhow::Result> { + let params = lsp::GotoDefinitionParams { + text_document_position_params: lsp::TextDocumentPositionParams { + text_document, + position, + }, + work_done_progress_params: lsp::WorkDoneProgressParams { + work_done_token: None, + }, + partial_result_params: lsp::PartialResultParams { + partial_result_token: None, + }, }; - Ok(items) + let response = self + .request::(params) + .await?; + + self.goto_generic(response).await + } + + pub async fn goto_implementation( + &self, + text_document: lsp::TextDocumentIdentifier, + position: lsp::Position, + ) -> anyhow::Result> { + let params = lsp::GotoDefinitionParams { + text_document_position_params: lsp::TextDocumentPositionParams { + text_document, + position, + }, + work_done_progress_params: lsp::WorkDoneProgressParams { + work_done_token: None, + }, + partial_result_params: lsp::PartialResultParams { + partial_result_token: None, + }, + }; + + let response = self + .request::(params) + .await?; + + self.goto_generic(response).await + } + + pub async fn goto_reference( + &self, + text_document: lsp::TextDocumentIdentifier, + position: lsp::Position, + ) -> anyhow::Result> { + let params = lsp::ReferenceParams { + text_document_position: lsp::TextDocumentPositionParams { + text_document, + position, + }, + context: lsp::ReferenceContext { + include_declaration: true, + }, + work_done_progress_params: lsp::WorkDoneProgressParams { + work_done_token: None, + }, + partial_result_params: lsp::PartialResultParams { + partial_result_token: None, + }, + }; + + let response = self.request::(params).await?; + + self.goto_generic(response.map(lsp::GotoDefinitionResponse::Array)) + .await } } diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index f07a3933..9b48e803 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -872,11 +872,19 @@ pub fn goto_definition(cx: &mut Context) { doc.mode = Mode::Normal; + log::info!("{:?}", res); + let filepath = doc.path.clone().unwrap(); + log::info!("{:?}", filepath); + match &res.as_slice() { [location] => { - let definition_pos = location.range.start; - let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text().slice(..), definition_pos); - doc.set_selection(Selection::point(new_pos)); + if filepath.to_str().unwrap() == location.uri.path() { + let definition_pos = location.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 { + // open new file + } } [] => (), // maybe show user message that no definition was found? _ => { 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(-) 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 a5f9080a9cbc765758ce8af0c19a970a05745c60 Mon Sep 17 00:00:00 2001 From: Jan Hrastnik Date: Wed, 10 Mar 2021 23:35:12 +0100 Subject: goto_request wip --- Cargo.lock | 1 - helix-lsp/src/client.rs | 43 ++++++++++++++++++++++--------------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ac1f172a..5fac3391 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -550,7 +550,6 @@ dependencies = [ "helix-view", "ignore", "log", - "lsp-types", "num_cpus", "once_cell", "pulldown-cmark", diff --git a/helix-lsp/src/client.rs b/helix-lsp/src/client.rs index cd07699d..9c315272 100644 --- a/helix-lsp/src/client.rs +++ b/helix-lsp/src/client.rs @@ -583,10 +583,26 @@ impl Client { Ok(response.unwrap_or_default()) } - pub async fn goto_generic( + pub async fn goto_request( &self, - response: Option, + text_document: lsp::TextDocumentIdentifier, + position: lsp::Position, ) -> anyhow::Result> { + let params = lsp::GotoDefinitionParams { + text_document_position_params: lsp::TextDocumentPositionParams { + text_document, + position, + }, + work_done_progress_params: lsp::WorkDoneProgressParams { + work_done_token: None, + }, + partial_result_params: lsp::PartialResultParams { + partial_result_token: None, + }, + }; + + let response = self.request::(params).await?; + let items = match response { Some(lsp::GotoDefinitionResponse::Scalar(location)) => vec![location], Some(lsp::GotoDefinitionResponse::Array(location_vec)) => location_vec, @@ -612,22 +628,7 @@ impl Client { text_document: lsp::TextDocumentIdentifier, position: lsp::Position, ) -> anyhow::Result> { - let params = lsp::GotoDefinitionParams { - text_document_position_params: lsp::TextDocumentPositionParams { - text_document, - position, - }, - work_done_progress_params: lsp::WorkDoneProgressParams { - work_done_token: None, - }, - partial_result_params: lsp::PartialResultParams { - partial_result_token: None, - }, - }; - - let response = self.request::(params).await?; - - self.goto_generic(response).await + self.goto_request(response).await } pub async fn goto_type_definition( @@ -652,7 +653,7 @@ impl Client { .request::(params) .await?; - self.goto_generic(response).await + self.goto_request(response).await } pub async fn goto_implementation( @@ -677,7 +678,7 @@ impl Client { .request::(params) .await?; - self.goto_generic(response).await + self.goto_request(response).await } pub async fn goto_reference( @@ -703,7 +704,7 @@ impl Client { let response = self.request::(params).await?; - self.goto_generic(response.map(lsp::GotoDefinitionResponse::Array)) + self.goto_request(response.map(lsp::GotoDefinitionResponse::Array)) .await } } -- cgit v1.2.3-70-g09d2 From 4240f757c0b219950f17797db325c2c4e14d732b Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Thu, 11 Mar 2021 13:01:28 +0900 Subject: lsp: Fix compilation errors. --- helix-lsp/src/client.rs | 53 ++++++++++++------------------------------------- 1 file changed, 13 insertions(+), 40 deletions(-) diff --git a/helix-lsp/src/client.rs b/helix-lsp/src/client.rs index 9c315272..1b67e215 100644 --- a/helix-lsp/src/client.rs +++ b/helix-lsp/src/client.rs @@ -583,7 +583,12 @@ impl Client { Ok(response.unwrap_or_default()) } - pub async fn goto_request( + async fn goto_request< + T: lsp::request::Request< + Params = lsp::GotoDefinitionParams, + Result = Option, + >, + >( &self, text_document: lsp::TextDocumentIdentifier, position: lsp::Position, @@ -628,7 +633,8 @@ impl Client { text_document: lsp::TextDocumentIdentifier, position: lsp::Position, ) -> anyhow::Result> { - self.goto_request(response).await + self.goto_request::(text_document, position) + .await } pub async fn goto_type_definition( @@ -636,24 +642,8 @@ impl Client { text_document: lsp::TextDocumentIdentifier, position: lsp::Position, ) -> anyhow::Result> { - let params = lsp::GotoDefinitionParams { - text_document_position_params: lsp::TextDocumentPositionParams { - text_document, - position, - }, - work_done_progress_params: lsp::WorkDoneProgressParams { - work_done_token: None, - }, - partial_result_params: lsp::PartialResultParams { - partial_result_token: None, - }, - }; - - let response = self - .request::(params) - .await?; - - self.goto_request(response).await + self.goto_request::(text_document, position) + .await } pub async fn goto_implementation( @@ -661,24 +651,8 @@ impl Client { text_document: lsp::TextDocumentIdentifier, position: lsp::Position, ) -> anyhow::Result> { - let params = lsp::GotoDefinitionParams { - text_document_position_params: lsp::TextDocumentPositionParams { - text_document, - position, - }, - work_done_progress_params: lsp::WorkDoneProgressParams { - work_done_token: None, - }, - partial_result_params: lsp::PartialResultParams { - partial_result_token: None, - }, - }; - - let response = self - .request::(params) - .await?; - - self.goto_request(response).await + self.goto_request::(text_document, position) + .await } pub async fn goto_reference( @@ -704,7 +678,6 @@ impl Client { let response = self.request::(params).await?; - self.goto_request(response.map(lsp::GotoDefinitionResponse::Array)) - .await + Ok(response.unwrap_or_default()) } } -- cgit v1.2.3-70-g09d2 From cf71625d4e879ef2e453a33b2eba41bc23174aa2 Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Thu, 11 Mar 2021 13:15:25 +0900 Subject: term: Simplify goto code, address lints. --- helix-term/src/commands.rs | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 87d7ea79..5a99d795 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -13,7 +13,6 @@ 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; @@ -26,6 +25,8 @@ use helix_view::{ use crossterm::event::{KeyCode, KeyEvent}; +use helix_lsp::lsp; + pub struct Context<'a> { pub count: usize, pub editor: &'a mut Editor, @@ -851,16 +852,16 @@ pub fn exit_select_mode(cx: &mut Context) { cx.doc().mode = Mode::Normal; } -pub fn goto_generic(cx: &mut Context, res: Vec) { +fn goto(cx: &mut Context, locations: Vec) { let doc = cx.doc(); doc.mode = Mode::Normal; - log::info!("{:?}", res); + log::info!("{:?}", locations); let filepath = doc.path().unwrap(); log::info!("{:?}", filepath); - match &res.as_slice() { + match locations.as_slice() { [location] => { if filepath.to_str().unwrap() == location.uri.path() { let definition_pos = location.range.start; @@ -874,10 +875,9 @@ pub fn goto_generic(cx: &mut Context, res: Vec) { } } [] => (), // maybe show user message that no definition was found? - _ => { - let snapshot = doc.state.clone(); + _locations => { let mut picker = ui::Picker::new( - res, + locations, |item| { let file = item.uri.as_str(); let line = item.range.start.line.to_string(); @@ -885,9 +885,6 @@ pub fn goto_generic(cx: &mut Context, res: Vec) { }, move |editor: &mut Editor, item| { let doc = &mut editor.view_mut().doc; - - // revert state to what it was before the last update - doc.state = snapshot.clone(); }, ); } @@ -907,7 +904,7 @@ pub fn goto_definition(cx: &mut Context) { // TODO: handle fails let res = smol::block_on(language_server.goto_definition(doc.identifier(), pos)).unwrap_or_default(); - goto_generic(cx, res); + goto(cx, res); } pub fn goto_type_definition(cx: &mut Context) { @@ -923,7 +920,7 @@ pub fn goto_type_definition(cx: &mut Context) { // TODO: handle fails let res = smol::block_on(language_server.goto_type_definition(doc.identifier(), pos)) .unwrap_or_default(); - goto_generic(cx, res); + goto(cx, res); } pub fn goto_implementation(cx: &mut Context) { @@ -939,7 +936,7 @@ pub fn goto_implementation(cx: &mut Context) { // TODO: handle fails let res = smol::block_on(language_server.goto_implementation(doc.identifier(), pos)) .unwrap_or_default(); - goto_generic(cx, res); + goto(cx, res); } pub fn goto_reference(cx: &mut Context) { @@ -955,7 +952,7 @@ pub fn goto_reference(cx: &mut Context) { // TODO: handle fails let res = smol::block_on(language_server.goto_reference(doc.identifier(), pos)).unwrap_or_default(); - goto_generic(cx, res); + goto(cx, res); } // NOTE: Transactions in this module get appended to history when we switch back to normal mode. -- cgit v1.2.3-70-g09d2 From 857763c52e69de5c3b5a8ef62b78d5ca8cbe3727 Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Thu, 11 Mar 2021 13:16:50 +0900 Subject: term: Remove stray files. --- helix-term/helix.log | 0 helix-term/test.rs | 36 -- helix-term/test.txt | 1000 -------------------------------------------------- helix-term/test2.txt | 1 - 4 files changed, 1037 deletions(-) delete mode 100644 helix-term/helix.log delete mode 100644 helix-term/test.rs delete mode 100644 helix-term/test.txt delete mode 100644 helix-term/test2.txt diff --git a/helix-term/helix.log b/helix-term/helix.log deleted file mode 100644 index e69de29b..00000000 diff --git a/helix-term/test.rs b/helix-term/test.rs deleted file mode 100644 index 713c23d0..00000000 --- a/helix-term/test.rs +++ /dev/null @@ -1,36 +0,0 @@ -pub struct TextArea { - properties: Properties, - frame: Rect, -} - -impl Component for TextArea { - type Message = (); - type Properties = Properties; - - fn create(properties: Self::Properties, frame: Rect, _link: ComponentLink) -> Self { - TextArea { properties, frame } - } - - fn change<'a>(&'a mut self, properties: Self::Properties) -> ShouldRender { - let a: &'static str = "ase"; - let q = 2u8; - let q = 2 as u16; - Some(0); - true; - self.properties = properties; - ShouldRender::Yes - } - - fn resize(&mut self, frame: Rect) -> ShouldRender { - println!("hello world! \" test"); - self.frame = frame; - ShouldRender::Yes - } - - fn view(&self) -> Layout { - let mut canvas = Canvas::new(self.frame.size); - canvas.clear(self.properties.theme.text); - self.draw_text(&mut canvas); - canvas.into() - } -} diff --git a/helix-term/test.txt b/helix-term/test.txt deleted file mode 100644 index 11798245..00000000 --- a/helix-term/test.txt +++ /dev/null @@ -1,1000 +0,0 @@ -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -20 -21 -22 -23 -24 -25 -26 -27 -28 -29 -30 -31 -32 -33 -34 -35 -36 -37 -38 -39 -40 -41 -42 -43 -44 -45 -46 -47 -48 -49 -50 -51 -52 -53 -54 -55 -56 -57 -58 -59 -60 -61 -62 -63 -64 -65 -66 -67 -68 -69 -70 -71 -72 -73 -74 -75 -76 -77 -78 -79 -80 -81 -82 -83 -84 -85 -86 -87 -88 -89 -90 -91 -92 -93 -94 -95 -96 -97 -98 -99 -100 -101 -102 -103 -104 -105 -106 -107 -108 -109 -110 -111 -112 -113 -114 -115 -116 -117 -118 -119 -120 -121 -122 -123 -124 -125 -126 -127 -128 -129 -130 -131 -132 -133 -134 -135 -136 -137 -138 -139 -140 -141 -142 -143 -144 -145 -146 -147 -148 -149 -150 -151 -152 -153 -154 -155 -156 -157 -158 -159 -160 -161 -162 -163 -164 -165 -166 -167 -168 -169 -170 -171 -172 -173 -174 -175 -176 -177 -178 -179 -180 -181 -182 -183 -184 -185 -186 -187 -188 -189 -190 -191 -192 -193 -194 -195 -196 -197 -198 -199 -200 -201 -202 -203 -204 -205 -206 -207 -208 -209 -210 -211 -212 -213 -214 -215 -216 -217 -218 -219 -220 -221 -222 -223 -224 -225 -226 -227 -228 -229 -230 -231 -232 -233 -234 -235 -236 -237 -238 -239 -240 -241 -242 -243 -244 -245 -246 -247 -248 -249 -250 -251 -252 -253 -254 -255 -256 -257 -258 -259 -260 -261 -262 -263 -264 -265 -266 -267 -268 -269 -270 -271 -272 -273 -274 -275 -276 -277 -278 -279 -280 -281 -282 -283 -284 -285 -286 -287 -288 -289 -290 -291 -292 -293 -294 -295 -296 -297 -298 -299 -300 -301 -302 -303 -304 -305 -306 -307 -308 -309 -310 -311 -312 -313 -314 -315 -316 -317 -318 -319 -320 -321 -322 -323 -324 -325 -326 -327 -328 -329 -330 -331 -332 -333 -334 -335 -336 -337 -338 -339 -340 -341 -342 -343 -344 -345 -346 -347 -348 -349 -350 -351 -352 -353 -354 -355 -356 -357 -358 -359 -360 -361 -362 -363 -364 -365 -366 -367 -368 -369 -370 -371 -372 -373 -374 -375 -376 -377 -378 -379 -380 -381 -382 -383 -384 -385 -386 -387 -388 -389 -390 -391 -392 -393 -394 -395 -396 -397 -398 -399 -400 -401 -402 -403 -404 -405 -406 -407 -408 -409 -410 -411 -412 -413 -414 -415 -416 -417 -418 -419 -420 -421 -422 -423 -424 -425 -426 -427 -428 -429 -430 -431 -432 -433 -434 -435 -436 -437 -438 -439 -440 -441 -442 -443 -444 -445 -446 -447 -448 -449 -450 -451 -452 -453 -454 -455 -456 -457 -458 -459 -460 -461 -462 -463 -464 -465 -466 -467 -468 -469 -470 -471 -472 -473 -474 -475 -476 -477 -478 -479 -480 -481 -482 -483 -484 -485 -486 -487 -488 -489 -490 -491 -492 -493 -494 -495 -496 -497 -498 -499 -500 -501 -502 -503 -504 -505 -506 -507 -508 -509 -510 -511 -512 -513 -514 -515 -516 -517 -518 -519 -520 -521 -522 -523 -524 -525 -526 -527 -528 -529 -530 -531 -532 -533 -534 -535 -536 -537 -538 -539 -540 -541 -542 -543 -544 -545 -546 -547 -548 -549 -550 -551 -552 -553 -554 -555 -556 -557 -558 -559 -560 -561 -562 -563 -564 -565 -566 -567 -568 -569 -570 -571 -572 -573 -574 -575 -576 -577 -578 -579 -580 -581 -582 -583 -584 -585 -586 -587 -588 -589 -590 -591 -592 -593 -594 -595 -596 -597 -598 -599 -600 -601 -602 -603 -604 -605 -606 -607 -608 -609 -610 -611 -612 -613 -614 -615 -616 -617 -618 -619 -620 -621 -622 -623 -624 -625 -626 -627 -628 -629 -630 -631 -632 -633 -634 -635 -636 -637 -638 -639 -640 -641 -642 -643 -644 -645 -646 -647 -648 -649 -650 -651 -652 -653 -654 -655 -656 -657 -658 -659 -660 -661 -662 -663 -664 -665 -666 -667 -668 -669 -670 -671 -672 -673 -674 -675 -676 -677 -678 -679 -680 -681 -682 -683 -684 -685 -686 -687 -688 -689 -690 -691 -692 -693 -694 -695 -696 -697 -698 -699 -700 -701 -702 -703 -704 -705 -706 -707 -708 -709 -710 -711 -712 -713 -714 -715 -716 -717 -718 -719 -720 -721 -722 -723 -724 -725 -726 -727 -728 -729 -730 -731 -732 -733 -734 -735 -736 -737 -738 -739 -740 -741 -742 -743 -744 -745 -746 -747 -748 -749 -750 -751 -752 -753 -754 -755 -756 -757 -758 -759 -760 -761 -762 -763 -764 -765 -766 -767 -768 -769 -770 -771 -772 -773 -774 -775 -776 -777 -778 -779 -780 -781 -782 -783 -784 -785 -786 -787 -788 -789 -790 -791 -792 -793 -794 -795 -796 -797 -798 -799 -800 -801 -802 -803 -804 -805 -806 -807 -808 -809 -810 -811 -812 -813 -814 -815 -816 -817 -818 -819 -820 -821 -822 -823 -824 -825 -826 -827 -828 -829 -830 -831 -832 -833 -834 -835 -836 -837 -838 -839 -840 -841 -842 -843 -844 -845 -846 -847 -848 -849 -850 -851 -852 -853 -854 -855 -856 -857 -858 -859 -860 -861 -862 -863 -864 -865 -866 -867 -868 -869 -870 -871 -872 -873 -874 -875 -876 -877 -878 -879 -880 -881 -882 -883 -884 -885 -886 -887 -888 -889 -890 -891 -892 -893 -894 -895 -896 -897 -898 -899 -900 -901 -902 -903 -904 -905 -906 -907 -908 -909 -910 -911 -912 -913 -914 -915 -916 -917 -918 -919 -920 -921 -922 -923 -924 -925 -926 -927 -928 -929 -930 -931 -932 -933 -934 -935 -936 -937 -938 -939 -940 -941 -942 -943 -944 -945 -946 -947 -948 -949 -950 -951 -952 -953 -954 -955 -956 -957 -958 -959 -960 -961 -962 -963 -964 -965 -966 -967 -968 -969 -970 -971 -972 -973 -974 -975 -976 -977 -978 -979 -980 -981 -982 -983 -984 -985 -986 -987 -988 -989 -990 -991 -992 -993 -994 -995 -996 -997 -998 -999 -1000 diff --git a/helix-term/test2.txt b/helix-term/test2.txt deleted file mode 100644 index aa674488..00000000 --- a/helix-term/test2.txt +++ /dev/null @@ -1 +0,0 @@ -awdwadwadwa njvkrnvjre eahdwau kjvreng -- cgit v1.2.3-70-g09d2 From 15f142bc4b79aa0ef60bea3f0faa2dc49b73505b Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Thu, 11 Mar 2021 13:19:53 +0900 Subject: lsp: Use into_iter->map->collect instead of manual loop. --- helix-lsp/src/client.rs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/helix-lsp/src/client.rs b/helix-lsp/src/client.rs index 1b67e215..97e5cfad 100644 --- a/helix-lsp/src/client.rs +++ b/helix-lsp/src/client.rs @@ -610,18 +610,14 @@ impl Client { let items = match response { Some(lsp::GotoDefinitionResponse::Scalar(location)) => vec![location], - Some(lsp::GotoDefinitionResponse::Array(location_vec)) => location_vec, - Some(lsp::GotoDefinitionResponse::Link(location_link_vec)) => { - let mut location_vec: Vec = Vec::new(); - location_link_vec.into_iter().for_each(|location_link| { - let link = lsp::Location { - uri: location_link.target_uri, - range: location_link.target_range, - }; - location_vec.push(link) - }); - location_vec - } + Some(lsp::GotoDefinitionResponse::Array(locations)) => locations, + Some(lsp::GotoDefinitionResponse::Link(locations)) => locations + .into_iter() + .map(|location_link| lsp::Location { + uri: location_link.target_uri, + range: location_link.target_range, + }) + .collect(), None => Vec::new(), }; -- cgit v1.2.3-70-g09d2 From 0828d1fdea0b69a2912bc6d550ec50c1b6bf874c Mon Sep 17 00:00:00 2001 From: Jan Hrastnik Date: Sun, 14 Mar 2021 22:24:46 +0100 Subject: picker wip --- helix-term/src/commands.rs | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 5a99d795..22a8737f 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -852,27 +852,21 @@ pub fn exit_select_mode(cx: &mut Context) { cx.doc().mode = Mode::Normal; } -fn goto(cx: &mut Context, locations: Vec) { +fn goto(cx: &'static mut Context<'static>, locations: Vec) { let doc = cx.doc(); doc.mode = Mode::Normal; log::info!("{:?}", locations); - let filepath = doc.path().unwrap(); - log::info!("{:?}", filepath); match locations.as_slice() { [location] => { - if filepath.to_str().unwrap() == location.uri.path() { - let definition_pos = location.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 { - // open new file - cx.editor - .open(PathBuf::from(location.uri.path()), cx.executor); - // TODO: go to position - } + cx.editor + .open(PathBuf::from(location.uri.path()), cx.executor); + let doc = cx.doc(); + let definition_pos = location.range.start; + let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text().slice(..), definition_pos); + doc.set_selection(Selection::point(new_pos)); } [] => (), // maybe show user message that no definition was found? _locations => { @@ -884,14 +878,20 @@ fn goto(cx: &mut Context, locations: Vec) { format!("{}:{}", file, line).into() }, move |editor: &mut Editor, item| { - let doc = &mut editor.view_mut().doc; + cx.editor.open(PathBuf::from(item.uri.path()), cx.executor); + let doc = cx.doc(); + let definition_pos = item.range.start; + let new_pos = + helix_lsp::util::lsp_pos_to_pos(doc.text().slice(..), definition_pos); + doc.set_selection(Selection::point(new_pos)); }, ); + cx.push_layer(Box::new(picker)); } } } -pub fn goto_definition(cx: &mut Context) { +pub fn goto_definition(cx: &'static mut Context<'static>) { let doc = cx.doc(); let language_server = match doc.language_server.as_ref() { Some(language_server) => language_server, @@ -907,7 +907,7 @@ pub fn goto_definition(cx: &mut Context) { goto(cx, res); } -pub fn goto_type_definition(cx: &mut Context) { +pub fn goto_type_definition(cx: &'static mut Context<'static>) { let doc = cx.doc(); let language_server = match doc.language_server.as_ref() { Some(language_server) => language_server, @@ -923,7 +923,7 @@ pub fn goto_type_definition(cx: &mut Context) { goto(cx, res); } -pub fn goto_implementation(cx: &mut Context) { +pub fn goto_implementation(cx: &'static mut Context<'static>) { let doc = cx.doc(); let language_server = match doc.language_server.as_ref() { Some(language_server) => language_server, @@ -939,7 +939,7 @@ pub fn goto_implementation(cx: &mut Context) { goto(cx, res); } -pub fn goto_reference(cx: &mut Context) { +pub fn goto_reference(cx: &'static mut Context<'static>) { let doc = cx.doc(); let language_server = match doc.language_server.as_ref() { Some(language_server) => language_server, -- 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(-) 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(-) 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(-) 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