Files
rust/editors/code/src/commands/expand_macro.ts

49 lines
1.4 KiB
TypeScript
Raw Normal View History

2019-11-18 02:47:50 +08:00
import * as vscode from 'vscode';
import { Position, TextDocumentIdentifier } from 'vscode-languageclient';
import { Server } from '../server';
2019-11-19 22:56:48 +08:00
interface ExpandedMacro {
name: string,
expansion: string,
}
2019-11-18 02:47:50 +08:00
2019-11-19 22:56:48 +08:00
function code_format(expanded: ExpandedMacro): vscode.MarkdownString {
2019-11-18 03:39:11 +08:00
const markdown = new vscode.MarkdownString(
2019-11-19 22:56:48 +08:00
`#### Recursive expansion of ${expanded.name}! macro`
2019-11-18 03:39:11 +08:00
);
2019-11-19 22:56:48 +08:00
markdown.appendCodeblock(expanded.expansion, 'rust');
2019-11-18 02:47:50 +08:00
return markdown;
}
export class ExpandMacroHoverProvider implements vscode.HoverProvider {
public provideHover(
document: vscode.TextDocument,
position: vscode.Position,
2019-11-18 03:39:11 +08:00
token: vscode.CancellationToken
2019-11-18 02:47:50 +08:00
): Thenable<vscode.Hover | null> | null {
async function handle() {
const request: MacroExpandParams = {
textDocument: { uri: document.uri.toString() },
2019-11-18 03:39:11 +08:00
position
2019-11-18 02:47:50 +08:00
};
2019-11-19 22:56:48 +08:00
const result = await Server.client.sendRequest<ExpandedMacro>(
2019-11-18 02:47:50 +08:00
'rust-analyzer/expandMacro',
request
);
if (result != null) {
const formated = code_format(result);
return new vscode.Hover(formated);
}
return null;
2019-11-18 03:39:11 +08:00
}
2019-11-18 02:47:50 +08:00
return handle();
}
}
interface MacroExpandParams {
textDocument: TextDocumentIdentifier;
position: Position;
}