Skip to content

Commit 456be10

Browse files
committed
bootstrap: Consolidate editor LSP setup
Consolidate LSP setup for different editors into one `./x setup editor`.
1 parent 9b24fae commit 456be10

File tree

5 files changed

+77
-62
lines changed

5 files changed

+77
-62
lines changed

src/bootstrap/src/core/build_steps/setup.rs

+71-45
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,35 @@ enum EditorKind {
528528
}
529529

530530
impl 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`.
637665
fn 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
}

src/bootstrap/src/core/builder.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -1001,15 +1001,7 @@ impl<'a> Builder<'a> {
10011001
run::GenerateCompletions,
10021002
),
10031003
Kind::Setup => {
1004-
describe!(
1005-
setup::Profile,
1006-
setup::Hook,
1007-
setup::Link,
1008-
setup::Vscode,
1009-
setup::Emacs,
1010-
setup::Helix,
1011-
setup::Vim
1012-
)
1004+
describe!(setup::Profile, setup::Hook, setup::Link, setup::Editor)
10131005
}
10141006
Kind::Clean => describe!(clean::CleanAll, clean::Rustc, clean::Std),
10151007
Kind::Vendor => describe!(vendor::Vendor),

src/bootstrap/src/core/config/flags.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -447,17 +447,14 @@ Arguments:
447447
The profile is optional and you will be prompted interactively if it is not given.
448448
The following profiles are available:
449449
{}
450-
To only set up the git hook, VS Code config or toolchain link, you may use
450+
To only set up the git hook, editor config or toolchain link, you may use
451451
./x.py setup hook
452-
./x.py setup vscode
453-
./x.py setup vim
454-
./x.py setup emacs
455-
./x.py setup helix
452+
./x.py setup editor
456453
./x.py setup link", Profile::all_for_help(" ").trim_end()))]
457454
Setup {
458455
/// Either the profile for `config.toml` or another setup action.
459456
/// May be omitted to set up interactively
460-
#[arg(value_name = "<PROFILE>|hook|vscode|vim|emacs|helix|link")]
457+
#[arg(value_name = "<PROFILE>|hook|editor|link")]
461458
profile: Option<PathBuf>,
462459
},
463460
/// Suggest a subset of tests to run, based on modified files

src/bootstrap/src/utils/change_tracker.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,6 @@ pub const CONFIG_CHANGE_HISTORY: &[ChangeInfo] = &[
273273
ChangeInfo {
274274
change_id: 131075,
275275
severity: ChangeSeverity::Info,
276-
summary: "New options for ./x setup added - ./x setup [vim|emacs|helix]",
276+
summary: "New option `./x setup editor` added, replacing `./x setup vscode` and adding support for vim, emacs and helix.",
277277
},
278278
];

src/etc/completions/x.py.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -2741,7 +2741,7 @@ _x.py() {
27412741
return 0
27422742
;;
27432743
x.py__setup)
2744-
opts="-v -i -j -h --verbose --incremental --config --build-dir --build --host --target --exclude --skip --include-default-paths --rustc-error-format --on-fail --dry-run --dump-bootstrap-shims --stage --keep-stage --keep-stage-std --src --jobs --warnings --error-format --json-output --color --bypass-bootstrap-lock --rust-profile-generate --rust-profile-use --llvm-profile-use --llvm-profile-generate --enable-bolt-settings --skip-stage0-validation --reproducible-artifact --set --help [<PROFILE>|hook|vscode|vim|emacs|helix|link] [PATHS]... [ARGS]..."
2744+
opts="-v -i -j -h --verbose --incremental --config --build-dir --build --host --target --exclude --skip --include-default-paths --rustc-error-format --on-fail --dry-run --dump-bootstrap-shims --stage --keep-stage --keep-stage-std --src --jobs --warnings --error-format --json-output --color --bypass-bootstrap-lock --rust-profile-generate --rust-profile-use --llvm-profile-use --llvm-profile-generate --enable-bolt-settings --skip-stage0-validation --reproducible-artifact --set --help [<PROFILE>|hook|editor|link] [PATHS]... [ARGS]..."
27452745
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
27462746
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
27472747
return 0

0 commit comments

Comments
 (0)