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-18 03:39:11 +08:00
|
|
|
type ExpandMacroResult = [string, string];
|
2019-11-18 02:47:50 +08:00
|
|
|
|
|
|
|
|
function code_format([name, text]: [string, string]): vscode.MarkdownString {
|
2019-11-18 03:39:11 +08:00
|
|
|
const markdown = new vscode.MarkdownString(
|
|
|
|
|
`#### Recursive expansion of ${name}! macro`
|
|
|
|
|
);
|
|
|
|
|
markdown.appendCodeblock(text, '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
|
|
|
};
|
|
|
|
|
const result = await Server.client.sendRequest<ExpandMacroResult>(
|
|
|
|
|
'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;
|
|
|
|
|
}
|