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 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'helix-lsp/src') 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) + } } -- 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(-) (limited to 'helix-lsp/src') 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 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(-) (limited to 'helix-lsp/src') 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 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(-) (limited to 'helix-lsp/src') 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 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(-) (limited to 'helix-lsp/src') 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(-) (limited to 'helix-lsp/src') 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 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(-) (limited to 'helix-lsp/src') 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