mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-15 06:33:58 +00:00
Move expand macro to the new context
This commit is contained in:
parent
68f47a5b10
commit
94be27fc44
5 changed files with 53 additions and 75 deletions
editors/code/src
|
@ -37,7 +37,7 @@ export function analyzerStatus(ctx: Ctx): Cmd {
|
||||||
|
|
||||||
class TextDocumentContentProvider
|
class TextDocumentContentProvider
|
||||||
implements vscode.TextDocumentContentProvider {
|
implements vscode.TextDocumentContentProvider {
|
||||||
ctx: Ctx;
|
private ctx: Ctx;
|
||||||
uri = vscode.Uri.parse('rust-analyzer-status://status');
|
uri = vscode.Uri.parse('rust-analyzer-status://status');
|
||||||
eventEmitter = new vscode.EventEmitter<vscode.Uri>();
|
eventEmitter = new vscode.EventEmitter<vscode.Uri>();
|
||||||
|
|
||||||
|
|
|
@ -1,60 +1,23 @@
|
||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
import { Position, TextDocumentIdentifier } from 'vscode-languageclient';
|
import * as lc from 'vscode-languageclient';
|
||||||
import { Server } from '../server';
|
|
||||||
|
|
||||||
export const expandMacroUri = vscode.Uri.parse(
|
import { Ctx, Cmd } from '../ctx';
|
||||||
'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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Opens the virtual file that will show the syntax tree
|
// Opens the virtual file that will show the syntax tree
|
||||||
//
|
//
|
||||||
// The contents of the file come from the `TextDocumentContentProvider`
|
// The contents of the file come from the `TextDocumentContentProvider`
|
||||||
export function createHandle(provider: ExpandMacroContentProvider) {
|
export function expandMacro(ctx: Ctx): Cmd {
|
||||||
|
const tdcp = new TextDocumentContentProvider(ctx);
|
||||||
|
ctx.pushCleanup(
|
||||||
|
vscode.workspace.registerTextDocumentContentProvider(
|
||||||
|
'rust-analyzer',
|
||||||
|
tdcp,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
return async () => {
|
return async () => {
|
||||||
const uri = expandMacroUri;
|
const document = await vscode.workspace.openTextDocument(tdcp.uri);
|
||||||
|
tdcp.eventEmitter.fire(tdcp.uri);
|
||||||
const document = await vscode.workspace.openTextDocument(uri);
|
|
||||||
|
|
||||||
provider.eventEmitter.fire(uri);
|
|
||||||
|
|
||||||
return vscode.window.showTextDocument(
|
return vscode.window.showTextDocument(
|
||||||
document,
|
document,
|
||||||
vscode.ViewColumn.Two,
|
vscode.ViewColumn.Two,
|
||||||
|
@ -63,11 +26,6 @@ export function createHandle(provider: ExpandMacroContentProvider) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
interface MacroExpandParams {
|
|
||||||
textDocument: TextDocumentIdentifier;
|
|
||||||
position: Position;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ExpandedMacro {
|
interface ExpandedMacro {
|
||||||
name: string;
|
name: string;
|
||||||
expansion: string;
|
expansion: string;
|
||||||
|
@ -81,3 +39,37 @@ function code_format(expanded: ExpandedMacro): string {
|
||||||
|
|
||||||
return result;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ import { joinLines } from './join_lines';
|
||||||
import { onEnter } from './on_enter';
|
import { onEnter } from './on_enter';
|
||||||
import { parentModule } from './parent_module';
|
import { parentModule } from './parent_module';
|
||||||
import { syntaxTree } from './syntax_tree';
|
import { syntaxTree } from './syntax_tree';
|
||||||
import * as expandMacro from './expand_macro';
|
import { expandMacro } from './expand_macro';
|
||||||
import * as inlayHints from './inlay_hints';
|
import * as inlayHints from './inlay_hints';
|
||||||
import * as runnables from './runnables';
|
import * as runnables from './runnables';
|
||||||
|
|
||||||
|
|
|
@ -66,10 +66,9 @@ interface SyntaxTreeParams {
|
||||||
|
|
||||||
class TextDocumentContentProvider
|
class TextDocumentContentProvider
|
||||||
implements vscode.TextDocumentContentProvider {
|
implements vscode.TextDocumentContentProvider {
|
||||||
ctx: Ctx;
|
private ctx: Ctx;
|
||||||
uri = vscode.Uri.parse('rust-analyzer://syntaxtree');
|
uri = vscode.Uri.parse('rust-analyzer://syntaxtree');
|
||||||
eventEmitter = new vscode.EventEmitter<vscode.Uri>();
|
eventEmitter = new vscode.EventEmitter<vscode.Uri>();
|
||||||
syntaxTree: string = 'Not available';
|
|
||||||
|
|
||||||
constructor(ctx: Ctx) {
|
constructor(ctx: Ctx) {
|
||||||
this.ctx = ctx;
|
this.ctx = ctx;
|
||||||
|
|
|
@ -2,7 +2,6 @@ import * as vscode from 'vscode';
|
||||||
import * as lc from 'vscode-languageclient';
|
import * as lc from 'vscode-languageclient';
|
||||||
|
|
||||||
import * as commands from './commands';
|
import * as commands from './commands';
|
||||||
import { ExpandMacroContentProvider } from './commands/expand_macro';
|
|
||||||
import { HintsUpdater } from './commands/inlay_hints';
|
import { HintsUpdater } from './commands/inlay_hints';
|
||||||
import { StatusDisplay } from './commands/watch_status';
|
import { StatusDisplay } from './commands/watch_status';
|
||||||
import * as events from './events';
|
import * as events from './events';
|
||||||
|
@ -20,6 +19,7 @@ export async function activate(context: vscode.ExtensionContext) {
|
||||||
ctx.registerCommand('joinLines', commands.joinLines);
|
ctx.registerCommand('joinLines', commands.joinLines);
|
||||||
ctx.registerCommand('parentModule', commands.parentModule);
|
ctx.registerCommand('parentModule', commands.parentModule);
|
||||||
ctx.registerCommand('syntaxTree', commands.syntaxTree);
|
ctx.registerCommand('syntaxTree', commands.syntaxTree);
|
||||||
|
ctx.registerCommand('expandMacro', commands.expandMacro);
|
||||||
|
|
||||||
function disposeOnDeactivation(disposable: vscode.Disposable) {
|
function disposeOnDeactivation(disposable: vscode.Disposable) {
|
||||||
context.subscriptions.push(disposable);
|
context.subscriptions.push(disposable);
|
||||||
|
@ -65,25 +65,12 @@ export async function activate(context: vscode.ExtensionContext) {
|
||||||
params => watchStatus.handleProgressNotification(params),
|
params => watchStatus.handleProgressNotification(params),
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
const expandMacroContentProvider = new ExpandMacroContentProvider();
|
|
||||||
|
|
||||||
// The events below are plain old javascript events, triggered and handled by vscode
|
// The events below are plain old javascript events, triggered and handled by vscode
|
||||||
vscode.window.onDidChangeActiveTextEditor(
|
vscode.window.onDidChangeActiveTextEditor(
|
||||||
events.changeActiveTextEditor.makeHandler(),
|
events.changeActiveTextEditor.makeHandler(),
|
||||||
);
|
);
|
||||||
|
|
||||||
disposeOnDeactivation(
|
|
||||||
vscode.workspace.registerTextDocumentContentProvider(
|
|
||||||
'rust-analyzer',
|
|
||||||
expandMacroContentProvider,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
registerCommand(
|
|
||||||
'rust-analyzer.expandMacro',
|
|
||||||
commands.expandMacro.createHandle(expandMacroContentProvider),
|
|
||||||
);
|
|
||||||
|
|
||||||
const startServer = () => Server.start(allNotifications);
|
const startServer = () => Server.start(allNotifications);
|
||||||
const reloadCommand = () => reloadServer(startServer);
|
const reloadCommand = () => reloadServer(startServer);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue