@@ -528,6 +528,35 @@ enum EditorKind {
528528}
529529
530530impl EditorKind {
531+ fn prompt_user ( ) -> io:: Result < Option < EditorKind > > {
532+ let prompt_str = "Available editors:
533+ 1. vscode
534+ 2. vim
535+ 3. emacs
536+ 4. helix
537+
538+ Select which editor you would like to set up [default: None]: " ;
539+
540+ let mut input = String :: new ( ) ;
541+ loop {
542+ print ! ( "{}" , prompt_str) ;
543+ io:: stdout ( ) . flush ( ) ?;
544+ input. clear ( ) ;
545+ io:: stdin ( ) . read_line ( & mut input) ?;
546+ match input. trim ( ) . to_lowercase ( ) . as_str ( ) {
547+ "1" | "vscode" => return Ok ( Some ( EditorKind :: Vscode ) ) ,
548+ "2" | "vim" => return Ok ( Some ( EditorKind :: Vim ) ) ,
549+ "3" | "emacs" => return Ok ( Some ( EditorKind :: Emacs ) ) ,
550+ "4" | "helix" => return Ok ( Some ( EditorKind :: Helix ) ) ,
551+ "" => return Ok ( None ) ,
552+ _ => {
553+ eprintln ! ( "ERROR: unrecognized option '{}'" , input. trim( ) ) ;
554+ eprintln ! ( "NOTE: press Ctrl+C to exit" ) ;
555+ }
556+ } ;
557+ }
558+ }
559+
531560 /// A list of historical hashes of each LSP settings file
532561 /// New entries should be appended whenever this is updated so we can detect
533562 /// outdated vs. user-modified settings files.
@@ -568,10 +597,10 @@ impl EditorKind {
568597
569598 fn settings_folder ( & self ) -> PathBuf {
570599 match self {
571- EditorKind :: Vscode => PathBuf :: new ( ) . join ( ".vscode" ) ,
572- EditorKind :: Vim => PathBuf :: new ( ) . join ( ".vim" ) ,
600+ EditorKind :: Vscode => PathBuf :: from ( ".vscode" ) ,
601+ EditorKind :: Vim => PathBuf :: from ( ".vim" ) ,
573602 EditorKind :: Emacs => PathBuf :: new ( ) ,
574- EditorKind :: Helix => PathBuf :: new ( ) . join ( ".helix" ) ,
603+ EditorKind :: Helix => PathBuf :: from ( ".helix" ) ,
575604 }
576605 }
577606
@@ -590,48 +619,47 @@ impl EditorKind {
590619 }
591620}
592621
593- /// Helper macro for implementing the necessary Step to support editor LSP setup
594- /// The first argument must match the argument set up in `flags.rs`
595- /// The second argument must match the name of some `EditorKind` variant
596- /// After using the macro, the editor needs to be registered in `builder.rs` with describe!()
597- macro_rules! impl_editor_support {
598- ( $( $editor: ident, $kind: ident) ,+ ) => { $(
599- #[ doc = concat!( " Sets up or displays the LSP config for " , stringify!( $editor) , "." ) ]
600- #[ derive( Clone , Debug , Eq , PartialEq , Hash ) ]
601- pub struct $kind;
602-
603- impl Step for $kind {
604- type Output = ( ) ;
605- const DEFAULT : bool = true ;
606- fn should_run( run: ShouldRun <' _>) -> ShouldRun <' _> {
607- run. alias( stringify!( $editor) )
608- }
609- fn make_run( run: RunConfig <' _>) {
610- if run. builder. config. dry_run( ) {
611- return ;
612- }
613- if let [ cmd] = & run. paths[ ..] {
614- if cmd. assert_single_path( ) . path. as_path( ) . as_os_str( ) == stringify!( $editor) {
615- run. builder. ensure( $kind) ;
616- }
617- }
622+ /// Sets up or displays the LSP config for one of the supported editors
623+ #[ derive( Clone , Debug , Eq , PartialEq , Hash ) ]
624+ pub struct Editor ;
625+
626+ impl Step for Editor {
627+ type Output = ( ) ;
628+ const DEFAULT : bool = true ;
629+
630+ fn should_run ( run : ShouldRun < ' _ > ) -> ShouldRun < ' _ > {
631+ run. alias ( "editor" )
632+ }
633+
634+ fn make_run ( run : RunConfig < ' _ > ) {
635+ if run. builder . config . dry_run ( ) {
636+ return ;
637+ }
638+ if let [ cmd] = & run. paths [ ..] {
639+ if cmd. assert_single_path ( ) . path . as_path ( ) . as_os_str ( ) == "editor" {
640+ run. builder . ensure ( Editor ) ;
618641 }
619- fn run( self , builder: & Builder <' _>) -> Self :: Output {
620- let config = & builder. config;
621- if config. dry_run( ) {
622- return ;
642+ }
643+ }
644+
645+ fn run ( self , builder : & Builder < ' _ > ) -> Self :: Output {
646+ let config = & builder. config ;
647+ if config. dry_run ( ) {
648+ return ;
649+ }
650+ match EditorKind :: prompt_user ( ) {
651+ Ok ( editor_kind) => {
652+ if let Some ( editor_kind) = editor_kind {
653+ while !t ! ( create_editor_settings_maybe( config, editor_kind. clone( ) ) ) { }
654+ } else {
655+ println ! ( "Ok, skipping editor setup!" ) ;
623656 }
624- while !t!( create_editor_settings_maybe( config, EditorKind :: $kind) ) { }
625657 }
658+ Err ( e) => eprintln ! ( "Could not determine the editor: {e}" ) ,
626659 }
627- ) + } ;
660+ }
628661}
629662
630- impl_editor_support ! ( vscode, Vscode ) ;
631- impl_editor_support ! ( vim, Vim ) ;
632- impl_editor_support ! ( emacs, Emacs ) ;
633- impl_editor_support ! ( helix, Helix ) ;
634-
635663/// Create the recommended editor LSP config file for rustc development, or just print it
636664/// If this method should be re-called, it returns `false`.
637665fn create_editor_settings_maybe ( config : & Config , editor : EditorKind ) -> io:: Result < bool > {
@@ -662,13 +690,11 @@ fn create_editor_settings_maybe(config: &Config, editor: EditorKind) -> io::Resu
662690 ) ;
663691
664692 match mismatched_settings {
665- Some ( true ) => eprintln ! (
666- "WARNING: existing `{}` is out of date, x.py will update it" ,
667- settings_filename
668- ) ,
693+ Some ( true ) => {
694+ eprintln ! ( "WARNING: existing `{settings_filename}` is out of date, x.py will update it" )
695+ }
669696 Some ( false ) => eprintln ! (
670- "WARNING: existing `{}` has been modified by user, x.py will back it up and replace it" ,
671- settings_filename
697+ "WARNING: existing `{settings_filename}` has been modified by user, x.py will back it up and replace it"
672698 ) ,
673699 _ => ( ) ,
674700 }
0 commit comments