on master help sign in
term: Simplify goto code, address lints.
cf71625Blaž Hrastnik
helix-term/src/commands.rs+11−14
@@ -13,7 +13,6 @@ 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;
17use std::path::PathBuf;
16use std::path::PathBuf;
18
17
19use smol::Executor;
18use smol::Executor;
@@ -26,6 +25,8 @@ use helix_view::{
26
25
27use crossterm::event::{KeyCode, KeyEvent};
26use crossterm::event::{KeyCode, KeyEvent};
28
27
28+use helix_lsp::lsp;
29+
29pub struct Context<'a> {
30pub struct Context<'a> {
30 pub count: usize,
31 pub count: usize,
31 pub editor: &'a mut Editor,
32 pub editor: &'a mut Editor,
@@ -851,16 +852,16 @@ pub fn exit_select_mode(cx: &mut Context) {
851 cx.doc().mode = Mode::Normal;
852 cx.doc().mode = Mode::Normal;
852}
853}
853
854
854−pub fn goto_generic(cx: &mut Context, res: Vec<lsp::Location>) {
855+fn goto(cx: &mut Context, locations: Vec<lsp::Location>) {
855 let doc = cx.doc();
856 let doc = cx.doc();
856
857
857 doc.mode = Mode::Normal;
858 doc.mode = Mode::Normal;
858
859
859− log::info!("{:?}", res);
860+ log::info!("{:?}", locations);
860 let filepath = doc.path().unwrap();
861 let filepath = doc.path().unwrap();
861 log::info!("{:?}", filepath);
862 log::info!("{:?}", filepath);
862
863
863− match &res.as_slice() {
864+ match locations.as_slice() {
864 [location] => {
865 [location] => {
865 if filepath.to_str().unwrap() == location.uri.path() {
866 if filepath.to_str().unwrap() == location.uri.path() {
866 let definition_pos = location.range.start;
867 let definition_pos = location.range.start;
@@ -874,10 +875,9 @@ pub fn goto_generic(cx: &mut Context, res: Vec<lsp::Location>) {
874 }
875 }
875 }
876 }
876 [] => (), // maybe show user message that no definition was found?
877 [] => (), // maybe show user message that no definition was found?
877− _ => {
878+ _locations => {
878− let snapshot = doc.state.clone();
879 let mut picker = ui::Picker::new(
879 let mut picker = ui::Picker::new(
880− res,
880+ locations,
881 |item| {
881 |item| {
882 let file = item.uri.as_str();
882 let file = item.uri.as_str();
883 let line = item.range.start.line.to_string();
883 let line = item.range.start.line.to_string();
@@ -885,9 +885,6 @@ pub fn goto_generic(cx: &mut Context, res: Vec<lsp::Location>) {
885 },
885 },
886 move |editor: &mut Editor, item| {
886 move |editor: &mut Editor, item| {
887 let doc = &mut editor.view_mut().doc;
887 let doc = &mut editor.view_mut().doc;
888−
889− // revert state to what it was before the last update
890− doc.state = snapshot.clone();
891 },
888 },
892 );
889 );
893 }
890 }
@@ -907,7 +904,7 @@ pub fn goto_definition(cx: &mut Context) {
907 // TODO: handle fails
904 // TODO: handle fails
908 let res =
905 let res =
909 smol::block_on(language_server.goto_definition(doc.identifier(), pos)).unwrap_or_default();
906 smol::block_on(language_server.goto_definition(doc.identifier(), pos)).unwrap_or_default();
910− goto_generic(cx, res);
907+ goto(cx, res);
911}
908}
912
909
913pub fn goto_type_definition(cx: &mut Context) {
910pub fn goto_type_definition(cx: &mut Context) {
@@ -923,7 +920,7 @@ pub fn goto_type_definition(cx: &mut Context) {
923 // TODO: handle fails
920 // TODO: handle fails
924 let res = smol::block_on(language_server.goto_type_definition(doc.identifier(), pos))
921 let res = smol::block_on(language_server.goto_type_definition(doc.identifier(), pos))
925 .unwrap_or_default();
922 .unwrap_or_default();
926− goto_generic(cx, res);
923+ goto(cx, res);
927}
924}
928
925
929pub fn goto_implementation(cx: &mut Context) {
926pub fn goto_implementation(cx: &mut Context) {
@@ -939,7 +936,7 @@ pub fn goto_implementation(cx: &mut Context) {
939 // TODO: handle fails
936 // TODO: handle fails
940 let res = smol::block_on(language_server.goto_implementation(doc.identifier(), pos))
937 let res = smol::block_on(language_server.goto_implementation(doc.identifier(), pos))
941 .unwrap_or_default();
938 .unwrap_or_default();
942− goto_generic(cx, res);
939+ goto(cx, res);
943}
940}
944
941
945pub fn goto_reference(cx: &mut Context) {
942pub fn goto_reference(cx: &mut Context) {
@@ -955,7 +952,7 @@ pub fn goto_reference(cx: &mut Context) {
955 // TODO: handle fails
952 // TODO: handle fails
956 let res =
953 let res =
957 smol::block_on(language_server.goto_reference(doc.identifier(), pos)).unwrap_or_default();
954 smol::block_on(language_server.goto_reference(doc.identifier(), pos)).unwrap_or_default();
958− goto_generic(cx, res);
955+ goto(cx, res);
959}
956}
960
957
961// NOTE: Transactions in this module get appended to history when we switch back to normal mode.
958// NOTE: Transactions in this module get appended to history when we switch back to normal mode.