diff --git a/crates/rust-analyzer/build.rs b/crates/rust-analyzer/build.rs index 05f9772c0410..d4b010c04e9b 100644 --- a/crates/rust-analyzer/build.rs +++ b/crates/rust-analyzer/build.rs @@ -1,12 +1,40 @@ //! Just embed git-hash to `--version` -use std::process::Command; +use std::{env, path::PathBuf, process::Command}; fn main() { + set_rerun(); + let rev = rev().unwrap_or_else(|| "???????".to_string()); println!("cargo:rustc-env=REV={}", rev) } +fn set_rerun() { + let mut manifest_dir = PathBuf::from( + env::var("CARGO_MANIFEST_DIR").expect("`CARGO_MANIFEST_DIR` is always set by cargo."), + ); + + while manifest_dir.parent().is_some() { + if manifest_dir.join(".git/HEAD").exists() { + let git_dir = manifest_dir.join(".git"); + + println!("cargo:rerun-if-changed={}", git_dir.join("HEAD").display()); + // current branch ref + if let Ok(output) = + Command::new("git").args(&["rev-parse", "--symbolic-full-name", "HEAD"]).output() + { + if let Ok(ref_link) = String::from_utf8(output.stdout) { + println!("cargo:rerun-if-changed={}", git_dir.join(ref_link).display()); + } + } + return; + } + + manifest_dir.pop(); + } + println!("cargo:warning=Could not find `.git/HEAD` from manifest dir!"); +} + fn rev() -> Option { let output = Command::new("git").args(&["rev-parse", "HEAD"]).output().ok()?; let stdout = String::from_utf8(output.stdout).ok()?; diff --git a/docs/user/features.md b/docs/user/features.md index 309d2775d5d9..a00fa35da0c0 100644 --- a/docs/user/features.md +++ b/docs/user/features.md @@ -89,6 +89,10 @@ Shows the full macro expansion of the macro at current cursor. Shows internal statistic about memory usage of rust-analyzer +#### Show RA Version + +Show current rust-analyzer version + #### Run garbage collection Manually triggers GC diff --git a/editors/code/package.json b/editors/code/package.json index c498c14b4290..72befe2b63a3 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -131,6 +131,11 @@ "command": "rust-analyzer.ssr", "title": "Structural Search Replace", "category": "Rust Analyzer" + }, + { + "command": "rust-analyzer.serverVersion", + "title": "Show RA Version", + "category": "Rust Analyzer" } ], "keybindings": [ diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index bebd99ca931f..839245f48756 100644 --- a/editors/code/src/commands/index.ts +++ b/editors/code/src/commands/index.ts @@ -13,6 +13,7 @@ export * from './syntax_tree'; export * from './expand_macro'; export * from './runnables'; export * from './ssr'; +export * from './server_version'; export function collectGarbage(ctx: Ctx): Cmd { return async () => { diff --git a/editors/code/src/commands/server_version.ts b/editors/code/src/commands/server_version.ts new file mode 100644 index 000000000000..421301b42cdf --- /dev/null +++ b/editors/code/src/commands/server_version.ts @@ -0,0 +1,21 @@ +import * as vscode from 'vscode'; +import { ensureServerBinary } from '../installation/server'; +import { Ctx, Cmd } from '../ctx'; +import { spawnSync } from 'child_process'; + +export function serverVersion(ctx: Ctx): Cmd { + return async () => { + const binaryPath = await ensureServerBinary(ctx.config.serverSource); + + if (binaryPath == null) { + throw new Error( + "Rust Analyzer Language Server is not available. " + + "Please, ensure its [proper installation](https://rust-analyzer.github.io/manual.html#installation)." + ); + } + + const version = spawnSync(binaryPath, ["--version"], { encoding: "utf8" }).stdout; + vscode.window.showInformationMessage('rust-analyzer version : ' + version); + }; +} + diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index a22e0bc66601..de19a44e517d 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -55,6 +55,7 @@ export async function activate(context: vscode.ExtensionContext) { ctx.registerCommand('run', commands.run); ctx.registerCommand('onEnter', commands.onEnter); ctx.registerCommand('ssr', commands.ssr); + ctx.registerCommand('serverVersion', commands.serverVersion); // Internal commands which are invoked by the server. ctx.registerCommand('runSingle', commands.runSingle);