2018-08-10 15:07:43 +03:00
|
|
|
import * as vscode from "vscode";
|
2022-04-21 13:39:53 -07:00
|
|
|
import * as lc from "vscode-languageclient/node";
|
2018-08-10 15:07:43 +03:00
|
|
|
|
2018-10-07 22:59:02 +02:00
|
|
|
import * as commands from "./commands";
|
2023-07-11 06:10:00 +09:00
|
|
|
import { type CommandFactory, Ctx, fetchWorkspace } from "./ctx";
|
2023-01-03 10:16:16 -05:00
|
|
|
import * as diagnostics from "./diagnostics";
|
2023-04-02 22:37:07 -03:00
|
|
|
import { activateTaskProvider } from "./tasks";
|
|
|
|
import { setContextValue } from "./util";
|
2024-01-03 14:55:39 +01:00
|
|
|
import type { JsonProject } from "./rust_project";
|
2018-08-10 15:07:43 +03:00
|
|
|
|
2020-05-27 19:40:13 +03:00
|
|
|
const RUST_PROJECT_CONTEXT_NAME = "inRustProject";
|
|
|
|
|
2023-10-17 11:29:11 -04:00
|
|
|
// This API is not stable and may break in between minor releases.
|
2022-04-21 13:39:53 -07:00
|
|
|
export interface RustAnalyzerExtensionApi {
|
2022-10-17 14:20:14 +02:00
|
|
|
readonly client?: lc.LanguageClient;
|
2023-10-17 11:29:11 -04:00
|
|
|
|
|
|
|
setWorkspaces(workspaces: JsonProject[]): void;
|
|
|
|
notifyRustAnalyzer(): Promise<void>;
|
2022-04-21 13:39:53 -07:00
|
|
|
}
|
|
|
|
|
2022-10-17 15:05:20 +02:00
|
|
|
export async function deactivate() {
|
|
|
|
await setContextValue(RUST_PROJECT_CONTEXT_NAME, undefined);
|
|
|
|
}
|
|
|
|
|
2022-04-21 13:39:53 -07:00
|
|
|
export async function activate(
|
2023-07-11 22:35:10 +09:00
|
|
|
context: vscode.ExtensionContext,
|
2022-04-21 13:39:53 -07:00
|
|
|
): Promise<RustAnalyzerExtensionApi> {
|
2022-10-17 14:20:14 +02: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) ` +
|
2023-06-19 00:24:42 -04: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.",
|
2023-07-11 22:35:10 +09:00
|
|
|
"Got it",
|
2022-10-17 14:20:14 +02:00
|
|
|
)
|
2023-06-19 00:24:42 -04:00
|
|
|
.then(() => {}, console.error);
|
2022-10-17 14:20:14 +02:00
|
|
|
}
|
2020-07-02 05:19:02 +03:00
|
|
|
|
2022-10-29 01:28:32 +02:00
|
|
|
const ctx = new Ctx(context, createCommands(), fetchWorkspace());
|
2022-10-17 14:20:14 +02:00
|
|
|
// VS Code doesn't show a notification when an extension fails to activate
|
|
|
|
// so we do it ourselves.
|
2022-10-17 15:05:20 +02:00
|
|
|
const api = await activateServer(ctx).catch((err) => {
|
2022-10-17 14:53:46 +02:00
|
|
|
void vscode.window.showErrorMessage(
|
2023-07-11 22:35:10 +09:00
|
|
|
`Cannot activate rust-analyzer extension: ${err.message}`,
|
2022-10-17 14:53:46 +02:00
|
|
|
);
|
2022-10-17 14:20:14 +02:00
|
|
|
throw err;
|
|
|
|
});
|
2022-10-17 15:05:20 +02:00
|
|
|
await setContextValue(RUST_PROJECT_CONTEXT_NAME, true);
|
|
|
|
return api;
|
2022-10-17 14:20:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function activateServer(ctx: Ctx): Promise<RustAnalyzerExtensionApi> {
|
|
|
|
if (ctx.workspace.kind === "Workspace Folder") {
|
|
|
|
ctx.pushExtCleanup(activateTaskProvider(ctx.config));
|
2020-03-31 09:05:22 +01:00
|
|
|
}
|
2020-03-30 18:12:22 +01:00
|
|
|
|
2023-01-03 10:16:16 -05:00
|
|
|
const diagnosticProvider = new diagnostics.TextDocumentProvider(ctx);
|
2022-11-18 19:47:45 +01:00
|
|
|
ctx.pushExtCleanup(
|
|
|
|
vscode.workspace.registerTextDocumentContentProvider(
|
2023-01-03 10:16:16 -05:00
|
|
|
diagnostics.URI_SCHEME,
|
2023-07-11 22:35:10 +09:00
|
|
|
diagnosticProvider,
|
|
|
|
),
|
2022-11-18 19:47:45 +01:00
|
|
|
);
|
|
|
|
|
2023-01-03 10:16:16 -05: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 22:35:10 +09:00
|
|
|
ctx.subscriptions,
|
2023-01-03 10:16:16 -05: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 22:35:10 +09:00
|
|
|
ctx.subscriptions,
|
2023-01-03 10:16:16 -05:00
|
|
|
);
|
|
|
|
vscode.window.onDidChangeVisibleTextEditors(
|
|
|
|
async (visibleEditors) => {
|
|
|
|
for (const editor of visibleEditors) {
|
|
|
|
diagnosticProvider.triggerUpdate(editor.document.uri);
|
|
|
|
await decorationProvider.provideDecorations(editor);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
null,
|
2023-07-11 22:35:10 +09:00
|
|
|
ctx.subscriptions,
|
2023-01-03 10:16:16 -05:00
|
|
|
);
|
|
|
|
|
2022-10-29 01:28:32 +02:00
|
|
|
vscode.workspace.onDidChangeWorkspaceFolders(
|
|
|
|
async (_) => ctx.onWorkspaceFolderChanges(),
|
|
|
|
null,
|
2023-07-11 22:35:10 +09:00
|
|
|
ctx.subscriptions,
|
2022-10-29 01:28:32 +02:00
|
|
|
);
|
2020-11-16 00:19:04 +02:00
|
|
|
vscode.workspace.onDidChangeConfiguration(
|
2022-10-17 14:53:46 +02:00
|
|
|
async (_) => {
|
2022-11-27 09:46:37 -08:00
|
|
|
await ctx.client?.sendNotification(lc.DidChangeConfigurationNotification.type, {
|
2022-10-29 00:44:37 +02:00
|
|
|
settings: "",
|
|
|
|
});
|
2022-10-17 14:53:46 +02:00
|
|
|
},
|
2020-11-16 00:19:04 +02:00
|
|
|
null,
|
2023-07-11 22:35:10 +09:00
|
|
|
ctx.subscriptions,
|
2020-11-16 00:19:04 +02:00
|
|
|
);
|
2022-04-21 13:39:53 -07:00
|
|
|
|
2022-10-29 01:28:32 +02:00
|
|
|
await ctx.start();
|
2022-10-29 00:44:37 +02:00
|
|
|
return ctx;
|
2020-11-16 00:19:04 +02:00
|
|
|
}
|
|
|
|
|
2022-10-21 16:00:43 +02:00
|
|
|
function createCommands(): Record<string, CommandFactory> {
|
|
|
|
return {
|
|
|
|
onEnter: {
|
|
|
|
enabled: commands.onEnter,
|
2023-04-02 22:37:07 -03:00
|
|
|
disabled: (_) => () => vscode.commands.executeCommand("default:type", { text: "\n" }),
|
2022-10-21 16:00:43 +02:00
|
|
|
},
|
2023-04-28 21:34:31 +02:00
|
|
|
restartServer: {
|
2022-10-21 16:00:43 +02:00
|
|
|
enabled: (ctx) => async () => {
|
2022-10-29 01:28:32 +02:00
|
|
|
await ctx.restart();
|
2022-10-21 16:00:43 +02:00
|
|
|
},
|
|
|
|
disabled: (ctx) => async () => {
|
2022-10-29 01:28:32 +02:00
|
|
|
await ctx.start();
|
2022-10-21 16:00:43 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
startServer: {
|
|
|
|
enabled: (ctx) => async () => {
|
2022-10-29 01:28:32 +02:00
|
|
|
await ctx.start();
|
2022-10-21 16:00:43 +02:00
|
|
|
},
|
|
|
|
disabled: (ctx) => async () => {
|
2022-10-29 01:28:32 +02:00
|
|
|
await ctx.start();
|
2022-10-21 16:00:43 +02: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-29 01:28:32 +02:00
|
|
|
await ctx.stopAndDispose();
|
2022-10-21 16:00:43 +02:00
|
|
|
ctx.setServerStatus({
|
2022-10-28 23:10:10 +02:00
|
|
|
health: "stopped",
|
2022-10-21 16:00:43 +02:00
|
|
|
});
|
|
|
|
},
|
2023-06-19 00:24:42 -04:00
|
|
|
disabled: (_) => async () => {},
|
2022-10-21 16:00:43 +02:00
|
|
|
},
|
2018-08-17 19:54:08 +03:00
|
|
|
|
2023-04-02 22:37:07 -03:00
|
|
|
analyzerStatus: { enabled: commands.analyzerStatus },
|
|
|
|
memoryUsage: { enabled: commands.memoryUsage },
|
|
|
|
shuffleCrateGraph: { enabled: commands.shuffleCrateGraph },
|
|
|
|
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 20:44:30 +03:30
|
|
|
interpretFunction: { enabled: commands.interpretFunction },
|
2023-04-02 22:37:07 -03: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 14:02:56 +02:00
|
|
|
openExternalDocs: { enabled: commands.openExternalDocs },
|
2023-04-02 22:37:07 -03: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-18 20:31:46 -04:00
|
|
|
viewMemoryLayout: { enabled: commands.viewMemoryLayout },
|
2023-08-12 08:25:51 +02:00
|
|
|
toggleCheckOnSave: { enabled: commands.toggleCheckOnSave },
|
2022-10-21 16:00:43 +02:00
|
|
|
// Internal commands which are invoked by the server.
|
2023-04-02 22:37:07 -03:00
|
|
|
applyActionGroup: { enabled: commands.applyActionGroup },
|
|
|
|
applySnippetWorkspaceEdit: { enabled: commands.applySnippetWorkspaceEditCommand },
|
|
|
|
debugSingle: { enabled: commands.debugSingle },
|
|
|
|
gotoLocation: { enabled: commands.gotoLocation },
|
|
|
|
linkToCommand: { enabled: commands.linkToCommand },
|
|
|
|
resolveCodeAction: { enabled: commands.resolveCodeAction },
|
|
|
|
runSingle: { enabled: commands.runSingle },
|
|
|
|
showReferences: { enabled: commands.showReferences },
|
|
|
|
triggerParameterHints: { enabled: commands.triggerParameterHints },
|
|
|
|
openLogs: { enabled: commands.openLogs },
|
|
|
|
revealDependency: { enabled: commands.revealDependency },
|
2022-10-21 16:00:43 +02:00
|
|
|
};
|
2020-12-18 18:39:51 +01:00
|
|
|
}
|