@@ -18,7 +18,7 @@ use crate::deserializers;
18
18
use crate :: deserializers:: editor_data:: CodeSnippet ;
19
19
use crate :: deserializers:: run_submit:: { ParsedResponse , Success } ;
20
20
use crate :: entities:: TopicTagModel ;
21
- use crate :: errors:: AppResult ;
21
+ use crate :: errors:: { AppResult , LcAppError } ;
22
22
use crate :: graphql:: run_code:: RunCode ;
23
23
use crate :: graphql:: submit_code:: SubmitCode ;
24
24
use crate :: graphql:: { Language , RunOrSubmitCode } ;
@@ -323,22 +323,44 @@ impl QuestionListWidget {
323
323
self . pending_event_actions . contains ( key)
324
324
}
325
325
326
- fn open_vim_editor ( & mut self , file_name : & Path ) {
327
- let vim_cmd = format ! ( "nvim {}" , file_name. display( ) ) ;
326
+ fn open_vim_like_editor ( & mut self , file_name : & Path , editor : & str ) -> AppResult < ( ) > {
328
327
let mut output = std:: process:: Command :: new ( "sh" )
329
328
. arg ( "-c" )
330
- . arg ( & vim_cmd )
329
+ . arg ( & format ! ( "{} {}" , editor , file_name . display ( ) ) )
331
330
. spawn ( )
332
- . expect ( "Can't run vim cmd" ) ;
331
+ . map_err ( |e| LcAppError :: EditorOpen ( format ! ( "Can't spawn {} editor: {e}" , editor ) ) ) ? ;
333
332
self . vim_running
334
333
. store ( true , std:: sync:: atomic:: Ordering :: Relaxed ) ;
335
- let vim_cmd_result = output. wait ( ) . expect ( "Run exits ok" ) ;
334
+ let vim_cmd_result = output
335
+ . wait ( )
336
+ . map_err ( |e| LcAppError :: EditorOpen ( format ! ( "Editor Error: {e}" ) ) ) ?;
336
337
self . vim_running
337
338
. store ( false , std:: sync:: atomic:: Ordering :: Relaxed ) ;
338
339
self . vim_tx . blocking_send ( 1 ) . unwrap ( ) ;
339
340
if !vim_cmd_result. success ( ) {
340
- println ! ( "error vim" ) ;
341
+ return Err ( LcAppError :: EditorOpen (
342
+ "Cannot open editor, Reason: Unknown" . to_string ( ) ,
343
+ ) ) ;
341
344
}
345
+ Ok ( ( ) )
346
+ }
347
+
348
+ fn open_editor ( & mut self , file_name : & Path ) -> AppResult < ( ) > {
349
+ if let Ok ( editor) = std:: env:: var ( "EDITOR" ) {
350
+ if editor. contains ( "vim" ) || editor. contains ( "nano" ) {
351
+ self . open_vim_like_editor ( file_name, editor. as_str ( ) ) ?;
352
+ } else {
353
+ std:: process:: Command :: new ( "sh" )
354
+ . arg ( "-c" )
355
+ . arg ( & format ! ( "{} {}" , editor, file_name. display( ) ) )
356
+ . spawn ( ) ?
357
+ . wait ( ) ?;
358
+ }
359
+ } else {
360
+ // try open vim
361
+ self . open_vim_like_editor ( file_name, "vim" ) ?;
362
+ }
363
+ Ok ( ( ) )
342
364
}
343
365
344
366
fn popup_list_notification (
@@ -957,12 +979,20 @@ impl super::Widget for QuestionListWidget {
957
979
Some ( selected_snippet) ,
958
980
question_id. to_string ( ) ,
959
981
) ;
982
+ let save_path = sf. get_save_path ( & dir) ;
960
983
sf. create_if_not_exists ( & dir) ?;
961
- self . open_vim_editor ( & sf. get_save_path ( & dir) ) ;
962
984
self . files
963
985
. entry ( sf. question_id . parse ( ) . unwrap ( ) )
964
986
. or_default ( )
965
987
. insert ( sf) ;
988
+
989
+ if let Err ( e) = self . open_editor ( & save_path) {
990
+ return Ok ( Some ( self . popup_paragraph_notification (
991
+ e. to_string ( ) ,
992
+ "Error opening editor" . to_string ( ) ,
993
+ IndexSet :: new ( ) ,
994
+ ) ) ) ;
995
+ } ;
966
996
}
967
997
( question, tt) => {
968
998
self . solution_file_popup_action ( question, tt, index) ?;
0 commit comments