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

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

218 lines
8.6 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";
2022-10-17 12:20:14 +00:00
import { Ctx, Workspace } from "./ctx";
import { isRustDocument } from "./util";
import { activateTaskProvider } from "./tasks";
import { setContextValue } from "./util";
2018-08-10 12:07:43 +00:00
const RUST_PROJECT_CONTEXT_NAME = "inRustProject";
export interface RustAnalyzerExtensionApi {
2022-10-17 12:20:14 +00:00
// FIXME: this should be non-optional
readonly client?: lc.LanguageClient;
}
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);
}
// 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"
);
const rustDocuments = vscode.workspace.textDocuments.filter((document) =>
isRustDocument(document)
);
2022-08-23 13:56:02 +00:00
if (folders.length === 0 && rustDocuments.length === 0) {
// 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" };
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) => {
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
}
2022-10-17 12:20:14 +00:00
await initCommonContext(ctx);
2022-10-17 12:20:14 +00:00
if (ctx.config.typingContinueCommentsOnNewline) {
ctx.pushExtCleanup(configureLanguage());
}
2021-09-28 16:53:25 +00:00
vscode.workspace.onDidChangeConfiguration(
async (_) => {
await ctx
.clientFetcher()
.client?.sendNotification("workspace/didChangeConfiguration", { settings: "" });
},
null,
ctx.subscriptions
);
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();
}
2022-10-17 12:20:14 +00:00
async function initCommonContext(ctx: Ctx) {
// 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
//
// ```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-02-17 12:40:20 +00:00
// Commands which invokes manually via command palette, shortcut, etc.
ctx.registerCommand("reload", (_) => 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
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);
ctx.registerCommand("memoryUsage", commands.memoryUsage);
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);
ctx.registerCommand("viewHir", commands.viewHir);
ctx.registerCommand("viewFileText", commands.viewFileText);
2021-05-21 21:59:52 +00:00
ctx.registerCommand("viewItemTree", commands.viewItemTree);
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);
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);
ctx.registerCommand("cancelFlycheck", commands.cancelFlycheck);
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);
ctx.registerCommand("applySnippetWorkspaceEdit", commands.applySnippetWorkspaceEditCommand);
ctx.registerCommand("resolveCodeAction", commands.resolveCodeAction);
2020-05-22 15:29:55 +00:00
ctx.registerCommand("applyActionGroup", commands.applyActionGroup);
ctx.registerCommand("gotoLocation", commands.gotoLocation);
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);
}
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 },
},
],
});
}