rust-analyzer/editors/code/src/main.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

217 lines
8.1 KiB
TypeScript
Raw Normal View History

2018-08-10 12:07:43 +00:00
import * as vscode from "vscode";
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";
import { type CommandFactory, Ctx, fetchWorkspace } from "./ctx";
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
const RUST_PROJECT_CONTEXT_NAME = "inRustProject";
export interface RustAnalyzerExtensionApi {
// FIXME: this should be non-optional
2022-10-17 12:20:14 +00:00
readonly client?: lc.LanguageClient;
}
2022-10-17 13:05:20 +00:00
export async function deactivate() {
await setContextValue(RUST_PROJECT_CONTEXT_NAME, undefined);
}
export async function activate(
2023-07-11 13:35:10 +00:00
context: vscode.ExtensionContext,
): Promise<RustAnalyzerExtensionApi> {
checkConflictingExtensions();
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) => {
void vscode.window.showErrorMessage(
2023-07-11 13:35:10 +00:00
`Cannot activate rust-analyzer extension: ${err.message}`,
);
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
}
const diagnosticProvider = new diagnostics.TextDocumentProvider(ctx);
ctx.pushExtCleanup(
vscode.workspace.registerTextDocumentContentProvider(
diagnostics.URI_SCHEME,
2023-07-11 13:35:10 +00:00
diagnosticProvider,
),
);
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,
);
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,
);
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,
);
vscode.workspace.onDidChangeWorkspaceFolders(
async (_) => ctx.onWorkspaceFolderChanges(),
null,
2023-07-11 13:35:10 +00:00
ctx.subscriptions,
);
vscode.workspace.onDidChangeConfiguration(
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: "",
});
},
null,
2023-07-11 13:35:10 +00:00
ctx.subscriptions,
);
2024-09-25 13:02:36 +00:00
if (ctx.config.debug.buildBeforeRestart) {
initializeDebugSessionTrackingAndRebuild(ctx);
}
if (ctx.config.initializeStopped) {
ctx.setServerStatus({
health: "stopped",
});
} else {
await ctx.start();
}
2022-10-28 22:44:37 +00:00
return ctx;
}
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" }),
},
restartServer: {
enabled: (ctx) => async () => {
await ctx.restart();
},
disabled: (ctx) => async () => {
await ctx.start();
},
},
startServer: {
enabled: (ctx) => async () => {
await ctx.start();
},
disabled: (ctx) => async () => {
await ctx.start();
},
},
stopServer: {
enabled: (ctx) => async () => {
// FIXME: We should re-use the client, that is ctx.deactivate() if none of the configs have changed
await ctx.stopAndDispose();
ctx.setServerStatus({
health: "stopped",
});
},
2024-01-22 11:33:26 +00:00
disabled: (_) => async () => {},
},
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 },
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 },
viewMemoryLayout: { enabled: commands.viewMemoryLayout },
toggleCheckOnSave: { enabled: commands.toggleCheckOnSave },
toggleLSPLogs: { enabled: commands.toggleLSPLogs },
openWalkthrough: { enabled: commands.openWalkthrough },
// 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 },
rename: { enabled: commands.rename },
2023-04-03 01:37:07 +00:00
openLogs: { enabled: commands.openLogs },
revealDependency: { enabled: commands.revealDependency },
};
}
function checkConflictingExtensions() {
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 11:33:26 +00:00
.then(() => {}, console.error);
}
2024-01-22 11:33:26 +00:00
}