Skip to content

Commit 406bb9c

Browse files
authored
Rollup merge of rust-lang#127793 - ChaiTRex:zed_support, r=Kobzol
Added project-specific Zed IDE settings This repository currently has project-specific VS Code IDE settings in `.vscode` and `compiler/rustc_codegen_cranelift/.vscode`. Now there are equivalent project-specific Zed IDE settings alongside those. This fixes `rust-analyzer` not being able to properly handle this project. Note that: 1. The contents of `src/tools/rust-analyzer/.vscode` could not be translated to Zed, as they aren't basic IDE settings. 2. One of the VS Code settings in `.vscode` has no corresponding setting in Zed, and so this has been noted like this: ```json "_settings_only_in_vs_code_not_yet_in_zed": { "git.detectSubmodulesLimit": 20 }, ```
2 parents 3b022d8 + 410331c commit 406bb9c

File tree

4 files changed

+128
-39
lines changed

4 files changed

+128
-39
lines changed

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

+53-30
Original file line numberDiff line numberDiff line change
@@ -523,48 +523,73 @@ undesirable, simply delete the `pre-push` file from .git/hooks."
523523
/// Handles editor-specific setup differences
524524
#[derive(Clone, Debug, Eq, PartialEq)]
525525
enum EditorKind {
526-
Vscode,
527-
Vim,
528526
Emacs,
529527
Helix,
528+
Vim,
529+
VsCode,
530+
Zed,
530531
}
531532

