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";
|
2022-10-21 14:00:43 +00:00
|
|
|
import { CommandFactory, Ctx, Workspace } from "./ctx";
|
2022-10-17 12:53:46 +00:00
|
|
|
import { isRustDocument } from "./util";
|
2020-03-30 17:12:22 +00:00
|
|
|
import { activateTaskProvider } from "./tasks";
|
2020-05-27 16:40:13 +00:00
|
|
|
import { setContextValue } from "./util";
|
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 {
|
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(
|
|
|
|
context: vscode.ExtensionContext
|
|
|
|
): Promise<RustAnalyzerExtensionApi> {
|
2022-10-17 12:20:14 +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) ` +
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
.then(() => {}, console.error);
|
|
|
|
}
|
2020-07-02 02:19:02 +00:00
|
|
|
|
2022-08-23 13:45:02 +00:00
|
|
|
// We only support local folders, not eg. Live Share (`vlsl:` scheme), so don't activate if
|
|
|
|
// only those are in use.
|
|
|
|
// (r-a still somewhat works with Live Share, because commands are tunneled to the host)
|
2022-08-23 13:56:02 +00:00
|
|
|
const folders = (vscode.workspace.workspaceFolders || []).filter(
|
|
|
|
(folder) => folder.uri.scheme === "file"
|
2022-08-23 13:45:02 +00:00
|
|
|
);
|
|
|
|
const rustDocuments = vscode.workspace.textDocuments.filter((document) =>
|
|
|
|
isRustDocument(document)
|
|
|
|
);
|
|
|
|
|
2022-10-28 22:44:37 +00:00
|
|
|
// FIXME: This can change over time
|
2022-10-17 12:20:14 +00:00
|
|
|
const workspace: Workspace =
|
|
|
|
folders.length === 0
|
2022-10-28 22:44:37 +00:00
|
|
|
? rustDocuments.length === 0
|
|
|
|
? { kind: "Empty" }
|
|
|
|
: {
|
|
|
|
kind: "Detached Files",
|
|
|
|
files: rustDocuments,
|
|
|
|
}
|
2022-10-17 12:20:14 +00:00
|
|
|
: { kind: "Workspace Folder" };
|
|
|
|
|
2022-10-28 22:44:37 +00:00
|
|
|
const ctx = new Ctx(context, createCommands(), workspace);
|
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(
|
|
|
|
`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
|
|
|
}
|
2020-03-30 17:12:22 +00:00
|
|
|
|
2020-11-15 22:19:04 +00:00
|
|
|
vscode.workspace.onDidChangeConfiguration(
|
2022-10-17 12:53:46 +00:00
|
|
|
async (_) => {
|
2022-10-28 22:44:37 +00:00
|
|
|
await ctx.client?.sendNotification("workspace/didChangeConfiguration", {
|
|
|
|
settings: "",
|
|
|
|
});
|
2022-10-17 12:53:46 +00:00
|
|
|
},
|
2020-11-15 22:19:04 +00:00
|
|
|
null,
|
|
|
|
ctx.subscriptions
|
|
|
|
);
|
2022-04-21 20:39:53 +00:00
|
|
|
|
2022-10-20 09:32:02 +00:00
|
|
|
await ctx.activate();
|
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,
|
|
|
|
disabled: (_) => () => vscode.commands.executeCommand("default:type", { text: "\n" }),
|
|
|
|
},
|
|
|
|
reload: {
|
|
|
|
enabled: (ctx) => async () => {
|
|
|
|
void vscode.window.showInformationMessage("Reloading rust-analyzer...");
|
|
|
|
// FIXME: We should re-use the client, that is ctx.deactivate() if none of the configs have changed
|
|
|
|
await ctx.stop();
|
|
|
|
await ctx.activate();
|
|
|
|
},
|
|
|
|
disabled: (ctx) => async () => {
|
|
|
|
void vscode.window.showInformationMessage("Reloading rust-analyzer...");
|
|
|
|
await ctx.activate();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
startServer: {
|
|
|
|
enabled: (ctx) => async () => {
|
|
|
|
await ctx.activate();
|
|
|
|
},
|
|
|
|
disabled: (ctx) => async () => {
|
|
|
|
await ctx.activate();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
stopServer: {
|
|
|
|
enabled: (ctx) => async () => {
|
|
|
|
// FIXME: We should re-use the client, that is ctx.deactivate() if none of the configs have changed
|
|
|
|
await ctx.stop();
|
|
|
|
ctx.setServerStatus({
|
2022-10-28 21:10:10 +00:00
|
|
|
health: "stopped",
|
2022-10-21 14:00:43 +00:00
|
|
|
});
|
|
|
|
},
|
2022-10-28 22:44:37 +00:00
|
|
|
disabled: (_) => async () => {},
|
2022-10-21 14:00:43 +00:00
|
|
|
},
|
2018-08-17 16:54:08 +00:00
|
|
|
|
2022-10-21 14:00:43 +00:00
|
|
|
analyzerStatus: { enabled: commands.analyzerStatus },
|
|
|
|
memoryUsage: { enabled: commands.memoryUsage },
|
|
|
|
shuffleCrateGraph: { enabled: commands.shuffleCrateGraph },
|
|
|
|
reloadWorkspace: { enabled: commands.reloadWorkspace },
|
|
|
|
matchingBrace: { enabled: commands.matchingBrace },
|
|
|
|
joinLines: { enabled: commands.joinLines },
|
|
|
|
parentModule: { enabled: commands.parentModule },
|
|
|
|
syntaxTree: { enabled: commands.syntaxTree },
|
|
|
|
viewHir: { enabled: commands.viewHir },
|
|
|
|
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 },
|
|
|
|
openCargoToml: { enabled: commands.openCargoToml },
|
|
|
|
peekTests: { enabled: commands.peekTests },
|
|
|
|
moveItemUp: { enabled: commands.moveItemUp },
|
|
|
|
moveItemDown: { enabled: commands.moveItemDown },
|
|
|
|
cancelFlycheck: { enabled: commands.cancelFlycheck },
|
|
|
|
ssr: { enabled: commands.ssr },
|
|
|
|
serverVersion: { enabled: commands.serverVersion },
|
|
|
|
// Internal commands which are invoked by the server.
|
|
|
|
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 },
|
|
|
|
};
|
2020-12-18 17:39:51 +00:00
|
|
|
}
|