on master help sign in
added goto functions in helix-term
3869d77Jan Hrastnik
Cargo.lock+1
@@ -550,6 +550,7 @@ dependencies = [
550 "helix-view",
550 "helix-view",
551 "ignore",
551 "ignore",
552 "log",
552 "log",
553+ "lsp-types",
553 "num_cpus",
554 "num_cpus",
554 "once_cell",
555 "once_cell",
555 "pulldown-cmark",
556 "pulldown-cmark",
helix-term/src/commands.rs+74−23
@@ -13,6 +13,11 @@ use once_cell::sync::Lazy;
13use crate::compositor::Compositor;
13use crate::compositor::Compositor;
14use crate::ui::{self, Popup, Prompt, PromptEvent};
14use crate::ui::{self, Popup, Prompt, PromptEvent};
15
15
16+use lsp_types as lsp;
17+use std::path::PathBuf;
18+
19+use smol::Executor;
20+
16use helix_view::{
21use helix_view::{
17 document::Mode,
22 document::Mode,
18 view::{View, PADDING},
23 view::{View, PADDING},
@@ -846,34 +851,13 @@ pub fn exit_select_mode(cx: &mut Context) {
846 cx.doc().mode = Mode::Normal;
851 cx.doc().mode = Mode::Normal;
847}
852}
848
853
849−pub fn goto_definition(cx: &mut Context) {
854+pub fn goto_generic(cx: &mut Context, res: Vec<lsp::Location>) {
850 let doc = cx.doc();
855 let doc = cx.doc();
851
856
852− let language_server = match doc.language_server.as_ref() {
853− Some(language_server) => language_server,
854− None => return,
855− };
856−
857− // TODO: blocking here is not ideal
858− let pos = helix_lsp::util::pos_to_lsp_pos(doc.text().slice(..), doc.selection().cursor());
859−
860− // TODO: handle fails
861− let res =
862− smol::block_on(language_server.goto_definition(doc.identifier(), pos)).unwrap_or_default();
863−
864− /*
865− if res.len() == 1 {
866− let definition_pos = res.get(0).unwrap().range.start;
867− let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text().slice(..), definition_pos);
868− doc.set_selection(Selection::point(new_pos));
869− } else {
870− // show menu picker i guess
871− }*/
872−
873 doc.mode = Mode::Normal;
857 doc.mode = Mode::Normal;
874
858
875 log::info!("{:?}", res);
859 log::info!("{:?}", res);
876− let filepath = doc.path.clone().unwrap();
860+ let filepath = doc.path().unwrap();
877 log::info!("{:?}", filepath);
861 log::info!("{:?}", filepath);
878
862
879 match &res.as_slice() {
863 match &res.as_slice() {
@@ -884,6 +868,9 @@ pub fn goto_definition(cx: &mut Context) {
884 doc.set_selection(Selection::point(new_pos));
868 doc.set_selection(Selection::point(new_pos));
885 } else {
869 } else {
886 // open new file
870 // open new file
871+ cx.editor
872+ .open(PathBuf::from(location.uri.path()), cx.executor);
873+ // TODO: go to position
887 }
874 }
888 }
875 }
889 [] => (), // maybe show user message that no definition was found?
876 [] => (), // maybe show user message that no definition was found?
@@ -907,6 +894,70 @@ pub fn goto_definition(cx: &mut Context) {
907 }
894 }
908}
895}
909
896
897+pub fn goto_definition(cx: &mut Context) {
898+ let doc = cx.doc();
899+ let language_server = match doc.language_server.as_ref() {
900+ Some(language_server) => language_server,
901+ None => return,
902+ };
903+
904+ // TODO: blocking here is not ideal
905+ let pos = helix_lsp::util::pos_to_lsp_pos(doc.text().slice(..), doc.selection().cursor());
906+
907+ // TODO: handle fails
908+ let res =
909+ smol::block_on(language_server.goto_definition(doc.identifier(), pos)).unwrap_or_default();
910+ goto_generic(cx, res);
911+}
912+
913+pub fn goto_type_definition(cx: &mut Context) {
914+ let doc = cx.doc();
915+ let language_server = match doc.language_server.as_ref() {
916+ Some(language_server) => language_server,
917+ None => return,
918+ };
919+
920+ // TODO: blocking here is not ideal
921+ let pos = helix_lsp::util::pos_to_lsp_pos(doc.text().slice(..), doc.selection().cursor());
922+
923+ // TODO: handle fails
924+ let res = smol::block_on(language_server.goto_type_definition(doc.identifier(), pos))
925+ .unwrap_or_default();
926+ goto_generic(cx, res);
927+}
928+
929+pub fn goto_implementation(cx: &mut Context) {
930+ let doc = cx.doc();
931+ let language_server = match doc.language_server.as_ref() {
932+ Some(language_server) => language_server,
933+ None => return,
934+ };
935+
936+ // TODO: blocking here is not ideal
937+ let pos = helix_lsp::util::pos_to_lsp_pos(doc.text().slice(..), doc.selection().cursor());
938+
939+ // TODO: handle fails
940+ let res = smol::block_on(language_server.goto_implementation(doc.identifier(), pos))
941+ .unwrap_or_default();
942+ goto_generic(cx, res);
943+}
944+
945+pub fn goto_reference(cx: &mut Context) {
946+ let doc = cx.doc();
947+ let language_server = match doc.language_server.as_ref() {
948+ Some(language_server) => language_server,
949+ None => return,
950+ };
951+
952+ // TODO: blocking here is not ideal
953+ let pos = helix_lsp::util::pos_to_lsp_pos(doc.text().slice(..), doc.selection().cursor());
954+
955+ // TODO: handle fails
956+ let res =
957+ smol::block_on(language_server.goto_reference(doc.identifier(), pos)).unwrap_or_default();
958+ goto_generic(cx, res);
959+}
960+
910// NOTE: Transactions in this module get appended to history when we switch back to normal mode.
961// NOTE: Transactions in this module get appended to history when we switch back to normal mode.
911pub mod insert {
962pub mod insert {
912 use super::*;
963 use super::*;
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− pub path: Option<PathBuf>, // pub for testing
22+ path: Option<PathBuf>,
23
23
24 /// Current editing mode.
24 /// Current editing mode.
25 pub mode: Mode,
25 pub mode: Mode,