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-17 12:20:14 +00:00
|
|
|
import { 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
|
|
|
// FIXME: this should be non-optional
|
|
|
|
readonly client?: lc.LanguageClient;
|
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-08-23 13:56:02 +00:00
|
|
|
if (folders.length === 0 && rustDocuments.length === 0) {
|
2022-08-23 13:45:02 +00:00
|
|
|
// FIXME: Ideally we would choose not to activate at all (and avoid registering
|
|
|
|
// non-functional editor commands), but VS Code doesn't seem to have a good way of doing
|
|
|
|
// that
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-10-17 12:20:14 +00:00
|
|
|
const workspace: Workspace =
|
|
|
|
folders.length === 0
|
|
|
|
? {
|
|
|
|
kind: "Detached Files",
|
|
|
|
files: rustDocuments,
|
|
|
|
}
|
|
|
|
: { kind: "Workspace Folder" };
|
|
|
|
|
2022-10-17 12:53:46 +00:00
|
|
|
const ctx = new Ctx(context, 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.
|
|
|
|
return 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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-10-17 12:20:14 +00:00
|
|
|
await initCommonContext(ctx);
|
2020-11-15 22:19:04 +00:00
|
|
|
|
2022-10-17 12:20:14 +00:00
|
|
|
if (ctx.config.typingContinueCommentsOnNewline) {
|
|
|
|
ctx.pushExtCleanup(configureLanguage());
|
2022-08-03 16:22:45 +00:00
|
|
|
}
|
2021-09-28 16:53:25 +00:00
|
|
|
|
2020-11-15 22:19:04 +00:00
|
|
|
vscode.workspace.onDidChangeConfiguration(
|
2022-10-17 12:53:46 +00:00
|
|
|
async (_) => {
|
|
|
|
await ctx
|
|
|
|
.clientFetcher()
|
|
|
|
.client?.sendNotification("workspace/didChangeConfiguration", { settings: "" });
|
|
|
|
},
|
2020-11-15 22:19:04 +00:00
|
|
|
null,
|
|
|
|
ctx.subscriptions
|
|
|
|
);
|
2022-04-21 20:39:53 +00:00
|
|
|
|
2022-10-17 12:53:46 +00:00
|
|
|
await ctx.activate().catch((err) => {
|
|
|
|
void vscode.window.showErrorMessage(`Cannot activate rust-analyzer server: ${err.message}`);
|
|
|
|
});
|
|
|
|
|
2022-10-17 12:20:14 +00:00
|
|
|
return ctx.clientFetcher();
|
2020-11-15 22:19:04 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 12:20:14 +00:00
|
|
|
async function initCommonContext(ctx: Ctx) {
|
2020-11-15 22:19:04 +00:00
|
|
|
// Register a "dumb" onEnter command for the case where server fails to
|
|
|
|
// start.
|
|
|
|
//
|
|
|
|
// FIXME: refactor command registration code such that commands are
|
|
|
|
// **always** registered, even if the server does not start. Use API like
|
|
|
|
// this perhaps?
|
2020-02-17 12:40:20 +00:00
|
|
|
//
|
2020-11-15 22:19:04 +00:00
|
|
|
// ```TypeScript
|
|
|
|
// registerCommand(
|
|
|
|
// factory: (Ctx) => ((Ctx) => any),
|
|
|
|
// fallback: () => any = () => vscode.window.showErrorMessage(
|
|
|
|
// "rust-analyzer is not available"
|
|
|
|
// ),
|
|
|
|
// )
|
|
|
|
const defaultOnEnter = vscode.commands.registerCommand("rust-analyzer.onEnter", () =>
|
|
|
|
vscode.commands.executeCommand("default:type", { text: "\n" })
|
|
|
|
);
|
2022-10-17 12:20:14 +00:00
|
|
|
ctx.pushExtCleanup(defaultOnEnter);
|
2020-02-17 12:40:20 +00:00
|
|
|
|
2021-02-09 14:12:46 +00:00
|
|
|
await setContextValue(RUST_PROJECT_CONTEXT_NAME, true);
|
2020-05-27 16:40:13 +00:00
|
|
|
|
2020-02-17 12:40:20 +00:00
|
|
|
// Commands which invokes manually via command palette, shortcut, etc.
|
2020-03-26 21:44:19 +00:00
|
|
|
ctx.registerCommand("reload", (_) => async () => {
|
|
|
|
void vscode.window.showInformationMessage("Reloading rust-analyzer...");
|
2022-10-17 12:53:46 +00:00
|
|
|
// FIXME: We should re-use the client, that is ctx.deactivate() if none of the configs have changed
|
2022-10-17 12:20:14 +00:00
|
|
|
await ctx.disposeClient();
|
|
|
|
await ctx.activate();
|
2020-02-17 20:09:44 +00:00
|
|
|
});
|
2020-02-17 11:17:01 +00:00
|
|
|
|
2019-12-30 13:53:43 +00:00
|
|
|
ctx.registerCommand("analyzerStatus", commands.analyzerStatus);
|
2020-07-07 10:10:14 +00:00
|
|
|
ctx.registerCommand("memoryUsage", commands.memoryUsage);
|
2021-12-07 14:38:12 +00:00
|
|
|
ctx.registerCommand("shuffleCrateGraph", commands.shuffleCrateGraph);
|
2020-07-01 12:57:59 +00:00
|
|
|
ctx.registerCommand("reloadWorkspace", commands.reloadWorkspace);
|
2019-12-30 14:20:13 +00:00
|
|
|
ctx.registerCommand("matchingBrace", commands.matchingBrace);
|
2019-12-30 14:50:15 +00:00
|
|
|
ctx.registerCommand("joinLines", commands.joinLines);
|
2019-12-30 16:03:05 +00:00
|
|
|
ctx.registerCommand("parentModule", commands.parentModule);
|
2019-12-30 18:05:41 +00:00
|
|
|
ctx.registerCommand("syntaxTree", commands.syntaxTree);
|
2020-12-28 18:29:58 +00:00
|
|
|
ctx.registerCommand("viewHir", commands.viewHir);
|
2022-03-31 12:50:33 +00:00
|
|
|
ctx.registerCommand("viewFileText", commands.viewFileText);
|
2021-05-21 21:59:52 +00:00
|
|
|
ctx.registerCommand("viewItemTree", commands.viewItemTree);
|
2021-05-11 14:15:31 +00:00
|
|
|
ctx.registerCommand("viewCrateGraph", commands.viewCrateGraph);
|
2021-07-01 22:08:05 +00:00
|
|
|
ctx.registerCommand("viewFullCrateGraph", commands.viewFullCrateGraph);
|
2019-12-30 18:30:30 +00:00
|
|
|
ctx.registerCommand("expandMacro", commands.expandMacro);
|
2019-12-30 18:58:44 +00:00
|
|
|
ctx.registerCommand("run", commands.run);
|
2021-02-10 11:28:13 +00:00
|
|
|
ctx.registerCommand("copyRunCommandLine", commands.copyRunCommandLine);
|
2020-05-11 13:06:57 +00:00
|
|
|
ctx.registerCommand("debug", commands.debug);
|
2020-05-11 15:00:15 +00:00
|
|
|
ctx.registerCommand("newDebugConfig", commands.newDebugConfig);
|
2020-08-30 08:02:29 +00:00
|
|
|
ctx.registerCommand("openDocs", commands.openDocs);
|
2020-11-13 01:48:07 +00:00
|
|
|
ctx.registerCommand("openCargoToml", commands.openCargoToml);
|
2021-02-27 17:04:43 +00:00
|
|
|
ctx.registerCommand("peekTests", commands.peekTests);
|
2021-03-16 12:37:00 +00:00
|
|
|
ctx.registerCommand("moveItemUp", commands.moveItemUp);
|
|
|
|
ctx.registerCommand("moveItemDown", commands.moveItemDown);
|
2022-08-19 06:52:31 +00:00
|
|
|
ctx.registerCommand("cancelFlycheck", commands.cancelFlycheck);
|
2020-02-24 11:32:15 +00:00
|
|
|
|
2020-02-17 20:09:44 +00:00
|
|
|
ctx.registerCommand("ssr", commands.ssr);
|
2020-02-21 02:04:03 +00:00
|
|
|
ctx.registerCommand("serverVersion", commands.serverVersion);
|
2019-12-30 19:07:04 +00:00
|
|
|
|
|
|
|
// Internal commands which are invoked by the server.
|
|
|
|
ctx.registerCommand("runSingle", commands.runSingle);
|
2020-03-09 21:06:45 +00:00
|
|
|
ctx.registerCommand("debugSingle", commands.debugSingle);
|
2019-12-30 19:07:04 +00:00
|
|
|
ctx.registerCommand("showReferences", commands.showReferences);
|
2020-05-21 12:26:44 +00:00
|
|
|
ctx.registerCommand("applySnippetWorkspaceEdit", commands.applySnippetWorkspaceEditCommand);
|
2020-06-02 20:21:48 +00:00
|
|
|
ctx.registerCommand("resolveCodeAction", commands.resolveCodeAction);
|
2020-05-22 15:29:55 +00:00
|
|
|
ctx.registerCommand("applyActionGroup", commands.applyActionGroup);
|
2020-06-10 20:01:19 +00:00
|
|
|
ctx.registerCommand("gotoLocation", commands.gotoLocation);
|
2022-05-16 18:53:00 +00:00
|
|
|
|
|
|
|
ctx.registerCommand("linkToCommand", commands.linkToCommand);
|
2018-08-17 16:54:08 +00:00
|
|
|
|
2022-10-17 12:20:14 +00:00
|
|
|
defaultOnEnter.dispose();
|
|
|
|
ctx.registerCommand("onEnter", commands.onEnter);
|
2020-12-18 17:39:51 +00:00
|
|
|
}
|
2021-09-28 16:53:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets up additional language configuration that's impossible to do via a
|
|
|
|
* separate language-configuration.json file. See [1] for more information.
|
|
|
|
*
|
|
|
|
* [1]: https://github.com/Microsoft/vscode/issues/11514#issuecomment-244707076
|
|
|
|
*/
|
|
|
|
function configureLanguage(): vscode.Disposable {
|
|
|
|
const indentAction = vscode.IndentAction.None;
|
|
|
|
return vscode.languages.setLanguageConfiguration("rust", {
|
|
|
|
onEnterRules: [
|
|
|
|
{
|
|
|
|
// Doc single-line comment
|
|
|
|
// e.g. ///|
|
|
|
|
beforeText: /^\s*\/{3}.*$/,
|
|
|
|
action: { indentAction, appendText: "/// " },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Parent doc single-line comment
|
|
|
|
// e.g. //!|
|
|
|
|
beforeText: /^\s*\/{2}\!.*$/,
|
|
|
|
action: { indentAction, appendText: "//! " },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Begins an auto-closed multi-line comment (standard or parent doc)
|
|
|
|
// e.g. /** | */ or /*! | */
|
|
|
|
beforeText: /^\s*\/\*(\*|\!)(?!\/)([^\*]|\*(?!\/))*$/,
|
|
|
|
afterText: /^\s*\*\/$/,
|
|
|
|
action: { indentAction: vscode.IndentAction.IndentOutdent, appendText: " * " },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Begins a multi-line comment (standard or parent doc)
|
|
|
|
// e.g. /** ...| or /*! ...|
|
|
|
|
beforeText: /^\s*\/\*(\*|\!)(?!\/)([^\*]|\*(?!\/))*$/,
|
|
|
|
action: { indentAction, appendText: " * " },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Continues a multi-line comment
|
|
|
|
// e.g. * ...|
|
|
|
|
beforeText: /^(\ \ )*\ \*(\ ([^\*]|\*(?!\/))*)?$/,
|
|
|
|
action: { indentAction, appendText: "* " },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Dedents after closing a multi-line comment
|
|
|
|
// e.g. */|
|
|
|
|
beforeText: /^(\ \ )*\ \*\/\s*$/,
|
|
|
|
action: { indentAction, removeText: 1 },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
}
|