2018-08-10 12:07:43 +00:00
|
|
|
import * as vscode from "vscode";
|
2022-04-21 20:39:53 +00:00
|
|
|
import * as lc from "vscode-languageclient/node";
|
2018-08-10 12:07:43 +00:00
|
|
|
|
2018-10-07 20:59:02 +00:00
|
|
|
import * as commands from "./commands";
|
2023-07-10 21:10:00 +00:00
|
|
|
import { type CommandFactory, Ctx, fetchWorkspace } from "./ctx";
|
2023-01-03 15:16:16 +00:00
|
|
|
import * as diagnostics from "./diagnostics";
|
2023-04-03 01:37:07 +00:00
|
|
|
import { activateTaskProvider } from "./tasks";
|
|
|
|
import { setContextValue } from "./util";
|
2024-09-25 13:02:36 +00:00
|
|
|
import { initializeDebugSessionTrackingAndRebuild } from "./debug";
|
2018-08-10 12:07:43 +00:00
|
|
|
|
2020-05-27 16:40:13 +00:00
|
|
|
const RUST_PROJECT_CONTEXT_NAME = "inRustProject";
|
|
|
|
|
2022-04-21 20:39:53 +00:00
|
|
|
export interface RustAnalyzerExtensionApi {
|
2024-07-23 17:34:03 +00:00
|
|
|
// FIXME: this should be non-optional
|
2022-10-17 12:20:14 +00:00
|
|
|
readonly client?: lc.LanguageClient;
|
2022-04-21 20:39:53 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 13:05:20 +00:00
|
|
|
export async function deactivate() {
|
|
|
|
await setContextValue(RUST_PROJECT_CONTEXT_NAME, undefined);
|
|
|
|
}
|
|
|
|
|
2022-04-21 20:39:53 +00:00
|
|
|
export async function activate(
|
2023-07-11 13:35:10 +00:00
|
|
|
context: vscode.ExtensionContext,
|
2022-04-21 20:39:53 +00:00
|
|
|
): Promise<RustAnalyzerExtensionApi> {
|
2024-01-26 12:12:13 +00:00
|
|
|
checkConflictingExtensions();
|
2020-07-02 02:19:02 +00:00
|
|
|
|
2022-10-28 23:28:32 +00:00
|
|
|
const ctx = new Ctx(context, createCommands(), fetchWorkspace());
|
2022-10-17 12:20:14 +00:00
|
|
|
// VS Code doesn't show a notification when an extension fails to activate
|
|
|
|
// so we do it ourselves.
|
2022-10-17 13:05:20 +00:00
|
|
|
const api = await activateServer(ctx).catch((err) => {
|
2022-10-17 12:53:46 +00:00
|
|
|
void vscode.window.showErrorMessage(
|
2023-07-11 13:35:10 +00:00
|
|
|
`Cannot activate rust-analyzer extension: ${err.message}`,
|
2022-10-17 12:53:46 +00:00
|
|
|
);
|
2022-10-17 12:20:14 +00:00
|
|
|
throw err;
|
|
|
|
});
|
2022-10-17 13:05:20 +00:00
|
|
|
await setContextValue(RUST_PROJECT_CONTEXT_NAME, true);
|
|
|
|
return api;
|
2022-10-17 12:20:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function activateServer(ctx: Ctx): Promise<RustAnalyzerExtensionApi> {
|
|
|
|
if (ctx.workspace.kind === "Workspace Folder") {
|
|
|
|
ctx.pushExtCleanup(activateTaskProvider(ctx.config));
|
2020-03-31 08:05:22 +00:00
|
|
|
}
|
2020-03-30 17:12:22 +00:00
|
|
|
|
2023-01-03 15:16:16 +00:00
|
|
|
const diagnosticProvider = new diagnostics.TextDocumentProvider(ctx);
|
2022-11-18 18:47:45 +00:00
|
|
|
ctx.pushExtCleanup(
|
|
|
|
vscode.workspace.registerTextDocumentContentProvider(
|
2023-01-03 15:16:16 +00:00
|
|
|
diagnostics.URI_SCHEME,
|
2023-07-11 13:35:10 +00:00
|
|
|
diagnosticProvider,
|
|
|
|
),
|
2022-11-18 18:47:45 +00:00
|
|
|
);
|
|
|
|
|
2023-01-03 15:16:16 +00:00
|
|
|
const decorationProvider = new diagnostics.AnsiDecorationProvider(ctx);
|
|
|
|
ctx.pushExtCleanup(decorationProvider);
|
|
|
|
|
|
|
|
async function decorateVisibleEditors(document: vscode.TextDocument) {
|
|
|
|
for (const editor of vscode.window.visibleTextEditors) {
|
|
|
|
if (document === editor.document) {
|
|
|
|
await decorationProvider.provideDecorations(editor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vscode.workspace.onDidChangeTextDocument(
|
|
|
|
async (event) => await decorateVisibleEditors(event.document),
|
|
|
|
null,
|
2023-07-11 13:35:10 +00:00
|
|
|
ctx.subscriptions,
|
2023-01-03 15:16:16 +00:00
|
|
|
);
|
|
|
|
vscode.workspace.onDidOpenTextDocument(decorateVisibleEditors, null, ctx.subscriptions);
|
|
|
|
vscode.window.onDidChangeActiveTextEditor(
|
|
|
|
async (editor) => {
|
|
|
|
if (editor) {
|
|
|
|
diagnosticProvider.triggerUpdate(editor.document.uri);
|
|
|
|
await decorateVisibleEditors(editor.document);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
null,
|
2023-07-11 13:35:10 +00:00
|
|
|
ctx.subscriptions,
|
2023-01-03 15:16:16 +00:00
|
|
|
);
|
|
|
|
vscode.window.onDidChangeVisibleTextEditors(
|
|
|
|
async (visibleEditors) => {
|
|
|
|
for (const editor of visibleEditors) {
|
|
|
|
diagnosticProvider.triggerUpdate(editor.document.uri);
|
|
|
|
await decorationProvider.provideDecorations(editor);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
null,
|
2023-07-11 13:35:10 +00:00
|
|
|
ctx.subscriptions,
|
2023-01-03 15:16:16 +00:00
|
|
|
);
|
|
|
|
|
2022-10-28 23:28:32 +00:00
|
|
|
vscode.workspace.onDidChangeWorkspaceFolders(
|
|
|
|
async (_) => ctx.onWorkspaceFolderChanges(),
|
|
|
|
null,
|
2023-07-11 13:35:10 +00:00
|
|
|
ctx.subscriptions,
|
2022-10-28 23:28:32 +00:00
|
|
|
);
|
2020-11-15 22:19:04 +00:00
|
|
|
vscode.workspace.onDidChangeConfiguration(
|
2022-10-17 12:53:46 +00:00
|
|
|
async (_) => {
|
2022-11-27 17:46:37 +00:00
|
|
|
await ctx.client?.sendNotification(lc.DidChangeConfigurationNotification.type, {
|
2022-10-28 22:44:37 +00:00
|
|
|
settings: "",
|
|
|
|
});
|
2022-10-17 12:53:46 +00:00
|
|
|
},
|
2020-11-15 22:19:04 +00:00
|
|
|
null,
|
2023-07-11 13:35:10 +00:00
|
|
|
ctx.subscriptions,
|
2020-11-15 22:19:04 +00:00
|
|
|
);
|
2022-04-21 20:39:53 +00:00
|
|
|
|
2024-09-25 13:02:36 +00:00
|
|
|
if (ctx.config.debug.buildBeforeRestart) {
|
|
|
|
initializeDebugSessionTrackingAndRebuild(ctx);
|
|
|
|
}
|
|
|
|
|
2024-10-21 08:58:54 +00:00
|
|
|
if (ctx.config.initializeStopped) {
|
|
|
|
ctx.setServerStatus({
|
|
|
|
health: "stopped",
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
await ctx.start();
|
|
|
|
}
|
|
|
|
|
2022-10-28 22:44:37 +00:00
|
|
|
return ctx;
|
2020-11-15 22:19:04 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 14:00:43 +00:00
|
|
|
function createCommands(): Record<string, CommandFactory> {
|
|
|
|
return {
|
|
|
|
onEnter: {
|
|
|
|
enabled: commands.onEnter,
|
2023-04-03 01:37:07 +00:00
|
|
|
disabled: (_) => () => vscode.commands.executeCommand("default:type", { text: "\n" }),
|
2022-10-21 14:00:43 +00:00
|
|
|
},
|
2023-04-28 19:34:31 +00:00
|
|
|
restartServer: {
|
2022-10-21 14:00:43 +00:00
|
|
|
enabled: (ctx) => async () => {
|
2022-10-28 23:28:32 +00:00
|
|
|
await ctx.restart();
|
2022-10-21 14:00:43 +00:00
|
|
|
},
|
|
|
|
disabled: (ctx) => async () => {
|
2022-10-28 23:28:32 +00:00
|
|
|
await ctx.start();
|
2022-10-21 14:00:43 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
startServer: {
|
|
|
|
enabled: (ctx) => async () => {
|
2022-10-28 23:28:32 +00:00
|
|
|
await ctx.start();
|
2022-10-21 14:00:43 +00:00
|
|
|
},
|
|
|
|
disabled: (ctx) => async () => {
|
2022-10-28 23:28:32 +00:00
|
|
|
await ctx.start();
|
2022-10-21 14:00:43 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
stopServer: {
|
|
|
|
enabled: (ctx) => async () => {
|
|
|
|
// FIXME: We should re-use the client, that is ctx.deactivate() if none of the configs have changed
|
2022-10-28 23:28:32 +00:00
|
|
|
await ctx.stopAndDispose();
|
2022-10-21 14:00:43 +00:00
|
|
|
ctx.setServerStatus({
|
2022-10-28 21:10:10 +00:00
|
|
|
health: "stopped",
|
2022-10-21 14:00:43 +00:00
|
|
|
});
|
|
|
|
},
|
2024-01-22 11:33:26 +00:00
|
|
|
disabled: (_) => async () => {},
|
2022-10-21 14:00:43 +00:00
|
|
|
},
|
2018-08-17 16:54:08 +00:00
|
|
|
|
2023-04-03 01:37:07 +00:00
|
|
|
analyzerStatus: { enabled: commands.analyzerStatus },
|
|
|
|
memoryUsage: { enabled: commands.memoryUsage },
|
|
|
|
reloadWorkspace: { enabled: commands.reloadWorkspace },
|
|
|
|
rebuildProcMacros: { enabled: commands.rebuildProcMacros },
|
|
|
|
matchingBrace: { enabled: commands.matchingBrace },
|
|
|
|
joinLines: { enabled: commands.joinLines },
|
|
|
|
parentModule: { enabled: commands.parentModule },
|
|
|
|
syntaxTree: { enabled: commands.syntaxTree },
|
|
|
|
viewHir: { enabled: commands.viewHir },
|
|
|
|
viewMir: { enabled: commands.viewMir },
|
2023-04-28 17:14:30 +00:00
|
|
|
interpretFunction: { enabled: commands.interpretFunction },
|
2023-04-03 01:37:07 +00:00
|
|
|
viewFileText: { enabled: commands.viewFileText },
|
|
|
|
viewItemTree: { enabled: commands.viewItemTree },
|
|
|
|
viewCrateGraph: { enabled: commands.viewCrateGraph },
|
|
|
|
viewFullCrateGraph: { enabled: commands.viewFullCrateGraph },
|
|
|
|
expandMacro: { enabled: commands.expandMacro },
|
|
|
|
run: { enabled: commands.run },
|
|
|
|
copyRunCommandLine: { enabled: commands.copyRunCommandLine },
|
|
|
|
debug: { enabled: commands.debug },
|
|
|
|
newDebugConfig: { enabled: commands.newDebugConfig },
|
|
|
|
openDocs: { enabled: commands.openDocs },
|
2023-10-18 12:02:56 +00:00
|
|
|
openExternalDocs: { enabled: commands.openExternalDocs },
|
2023-04-03 01:37:07 +00:00
|
|
|
openCargoToml: { enabled: commands.openCargoToml },
|
|
|
|
peekTests: { enabled: commands.peekTests },
|
|
|
|
moveItemUp: { enabled: commands.moveItemUp },
|
|
|
|
moveItemDown: { enabled: commands.moveItemDown },
|
|
|
|
cancelFlycheck: { enabled: commands.cancelFlycheck },
|
|
|
|
clearFlycheck: { enabled: commands.clearFlycheck },
|
|
|
|
runFlycheck: { enabled: commands.runFlycheck },
|
|
|
|
ssr: { enabled: commands.ssr },
|
|
|
|
serverVersion: { enabled: commands.serverVersion },
|
2023-06-19 00:31:46 +00:00
|
|
|
viewMemoryLayout: { enabled: commands.viewMemoryLayout },
|
2023-08-12 06:25:51 +00:00
|
|
|
toggleCheckOnSave: { enabled: commands.toggleCheckOnSave },
|
2024-06-17 09:48:37 +00:00
|
|
|
toggleLSPLogs: { enabled: commands.toggleLSPLogs },
|
2024-06-28 12:30:13 +00:00
|
|
|
openWalkthrough: { enabled: commands.openWalkthrough },
|
2022-10-21 14:00:43 +00:00
|
|
|
// Internal commands which are invoked by the server.
|
2023-04-03 01:37:07 +00:00
|
|
|
applyActionGroup: { enabled: commands.applyActionGroup },
|
|
|
|
applySnippetWorkspaceEdit: { enabled: commands.applySnippetWorkspaceEditCommand },
|
|
|
|
debugSingle: { enabled: commands.debugSingle },
|
|
|
|
gotoLocation: { enabled: commands.gotoLocation },
|
2024-06-19 07:36:22 +00:00
|
|
|
hoverRefCommandProxy: { enabled: commands.hoverRefCommandProxy },
|
2023-04-03 01:37:07 +00:00
|
|
|
resolveCodeAction: { enabled: commands.resolveCodeAction },
|
|
|
|
runSingle: { enabled: commands.runSingle },
|
|
|
|
showReferences: { enabled: commands.showReferences },
|
|
|
|
triggerParameterHints: { enabled: commands.triggerParameterHints },
|
2024-07-11 08:55:34 +00:00
|
|
|
rename: { enabled: commands.rename },
|
2023-04-03 01:37:07 +00:00
|
|
|
openLogs: { enabled: commands.openLogs },
|
|
|
|
revealDependency: { enabled: commands.revealDependency },
|
2022-10-21 14:00:43 +00:00
|
|
|
};
|
2020-12-18 17:39:51 +00:00
|
|
|
}
|
2024-01-22 10:49:39 +00:00
|
|
|
|
2024-01-26 12:12:13 +00:00
|
|
|
function checkConflictingExtensions() {
|
2024-01-22 10:49:39 +00:00
|
|
|
if (vscode.extensions.getExtension("rust-lang.rust")) {
|
|
|
|
vscode.window
|
|
|
|
.showWarningMessage(
|
|
|
|
`You have both the rust-analyzer (rust-lang.rust-analyzer) and Rust (rust-lang.rust) ` +
|
2024-01-22 11:33:26 +00:00
|
|
|
"plugins enabled. These are known to conflict and cause various functions of " +
|
|
|
|
"both plugins to not work correctly. You should disable one of them.",
|
|
|
|
"Got it",
|
2024-01-22 10:49:39 +00:00
|
|
|
)
|
2024-01-22 11:33:26 +00:00
|
|
|
.then(() => {}, console.error);
|
2024-01-22 10:49:39 +00:00
|
|
|
}
|
2024-01-22 11:33:26 +00:00
|
|
|
}
|