import * as vscode from 'vscode';
-import { Position, TextDocumentIdentifier } from 'vscode-languageclient';
-import { Server } from '../server';
+import * as lc from 'vscode-languageclient';
-export const expandMacroUri = vscode.Uri.parse(
- 'rust-analyzer://expandMacro/[EXPANSION].rs',
-);
-
-export class ExpandMacroContentProvider
- implements vscode.TextDocumentContentProvider {
- public eventEmitter = new vscode.EventEmitter<vscode.Uri>();
-
- public provideTextDocumentContent(
- _uri: vscode.Uri,
- ): vscode.ProviderResult<string> {
- async function handle() {
- const editor = vscode.window.activeTextEditor;
- if (editor == null) {
- return '';
- }
-
- const position = editor.selection.active;
- const request: MacroExpandParams = {
- textDocument: { uri: editor.document.uri.toString() },
- position,
- };
- const expanded = await Server.client.sendRequest<ExpandedMacro>(
- 'rust-analyzer/expandMacro',
- request,
- );
-
- if (expanded == null) {
- return 'Not available';
- }
-
- return code_format(expanded);
- }
-
- return handle();
- }
-
- get onDidChange(): vscode.Event<vscode.Uri> {
- return this.eventEmitter.event;
- }
-}
+import { Ctx, Cmd } from '../ctx';
// Opens the virtual file that will show the syntax tree
//
// The contents of the file come from the `TextDocumentContentProvider`
-export function createHandle(provider: ExpandMacroContentProvider) {
- return async () => {
- const uri = expandMacroUri;
-
- const document = await vscode.workspace.openTextDocument(uri);
-
- provider.eventEmitter.fire(uri);
+export function expandMacro(ctx: Ctx): Cmd {
+ const tdcp = new TextDocumentContentProvider(ctx);
+ ctx.pushCleanup(
+ vscode.workspace.registerTextDocumentContentProvider(
+ 'rust-analyzer',
+ tdcp,
+ ),
+ );
+ return async () => {
+ const document = await vscode.workspace.openTextDocument(tdcp.uri);
+ tdcp.eventEmitter.fire(tdcp.uri);
return vscode.window.showTextDocument(
document,
vscode.ViewColumn.Two,
};
}
-interface MacroExpandParams {
- textDocument: TextDocumentIdentifier;
- position: Position;
-}
-
interface ExpandedMacro {
name: string;
expansion: string;
return result;
}
+
+class TextDocumentContentProvider
+ implements vscode.TextDocumentContentProvider {
+ private ctx: Ctx;
+ uri = vscode.Uri.parse('rust-analyzer://expandMacro/[EXPANSION].rs');
+ eventEmitter = new vscode.EventEmitter<vscode.Uri>();
+
+ constructor(ctx: Ctx) {
+ this.ctx = ctx;
+ }
+
+ async provideTextDocumentContent(_uri: vscode.Uri): Promise<string> {
+ const editor = vscode.window.activeTextEditor;
+ if (editor == null) return '';
+
+ const position = editor.selection.active;
+ const request: lc.TextDocumentPositionParams = {
+ textDocument: { uri: editor.document.uri.toString() },
+ position,
+ };
+ const expanded = await this.ctx.client.sendRequest<ExpandedMacro>(
+ 'rust-analyzer/expandMacro',
+ request,
+ );
+
+ if (expanded == null) return 'Not available';
+
+ return code_format(expanded);
+ }
+
+ get onDidChange(): vscode.Event<vscode.Uri> {
+ return this.eventEmitter.event;
+ }
+}
import * as lc from 'vscode-languageclient';
import * as commands from './commands';
-import { ExpandMacroContentProvider } from './commands/expand_macro';
import { HintsUpdater } from './commands/inlay_hints';
import { StatusDisplay } from './commands/watch_status';
import * as events from './events';
ctx.registerCommand('joinLines', commands.joinLines);
ctx.registerCommand('parentModule', commands.parentModule);
ctx.registerCommand('syntaxTree', commands.syntaxTree);
+ ctx.registerCommand('expandMacro', commands.expandMacro);
function disposeOnDeactivation(disposable: vscode.Disposable) {
context.subscriptions.push(disposable);
params => watchStatus.handleProgressNotification(params),
],
];
- const expandMacroContentProvider = new ExpandMacroContentProvider();
// The events below are plain old javascript events, triggered and handled by vscode
vscode.window.onDidChangeActiveTextEditor(
events.changeActiveTextEditor.makeHandler(),
);
- disposeOnDeactivation(
- vscode.workspace.registerTextDocumentContentProvider(
- 'rust-analyzer',
- expandMacroContentProvider,
- ),
- );
-
- registerCommand(
- 'rust-analyzer.expandMacro',
- commands.expandMacro.createHandle(expandMacroContentProvider),
- );
-
const startServer = () => Server.start(allNotifications);
const reloadCommand = () => reloadServer(startServer);