on master help sign in
more goto lsp functions
b738ae1Jan Hrastnik
helix-lsp/src/client.rs+100−16
@@ -583,6 +583,30 @@ impl Client {
583 Ok(response.unwrap_or_default())
583 Ok(response.unwrap_or_default())
584 }
584 }
585
585
586+ pub async fn goto_generic(
587+ &self,
588+ response: Option<lsp::GotoDefinitionResponse>,
589+ ) -> anyhow::Result<Vec<lsp::Location>> {
590+ let items = match response {
591+ Some(lsp::GotoDefinitionResponse::Scalar(location)) => vec![location],
592+ Some(lsp::GotoDefinitionResponse::Array(location_vec)) => location_vec,
593+ Some(lsp::GotoDefinitionResponse::Link(location_link_vec)) => {
594+ let mut location_vec: Vec<lsp::Location> = Vec::new();
595+ location_link_vec.into_iter().for_each(|location_link| {
596+ let link = lsp::Location {
597+ uri: location_link.target_uri,
598+ range: location_link.target_range,
599+ };
600+ location_vec.push(link)
601+ });
602+ location_vec
603+ }
604+ None => Vec::new(),
605+ };
606+
607+ Ok(items)
608+ }
609+
586 pub async fn goto_definition(
610 pub async fn goto_definition(
587 &self,
611 &self,
588 text_document: lsp::TextDocumentIdentifier,
612 text_document: lsp::TextDocumentIdentifier,
@@ -603,23 +627,83 @@ impl Client {
603
627
604 let response = self.request::<lsp::request::GotoDefinition>(params).await?;
628 let response = self.request::<lsp::request::GotoDefinition>(params).await?;
605
629
606− let items = match response {
630+ self.goto_generic(response).await
607− Some(lsp::GotoDefinitionResponse::Scalar(location)) => vec![location],
631+ }
608− Some(lsp::GotoDefinitionResponse::Array(location_vec)) => location_vec,
632+
609− Some(lsp::GotoDefinitionResponse::Link(location_link_vec)) => {
633+ pub async fn goto_type_definition(
634+ &self,
635+ text_document: lsp::TextDocumentIdentifier,
636+ position: lsp::Position,
610− let mut location_vec: Vec<lsp::Location> = Vec::new();
637+ ) -> anyhow::Result<Vec<lsp::Location>> {
611− location_link_vec.into_iter().for_each(|location_link| {
638+ let params = lsp::GotoDefinitionParams {
612− let link = lsp::Location {
639+ text_document_position_params: lsp::TextDocumentPositionParams {
613− uri: location_link.target_uri,
640+ text_document,
614− range: location_link.target_range,
641+ position,
615− };
642+ },
616− location_vec.push(link)
643+ work_done_progress_params: lsp::WorkDoneProgressParams {
617− });
644+ work_done_token: None,
618− location_vec
619− }
645+ },
620− None => Vec::new(),
646+ partial_result_params: lsp::PartialResultParams {
647+ partial_result_token: None,
648+ },
621 };
649 };
622
650
623− Ok(items)
651+ let response = self
652+ .request::<lsp::request::GotoTypeDefinition>(params)
653+ .await?;
654+
655+ self.goto_generic(response).await
656+ }
657+
658+ pub async fn goto_implementation(
659+ &self,
660+ text_document: lsp::TextDocumentIdentifier,
661+ position: lsp::Position,
662+ ) -> anyhow::Result<Vec<lsp::Location>> {
663+ let params = lsp::GotoDefinitionParams {
664+ text_document_position_params: lsp::TextDocumentPositionParams {
665+ text_document,
666+ position,
667+ },
668+ work_done_progress_params: lsp::WorkDoneProgressParams {
669+ work_done_token: None,
670+ },
671+ partial_result_params: lsp::PartialResultParams {
672+ partial_result_token: None,
673+ },
674+ };
675+
676+ let response = self
677+ .request::<lsp::request::GotoImplementation>(params)
678+ .await?;
679+
680+ self.goto_generic(response).await
681+ }
682+
683+ pub async fn goto_reference(
684+ &self,
685+ text_document: lsp::TextDocumentIdentifier,
686+ position: lsp::Position,
687+ ) -> anyhow::Result<Vec<lsp::Location>> {
688+ let params = lsp::ReferenceParams {
689+ text_document_position: lsp::TextDocumentPositionParams {
690+ text_document,
691+ position,
692+ },
693+ context: lsp::ReferenceContext {
694+ include_declaration: true,
695+ },
696+ work_done_progress_params: lsp::WorkDoneProgressParams {
697+ work_done_token: None,
698+ },
699+ partial_result_params: lsp::PartialResultParams {
700+ partial_result_token: None,
701+ },
702+ };
703+
704+ let response = self.request::<lsp::request::References>(params).await?;
705+
706+ self.goto_generic(response.map(lsp::GotoDefinitionResponse::Array))
707+ .await
624 }
708 }
625}
709}
helix-term/src/commands.rs+11−3
@@ -872,11 +872,19 @@ pub fn goto_definition(cx: &mut Context) {
872
872
873 doc.mode = Mode::Normal;
873 doc.mode = Mode::Normal;
874
874
875+ log::info!("{:?}", res);
876+ let filepath = doc.path.clone().unwrap();
877+ log::info!("{:?}", filepath);
878+
875 match &res.as_slice() {
879 match &res.as_slice() {
876 [location] => {
880 [location] => {
881+ if filepath.to_str().unwrap() == location.uri.path() {
877− let definition_pos = location.range.start;
882+ let definition_pos = location.range.start;
878− let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text().slice(..), definition_pos);
883+ let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text().slice(..), definition_pos);
879− doc.set_selection(Selection::point(new_pos));
884+ doc.set_selection(Selection::point(new_pos));
885+ } else {
886+ // open new file
887+ }
880 }
888 }
881 [] => (), // maybe show user message that no definition was found?
889 [] => (), // maybe show user message that no definition was found?
882 _ => {
890 _ => {
helix-view/src/document.rs+1−1
@@ -19,7 +19,7 @@ pub enum Mode {
19pub struct Document {
19pub struct Document {
20 pub state: State, // rope + selection
20 pub state: State, // rope + selection
21 /// File path on disk.
21 /// File path on disk.
22− path: Option<PathBuf>,
22+ pub path: Option<PathBuf>, // pub for testing
23
23
24 /// Current editing mode.
24 /// Current editing mode.
25 pub mode: Mode,
25 pub mode: Mode,