532533
impl EditorKind {
534+
// Used in `./tests.rs`.
535+
#[allow(dead_code)]
536+
pub const ALL: &[EditorKind] = &[
537+
EditorKind::Emacs,
538+
EditorKind::Helix,
539+
EditorKind::Vim,
540+
EditorKind::VsCode,
541+
EditorKind::Zed,
542+
];
543+
533544
fn prompt_user() -> io::Result<Option<EditorKind>> {
534545
let prompt_str = "Available editors:
535-
1. vscode
536-
2. vim
537-
3. emacs
538-
4. helix
546+
1. Emacs
547+
2. Helix
548+
3. Vim
549+
4. VS Code
550+
5. Zed
539551
540552
Select which editor you would like to set up [default: None]: ";
541553

542554
let mut input = String::new();
543555
loop {
544556
print!("{}", prompt_str);
545557
io::stdout().flush()?;
546-
input.clear();
547558
io::stdin().read_line(&mut input)?;
548-
match input.trim().to_lowercase().as_str() {
549-
"1" | "vscode" => return Ok(Some(EditorKind::Vscode)),
550-
"2" | "vim" => return Ok(Some(EditorKind::Vim)),
551-
"3" | "emacs" => return Ok(Some(EditorKind::Emacs)),
552-
"4" | "helix" => return Ok(Some(EditorKind::Helix)),
553-
"" => return Ok(None),
559+
560+
let mut modified_input = input.to_lowercase();
561+
modified_input.retain(|ch| !ch.is_whitespace());
562+
match modified_input.as_str() {
563+
"1" | "emacs" => return Ok(Some(EditorKind::Emacs)),
564+
"2" | "helix" => return Ok(Some(EditorKind::Helix)),
565+
"3" | "vim" => return Ok(Some(EditorKind::Vim)),
566+
"4" | "vscode" => return Ok(Some(EditorKind::VsCode)),
567+
"5" | "zed" => return Ok(Some(EditorKind::Zed)),
568+
"" | "none" => return Ok(None),
554569
_ => {
555570
eprintln!("ERROR: unrecognized option '{}'", input.trim());
556571
eprintln!("NOTE: press Ctrl+C to exit");
557572
}
558-
};
573+
}
574+
575+
input.clear();
559576
}
560577
}
561578

562579
/// A list of historical hashes of each LSP settings file
563580
/// New entries should be appended whenever this is updated so we can detect
564581
/// outdated vs. user-modified settings files.
565-
fn hashes(&self) -> Vec<&str> {
582+
fn hashes(&self) -> &'static [&'static str] {
566583
match self {
567-
EditorKind::Vscode | EditorKind::Vim => vec![
584+
EditorKind::Emacs => &[
585+
"51068d4747a13732440d1a8b8f432603badb1864fa431d83d0fd4f8fa57039e0",
586+
"d29af4d949bbe2371eac928a3c31cf9496b1701aa1c45f11cd6c759865ad5c45",
587+
],
588+
EditorKind::Helix => &[
589+
"2d3069b8cf1b977e5d4023965eb6199597755e6c96c185ed5f2854f98b83d233",
590+
"6736d61409fbebba0933afd2e4c44ff2f97c1cb36cf0299a7f4a7819b8775040",
591+
],
592+
EditorKind::Vim | EditorKind::VsCode => &[
568593
"ea67e259dedf60d4429b6c349a564ffcd1563cf41c920a856d1f5b16b4701ac8",
569594
"56e7bf011c71c5d81e0bf42e84938111847a810eee69d906bba494ea90b51922",
570595
"af1b5efe196aed007577899db9dae15d6dbc923d6fa42fa0934e68617ba9bbe0",
@@ -576,12 +601,8 @@ Select which editor you would like to set up [default: None]: ";
576601
"4eecb58a2168b252077369da446c30ed0e658301efe69691979d1ef0443928f4",
577602
"c394386e6133bbf29ffd32c8af0bb3d4aac354cba9ee051f29612aa9350f8f8d",
578603
],
579-
EditorKind::Emacs => vec![
580-
"51068d4747a13732440d1a8b8f432603badb1864fa431d83d0fd4f8fa57039e0",
581-
"d29af4d949bbe2371eac928a3c31cf9496b1701aa1c45f11cd6c759865ad5c45",
582-
],
583-
EditorKind::Helix => {
584-
vec!["2d3069b8cf1b977e5d4023965eb6199597755e6c96c185ed5f2854f98b83d233"]
604+
EditorKind::Zed => {
605+
&["bbce727c269d1bd0c98afef4d612eb4ce27aea3c3a8968c5f10b31affbc40b6c"]
585606
}
586607
}
587608
}
@@ -592,29 +613,31 @@ Select which editor you would like to set up [default: None]: ";
592613

593614
fn settings_short_path(&self) -> PathBuf {
594615
self.settings_folder().join(match self {
595-
EditorKind::Vscode => "settings.json",
596-
EditorKind::Vim => "coc-settings.json",
597616
EditorKind::Emacs => ".dir-locals.el",
598617
EditorKind::Helix => "languages.toml",
618+
EditorKind::Vim => "coc-settings.json",
619+
EditorKind::VsCode | EditorKind::Zed => "settings.json",
599620
})
600621
}
601622

602623
fn settings_folder(&self) -> PathBuf {
603624
match self {
604-
EditorKind::Vscode => PathBuf::from(".vscode"),
605-
EditorKind::Vim => PathBuf::from(".vim"),
606625
EditorKind::Emacs => PathBuf::new(),
607626
EditorKind::Helix => PathBuf::from(".helix"),
627+
EditorKind::Vim => PathBuf::from(".vim"),
628+
EditorKind::VsCode => PathBuf::from(".vscode"),
629+
EditorKind::Zed => PathBuf::from(".zed"),
608630
}
609631
}
610632

611-
fn settings_template(&self) -> &str {
633+
fn settings_template(&self) -> &'static str {
612634
match self {
613-
EditorKind::Vscode | EditorKind::Vim => {
614-
include_str!("../../../../etc/rust_analyzer_settings.json")
615-
}
616635
EditorKind::Emacs => include_str!("../../../../etc/rust_analyzer_eglot.el"),
617636
EditorKind::Helix => include_str!("../../../../etc/rust_analyzer_helix.toml"),
637+
EditorKind::Vim | EditorKind::VsCode => {
638+
include_str!("../../../../etc/rust_analyzer_settings.json")
639+
}
640+
EditorKind::Zed => include_str!("../../../../etc/rust_analyzer_zed.json"),
618641
}
619642
}
620643

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

+12-9
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ use crate::utils::helpers::hex_encode;
55

66
#[test]
77
fn check_matching_settings_hash() {
8-
let editor = EditorKind::Vscode;
9-
let mut hasher = sha2::Sha256::new();
10-
hasher.update(&editor.settings_template());
11-
let hash = hex_encode(hasher.finalize().as_slice());
12-
assert_eq!(
13-
&hash,
14-
editor.hashes().last().unwrap(),
15-
"Update `EditorKind::hashes()` with the new hash of `src/etc/rust_analyzer_settings.json`"
16-
);
8+
for editor in EditorKind::ALL {
9+
let mut hasher = sha2::Sha256::new();
10+
hasher.update(&editor.settings_template());
11+
let hash = hex_encode(hasher.finalize().as_slice());
12+
assert_eq!(
13+
&hash,
14+
editor.hashes().last().unwrap(),
15+
"Update `EditorKind::hashes()` with the new hash of `{}` for `EditorKind::{:?}`",
16+
editor.settings_template(),
17+
editor,
18+
);
19+
}
1720
}

src/doc/rustc-dev-guide/src/building/suggested.md

+11
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,16 @@ You can run `./x setup editor` and select `helix`, which will prompt you to
154154
create `languages.toml` with the recommended configuration for Helix. The
155155
recommended settings live at [`src/etc/rust_analyzer_helix.toml`].
156156

157+
### Zed
158+
159+
Zed comes with built-in LSP and rust-analyzer support.
160+
It can be configured through `.zed/settings.json`, as described
161+
[here](https://zed.dev/docs/configuring-languages). Selecting `zed`
162+
in `./x setup editor` will prompt you to create a `.zed/settings.json`
163+
file which will configure Zed with the recommended configuration. The
164+
recommended `rust-analyzer` settings live
165+
at [`src/etc/rust_analyzer_zed.json`].
166+
157167
## Check, check, and check again
158168

159169
When doing simple refactoring, it can be useful to run `./x check`
@@ -381,4 +391,5 @@ load this completion.
381391
[`src/etc/rust_analyzer_settings.json`]: https://github.com/rust-lang/rust/blob/master/src/etc/rust_analyzer_settings.json
382392
[`src/etc/rust_analyzer_eglot.el`]: https://github.com/rust-lang/rust/blob/master/src/etc/rust_analyzer_eglot.el
383393
[`src/etc/rust_analyzer_helix.toml`]: https://github.com/rust-lang/rust/blob/master/src/etc/rust_analyzer_helix.toml
394+
[`src/etc/rust_analyzer_zed.json`]: https://github.com/rust-lang/rust/blob/master/src/etc/rust_analyzer_zed.json
384395
[`src/etc/pre-push.sh`]: https://github.com/rust-lang/rust/blob/master/src/etc/pre-push.sh

src/etc/rust_analyzer_zed.json

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"lsp": {
3+
"rust-analyzer": {
4+
"initialization_options": {
5+
"cargo": {
6+
"buildScripts": {
7+
"enable": true,
8+
"invocationLocation": "root",
9+
"invocationStrategy": "once",
10+
"overrideCommand": ["python3", "x.py", "check", "--json-output"]
11+
},
12+
"extraEnv": {
13+
"RUSTC_BOOTSTRAP": "1"
14+
},
15+
"sysrootSrc": "./library"
16+
},
17+
"check": {
18+
"invocationLocation": "root",
19+
"invocationStrategy": "once",
20+
"overrideCommand": ["python3", "x.py", "check", "--json-output"]
21+
},
22+
"linkedProjects": [
23+
"Cargo.toml",
24+
"library/Cargo.toml",
25+
"src/tools/x/Cargo.toml",
26+
"src/bootstrap/Cargo.toml",
27+
"src/tools/rust-analyzer/Cargo.toml",
28+
"compiler/rustc_codegen_cranelift/Cargo.toml",
29+
"compiler/rustc_codegen_gcc/Cargo.toml"
30+
],
31+
"procMacro": {
32+
"enable": true,
33+
"server": "${workspaceFolder}/build/host/stage0/libexec/rust-analyzer-proc-macro-srv"
34+
},
35+
"rustc": {
36+
"source": "./Cargo.toml"
37+
},
38+
"rustfmt": {
39+
"overrideCommand": [
40+
"${workspaceFolder}/build/host/rustfmt/bin/rustfmt",
41+
"--edition=2021"
42+
]
43+
},
44+
"server": {
45+
"extraEnv": {
46+
"RUSTUP_TOOLCHAIN": "nightly"
47+
}
48+
}
49+
}
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)