Skip to content

Commit

Permalink
Rollup merge of rust-lang#127793 - ChaiTRex:zed_support, r=Kobzol
Browse files Browse the repository at this point in the history
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
      },
    ```
  • Loading branch information
matthiaskrgr authored Feb 18, 2025
2 parents 3b022d8 + 410331c commit 1611cf1
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 39 deletions.
83 changes: 53 additions & 30 deletions src/bootstrap/src/core/build_steps/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,48 +523,73 @@ undesirable, simply delete the `pre-push` file from .git/hooks."
/// Handles editor-specific setup differences
#[derive(Clone, Debug, Eq, PartialEq)]
enum EditorKind {
Vscode,
Vim,
Emacs,
Helix,
Vim,
VsCode,
Zed,
}

impl EditorKind {
// Used in `./tests.rs`.
#[allow(dead_code)]
pub const ALL: &[EditorKind] = &[
EditorKind::Emacs,
EditorKind::Helix,
EditorKind::Vim,
EditorKind::VsCode,
EditorKind::Zed,
];

fn prompt_user() -> io::Result<Option<EditorKind>> {
let prompt_str = "Available editors:
1. vscode
2. vim
3. emacs
4. helix
1. Emacs
2. Helix
3. Vim
4. VS Code
5. Zed
Select which editor you would like to set up [default: None]: ";

let mut input = String::new();
loop {
print!("{}", prompt_str);
io::stdout().flush()?;
input.clear();
io::stdin().read_line(&mut input)?;
match input.trim().to_lowercase().as_str() {
"1" | "vscode" => return Ok(Some(EditorKind::Vscode)),
"2" | "vim" => return Ok(Some(EditorKind::Vim)),
"3" | "emacs" => return Ok(Some(EditorKind::Emacs)),
"4" | "helix" => return Ok(Some(EditorKind::Helix)),
"" => return Ok(None),

let mut modified_input = input.to_lowercase();
modified_input.retain(|ch| !ch.is_whitespace());
match modified_input.as_str() {
"1" | "emacs" => return Ok(Some(EditorKind::Emacs)),
"2" | "helix" => return Ok(Some(EditorKind::Helix)),
"3" | "vim" => return Ok(Some(EditorKind::Vim)),
"4" | "vscode" => return Ok(Some(EditorKind::VsCode)),
"5" | "zed" => return Ok(Some(EditorKind::Zed)),
"" | "none" => return Ok(None),
_ => {
eprintln!("ERROR: unrecognized option '{}'", input.trim());
eprintln!("NOTE: press Ctrl+C to exit");
}
};
}

input.clear();
}
}

/// A list of historical hashes of each LSP settings file
/// New entries should be appended whenever this is updated so we can detect
/// outdated vs. user-modified settings files.
fn hashes(&self) -> Vec<&str> {
fn hashes(&self) -> &'static [&'static str] {
match self {
EditorKind::Vscode | EditorKind::Vim => vec![
EditorKind::Emacs => &[
"51068d4747a13732440d1a8b8f432603badb1864fa431d83d0fd4f8fa57039e0",
"d29af4d949bbe2371eac928a3c31cf9496b1701aa1c45f11cd6c759865ad5c45",
],
EditorKind::Helix => &[
"2d3069b8cf1b977e5d4023965eb6199597755e6c96c185ed5f2854f98b83d233",
"6736d61409fbebba0933afd2e4c44ff2f97c1cb36cf0299a7f4a7819b8775040",
],
EditorKind::Vim | EditorKind::VsCode => &[
"ea67e259dedf60d4429b6c349a564ffcd1563cf41c920a856d1f5b16b4701ac8",
"56e7bf011c71c5d81e0bf42e84938111847a810eee69d906bba494ea90b51922",
"af1b5efe196aed007577899db9dae15d6dbc923d6fa42fa0934e68617ba9bbe0",
Expand All @@ -576,12 +601,8 @@ Select which editor you would like to set up [default: None]: ";
"4eecb58a2168b252077369da446c30ed0e658301efe69691979d1ef0443928f4",
"c394386e6133bbf29ffd32c8af0bb3d4aac354cba9ee051f29612aa9350f8f8d",
],
EditorKind::Emacs => vec![
"51068d4747a13732440d1a8b8f432603badb1864fa431d83d0fd4f8fa57039e0",
"d29af4d949bbe2371eac928a3c31cf9496b1701aa1c45f11cd6c759865ad5c45",
],
EditorKind::Helix => {
vec!["2d3069b8cf1b977e5d4023965eb6199597755e6c96c185ed5f2854f98b83d233"]
EditorKind::Zed => {
&["bbce727c269d1bd0c98afef4d612eb4ce27aea3c3a8968c5f10b31affbc40b6c"]
}
}
}
Expand All @@ -592,29 +613,31 @@ Select which editor you would like to set up [default: None]: ";

fn settings_short_path(&self) -> PathBuf {
self.settings_folder().join(match self {
EditorKind::Vscode => "settings.json",
EditorKind::Vim => "coc-settings.json",
EditorKind::Emacs => ".dir-locals.el",
EditorKind::Helix => "languages.toml",
EditorKind::Vim => "coc-settings.json",
EditorKind::VsCode | EditorKind::Zed => "settings.json",
})
}

fn settings_folder(&self) -> PathBuf {
match self {
EditorKind::Vscode => PathBuf::from(".vscode"),
EditorKind::Vim => PathBuf::from(".vim"),
EditorKind::Emacs => PathBuf::new(),
EditorKind::Helix => PathBuf::from(".helix"),
EditorKind::Vim => PathBuf::from(".vim"),
EditorKind::VsCode => PathBuf::from(".vscode"),
EditorKind::Zed => PathBuf::from(".zed"),
}
}

fn settings_template(&self) -> &str {
fn settings_template(&self) -> &'static str {
match self {
EditorKind::Vscode | EditorKind::Vim => {
include_str!("../../../../etc/rust_analyzer_settings.json")
}
EditorKind::Emacs => include_str!("../../../../etc/rust_analyzer_eglot.el"),
EditorKind::Helix => include_str!("../../../../etc/rust_analyzer_helix.toml"),
EditorKind::Vim | EditorKind::VsCode => {
include_str!("../../../../etc/rust_analyzer_settings.json")
}
EditorKind::Zed => include_str!("../../../../etc/rust_analyzer_zed.json"),
}
}

Expand Down
21 changes: 12 additions & 9 deletions src/bootstrap/src/core/build_steps/setup/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ use crate::utils::helpers::hex_encode;

#[test]
fn check_matching_settings_hash() {
let editor = EditorKind::Vscode;
let mut hasher = sha2::Sha256::new();
hasher.update(&editor.settings_template());
let hash = hex_encode(hasher.finalize().as_slice());
assert_eq!(
&hash,
editor.hashes().last().unwrap(),
"Update `EditorKind::hashes()` with the new hash of `src/etc/rust_analyzer_settings.json`"
);
for editor in EditorKind::ALL {
let mut hasher = sha2::Sha256::new();
hasher.update(&editor.settings_template());
let hash = hex_encode(hasher.finalize().as_slice());
assert_eq!(
&hash,
editor.hashes().last().unwrap(),
"Update `EditorKind::hashes()` with the new hash of `{}` for `EditorKind::{:?}`",
editor.settings_template(),
editor,
);
}
}
11 changes: 11 additions & 0 deletions src/doc/rustc-dev-guide/src/building/suggested.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@ You can run `./x setup editor` and select `helix`, which will prompt you to
create `languages.toml` with the recommended configuration for Helix. The
recommended settings live at [`src/etc/rust_analyzer_helix.toml`].

### Zed

Zed comes with built-in LSP and rust-analyzer support.
It can be configured through `.zed/settings.json`, as described
[here](https://zed.dev/docs/configuring-languages). Selecting `zed`
in `./x setup editor` will prompt you to create a `.zed/settings.json`
file which will configure Zed with the recommended configuration. The
recommended `rust-analyzer` settings live
at [`src/etc/rust_analyzer_zed.json`].

## Check, check, and check again

When doing simple refactoring, it can be useful to run `./x check`
Expand Down Expand Up @@ -381,4 +391,5 @@ load this completion.
[`src/etc/rust_analyzer_settings.json`]: https://github.com/rust-lang/rust/blob/master/src/etc/rust_analyzer_settings.json
[`src/etc/rust_analyzer_eglot.el`]: https://github.com/rust-lang/rust/blob/master/src/etc/rust_analyzer_eglot.el
[`src/etc/rust_analyzer_helix.toml`]: https://github.com/rust-lang/rust/blob/master/src/etc/rust_analyzer_helix.toml
[`src/etc/rust_analyzer_zed.json`]: https://github.com/rust-lang/rust/blob/master/src/etc/rust_analyzer_zed.json
[`src/etc/pre-push.sh`]: https://github.com/rust-lang/rust/blob/master/src/etc/pre-push.sh
52 changes: 52 additions & 0 deletions src/etc/rust_analyzer_zed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"lsp": {
"rust-analyzer": {
"initialization_options": {
"cargo": {
"buildScripts": {
"enable": true,
"invocationLocation": "root",
"invocationStrategy": "once",
"overrideCommand": ["python3", "x.py", "check", "--json-output"]
},
"extraEnv": {
"RUSTC_BOOTSTRAP": "1"
},
"sysrootSrc": "./library"
},
"check": {
"invocationLocation": "root",
"invocationStrategy": "once",
"overrideCommand": ["python3", "x.py", "check", "--json-output"]
},
"linkedProjects": [
"Cargo.toml",
"library/Cargo.toml",
"src/tools/x/Cargo.toml",
"src/bootstrap/Cargo.toml",
"src/tools/rust-analyzer/Cargo.toml",
"compiler/rustc_codegen_cranelift/Cargo.toml",
"compiler/rustc_codegen_gcc/Cargo.toml"
],
"procMacro": {
"enable": true,
"server": "${workspaceFolder}/build/host/stage0/libexec/rust-analyzer-proc-macro-srv"
},
"rustc": {
"source": "./Cargo.toml"
},
"rustfmt": {
"overrideCommand": [
"${workspaceFolder}/build/host/rustfmt/bin/rustfmt",
"--edition=2021"
]
},
"server": {
"extraEnv": {
"RUSTUP_TOOLCHAIN": "nightly"
}
}
}
}
}
}

0 comments on commit 1611cf1

Please sign in to comment.