Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'editors/code/src/ctx.ts')
-rw-r--r--editors/code/src/ctx.ts54
1 files changed, 53 insertions, 1 deletions
diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts
index a7b7be03b5..e07fbb8bea 100644
--- a/editors/code/src/ctx.ts
+++ b/editors/code/src/ctx.ts
@@ -1,10 +1,11 @@
import * as vscode from "vscode";
-import type * as lc from "vscode-languageclient/node";
+import * as lc from "vscode-languageclient/node";
import * as ra from "./lsp_ext";
import { Config, prepareVSCodeConfig } from "./config";
import { createClient } from "./client";
import {
+ findRustToolchainFiles,
isCargoTomlEditor,
isDocumentInWorkspace,
isRustDocument,
@@ -266,6 +267,17 @@ export class Ctx implements RustAnalyzerExtensionApi {
this.outputChannel!.show();
}),
);
+ this.pushClientCleanup(
+ this._client.onNotification(
+ lc.ShowMessageNotification.type,
+ async (params: lc.ShowMessageParams) => {
+ // When an MSRV warning is detected and a rust-toolchain file exists,
+ // show an additional message with actionable guidance about adding
+ // the rust-analyzer component.
+ await handleMsrvWarning(params.message);
+ },
+ ),
+ );
}
return this._client;
}
@@ -592,3 +604,43 @@ export interface Disposable {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Cmd = (...args: any[]) => unknown;
+
+/**
+ * Pattern to detect MSRV warning messages from the rust-analyzer server.
+ */
+const MSRV_WARNING_PATTERN = /using an outdated toolchain version.*rust-analyzer only supports/is;
+
+/**
+ * Handles the MSRV warning by checking for rust-toolchain files and showing
+ * an enhanced message if found.
+ */
+export async function handleMsrvWarning(message: string): Promise<boolean> {
+ if (!MSRV_WARNING_PATTERN.test(message)) {
+ return false;
+ }
+
+ const toolchainFiles = await findRustToolchainFiles();
+ if (toolchainFiles.length === 0) {
+ return false;
+ }
+
+ const openFile = "Open rust-toolchain file";
+ const result = await vscode.window.showWarningMessage(
+ "Your workspace uses a rust-toolchain file with a toolchain too old for the extension shipped rust-analyzer to work properly. " +
+ "Consider adding the rust-analyzer component to the toolchain file to use a compatible rust-analyzer version. " +
+ "Add the following to your rust-toolchain file's `[toolchain]` section:\n" +
+ 'components = ["rust-analyzer"]',
+ { modal: true },
+ openFile,
+ );
+
+ if (result === openFile) {
+ const fileToOpen = toolchainFiles[0];
+ if (fileToOpen) {
+ const document = await vscode.workspace.openTextDocument(fileToOpen);
+ await vscode.window.showTextDocument(document);
+ }
+ }
+
+ return true;
+}